Solved HashMap adding value help!

Discussion in 'Plugin Development' started by Charliechumbuck, Jun 28, 2014.

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

    Charliechumbuck

    I made a hashmap to store a player's xp (I'm not talking about actual Minecraft xp, I'm talking about the hashmap which is names "playerXp" that has an integer value).

    The problem is, I don't know how to add to the value of the hashmap.

    Here's what I did so far:

    public HashMap<Player,Integer> playerXp = new HashMap<Player,Integer>();

    Then, in a method, I said:

    playerXp.put(p,10); (I want to change the "10" to the value of the hashmap plus ten)

    Please tag me if you can help me! Help is appreciated! I don't know why it wouldn't be! :)
     
  2. Offline

    1Rogue

    Get the value first, then add to it.

    Also, store by the player's UUID, not the player object.
     
    Garris0n likes this.
  3. Offline

    Charliechumbuck

    I don't know what you mean

    Here's my code:

    Code:java
    1. package minecraft.Charliechumbuck.me;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.BlockBreakEvent;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class RPGMain extends JavaPlugin implements Listener{
    14.  
    15. public int level;
    16. public int coins;
    17. public int neededXp;
    18. public int xp;
    19. public int world;
    20. public double maxHealth;
    21. public HashMap<Player,Integer> playerXp = new HashMap<Player,Integer>();
    22.  
    23. public void onEnable(){
    24. getServer().getPluginManager().registerEvents(this,this);
    25. }
    26. public void onDisable(){}
    27.  
    28. public void getWorld(){
    29. switch(world){
    30. case 1:
    31.  
    32. }
    33. }
    34.  
    35. @EventHandler
    36. public void onStartup(PlayerJoinEvent e){
    37. Player p = e.getPlayer();
    38. world = 1;
    39. level = 1;
    40. playerXp.put(p, 0);
    41. coins = 0;
    42. neededXp = 50;
    43. maxHealth = 20;
    44. }
    45.  
    46. @EventHandler
    47. public void onBlockBreak(BlockBreakEvent e){
    48. Player p = e.getPlayer();
    49. if(e.getBlock().getType().equals(Material.IRON_ORE)){
    50. //playerXp.put(p, playerXp.get(p)+10);
    51. p.sendMessage("PlayerXp: "+playerXp.get(p));
    52. }
    53. //if(xp >= neededXp){
    54. //level ++;
    55. //neededXp += 10;
    56. //xp = 0;
    57. //maxHealth++;
    58. //p.setMaxHealth(maxHealth);
    59. //p.setHealth(maxHealth);
    60. //p.setExhaustion(20);
    61. //}
    62. }
    63. }


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

    Garris0n

    He means get the value of of the Map (map.get(yourKey)) and then set the value to whatever you want.

    He also means that you should use a HashMap<UUID, Integer> instead of a HashMap<Player, Integer> because storing player instances is bad. You can then use put(player.getUniqueId(), 10) and get(player.getUniqueId()). Alternatively, you could use a WeakHashMap, but I don't think trying to explain that is a good idea seeing as don't seem to know how to use a map at all.
     
  5. Offline

    Charliechumbuck

    How am I supposed to get the player UUID from the event?

    Now I have "Player p = e.getPlayer();" Garris0n
     
  6. Offline

    Garris0n

    You could try reading what I said again.
     
  7. Offline

    Charliechumbuck

    Garris0n
    I did read what you said! I'm saying I don't know how to get they UUID of the player that performed a certain event.

    Here's how I get the player who performed the event:

    public void blockBreak(BlockBreakEvent e){
    Player p = e.getPlayer();
    }

    I'm saying that I don't know how to do the same with the Player UUID.

    Garris0n
    Hey! I finally got the whole UUID thing down pat! Only one problem: I still don't know how to add to the integer value.

    Here's the line of coding where I put the new Integer value in (I changed the HashMap from <Player, Integer> to <UUID, Integer>):

    playerXp.put(p.getUniqueId(),the current integer value goes here+10);

    So, the problem is I don't know how to add on to the current integer.

    By the way, I might not reply until tomorrow.

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

    Garris0n

  9. Offline

    Charliechumbuck

    Garris0n
    Okay, I tried to do this:

    playerXp.put(p.getUniqueID,playerXp.get(p.UniqueID)+10);

    However, when I break an iron ore block (because that's what triggers this code), it has an error, but when I take out the "playerXp.get(p.UniqueID)" and leave the code at "playerXp.put(p.getUniqueID,10)", it has no error, but it doesn't do what I want it to do as it keeps the current value at ten and doesn't increase it (but you already knew that :))
     
  10. Offline

    61352151511

    You need the parenthesis/brackets/whatever people call them at the end of getUniqueId so It's getUniqueId()

    Code:java
    1. playerXp.put(p.getUniqueId(), playerXp.get(p.getUniqueId()) + 10);

    However you'll want to make sure they are in the hashmap before you do that because if they're not you can't add 10 to NULL

    Code:java
    1. if (playerXp.containsKey(p.getUniqueId())) {
    2. playerXp.put(p.getUniqueId(), playerXp.get(p.getUniqueId()) + 10);
    3. } else {
    4. playerXp.put(p.getUniqueId(), 10);
    5. }
     
  11. Offline

    Charliechumbuck

    Garris0n
    FORGET THAT POST!!!!!! I FINALLY DID IT!!! Thanks to you, I can finally do a variable that changes only to a player which can lead up to some great mini-games possibly in the near future. :D
     
Thread Status:
Not open for further replies.

Share This Page