'Bukkit ArrayList contains cant find element

I'm trying to do Chat Channels and have problems with lists. Im adding one element to the list when executing command and later im trying to check if the element is added on list but it's always shows that it isnt.

Here is my code:

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

    }

In game I'm getting Debug: OOC Message Back so ooc.add(s) should be OK, but the second d message from my listener is always returning false and never gets into if statement. I thought it may be caused by casting Sender to Player, so I also tried to do it on string arrays, and both debug messages from command and listener was returning exactly the same playername but the contains was always false. Here is also code with String attempt.

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

    }


Solution 1:[1]

Making your ArrayList fields static is a poor choice/workaround for what is likely a bug elsewheres in your code. When you use new RpChat(), you are creating a new object with a separate copy of the lists in the class. If you registered RpChat like so:

getServer().getPluginManager().registerEvents(new RpChat(), plugin);
plugin.getCommand("ooc").setExecutor(new RpChat());

Then the two RpChat variables would be distinct, and the lists contained in them would never interact between the listener and the command execution. Instead, you should register the same object to both:

RpChat chat = new RpChat();
getServer().getPluginManager().registerEvents(chat, plugin);
plugin.getCommand("ooc").setExecutor(chat);

This is a fairly common developer bug for bukkit that I've seen a lot over the last decade. Making all your fields static will quickly lead to a plugin with no OOP design, and static having to be added to everything you can think of.

Solution 2:[2]

The solution for this is pretty simple:

Turn all your ArrayLists static. The EventHandler handles classes in different ways, creating a new one everytime, making your declaration of variables useless to the events.

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

This way, your Lists are used by all the event handlers, rendering it a unique list.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rogue
Solution 2 LeoColman