Storing HashMap to config

Discussion in 'Plugin Development' started by XxZHALO13Xx, Nov 17, 2014.

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

    XxZHALO13Xx

    After a player kill i want to have a method i can call in every class. something like getPKills(); and when thats called itll take the player and itll show on my scoreboard the amount of player kills. I created the scoreboard and a basic death event for a player. I created the hashmap private HashMap<String, Integer> playerKills = new HashMap<String, Integer>();

    My question is when the player kills someone. It adds to the hashmap and gets saved to the config. After that, how do i get the kills in the config or Hashmap i guess to display in the scoreboard?
     
  2. Offline

    Hawktasard

    XxZHALO13Xx
    You don't need to store the entire hashmap, Just store the kills the player has had, then put them in the hashmap on enable or on join.
     
  3. Offline

    XxZHALO13Xx

    Hawktasard but when they kill a player it needs to update

    Hawktasard Heres some of the zombie kill code i have

    Code:java
    1. public HashMap<String, Integer> kills = new HashMap<>();

    Code:java
    1.  
    2. @EventHandler
    3. public void onZombieDeath(EntityDeathEvent e){
    4. Entity deadEntity = e.getEntity();
    5. Entity killer = e.getEntity().getKiller();
    6.  
    7. if(killer instanceof Player && deadEntity instanceof Zombie){
    8. Player p = (Player) killer;
    9.  
    10. if (!kills.containsKey(p.getName())) {
    11. kills.put(p.getName(), 1);
    12. saveConfig();
    13. p.sendMessage(MAIN + ChatColor.RED + "You now have " + kills.get(p.getName() + " Zombie Kills!"));
    14.  
    15. } else {
    16. kills.put(p.getName(), kills.get(p.getName()) + 1);
    17. saveConfig();
    18. p.sendMessage(MAIN + ChatColor.RED + "You now have " + kills.get(p.getName() + " Zombie Kills!"));
    19.  
    20.  
    21. }
    22. }


    The message i get right now is you now have null zombie kills. and the other thing is that its not in my scoreboard class so how would i add it to the scoreboard

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

    Th3Br1x

    XxZHALO13Xx
    What Hawktasard meant is, you just put the kills in your HashMap like you already do. Then write the kills in your plugin's onDisable() method into a config file. And in your onEnable() you can read from that file and refill your HashMap.

    For the scoreboard, you may want to make use of Bukkit's
    Code:java
    1. Bukkit.getServer().getScoreboard()
    function.
     
    Hawktasard likes this.
  5. Offline

    XxZHALO13Xx

    Th3Br1x How would i do that? im really bad at hashmaps and configs D: and im using a API for the scoreboard
     
  6. Offline

    Th3Br1x

    XxZHALO13Xx I'll have a look into it tomorrow, as i am tired now :)
    If nobody comes here to 'do my job' you'll have my solution tomorrow.

    Well you can read up some stuff about iteration already and check the methods of Bukkit's Configuration class.
    Maybe you come to a solution by yourself till then :D
     
  7. Offline

    XxZHALO13Xx

  8. Offline

    Th3Br1x

    XxZHALO13Xx
    So, let's say, your config looks like this:
    Code:
    kills:
        Player1:
            count: 13
        Player2:
            count: 4
    And your HashMap<String, Integer> is named kMap.

    To get all Keys in a specific section ('kills' in our case), we can use the function
    Code:java
    1. getConfig().getConfigurationSection("kills").getKeys(false);


    This will return a set of strings (Set<String>) containing (in our case) the playernames saved in our config (Player1, Player2).

    Now you can iterate through that set and put the amout you get from the config into your hashmap:
    Code:java
    1. //Iteration, it's current value is 'p'.
    2.  
    3. kMap.put(p, getConfig().getInt("kills."+p+"count"));
    4.  
    5. //End of iteration


    So, we loaded the config into our hashmap, but how to save the values back from our hashmap into the config file?
    Again, with iteration. But this time, we'll iterate through the set of the keys of our hashmap. We can get these with
    Code:java
    1. kMap.keySet();


    Your iteration would look like this:

    Code:java
    1. //Iteration, it's current value is 'p'.
    2.  
    3. getConfig().set("kills."+p+".count", kMap.get(p));
    4.  
    5. //don't forget to save your config properly!
    6.  
    7. //End of iteration


    And you should be done :)
    Please consider that i won't give you the whole code, since that's not what 'we' do here on the bukkit forums.
    These are just some helpful information, the part about iteration can be read on the internet, just search in google for "java iteration". :)
     
    Skionz likes this.
Thread Status:
Not open for further replies.

Share This Page