Get all players from HashMap.

Discussion in 'Plugin Development' started by Muod, Dec 8, 2013.

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

    Muod

    I have the following hashmap
    HashMap<String, Player> gamelist = ex.gamelist;

    For example when a player does /join it does
    Player p = (Player) sender;
    String player = p.getPlayer().getName();
    gamelist.put(player, p);

    , I need to make someway of it getting all the names in the hashmap gamelist.
    Like for example if i can do something like
    String blah = hashmap.GETEVERYONE //if that were a thing
    armor.put(BLAH, p.getInventory().getArmorContents());

    I need to get everyone in a hash so I can do stuff with there name like the example in green of what i want to do.
     
  2. Offline

    NinjaWAffles

    First, as the player variable can change from time to time, it's not best to store the instance of Player, rather you'd save the player name as you were doing and then doing:
    Code:Java
    1. Bukkit.getPlayer([Player's Name]);


    For retrieving player's, I'd just put the names of all the players in a list and then iterate through it like so:
    Code:Java
    1. for(String pl : players)
    2. {
    3. Player player = Bukkit.getPlayer(pl);
    4.  
    5. if(player != null)
    6. {
    7. //Do whatever...
    8. }
    9. }
     
  3. Offline

    The_Doctor_123

    Why the heck do you have a HashMap with player names as the key and Player objects as the value..??
     
    Chlorek and reider45 like this.
  4. Offline

    reider45

    For the first part can't you just use
    Code:
    Player p = (Player) sender;
    gamelist.put(p.getName());
    ? Anyways,
    Code:java
    1. for(String inthegame : gamelist){
    2. Player player = Bukkit.getServer().getPlayerExact(inthegame);
    3. if(player != null){
    4. // Bleh do your worst
    5. }
    6. }


    I believe that should work..? It basically cycles through the gamelist, and says that the string in there is a player and does things to that player :)

    --
    Well looks like I've been beaten to it, oh well
     
  5. Offline

    Muod

    reider45
    This error, Can only iterate over an array or an instance of java.lang.Iterable

    What would i put for players? Also in my case i have String player = p.getPlayer().getName();

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  6. Offline

    reider45


    Oh my bad i thought you stored them in an array list xD
     
  7. Offline

    Muod

  8. First off, you dont need to store player objects in the hashmap.
    too loop through the players in the hashmap do this:
    Code:java
    1. for(String pn : this.hashmap.keyset()){
    2. Player player = Bukkit.getServer().getPlayerExact(pn);
    3. if(player != null){
    4. // Here is where youd manipulate every player in the hashmap by using someting like:
    5. player.sendMessage("Coding is FUN!");
    6. }
    7. }
     
    reider45 likes this.
  9. Offline

    Wingzzz

    You can not call "map.put(object);" as it requires 2 parameters, Map<K, V> so in order to add an entry you must supply the key and the value. This way you can map a key to a value.

    EDIT: Ah, you thought it was a List, but even still lists call .add(...) either or- my bad.

    Alright, in order to retrieve all the values from a Map you'll want to call
    Code:java
    1. Map<String, Player> players = new HashMap<String, Player>();
    2. Collection<Player> = players.values();

    This will return a Collection<V> (essentially a collection of values with the type specified as the generic V- value in the map you're calling it from).

    You can loop through them a variety of ways, ie:
    Code:java
    1. Map<String, Player> players = new HashMap<String, Player>();
    2. for(Player player : players.values()) {
    3. // do something
    4. }


    I would like to point out that it's not particularly safe to be storing Player instances since it can lead to memory leaks/problems. A way to handle it properly is to ensure the instance is removed upon disconnect/kick etc... You need to ensure you manage the collection properly if you need to store Player instances.

    Now, if you don't really have any need to store them, then you can simple store the player names and retrieve a player instance via Bukkit.getPlayer(String name); whenever you need to use them. If you move to this method of storing them, then you would no longer need to use a map- instead you could use one of the many other collections.

    Perhaps a List or a Set would work? Collections have a variety of implementations so I'm sure you'll find one that suits the situation best.
     
  10. Offline

    reider45

    Store them in an arraylist?

    Code:java
    1. public List<String> gamelist = new ArrayList<String>();


    then

    Code:java
    1. gamelist.add(player.getName());
    2.  
    3. if(gamelist.contains(player.getName()){
    4. // Bleh bleh bleh
    5. }
    6.  
    7. gamelist.remove(player.getName());
    8.  
    9. // etc.


    I just have a personal preference towards arraylists.
     
  11. Offline

    xTrollxDudex

    sender.getName() *

    Muod
    Store the string in an ArrayList and get the player using Bukkit#getPlayerExact(String)
     
  12. Offline

    reider45

    xTrollxDudex Wouldn't mine technically do the same thing, storing the player's name, but yours doesn't check if it's a player?
     
  13. Offline

    xTrollxDudex

    reider45
    I was pointing out that you should use sender.getName() instead of getting a redundant cast to player in order to get the name.
     
  14. Offline

    reider45


    Oh i see what you're saying, I was in a totally different mindset of what Muod was saying xD
     
  15. Offline

    Muod

    reider45 , Instead of using a hashmap should i use a list? Would it work the same?
     
  16. Offline

    reider45

    Id personally use a list, they're a bit simpler IMO and for what youre using it for they would work well Muod
     
  17. Offline

    Muod

    reider45, Now Im using a list called shooters and runners. How would I get the players from shooters. I want to message them all? Now that I have a list.

    NinjaWAffles
    The_Doctor_123

    Mind helping me now that its a list? How do I get the players from a list?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  18. Offline

    xTrollxDudex

    Muod
    Please learn java, this is basic stuff... You use a for-each loop, hint, hint
     
  19. Offline

    Muod

    xTrollxDudex Sorry I just never needed to store data in any of the plugins I have made. Also the ones I did I just used the configuration..

    xTrollxDudex , Like this?

    Code:java
    1. for (Player pl : yourArray){
    2. pl.sendMessage(".............");
    3. }



    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  20. Offline

    xTrollxDudex

  21. Offline

    The_Doctor_123

    Umm.. but don't you think you would need to know that information?
     
Thread Status:
Not open for further replies.

Share This Page