Sending players in a Hashmap messages

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

Thread Status:
Not open for further replies.
  1. Hello guys,

    I'm now making a Mobparty plugin. I made a Hashmap for Players who are in a specific game like this:
    Code:
    public static Hashmap<Player, String> ingame = new HashMap<Player, String>();
    Now I want to send all the players in the hashmap where the String equal is to the name of the game where they are playing in.
    Can you help me?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @janderup Why not store HashMap<String, List<UUID>> instead?
    And please avoid public static
     
  3. I dont know is this is what you mean but it does not work.
    Code:
    public static HashMap<String, List<UUID>> ingame = new HashMap<String, List<UUID>>();
        private static List<UUID> list = new ArrayList<UUID>();
       
        public static void addPlayer(Player p, String game) {
            list.add(p.getUniqueId());
           
            ingame.put(game, list);
            for (Player players : list) {
                players.sendMessage("§6The game is starting.");
            }
        }
     
  4. Offline

    timtower Administrator Administrator Moderator

    @janderup Again: remove the public static.
    You don't need to have that List<UUID> list
    Get a game, loop through the list that it returns.
     
  5. When I remove the public static, It is gonna ask me to make the Hashmap static. And what I did was: I removed the List and I changed the UUID to Player because when it is a UUID, I cant send them messages. But It is not gonne work.
    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)) {
                players.sendMessage("§6The game starts");
            }
        }
     
  6. Offline

    timtower Administrator Administrator Moderator

    @janderup Then you need to remove all statics and start moving instances around.
    And then you call Bukkit.getPlayer(uuid), this will throw errors when the player leaves.
    And define "it is not gonne work"
    Why not? How not?
     
  7. When I remove every static, I can't use it in other classes. And when I move this public void, It has an error again. The server says that the problem is: ingame.get(game).add(p.getUniqueID());
    But I don't what wrong is with this.
    This is my current code:
    Code:
    HashMap<String, List<UUID>> ingame = new HashMap<String, List<UUID>>();
      
        public void addPlayer(Player p, String game) {
          
            ingame.get(game).add(p.getUniqueId());
            for (UUID players : ingame.get(game)) {
               
                Player player = Bukkit.getServer().getPlayer(players);
                player.sendMessage("§6The game starts.");
            }
        }
     
    Last edited: Jan 4, 2017
  8. Offline

    MCJoshua345

    To access functions without public static (I don't use this method myself because I am way too deep in now to turn back) is as displayed below:

    Code:
    public static <class name> instance;
    
    public static <classname> getInstance(){
      return instance;
    }
    Sorry if this is contradictory or such, I'm pretty rusty and so this may be incorrect. If so, please notify me so I can be quiet and let some better more active devs take over this thread.
     
  9. I made something which works but I don't know if it will also work when there are two games at the same time running.
    Code:
    private static HashMap<String, List<Player>> ingame = new HashMap<String, List<Player>>();
      
        public static void addPlayer(Player p, String game) {
          
            List<Player> list = new ArrayList<Player>();
            list.add(p);
          
            ingame.put(game, list);
            for (Player players : ingame.get(game)) {
               
                players.sendMessage("§6The game starts.");
            }
        }
    I think I only need to translate a Player to a List
     
  10. Offline

    Zombie_Striker

    @janderup
    You can't keep creating new lists. That overrides the existing game. Try getting the List from the map first, and if it is null, then create a new arraylist.
     
  11. @Zombie_Striker You said: Try getting the List from the map. Thats what i'm trying. But I don't know how.
     
  12. Wouldn't a for loop be simpler than a hashmap?
    Code:
    for (Player p : this.getServer().getOnlinePlayers()) { 
    }
    Am away from IDE so can't test just now
     
  13. Offline

    Zombie_Striker

    @janderup
    "inGame.get(game)" returns the list. Set "list" equal to this list, and only set it to a new arraylist if the list is null.
     
Thread Status:
Not open for further replies.

Share This Page