Setting player locations when a player leaves

Discussion in 'Plugin Development' started by RedstoneForDayz, Jan 10, 2015.

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

    RedstoneForDayz

    So I have a plugin set up for King of The Ladder, so when players join they go to a pod. What the code basically does is when a player joins, they are taken to a pod, and then a integer is added by 1 each time a player joins. The player goes to a pod based on what number the integer is at, example int == 1, pod 1, int == 2, pod 2, ect. The only problem I have is when a player leaves, I want it so when the last player joins, they can fill the pod back up to where the player that left was. How would you do that?

    P.S. I dont have the code :( I got a short little power outage and my computer turned off (Screw Desktops!).
    But ill make an example of what the code looked like:
    Code:
    @EventHandler
    public void join(PLayerJoinEvent e)
    int join = 1;
    
    player p = e.getPlayer();
    
    if (join == 1){
    //teleports player to pod 1
    join = 2;
    }
    if (join == 2){
    //teleports player to pod 2
    join = 3;
    }
    //ect.
     
  2. Offline

    1Rogue

    I would say use a combination of a boolean array and a map for speed:

    Code:java
    1. public class ExamplePodThing {
    2.  
    3. private final Map<UUID, Integer> seat = new HashMap<>();
    4. private final boolean[] occupied;
    5.  
    6. public ExamplePodThing(int numOfPods) {
    7. this.occupied = new boolean[numOfPods];
    8. }
    9.  
    10. /**
    11.   * Called when a player joins
    12.   */
    13. public void join(Player p) {
    14. int pod = this.getNextPod();
    15. //set the player's pod using either pod or pod+1 (since it's 0-based index)
    16. //But be consistent
    17. this.occupied[pod] = true;
    18. this.seat.put(p.getUniqueId(), pod);
    19. }
    20.  
    21. /**
    22.   * Finds the next pod number
    23.   *
    24.   * @return The next pod number, or -1 if none are available
    25.   */
    26. public int getNextPod() {
    27. for (int w = 0; w < this.occupied.length; w++) {
    28. if (!this.occupied[w]) {
    29. return w;
    30. }
    31. }
    32. return -w;
    33. }
    34.  
    35. /**
    36.   * Called when a player leaves
    37.   */
    38. public void leave(Player p) {
    39. this.occupied[this.seat.remove(p.getUniqueId())] = false;
    40. }
    41.  
    42. }
     
Thread Status:
Not open for further replies.

Share This Page