ArrayList errors?

Discussion in 'Plugin Development' started by Eggspurt, Jun 7, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Eggspurt

    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"

    [​IMG]
    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.
     
  2. Offline

    mine-care

    @Eggspurt can we see the full class(es) where this is in?
     
  3. Offline

    ForsakenRealmz

    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.
     
  4. @ForsakenRealmz Final just stops you changing which object the field references to. You can still call the methods of that object.
     
  5. Offline

    Eggspurt

    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.>
     
    Last edited by a moderator: Jun 8, 2015
  6. Offline

    mine-care

    @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...
     
    Eggspurt likes this.
  7. Offline

    Eggspurt

    Thanks, all I had to do was put "private static ect..."

    Thanks a lot actually :p
     
    mine-care likes this.
  8. Offline

    ForsakenRealmz

    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.
     
  9. Offline

    Eggspurt

    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
     
  10. 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
     
  11. Offline

    aaomidi

    Oh lordie lord....
     
    SuperOriginal likes this.
  12. If you're just doing existance checks a HashMap containing the players name/UUID/entity ID will be much more efficient.
     
Thread Status:
Not open for further replies.

Share This Page