-2

我正在尝试使用聊天频道并遇到列表问题。我在执行命令时将一个元素添加到列表中,稍后我尝试检查该元素是否已添加到列表中,但它总是显示它不是。

这是我的代码:

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);

}

请告诉我它有什么问题,因为我已经疯了,对不起我的英语不好;/

4

1 回答 1

0

解决方案非常简单:

将所有 ArrayLists 变成静态的。EventHandler 以不同的方式处理类,每次都创建一个新的类,使您的变量声明对事件无用。

private static ArrayList<String> ooc = new ArrayList<String>();
private static ArrayList<String> ic = new ArrayList<String>();
private static ArrayList<String> shout = new ArrayList<String>();
private static ArrayList<String> whisper = new ArrayList<String>();

这样,所有事件处理程序都使用您的列表,从而将其呈现为唯一列表。

于 2017-03-26T23:48:51.037 回答