Getting all values from hashmap

Discussion in 'Plugin Development' started by ProMCKingz, Dec 15, 2014.

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

    ProMCKingz

    Hey,
    How would I iterate in order to get all values from a hashmap?
    My current code, gives me error on the getPlayer part, how would I get all the players in the map?
    Code:
    public class LobbyCount extends BukkitRunnable{
     
        Main plugin;
        public LobbyCount(Main pl){
            plugin = pl;
        }
     
        int min = JavaPlugin.getPlugin(Main.class).getConfig().getInt("Min-Players");
    
        public static int tts;
     
        public void run(){
                    if(tts == 0){
                        Game.start();
                    }
                 
                    if (tts % 10 ==0 || tts<10){
                        ChatUtils.broadcast(String.valueOf(tts) + " seconds until the game starts!");
            }
                    if (tts >= 1){
                        tts-= 1;
        }else if(tts <= 1){
            plugin.stopCountdown();
           int i = 0;
            for(Player player : Bukkit.getPlayer(plugin.InGame.values())){
                if(i >= Team.getAllTeams().size())
                i = 0;
                Team.getTeam(Team.getAllTeams().get(i++).getName()).add(player);
        }
    }
    }
    }
     
  2. hashmap.keySet()
     
  3. Offline

    ProMCKingz

    @FisheyLP
    That still gives errors

    Code:
      int i = 0;
             for(Player player : Bukkit.getPlayer(plugin.InGame.keySet())){
                if(i >= Team.getAllTeams().size())
                 i = 0;
                 Team.getTeam(Team.getAllTeams().get(i++).getName()).add(player);
    
     
  4. Bukkit.getPlayer() needs a String in it and not a Set<String>. loop through the Hashmap and then get the player:
    Code:java
    1.  
    2. for (String key : hashmap.keySet()) {
    3. Player player = Bukkit.getPlayer(key);
    4. }
    5.  
     
  5. Offline

    ProMCKingz

    @FisheyLP
    Code:
    plugin.InGame.keySet()) {
    wants to be a Player instead of string; and
    Code:
    Player player = Bukkit.getPlayer(key);
    wants String

    PS, I want all the players in the map, not just one
     
  6. maybe explain more detailed what HashMap this is??
     
  7. Offline

    ProMCKingz

    @FisheyLP
    This hashmap located in main class:
    Code:
    public static HashMap<Player, ArrayList<Block>> InGame = new HashMap<Player, ArrayList<Block>>();
    Used to check players in game.
     
  8. Offline

    Webbeh

    Code:
    for (Player p : InGame.keySet())
    {
     ..do your code on the player here.
    }
    
    Also storing "Player" instances in a hashmap is a bad practice and WILL result in something buggy. Store the UUID of the player and check if online when you need it.
     
  9. Offline

    ProMCKingz

    @Webbeh
    Will that code get all players in the map?
     
  10. Offline

    Webbeh

    I think you should go read some java tutorials before going further, because a for loop is one of the basics of any language.

    Short answer : yes.
     
  11. Offline

    mythbusterma

    @Webbeh

    It's not buggy, it will just leak memory all over the place if he doesn't remove the player when they log off, which, considering he's having trouble making a for-each loop, wouldn't surprise me.


    @ProMCKingz

    Read this: The Java™ Tutorials
     
  12. Offline

    Webbeh

    @mythbusterma Indeed. But there are real failures when you use the Player instance even without logging off, I've had that when I dealt with Player instances with my plugin and didn't handle them right.
     
  13. Offline

    ProMCKingz

    @mythbusterma
    I do remove it when they logg off.
    Team.getTeam(player).remove(player);

    @Webbeh
    Thank you, it worked
     
    Last edited: Dec 15, 2014
Thread Status:
Not open for further replies.

Share This Page