1.8.8 Hashmaps issue when logging out

Discussion in 'Spigot Plugin Development' started by WhiteShiro, Sep 24, 2020.

  1. Code (Text):
        public static void statsupdate(Player player) {
            if(health.containsKey(player)) {
            ActionBarAPI.sendActionBar(player, String.valueOf(health.get(player)));
            player.sendMessage(String.valueOf(health.containsKey(player)));
            return;
            }
            health.put(player , 100);
            player.sendMessage(String.valueOf(health.containsKey(player)));
        }
    this function is put on a repeating task. Everytime i log out and relog, the health is set to 100 again, the message sent is also always true and not a single false is sent why?
     
  2. You are setting it to 100 everytime they login
     
  3. Well first don't store the Player as an object, store their UUID.

    This returns if the player is in the map, not their value in it.
    Should be #get()
     
  4. No?
    Code (Text):
        public static void statsupdate(Player player) {
            if(health.containsKey(player)) { <<--
            ActionBarAPI.sendActionBar(player, String.valueOf(health.get(player)));
            player.sendMessage(String.valueOf(health.containsKey(player)));
            return;
            }
            health.put(player , 100);
            player.sendMessage(String.valueOf(health.containsKey(player)));
        }
     
  5. Oh didn't see return, my bad
     
  6. Because you are saving the player object in the hashmap as the key. If a player logs out and back in they have a new player object. You should use their uuid as a key instead.
     
    • Agree Agree x 1