Add to Hashmap?

Discussion in 'Plugin Development' started by Chrisdamonster, May 12, 2014.

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

    Chrisdamonster

    So, while I was making a plugin that gives you "Coins", I ran into a problem

    I have a command, /givecoins, where I can give coins to a player. For example:
    /givecoins randomuser123 20 - Would give randomuser123 20 coins.

    And then I wanted to have the command /coins, where the player can see the amount of coins they have. But, when I do /givecoins to myself, then another givecoins, it doesn't add it into the coins.

    Code:
     
    Map<String, Integer> coins1 = new HashMap<String, Integer>();
    //there is more code here, just its useless
    if (cmd.getName().equalsIgnoreCase("givecoins")){
                    if(args.length == 0){
                        player.sendMessage(ChatColor.RED + "[Error] " + ChatColor.BLUE + "Not enough arguments!" );
                    }
                    if(args.length == 1){
                        int coins = Integer.parseInt(args[0]);
                        coins1.put(player.getName(), coins);
                        player.sendMessage(ChatColor.GRAY + "[Coins] " + ChatColor.BLUE + "You have given yourself " + ChatColor.YELLOW + coins + ChatColor.BLUE + " coins.");
                        ItemStack[] items = {new ItemStack(Material.EMERALD, coins)};
                        player.getInventory().addItem(items);
                    }
                }
                if (cmd.getName().equalsIgnoreCase("coins")){
                    int current = coins1.get(player.getName());
                    player.sendMessage(ChatColor.GRAY + "[Coins] " + ChatColor.BLUE + "You currently have " + ChatColor.YELLOW + current + ChatColor.BLUE + " coins.");
                }
    Ok, let me explain this better:

    I do the command /givecoins 20 - Gives myself 20 coins
    I do /coins - Says I have 20 coins
    I do /givecoins 1000 - Gives myself 1000 coins. I should now have 1020 coins
    I do /coins - and it says I have 1000 coins, when I want it to say 1020.

    Thanks for the help!
     
  2. Offline

    thecrystalflame

    its because your putting the value directly into the hashmap overwriting the previus one. what you need to do is get the current value then add to that. like this:

    Code:java
    1.  
    2. if (coins1.contains(player.getName()) {
    3. coins1.put(player.getName(), coins1.get(player.getName()+coins);
    4. } else {
    5. coins1.put(player.getName(), coins);
    6. }
    7.  
     
  3. Offline

    Chrisdamonster

    thecrystalflame Its still not working. :/ Its giving me an error at the contains part, saying "The method contains(String) is undefined for the type Map<String,Integer>.

    Also, where would I put this piece of code exactly. The place I put it in doesn't seem right.
     
  4. Offline

    metalhedd

    its containsKey(key) not contains(key)
     
  5. Offline

    Chrisdamonster

    Heres where I put my code, tell me if this is correct:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("givecoins")){
    2. if(args.length == 0){
    3. player.sendMessage(ChatColor.RED + "[Error] " + ChatColor.BLUE + "Not enough arguments!" );
    4. }
    5. if(args.length == 1){
    6. int coins = Integer.parseInt(args[0]);
    7. if (coins1.containsKey(player.getName())){
    8. coins1.put(player.getName(), coins1.get(player.getName()+coins));
    9. } else {
    10. coins1.put(player.getName(), coins);
    11. }
    12. coins1.put(player.getName(), coins);
    13. player.sendMessage(ChatColor.GRAY + "[Coins] " + ChatColor.BLUE + "You have given yourself " + ChatColor.YELLOW + coins + ChatColor.BLUE + " coins.");
    14. ItemStack[] items = {new ItemStack(Material.EMERALD, coins)};
    15. player.getInventory().addItem(items);
    16. }
    17. }
     
  6. Offline

    metalhedd

    Code:
    coins1.put(player.getName(), coins1.get(player.getName()+coins));
    should be
    Code:
    coins1.put(player.getName(), coins1.get(player.getName())+coins);
     
  7. Offline

    Chrisdamonster

    metalhedd Still not working! Its just saying what I gave myself, rather than what the total amount of coins I have.
     
  8. Offline

    metalhedd

    delete line 12:
    Code:
    coins1.put(player.getName(), coins);
    
    you're just overwriting the value you put previously
     
  9. Offline

    Chrisdamonster

    Ok, now I have another problem :(. Whenever I reload my test server, the amount of coins reset to 0, and I am getting errors. Anyway to fix this?
     
  10. Offline

    AoH_Ruthless

    Chrisdamonster
    You have to save the values to a config, or handle the reload / restart.
    Saving the values to a config is a hell of a lot easier in my opinion.
     
  11. Offline

    Chrisdamonster

    AoH_Ruthless Well, saving it to a config might be hard, as I need to save it for each player. Not exactly sure how to do that :p.
     
  12. Offline

    AoH_Ruthless

    Chrisdamonster
    Simple. For my own economy plugin, I just saved their name followed by their coins. Granted, with the recent updates I had to shift to UUID's but it's the same concept really.
     
    itzrobotix likes this.
  13. Offline

    itzrobotix

    Simples.
    If you are using the default config file;
    public int getCoins(String uuid){
    config.get("coins." + uuid);
    }
    public void addCoins(String uuid, int amount){
    setCoins("coins." + uuid, getCoins(uuid) + amount);
    }
    public void removeCoins(String uuid, int amount){
    setCoins("coins." + uuid, getCoins(uuid) - amount);
    }
    public void setCoins(String uuid, int amount){
    config.set("coins." + uuid, amount);
    saveConfig();
    }
     
  14. Offline

    Chrisdamonster

    itzrobotix What should I put the variable "config" as. It gives me an error when its a string, and more of an error while an int.
     
  15. Offline

    AoH_Ruthless

    Chrisdamonster
    config is a variable for your getConfig(), which is a FileConfiguration
     
    itzrobotix likes this.
  16. Offline

    Wizehh

    Eh, I'd personally just let your econony class or whatever implement the Serializable interface*. It's a bit easier than using a configuration file, and a Map<UUID, Integer> implements Serializable as well. Then, you can use ObjectOutputStream to save the map to a file, and ObjectInputStream to load the object (not create a new instance of it).
    If you go this route, don't forget to mark all fields that you don't want serialized (say, a Thread object) with the transient keyword.
     
  17. Offline

    Chrisdamonster

    Whenever I reload my test server, and I do /coins, it still gives me an error!

    AoH_Ruthless Hmm, in my getCoins method, it is forcing me to put a "return 0;" after my code. This is making the amount of coins I have be always 0. Do you have anyway to fix this?

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

    AoH_Ruthless

    Chrisdamonster
    You have to return something ... that's what a getter is .. You can change it to whatever you want, the default integer is 0.

    This is pretty simple Java that you should know. If you don't know this, I would advise stopping and going to learn some Java first.
     
  19. Offline

    Chrisdamonster

    AoH_Ruthless I figured it out. All I did was just set a variable to see how many coins the player has. Thanks for the help anyways! :)
     
Thread Status:
Not open for further replies.

Share This Page