Solved HashMap doesn't see the player

Discussion in 'Plugin Development' started by Krolewicz, Jul 21, 2018.

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

    Krolewicz

    Hello
    I add the player to the hashmap, but when the player leaves from the server and join, hashmap doesn't see the player.


    Code:
    public static final HashMap<Player, Long> mutePlayers = Maps.newHashMap();
    
    Next I add the player to the hashmap
    Code:
    Main.mutePlayers.put(target, endTime);
    
    And in the chat event

    Code:
    @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
                if (Main.mutePlayers.containsKey(e.getPlayer())) {
                        e.setCancelled(true);
                }
        }
    
    And this work.
    But when the player leaves from the server and join again the hashmap doesn't see the player, but he is in the hashmap and the player can write on chat.
    Because of this code:
    Code:
    @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
                if (Main.mutePlayers.containsKey(e.getPlayer())) {
                        e.setCancelled(true);
                }
        }
    
    doesn't cancel writing.
    Please help me :)
     
  2. Offline

    Zombie_Striker

    @Krolewicz
    This is another reason why you should not store players in the hashmap. Since a new instance of the player is created every time they relog, the player instance from before is not the same as the current one.

    Instead of storing the whole player instance, store their UUID instead.
     
    au2001 and Krolewicz like this.
Thread Status:
Not open for further replies.

Share This Page