Saving a Hashmap?

Discussion in 'Plugin Development' started by BeefySticks, Oct 16, 2014.

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

    BeefySticks

    Here we go again.... So my problem is, I am saving the players kills in the config, it does save but how would I get it? So far here is what I have.

    Code:java
    1. private static HashMap<Player, Integer> kills = new HashMap<Player, Integer>();
    2.  
    3. // Saving Config
    4.  
    5. public void onDisable() {
    6. System.out.println("[FFA] has been disabled!");
    7. getConfig().options().copyDefaults(true);
    8. List<String> s = getConfig().getStringList("kills");
    9.  
    10. for (OfflinePlayer p : kills.keySet()) {
    11. s.add(p.getName() + ":" + kills.get(p));
    12. }
    13.  
    14. getConfig().set("kills", s);
    15. saveConfig();
    16. }
    17.  
    18. // Adding Kills
    19.  
    20. @EventHandler
    21. public void kills(PlayerDeathEvent e){
    22.  
    23. Player killer = e.getEntity().getKiller();
    24. Player killed = (Player) e.getEntity();
    25. if(e.getEntity().getKiller() instanceof Player){
    26. if(e.getEntity() instanceof Player){
    27.  
    28. if(!kills.containsKey(killer)){
    29.  
    30. kills.put(killer, 1);
    31. saveConfig();
    32. }
    33.  
    34. if(kills.containsKey(killer)){
    35. kills.put(killer, kills.get(killer) + 1);
    36. saveConfig();
    37.  
    38.  
    39. }
    40. }
    41. }
    42. }
    43. // Where I want to get the kills
    44. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    45. Player player = (Player) sender;
    46. if(label.equalsIgnoreCase("stats")) {
    47. if (args.length == 0) {
    48.  
    49. player.sendMessage(prefix + ChatColor.GOLD + player.getName() + "'s" + ChatColor.GREEN + " Stats:");
    50. player.sendMessage(prefix + ChatColor.YELLOW + "Bronze: " + ChatColor.GOLD + bronze.get(player));
    51. player.sendMessage(prefix + ChatColor.YELLOW + "Kills: " + ChatColor.GOLD + kills.get(player));
    52. player.sendMessage(prefix + ChatColor.YELLOW + "Deaths: " + ChatColor.GOLD + deaths.get(player));
    53. player.sendMessage(prefix + ChatColor.RED + ChatColor.BOLD + "FEATURE NOT FINISHED");
    54. return true;
    55. }
    56. Player target = Bukkit.getServer().getPlayer(args[0]);
    57. if (target == null) {
    58. player.sendMessage(prefix + ChatColor.DARK_RED + "That player is currently not online!");
    59. }
    60. if (target != null) {
    61.  
    62. player.sendMessage(prefix + ChatColor.GOLD + target.getName() + "'s" + ChatColor.GREEN + "' Stats:");
    63. player.sendMessage(prefix + ChatColor.YELLOW + "Bronze: " + ChatColor.GOLD + bronze.get(target));
    64. player.sendMessage(prefix + ChatColor.YELLOW + "Kills: " + ChatColor.GOLD);
    65. player.sendMessage(prefix + ChatColor.YELLOW + "Deaths: " + ChatColor.GOLD + deaths.get(target));
    66. player.sendMessage(prefix + ChatColor.RED + ChatColor.BOLD + "FEATURE NOT FINISHED");
    67. }
    68. }
    69. return false;
    70. }


    So far I've tried coming up with this, but not sure if it would work.
    Code:java
    1. List<String> cut = getConfig().getStringList("scores");
    2. for (String cut : array) {
    3. // Spliting [Player Name] : Kiills
    4. String[] words = cut.split(":");
    5. kills.put(???),
     
  2. Offline

    FerusGrim

    Assuming that StringList is UUID:Kill-Count, and the HashMap is UUID:Integer:

    Code:
    kills.put(UUID.fromString(words[0]), Integer.valueOf(words[1]))
     
    BeefySticks likes this.
  3. Offline

    fireblast709

    BeefySticks you update the Map, but you never update the config. Also, remove the static keyword.
     
    BeefySticks and FerusGrim like this.
  4. Offline

    BeefySticks

    FerusGrim


    Sorry for the late response!
    The hashmap is Player, Integer and the string list isn't.
    Should I changed it?

    fireblast709
    I've fixed that!
     
  5. Offline

    FerusGrim

    BeefySticks It would be much easier to store UUIDs, rather than Player objects. Showing us this magical config file that you have may assist in determining the best way to save (and load) the HashMap. :)
     
  6. Offline

    BeefySticks

    FerusGrim
    Here is the config file :)

    Code:java
    1. kills:
    2. - ItsBeefu: 1
    3. - ItsBeefu: 2
    4. - ItsBeefu: 5


    Note, another problem is it does not add onto my current kills in the config. It just adds another one when reloading the server.
     
  7. Offline

    Konato_K

Thread Status:
Not open for further replies.

Share This Page