使用api Pircbot(Irc Bot API)时,我需要调用一个方法,该方法要等到hasmap被其他方法更改为止。示例:用户执行命令!isRegistered。现在,Pircbot将检查用户是否已由NickServ注册。
@Override
protected void onMessage(String channel, String sender, String login, String hostname, String message) {
if (command("isRegistered", message) && checkRegistered(sender)) {
sendMessage(sender,"You are registered);
}
};
我认为,aHashMap<String, Boolean>
是执行此操作的正确方法。
private void checkRegistered(String sender) {
checkRegister.put(Sender,null);
sendMessage("NickServ", "info sender");
//Wait till onNotices changes the HashMap so that i can get an result (hasmap content: null -> true, or false) and returning this result)
//A simple Thread.sleep(); does not work, because everything runs on the main Thread. -> When sleeping the bot times out, because it can't reply to the /ping command from the server.
}
每当pircbot收到通知时,我都可以使用
@Override protected void onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice) {
所以我做到了:
@Override
protected void onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice) {
if (sourceLogin.equals("NickServ")) {
for (String s : checkRegister.keySet()) {
if (notice.contains("Nickname:") && notice.contains(s)) {
checkRegister.put(s, notice.contains("<< ONLINE >>"));
}
}
}
}
我现在如何检查方法中hasmap是否从null-> true或从null-> false更改checkRegistered();
?
您可以混合使用Observer-Observable和HashMap。
最初,客户端将自己注册为观察者或HashMap,并且当Map中发生某些更改时,将向所有观察者发送一条消息。
观察者示例代码:
http://javarevisited.blogspot.ca/2011/12/observer-design-pattern-java-example.html http://www.journaldev.com/1739/observer-design-pattern-in-java-example-tutorial
具体来说,对您的问题“我现在如何检查hasmap是否在checkRegistered();方法内从null-> true或从null-> false更改了?”
将通知客户端该属性的值已从一个值更改为另一个值,这意味着消息将包含属性名称,当前值和新值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句