Saving hashmaps

Discussion in 'Plugin Development' started by CraftBang, Oct 5, 2013.

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

    CraftBang

    Hey guys,
    I'm trying to save a hashmap but I can't figure it out.
    I tried to edit this code : www.youtube.com/watch?v=IMA2dXJR-7E so it would work the way I wanted but I couldn't figure it out.

    So here's my code for creating the kills and hashmap

    Code:
         public HashMap<String, Integer> kills = new HashMap<String, Integer>(); //before onload
    Than this event
    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerDeath(PlayerDeathEvent event) {
            if (event.getEntity().getKiller() instanceof Player) {
                Player killer = event.getEntity().getKiller();
                if (!kills.containsKey(killer.getName()))
                    kills.put(killer.getName(), 0);
                kills.put(killer.getName(), kills.get(killer.getName()) + 1);
            }
        }
    Which works fine btw!
    But I need to save it to a config file.
    Can someone help me with this ?

    Thanks in advance,
    CB
     
  2. Offline

    Compressions

    CraftBang
    Code:
        public void serializeHashMap(HashMap<String, Integer> map) {
            for(Entry<String, Integer> entry : map.entrySet()) {
                getConfig().set(entry.getKey(), entry.getValue());
            }
            saveConfig();
        }
     
    CraftBang and 3ptO like this.
  3. Offline

    CraftBang

    Do you maybe also know to use onLoad the code to let it split and things?

    Thanks a lot already.

    And is it possible to make more configs so also for death config? how should I do that.

    Thanks in advance,
    CB

    But I can safe it in the same config? Or?
    3ptO Thanks a lot already bro!
    You're a hero , going to add and test it now :)

    Do I need to import the Entry from Java.util.map ?

    How do I let these 2 voids run, like one onLoad and one on Disable, I tried serializeKills();
    but than it gives an error (in the code)

    3ptO Hehe Okay thanks bro!
    Problem :
    OnLoad :
    killMap cannot be resolved to a variable

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

    CraftBang

    Main code :
    Code:
    package me.CraftBang.MCInfinite;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map.Entry;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;
     
     
    public class MCInfinite extends JavaPlugin implements Listener {
    public final Logger logger = Logger.getLogger("Minecraft");
    public static MCInfinite plugin;
    public final MyBlockListener bl = new MyBlockListener(this);
     
    public static ScoreboardManager sbManager;
    public static Scoreboard sBoard;
    public static Objective obj;
     
    @Override
    public void onEnable(){
    PluginDescriptionFile pdffile = this.getDescription();
    this.logger.info(pdffile.getName() + " has been enabled");
    getServer().getPluginManager().registerEvents(this.bl, this);
     
      sbManager = getServer().getScoreboardManager();
    sBoard = sbManager.getNewScoreboard();
    obj = sBoard.registerNewObjective("SideBoard", "dummy");
     
    ScoreBoardCreater();
    for (Player player : Bukkit.getOnlinePlayers()){
    player.setScoreboard(sBoard);
     
    deserializeKills(killMap);
    }
     
    }
    @Override
    public void onDisable() {
    PluginDescriptionFile pdffile = this.getDescription();
    this.logger.info(pdffile.getName() + "has been disabled "+ "Created by " + pdffile.getAuthors() + " Version :"+ pdffile.getVersion());
    }
     
    // SAFE KILLS
    public void serializeKills(HashMap<String, Integer> killMap) {
      List<String> list = new ArrayList<>();
     
      for (Entry<String, Integer> entry : killMap.entrySet()) {
          list.add(entry.getKey() + ":" + entry.getValue());
      }
      getConfig().set("Kills", list);
      saveConfig();
    }
     
    public void deserializeKills(HashMap<String, Integer> killMap) {
      List<String> list = getConfig().getStringList("Kills");
     
      for (String string : list) {
          String one = string.split(":")[0];
          String two = string.split(":")[1];
     
          killMap.put(one, Integer.getInteger(two));
      }
    }
    // SAFE KILLS DONE
    
    MyBlockListener.java
    Code:
    package me.CraftBang.MCInfinite;
     
    import java.util.HashMap;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.scoreboard.Score;
     
     
    public class MyBlockListener implements Listener{
    public final Logger logger = Logger.getLogger("Minecraft");
    public static MCInfinite plugin;
     
    //killsshit
        public static HashMap<String, Integer> kills = new HashMap<String, Integer>();
    //kills thingey
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerDeath(PlayerDeathEvent event) {
            if (event.getEntity().getKiller() instanceof Player) {
                Player killer = event.getEntity().getKiller();
                if (!kills.containsKey(killer.getName()))
                    kills.put(killer.getName(), 0);
                kills.put(killer.getName(), kills.get(killer.getName()) + 1);
            }
        }
       
       
    //kills thingeydone
    
    Now it seems like I'm using 2 different hashmaps, kills and killMap ???
     
  5. Offline

    CraftBang

    3ptO

    No error now :)
    Just tested it:

    Results:
    Saving is fine, config.yml :
    Kills:
    - Priceuser_:1
    - 99IRock:0
    But...,
    OnEnable, deserializeKills(kills) isn't working.
    My kills are again 0
    Is it maybe a problem with this :
    onCommand /kills
    Code:
    if(commandlabel.equalsIgnoreCase(("kills"))){
    if(args.length == 0){
    if(kills.get(sender.getName()) == null){
                kills.put(sender.getName(), 0);
    }
    sender.sendMessage("Kills : " + kills.get(sender.getName()));
    }
    
    Because when I remove that about kills == null and set to 0, the problem is that the whole kills counting won't work anymore. (null + 1 = still 0 xD) but I might have an idea for that in the kill code. but about the deserializeKills no idea

    I tried something but I couldn't figure out, I tried changing kills.put to MCInfinite.kills.put (MCInfinite is the main class)
    But didnt work... And I still don't understand why that deserialize doesn't works.

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

    CraftBang

    3ptO I just don't know whats wrong with the code, I checked it and it looks fine but mhm ?
     
  7. Offline

    Skyost

  8. Offline

    Doodledew

    I also need help with this! 3ptO
    EDIT: How can I refresh the scorboard (like every minute)?
     
  9. Offline

    CraftBang

    I don't really understand why 3ptO 's post are deleted or why he removed them ( I don't know if he did)

    But the code I use :
    Code:
     public void serializeKills(HashMap<String, Integer> kills) {
         List<String> list = new ArrayList<>();
      
         for (Entry<String, Integer> entry : kills.entrySet()) {
             list.add(entry.getKey() + ":" + entry.getValue());
         }
         getConfig().set("Kills", list);
         saveConfig();
     }
     
     public void deserializeKills(HashMap<String, Integer> kills) {
         List<String> list = getConfig().getStringList("Kills");
      
         for (String string : list) {
             String one = string.split(":")[0];
             String two = string.split(":")[1];
      
             kills.put(one, Integer.getInteger(two));
         }
     }
    
    Serialize DOES work fine, but Deserialize NOT.
    Still don't understand why not, I don't see any problems..
    I don't like to bumb but can someone please help me?
    IS something wrong with the splitting ?
     
Thread Status:
Not open for further replies.

Share This Page