Saving hashmaps not working...

Discussion in 'Spigot Plugin Development' started by JacksonHogan, Jun 12, 2016.

  1. I'm trying to save/load hashmaps, This is how i use them..
    Main:

    Code (Text):
    public static HashMap<Player, ItemStack[]> inventory = new HashMap<Player, ItemStack[]>();
        public static HashMap<Player, ItemStack[]> armor = new HashMap<Player, ItemStack[]>();
        public static HashMap<Player, Integer> exp = new HashMap<Player, Integer>();

     
    on the command join:
    Code (Text):
    } else if ((args.length == 1) && (args[0].equalsIgnoreCase("join"))) {
                        if (p.hasPermission("lms.join")) {
                            if (joined.contains(p.getName())) {
                                p.sendMessage(prefix + ChatColor.GRAY + "You have already joined!");
                            } else if (lmstarted) {
                                p.sendMessage(prefix + ChatColor.RED + "This event has already started!");
                            } else if (lmse.size() == 1) {
                                p.sendMessage(prefix + ChatColor.GRAY + "Joined LMS!");
                                joined.add(p.getName());
                                joinedn.add(Integer.valueOf(1));
                               
                                ty.saveInv(p);
                               
                                Bukkit.broadcastMessage(prefix + ChatColor.GOLD + p.getName() + ChatColor.GRAY + " Joined! "
                                        + ChatColor.GREEN + joined.size() + ChatColor.GRAY + "/" + ChatColor.GREEN + Bukkit.getOnlinePlayers().size());
                            } else {
                                p.sendMessage(prefix + ChatColor.GRAY + "This event is not being hosted!");
                            }
                        } else {
                            p.sendMessage(prefix + ChatColor.RED + "You dont have permissions to this command!");
                        }
    when game starts:
    Code (Text):
    LMS.lmstarted = true;
                            for (String pl : LMS.joined) {
                                Player p = Bukkit.getPlayer(pl);
                                if (LMS.joined.contains(p.getName())) {
                                    for (int i = 0; i < Bukkit.getOnlinePlayers().size(); i++){
                                       
                                        p.teleport(new Location(
                                                Bukkit.getWorld(Main.config.getConfig().getString("LMS.World")),
                                                Main.config.getConfig().getInt("LMS.X"),
                                                Main.config.getConfig().getInt("LMS.Y"),
                                                Main.config.getConfig().getInt("LMS.Z")));
                                        p.setNoDamageTicks(200);
                                        p.setHealth(20.0D);
                                        p.setFoodLevel(20);
                                        for (PotionEffect effect : p.getActivePotionEffects()) {
                                            p.removePotionEffect(effect.getType());
                                        }
                                    }
                                    ty.clearInv(p);
                                    Bukkit.getServer().dispatchCommand(p, "kit lms");
                                }
                            }
    Code (Text):
    @EventHandler
        public void playerRespawn(PlayerRespawnEvent e){
            if(respawn.contains(e.getPlayer())){
                ty.clearInv(e.getPlayer());
                ty.restoreInv(e.getPlayer());
            }
        }

        @EventHandler
        public void PlayerDeath(PlayerDeathEvent e) {
            Player player = e.getEntity().getPlayer();
            if (LMS.joined.contains(player.getName()) && LMS.lmstarted) {
                respawn.add(player);
                LMS.joined.remove(player.getName());
                LMS.joinedn.add(-1);
                Bukkit.broadcastMessage(LMS.prefix + ChatColor.RED + player.getName() + ChatColor.GRAY + " Has been eliminated by " + ChatColor.GREEN + player.getKiller().getName());
                if (LMS.joined.size() == 1) {
                    Player k = Bukkit.getPlayer(LMS.joined.get(0));
                    Bukkit.broadcastMessage(LMS.prefix + ChatColor.GREEN + k.getName() + ChatColor.GRAY + " Won the event!");
                    k.setFireTicks(1);
                    k.teleport(Bukkit.getWorld("world").getSpawnLocation());
                   
                    ty.clearInv(player.getKiller());
                    ty.restoreInv(player.getKiller());
                   
                    LMS.joinedn.clear();
                    LMS.joined.clear();
                    LMS.lmstarted = false;
                    LMS.lms = false;
                    LMS.lmse.clear();
                }
            }
        }
    Code (Text):
    public void saveInv(Player player){
            if(Main.armor.containsKey(player)){
                Main.armor.remove(player);
                Main.armor.put(player, player.getInventory().getArmorContents());
            }
           
            if(Main.inventory.containsKey(player)){
                Main.inventory.remove(player);
                Main.inventory.put(player, player.getInventory().getContents());
            }
           
            if(Main.exp.containsKey(player)){
                Main.exp.remove(player);
                Main.exp.put(player, (int)player.getExp());
            }
        }
       
        public void clearInv(Player player){
            player.getInventory().clear();
            if(player.getInventory().getHelmet() != null){
                player.getInventory().getHelmet().setType(Material.AIR);
            }
           
            if(player.getInventory().getChestplate() != null){
                player.getInventory().getChestplate().setType(Material.AIR);
            }
           
            if(player.getInventory().getLeggings() != null){
                player.getInventory().getLeggings().setType(Material.AIR);
            }
           
            if(player.getInventory().getBoots() != null){
                player.getInventory().getBoots().setType(Material.AIR);
            }
           
            if(player.getExp() > 0){
                player.setExp(0);
            }
        }

        public void restoreInv(Player player) {
            if(Main.armor.containsKey(player)){
                player.getInventory().setArmorContents(Main.armor.get(player));
            }
            if(Main.inventory.containsKey(player)){
                player.getInventory().setContents(Main.inventory.get(player));
            }
            if(Main.exp.containsKey(player)){
                player.setExp(Main.exp.get(player));
            }
            player.sendMessage(LMS.prefix + ChatColor.GREEN + "Your inventory has been restored!");
        }
     
  2. Okay... but what's your issue?
     
  3. I dont need to save it to a file, I just need to load them back after the arena ends...
    My issue is that it doesn't want to load them or save them... Idk which.
     
  4. It sends the player the message that it restored there inv, But it doesnt restore it.
     
  5. Your code looks right, try using UUIDs instead of Player instance. Also, you should put sysouts in your load inventory to see the content of the hashmap.
     
  6. Ok i changed it to UUIDS didnt work...
     
  7. Well start debugging then, you don't need to make a post when you can debug yourself. Put sysouts around, check the values inside of your HashMap and make sure that they contain the values you actually expect. If they're not, find out why
     
  8. Code (Text):
     public void saveInv(UUID player){
            Player pl = Bukkit.getPlayer(player);
            if(Main.armor.containsKey(player)){
                Main.armor.remove(player);
                Main.armor.put(player, pl.getInventory().getArmorContents());
            }
           
            if(Main.inventory.containsKey(player)){
                Main.inventory.remove(player);
                Main.inventory.put(player, pl.getInventory().getContents());
            }
           
            if(Main.exp.containsKey(player)){
                Main.exp.remove(player);
                Main.exp.put(player, (int)pl.getExp());
            }
           
            Bukkit.broadcastMessage("" + Main.armor.get(0));
            Bukkit.broadcastMessage("" + Main.inventory.get(0));
        }
    This returned null...
     
  9. @JacksonHogan I just realized something.. In your save method.. You ONLY have if statements for IF the player is IN the HashMap.. You need a method for if NOT. Your welcome.
     
    • Winner Winner x 1
  10. How did I not notice that, have a +1
     
  11. Why does it matter if hes not in the HashMap?
     
  12. In your saveInv method, you only check if the player is inside the HashMaps. What happens if he's not?
     
  13. When you register a new HashMap.. NO data at all is inside.. So you have to add it first.
     
  14. I tried this:

    Code (Text):
     public void saveInv(UUID player){
            Player pl = Bukkit.getPlayer(player);
            if(Main.armor.containsKey(player)){
                Main.armor.remove(player);
                Main.armor.put(player, pl.getInventory().getArmorContents());
            }else{
                Main.armor.put(player, pl.getInventory().getArmorContents());
            }
           
            if(Main.inventory.containsKey(player)){
                Main.inventory.remove(player);
                Main.inventory.put(player, pl.getInventory().getContents());
            }else{
                Main.inventory.put(player, pl.getInventory().getContents());
            }
           
            if(Main.exp.containsKey(player)){
                Main.exp.remove(player);
                Main.exp.put(player, (int)pl.getExp());
            }else{
                Main.exp.put(player, (int)pl.getExp());
            }
           
            Bukkit.broadcastMessage("" + Main.armor.get(0));
            Bukkit.broadcastMessage("" + Main.inventory.get(0));
        }
    Returns null still;
     
  15. Post the console log in pastebin please.
     
  16. How about simply Bukkit.broadcastMessage("" + Main.armor); ?
     
  17. 12.06 02:01:50 [Server] INFO xXLightbulbXx issued server command: /lms host 12.06 02:01:50 [Server] INFO LMS » xXLightbulbXx Has started an event! 12.06 02:01:51 [Server] INFO _OhTemperz issued server command: /lms join 12.06 02:01:51 [Server] INFO null 12.06 02:01:51 [Server] INFO null 12.06 02:01:51 [Server] INFO LMS » _OhTemperz Joined! 1/7 12.06 02:01:52 [Server] INFO xXLightbulbXx issued server command: /lms join 12.06 02:01:52 [Server] INFO null 12.06 02:01:52 [Server] INFO null 12.06 02:01:52 [Server] INFO LMS » xXLightbulbXx Joined! 2/7 12.06 02:01:53 [Server] INFO LMS » LMS has started! Players: 2/7 12.06 02:01:54 [Server] INFO ikillu765 issued server command: /lms join 12.06 02:02:02 [Server] INFO Danny28 issued server command: /lms join 12.06 02:02:04 [Server] INFO LMS » _OhTemperz Has been eliminated by xXLightbulbXx 12.06 02:02:04 [Server] INFO LMS » xXLightbulbXx Won the event! 12.06 02:02:04 [Server] INFO null 12.06 02:02:04 [Server] INFO null

    EDIT: Still returns null