Add a variable to a player? [SOLVED]

Discussion in 'Plugin Development' started by MrSoLegitimate, Aug 24, 2012.

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

    MrSoLegitimate

    I need to have an integer variable for every player. Like say when they kill a zombie, I can keep track of how many zombies they've killed.

    Anyone know how to do this?
     
  2. Offline

    kyle1320

    MrSoLegitimate
    Using a hashmap is a very common way to do this:
    Code:
    HashMap<String, Integer> zombieCount = new HashMap<String, Integer>();
    A hashmap will store 1 variable per 1 key. In this case your key would be the player name, and your variable would be the number of zombies they killed. To add them to the hashmap you would use:
    Code:
    zombieCount.put(player.getName(), 0);
    And to increase their count by 1 you could use:
    Code:
    zombieCount.put(player.getName(), zombieCount.get(player.getName()) + 1);
    zombieCount.get(player.getName()) returns the variable stored with that player.
    Make sure they are in the hashmap before trying to get their variable, or you will get a NullPointerError. You can use:
    Code:
    if (!zombieCount.containsKey(player.getName())){
        zombieCount.put(player.getName(), 0);
    }
    before working with the hashmap to avoid these.
    EDIT: Okay people, I changed it to use a name instead, it just seemed easier to explain by using a Player. >_>
     
    MrSoLegitimate likes this.
  3. Offline

    MrSoLegitimate

    This works yes, But I was looking for a more permanent way of doing this. Like having one of those .yml's that other plugins use. like for say it has a list of all the players, and for each player it has an integer 'killCount'. Each time they get a kill it will write to the yml and increase it by 1 according to who killed the zombie.
     
  4. Offline

    greatman

    Stocking the Player class can lead to memory problem. It's better to keep only the player name.
     
  5. Please don't use that code, instead use this, almost the same:
    Code:
    HashMap<String, Integer> zombieCount = new HashMap<String, Integer>();
    And than you have to do this when you put the count in the hashmap:
    Code:
    zombieCount.put(player.getName(), count);
    As you see you have to use the players name instead of a player, you also have to use the players name in the other functions kyle showed you.
    Storing a Player in a hashmap is a very bad idea, see this page why: http://forums.bukkit.org/threads/why-not-to-save-players-to-lists-sets-whatever.83975/

    greetz blackwolf12333
     
  6. Offline

    kyle1320

    Ah, you're right. I'll edit my post, just figured it would be easier to explain if I just used a Player in the post :p
     
  7. Offline

    MrSoLegitimate

    This works yes, But I was looking for a more permanent way of doing this. Like having one of those .yml's that other plugins use. like for say it has a list of all the players, and for each player it has an integer 'killCount'. Each time they get a kill it will write to the yml and increase it by 1 according to who killed the zombie.
     
  8. Than take a look at this: http://wiki.bukkit.org/Introduction...Reloading.2C_and_Saving_Custom_Configurations
     
  9. Offline

    sternmin8or

    I dont reccomend writing to a file everytime someone kills something. you should look into saving hashmaps when your server shuts down and loading them when it restarts
     
  10. Offline

    MrSoLegitimate

    Ah ok I got it now. Thanks guys!

    Actually no I have one problem:

    Code:
    17:38:24 [INFO] [ZabaCraft] Disabling ZabaCraft v1.1
    17:38:24 [SEVERE] java.io.FileNotFoundException: plugins\ZabaCraft\killCounts.bi
    n (The system cannot find the path specified)
    17:38:24 [SEVERE]       at java.io.FileOutputStream.open(Native Method)
    17:38:24 [SEVERE]       at java.io.FileOutputStream.<init>(Unknown Source)
    17:38:24 [SEVERE]       at java.io.FileOutputStream.<init>(Unknown Source)
    17:38:24 [SEVERE]       at zaba.ZabaCraft.saveKillCounts(ZabaCraft.java:22)
    17:38:24 [SEVERE]       at zaba.ZabaCraft.onDisable(ZabaCraft.java:60)
    This is getting thrown in my 'onDisable()' function.

    Here is my 'onEnable()':
    Code:
      public void onEnable() {
      File killCountFile = new File(getDataFolder() + File.separator + "killCounts.bin");
      if(killCountFile.exists())
      killCount = loadKillCounts();
    }
    Here is my 'onDisable()':
    Code:
    public void onDisable() {
      saveKillCounts();
        }
    Here is my 'loadKillCounts()':
    Code:
    public HashMap<String, Integer> loadKillCounts()
    {
    try
    {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(getDataFolder() + File.separator + "killCounts.bin"));
    Object result = ois.readObject();
    //you can feel free to cast result to HashMap<String, Integer> if you know there's that HashMap in the file
    return (HashMap<String, Integer>)result;
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    return new HashMap<String, Integer>();
    }
    Here is my 'saveKillCounts()':
    Code:
    public void saveKillCounts() {
    try {
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(getDataFolder() + File.separator + "killCounts.bin"));
    oos.writeObject(killCount);
    oos.flush();
    oos.close();
    }
    catch(Exception e) {
    e.printStackTrace();
    }
    }
    Bump. I really need help with this D:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  11. Offline

    ryr11

    So how do I get the value of the hashmap?
     
Thread Status:
Not open for further replies.

Share This Page