Remove player from HashMap in the order they were added?

Discussion in 'Plugin Development' started by DirtyStarfish, Oct 24, 2011.

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



    This is what I am trying to do.

    Player breaks a block, they are added to a HashMap.
    A task is then ran to remove the player from the HashMap in 200 ticks.
    After 200 ticks the player is removed from the HashMap.
    Meanwhile, other players can break a block and be added to the HashMap, and be removed after 200 ticks.

    But what happens if another player breaks a block while the task is running?
    Does it restart, or start a new task while the other is still running?

    How can I remove the players in the order that they were added?

    I'm confusing myself a lot about this, and it doesn't help that I can't test it on my own!
  2. Offline


  3. Offline


    Use a TreeMap then to remove the first player off the map do MapVariable.remove(MapVariable.firstKey());

    Alternatively, if you do not actually need a Key/Element pair relationship you could use any form of List such as ArrayList or LinkedList and then use ListVariable.remove(Object); where Object is whatever you are using to represent the player; this will remove only the first entry for that player, though.

    To achieve just cutting off a top again you could use ListVariable.remove(1); (Someone correct me if I am wrong and lists start on 0)

    You could also in the event of using a LinkedList use LinkedListVariable.removeFirst();


    In addition to what is linked above, you'll want to pick an implementation that makes the remove function remove the first added; LinkedList implements Quere though, so that is one choice for the Quere method.
  4. Offline


    Wow, thanks for the fast response! I think I'll have a look at TreeMaps :)
  5. Offline


    TreeMaps don't maintain insertion order, they get ordered naturally (based on the passed Comparer or by compareTo if they are Comparable).

    If you're creating a new task for the scheduler for each player, you shouldn't run into any problems at all. No reason to worry about this.
  6. Offline


    Well, I have a task that I use whenever a player clicks holding a certain item. If say, three players all click within a few seconds, will a new task be created each time? I'm using a synchronized delayed task.

    I don't know much about scheduler tasks so I'm thinking that because the player variable will change when a new player clicks, it will be using the wrong player, is this not the case?
  7. Offline


    Are you creating a new task each time? If so, there will be separate tasks.
Thread Status:
Not open for further replies.

Share This Page