How to test hashmap players/ send messages to hashmap players

Discussion in 'Plugin Development' started by SeanyJo, Sep 13, 2013.

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

    SeanyJo

    Hi, I was wondering how you would check to see how many players are in a hashmap, and how to send messages only to the players in the hashmap.
     
  2. Offline

    tommycake50

    map.entrySet()
     
  3. Offline

    SeanyJo

    I need a little more information than that..
     
  4. Offline

    tommycake50

    Bukkit.getPayer(string).sendMessage()
    map.entrySet().size()
    Really its not hard at all.
     
  5. Offline

    SeanyJo

    t says map can not be resolved, and do I put the plugin.hashmap replacing String? How do I only broadcast messages to people in the hashmap?

    Hi, I was wondering how you would check to see how many players are in a hashmap, and how to send messages only to the players in the hashmap.

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

    Chinwe

    SeanyJo
    That's because by 'map' he means your map, whatever its name is. Then you loop through yourMap.keyset(), and for every String in there (assuming it is <String, ???>, .sendMessage() to Bukkit.getPlayerExact(theStringFromTheLoop); :)
     
    tommycake50 likes this.
  7. Offline

    tommycake50

    Code:java
    1.  
    2. size = hashmap.entrySet().size();
    3. for(String s : hashmap.keySet()){
    4. Player p : Bukkit.getPlayer(s);
    5. s.sendMessage(...);
    6. }
    7.  
     
  8. Offline

    OracleTarget

    @SeanyJo

    Use something like this:
    Code:
     
    for(Entry<//first thing in your hashmap, //secondthing in your hashmap > pl : //hashmapname.entrySet()){
        pl.getKey().sendMessage("HELLO!");
     
    }
    Hope its clear :D
     
  9. Offline

    SeanyJo

    Bukkit.getPlayer(string).sendMessage(ChatColor.GREEN + "HI!");

    plugin.hashmap.entrySet().size();
    thats what I have so far, and I still don't get the string thing, and I don't know if every other part of this is correct or not.
     
  10. Offline

    tommycake50

    Except you wouldn't be storing a player variable.
     
  11. Offline

    sharp237

    Do (if your hashmap is called players) players.getKeys();
    then just do a for loop through them and send the message.
     
  12. Offline

    OracleTarget

    tommycake50

    You can fix it like this (or not, I am not sure){
    Code:
    for(Entry<Player, //secondthing in your hashmap > pl : //hashmapname.entrySet()){
    pl.getKey().sendMessage("HELLO!");
     
    }
     
  13. Offline

    SeanyJo

  14. Offline

    tommycake50

    One thing Never EVER store player instances(EVER)!
     
    mattrick16 likes this.
  15. Offline

    SeanyJo

    I don't get that..
    I'm a beginner in Java also.
     
  16. Offline

    OracleTarget

    tommycake50

    Sorry, I am also quite new in developing plugins(3 months or so), but could you please explain it why I shouldn't do that?

    thnx!
     
  17. Offline

    tommycake50

    Well, if you have an instance of a player, Java's garbage collection cant finalize and destroy the instance so that means all the loaded chunks that it has loaded, all the plugins that use that player etc, etc will still think the player exists rather than the player leaving and no further calls being made to it so the garbage collector disposes of it and cleans up properly that doesn't happen and you'll probably get problems because of it(Not to mention it causes a memory leak so it wastes RAM).
    Essentially it just locks things up in a bad way.
    So by having a player in your list the garbage collector can't get rid of it because calls can be made to lists maps and RAM stored mediums at any time so its not fit for collection.
     
  18. Offline

    SeanyJo

    are you talking to me or OracleTarget?
     
  19. Offline

    tommycake50

    You both really need to know that.
     
  20. Offline

    SeanyJo

    How could I fix this problem?
     
  21. Offline

    tommycake50

    Store string objects and use Bukkit.getPlayerExact().
     
  22. Offline

    SeanyJo

    Where would I put that? Sorry, i'm a beginner in Bukkit and Java API

    Or, could I make an event on Player Quit where they leave the hashmap on certain commands and leaving the server?

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

    tommycake50

    No, that's bad coding practice.
    But you can remove their name on command and leaving the server :).
     
  24. Offline

    SeanyJo

    How would I do that?
     
  25. Offline

    tommycake50

    map.remove(player.getName());
     
  26. Offline

    mattrick

    Many people active on the forums have a signature explaining why you should never include player instances inside a hashmap. It can lead to memory leaks, lag, and is an overall bad coding practice in general. Use player.getName() store it in a hashmap and for loop for players and get 1 player at a time and send message to said player.
     
  27. Offline

    Quantix

    This code below is an example of sending players in a HashMap a message using their stored names.
    Code:java
    1. public HashMap<String, Integer> examplePlayerMap = new HashMap<String, Integer>();
    2. //The integer is just an example as I'm not sure what your values are for the keys in the HashMap
    3.  
    4. public void tellPlayersInMap(String message) {
    5. for (String playerName : examplePlayerMap.keySet()) {
    6. Player player = Bukkit.getPlayerExact(playerName);
    7. if (player != null) {
    8. player.sendMessage(message);
    9. }
    10. }
    11. }
    12.  
    13. //This is an example usage of the tellPlayersInMap() method
    14. tellPlayersInMap("Hey! You're in a HashMap.");
     
  28. Offline

    SeanyJo

    Would the tellPlayersInMap be replaced with anything? My hashmap name is hashmap.
     
  29. Offline

    Quantix

    You can change the name of the method to anything you want. All you have to do is replace the examplePlayerMap (the name of my example HashMap) to the name of your HashMap. (If you want that method to be even more versatile you could add a HashMap parameter and specify which HashMap's keys the method will iterate through.)
     
Thread Status:
Not open for further replies.

Share This Page