我正在尝试使用聊天频道并遇到列表问题。我在执行命令时将一个元素添加到列表中,稍后我尝试检查该元素是否已添加到列表中,但它总是显示它不是。
这是我的代码:
public class RpChat implements CommandExecutor, Listener {
private ArrayList<Player> ooc = new ArrayList<Player>();
private ArrayList<Player> ic = new ArrayList<Player>();
private ArrayList<Player> shout = new ArrayList<Player>();
private ArrayList<Player> whisper = new ArrayList<Player>();
public boolean onCommand(CommandSender cs, org.bukkit.command.Command cmnd, String string, String[] strings) {
Player s = (Player) cs;
if (cmnd.getName().equalsIgnoreCase("ooc")) {
s.sendMessage("Debug: OOC");
ooc.add(s);
if (ic.contains(s))
ooc.remove(s);
if (shout.contains(s))
ooc.remove(s);
if (whisper.contains(s))
ooc.remove(s);
}
return false;
}
@EventHandler
public void onPlayerChatEvent(AsyncPlayerChatEvent event) {
Player p = event.getPlayer();
Bukkit.getScheduler().scheduleSyncRepeatingTask(Dungeon.getPlugin(), new Runnable() {
public void run() {
if (ooc.contains(p)) {
event.setFormat(ChatColor.RED + "[OOC] " + ChatColor.WHITE + "%s" + ": " + "%s");
p.sendMessage("Debug Player = " + ooc.contains(p));
}
else{
p.sendMessage("Debug Player = " + ooc.contains(p));
}
}
}, 0, 40);
}
在游戏中我得到 Debug: OOC Message Back 所以 ooc.add(s) 应该没问题,但是来自我的听众的第二条 d 消息总是返回 false 并且永远不会进入 if 语句。我认为这可能是由于将 Sender 转换为 Player 造成的,这就是为什么我也尝试在 Strings Arrays 上执行此操作,并且来自命令和侦听器的调试消息都返回完全相同的玩家名称,但包含始终是错误的。这里也是字符串尝试的代码。
public class RpChat implements CommandExecutor, Listener {
private ArrayList<String> ooc = new ArrayList<String>();
private ArrayList<String> ic = new ArrayList<String>();
private ArrayList<String> shout = new ArrayList<String>();
private ArrayList<String> whisper = new ArrayList<String>();
public boolean onCommand(CommandSender cs, org.bukkit.command.Command cmnd, String string, String[] strings) {
Player p = (Player) cs;
String s = p.getName();
if (cmnd.getName().equalsIgnoreCase("ooc")) {
p.sendMessage("Debug: OOC: Plaer Name: "+ s);
if (ic.contains(s))
ic.remove(s);
if (shout.contains(s))
shout.remove(s);
if (whisper.contains(s))
whisper.remove(s);
ooc.add(s);
}
return true;
}
@EventHandler
public void onPlayerChatEvent(AsyncPlayerChatEvent event) {
Player p = event.getPlayer();
String s = p.getName();
Bukkit.getScheduler().scheduleSyncRepeatingTask(Dungeon.getPlugin(), new Runnable() {
public void run() {
if (ooc.contains(s)) {
event.setFormat(ChatColor.RED + "[OOC] " + ChatColor.WHITE + "%s" + ": " + "%s");
p.sendMessage("Debug Player = " + ooc.contains(s)+ " Plaer Name: "+ s);
}
else{
p.sendMessage("Debug Player = " + ooc.contains(s)+ " Plaer Name: "+ s);
}
}
}, 0, 40);
}
请告诉我它有什么问题,因为我已经疯了,对不起我的英语不好;/