Solved Adding players to a Player list in a HashMap

Discussion in 'Plugin Development' started by janderup, Jan 20, 2017.

Thread Status:
Not open for further replies.
  1. Guys, I am making a minigame and I want to run multiple games at the same time. So the simplest way to that is making a HashMap<String, List<Player>. But when I want to add players to the list, it doesnt't work.

    Here is my code:
    Code:
    private static HashMap<String, List<Player>> ingame = new HashMap<String, List<Player>>();
      
        public static void addPlayer(Player p, String game) {
          
            ingame.get(game).add(p);
          
            for (Player players : ingame.get(game)) {
                 // Do something
            }
        }
    The String means the name of the map. The List means the players in that map
     
  2. Offline

    Zombie_Striker

    @janderup
    What do you mean by "doesn't work"? Is the player added to the list?

    Also, Do not store players. Doing that will cause memory leaks. Instead, use the player's UUID.
     
  3. @Zombie_Striker how can I send the players messages when there is a list off UUID's. And you asked: Is the player added to the list. No it isn't. The error in the console says that the problem is: ingame.get(game).add(p);
     
  4. Offline

    Zombie_Striker

    @janderup
    Bukkit.getPlayer(UUID); Just remember to null check this, since this will return a null value if the player is not online.

    What does the error print out? Post the full console log here or on Pastebin.com.
     
  5. Offline

    Zombie_Striker

    @janderup
    The List is most likely null. Are you sure you are creating the list before you try adding the UUID to it?
     
  6. I think is does because the HashMap is above the place where I add the player.
    Code:
    private static HashMap<String, List<UUID>> ingame = new HashMap<String, List<UUID>>();
       
        @SuppressWarnings("deprecation")
        public static void addPlayer(Player p, String game) {
            ingame.get(game).add(p.getUniqueId());
           
            Player players = Bukkit.getPlayer(ingame.get(game) + "");
           
            if (players == null) {
                return;
            }
           
            players.sendMessage("Test");
        }
     
  7. Offline

    Zombie_Striker

    @janderup
    For a quick fix, just add this above the add line
    Code:
    if(ingame.get(game)==null)ingame.put(game, new ArrayList<UUID>());
     
  8. I made almost the same what you did and it kind of works because it goes further. It tps me now to the waiting lobby. But it wont send me the test message.
    Code:
    ArrayList<UUID> fix = new ArrayList<UUID>();
    fix.add(UUID.randomUUID());
    ingame.put(game, fix);
     
  9. Offline

    Zombie_Striker

    @janderup
    That means the player is null.

    Edit:
    What is this meant to achieve? You store Lists of UUIDs, so why should it return only one UUID? Remove the +"" as that turns this into a string, not a UUID, and either use the player instance from the parameter (to send only one message), or for loop through the list to get the UUIDs (*to send the message to everyone in the minigame)
     
  10. When I do that, getPlayer is red underlined and when I click on it it says:
    The method getPlayer(String) in the type Bukkit is not applicable for the arguments (List<UUID>)
     
  11. Offline

    kameronn

    Wouldnt it just be better to create a Game object that contains whatever you need and just loop through the game object if you ever need to get the game? Since that would have support for more things if you ever wanted to change something.

    @janderup
    Because that is you have a list, how is java supposed to know what player you're talking about in that list? Plus getPlayer() returns a string or uuid not a List<UUID> or List<String>();

    make a for loop and loop through all the players in the map. To create a for loop, just define the player object in the for loop, seperate it by a ":" and then use ingame.get(game);.
     
  12. Is there some way like ingame.get(game).split() or something because what I now got does also not work
    Code:
    private static HashMap<String, List<UUID>> ingame = new HashMap<String, List<UUID>>();
       
        public static void addPlayer(Player p, String game) {
           
            if (ingame.get(game) == null) {
                ArrayList<UUID> fix = new ArrayList<UUID>();
                fix.add(UUID.randomUUID());
                ingame.put(game, fix);
            }
           
            ingame.get(game).add(p.getUniqueId());
           
            for (UUID uuid : ingame.get(game)) {
                Player players = Bukkit.getPlayer(uuid);
                players.sendMessage("Test");
            }
           
        }
     
  13. Offline

    Zombie_Striker

    @janderup
    This is most likely why this does not work. Remove this line.

    Does this print out any errors? How do it "not work"? Are you nullchecking the players before you use it?
     
  14. Dude, you are right! It works
     
Thread Status:
Not open for further replies.

Share This Page