HashMap Troubles Replacing A String Name with player.getName()

Discussion in 'Plugin Development' started by mkezar, Mar 9, 2015.

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

    mkezar

    hello! im writing a plugin where you join the server, and you get a score. that score increments by 1 every 20 ticks. in the hashmap i put player.getName(), when i tested it, all it said was "Your Score is 0" and kept spamming it. when i put "Mkezar" in the string, it would increment. heres my code:
    Code:
    package plugins.mkezar;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class Age extends JavaPlugin implements Listener {
       
        HashMap<String, Integer> hm = new HashMap<String, Integer>();
        private static BukkitRunnable age = null;
    
        public void onEnable() {
             Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
            public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            hm.put("Mkezar", 0);
            age = new BukkitRunnable() {
                @Override
                public void run() {
                    Integer i = hm.get("Mkezar");
                    i = i + 1;
                    hm.put("Mkezar", i);
                    player.sendMessage("Your Age Is " + i);
                    if(i == 100) {
                        player.setHealth(0);
                        i = 0;
                        hm.put("Mkezar", i);
                    }
                     
                }
              };
                
              age.runTaskTimer(this, 0, 20);
             
         }
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent e) {
            age.cancel();
        }
           
           
    }
    
    this is the code i have now that increments my score. Please help and maybe tell me why the score isnt incrementing with player.getName()? thnx :D
     
  2. @mkezar Ahh, this code looks like it was built for errors. You're using a static BukkitRunnable for starters. And why are you hardcoding the name?
    • Why are you using Integer instead of int? (line 31)
    • Have you learnt the Java way of incrementing values? (line 32)
      Code:
      i++;
    • Line 33 and line 38 do the same thing. Just run it once (I suggest putting it at line 40)
    • For line 38, why are you setting the value of i to 0? Why not just put the name and the number 0.
    • What happens when another player joins then leaves? It'll overwrite your old runnable and keep one running forever.
     
  3. Offline

    mkezar

    The reason why I have the same code for line 33 and 38 is because I'm setting I to 0 so I want to put that in the hashmap. It really dosnt matter int or Integer. (Hashmaps don't accept int, only Integer), when the player leaves I put a age.cancel() so it dosnt run forever. Im using my name because player.getName() would only tell me, "your score is 0". And I find bukkit runnables a tiny bit better in this situation.

    Please? :(

    i got it working. i replaced player.getName() with player.getUniqueID().toString() and that did the trick!!!!! :D now i need to put the hashmap in a config.yml :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page