If a player is in the ArrayList then it will freeze the player basically is what I am trying to do. Code: if (cmd.getName().equalsIgnoreCase("arraylist")) { if (!Players.contains(player.getName())) { Players.add(player.getName()); } else { Players.remove(player.getName()); } player.sendMessage("Current Users: "+Players.toString()); } return false; } This is to add/remove the player to the ArrayList but if I test that the player is in the arraylist when I move it's "empty" Code: @EventHandler public void onPlayerMove(PlayerMoveEvent e) { e.getPlayer().sendMessage("Users in Array:" + Players.toString()); As you can see it SHOULD say [Klowerz] as well when it says "Users in Array" private final ArrayList<String> Players = new ArrayList<String>(); The Arraylist is just at the top of the class.
Is it because you have a final modifier on your ArrayList? If you have a final modifier on anything then it says you cannot change it. I don't even know how you have a final modifier on an ArrayList... It should have thrown an error in Eclipse instantly.
@ForsakenRealmz Final just stops you changing which object the field references to. You can still call the methods of that object.
I didn't have the final at all before and still had issues, I just added that to test it. Any idea what the issue is? That should be everything you'd need... From what I observed, it's somehow not updating between Commands/Events Because if I do this - @EventHandler public void onPlayerMove(PlayerMoveEvent e) { Player player = e.getPlayer(); if (!Players.contains(player.getName())) { Players.add(player.getName()); player.sendMessage("Current Users: " + Players.toString()); } } It display works but if I do /arraylist when I removed the addPlayer part it's empty. So how do I update the Arraylist to work everywhere? <Edit by mrCookieSlime: Merged posts. Please don't double post. There is an Edit Button right next to the Date.>
@Eggspurt No it is not. i asked for the whole class just because i want to see how and where your array is implemented because if it is defined in a class and is used by different objects of this class, then it has to be static, i want to see where your command is, is it in the same class as the listener? Therefore having the full class(es) is verry helpful to find issues that may be the cause... Also dont worry, none is gonna steal your code...
I don't know what you have been testing, but if you declare an ArrayList final it gives an error saying it has to be static. If you assigned the ArrayList values at the declaration then I would understand, but Eggspurt is not trying to do that. He's trying to add people to the list continuously, and obviously (as the issue is now solved) it was because of the modifier being final.
As I said, I didn't start with final, I put it there for testing purposes and it still didn't work before I didn't have static/final or anything just private Array
I've not been testing anything, I know what the final keyword does, and it's not what you're saying http://ideone.com/t3s1Hl
If you're just doing existance checks a HashMap containing the players name/UUID/entity ID will be much more efficient.