HashMaps only saving temporarily

Discussion in 'Plugin Development' started by ReflectionCraft, Jul 8, 2019.

  1. Offline

    ReflectionCraft

    My hashmap 'playerdust' stores the balance of dust for each player. It then saves those values to the config.
    Let's say my balance is 6 dust. When I reload or restart the server my balance is still 6 dust until I consume another dust my balance is reset and set to however many dust I consumed. Curious if anyone can give me some insight on this.

    Main.java (open)

    Code:
    package me.alton.MagicDust;
    
    
    
    import java.util.ArrayList;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import me.alton.MagicDust.Events.EventsClass;
    
    public class Main extends JavaPlugin
    {
        EventsClass ec = new EventsClass();
       
       
        public void onEnable()
        {
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "\n\n[MagicDust] Plugin Enabled\n\n"); //sends message to console saying plugin enabled
            getServer().getPluginManager().registerEvents(ec, this);
            loadConfig();
        }
        public void onDisable()
        {
            getServer().getConsoleSender().sendMessage(ChatColor.RED + "\n\n[MagicDust] Plugin Disabled\n\n"); //sends message to console saying plugin disabled
        }
        public void loadConfig()
        {       
            getConfig().options().copyDefaults(true);
            saveConfig();
            reloadConfig();
        }
       
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
           
            Player player = (Player)sender;
            UUID playerID = player.getUniqueId();
           
           
           
            //GET DUST
            if (cmd.getName().equalsIgnoreCase("getdust") && player.hasPermission("dust.get")) //permission node is "dust.get"
            {
                ItemStack dust = new ItemStack(Material.valueOf(Main.getPlugin(Main.class).getConfig().getString("magicDustType")));
                ItemMeta dustMeta = dust.getItemMeta();
                dustMeta.setDisplayName(ChatColor.YELLOW + "" + ChatColor.BOLD + "Magic Dust");
                dustMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
                dustMeta.addEnchant(Enchantment.MENDING, 1, true);
                ArrayList<String> dustlore = new ArrayList<String>();
                dustlore.add(ChatColor.WHITE + "" + ChatColor.ITALIC + "Right Click To Consume To Balance");
                dustMeta.setLore(dustlore);
                dust.setItemMeta(dustMeta); 
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getPlugin(Main.class).getConfig().getString("dustGetMessage"))); //sends message defines in config
                player.getInventory().addItem(dust);
                return true;
            }
            //GUI
            if(cmd.getName().equalsIgnoreCase("dbal")) //permission node is "dust.bal"
            {
               
                Inventory dustGUI = Bukkit.createInventory(null, 27, ChatColor.DARK_AQUA + "" + ChatColor.BOLD + "Magic Dust");
               
                ItemStack dust1 = new ItemStack(Material.GLOWSTONE_DUST);
                ItemMeta dust1Meta = dust1.getItemMeta();
               
                dust1Meta.setDisplayName(ChatColor.YELLOW + "" + ChatColor.BOLD + "Magic Dust Balance");
               
                ArrayList<String> dust1lore = new ArrayList<String>();
                //dust1lore.add(ChatColor.WHITE + "" + ChatColor.ITALIC + ec.getDustMap());
                dust1lore.add(ChatColor.WHITE + "" + ChatColor.ITALIC + Main.getPlugin(Main.class).getConfig().getString("Users."+playerID+".Balance"));
                dust1Meta.setLore(dust1lore);
               
                dust1.setItemMeta(dust1Meta);
               
                dustGUI.setItem(13, dust1);
                player.openInventory(dustGUI);
            }
            return true;
         
        }
       
       
       
       
    
    }
    


    EventsClass.java (open)

    Code:
    package me.alton.MagicDust.Events;
    
    
    
    
    import java.util.HashMap;
    import java.util.UUID;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    
    import me.alton.MagicDust.Main;
    
    public class EventsClass implements Listener {
       
        HashMap<UUID, Integer> playerdust = new HashMap<>();
    
        public HashMap<UUID, Integer> getDustMap() {
            return playerdust;
       }
    
    
    
       
        @EventHandler //consume dust to balance
        public void onRightClick(PlayerInteractEvent event) // method for event PlayerInteract
        {
        Player player = event.getPlayer();
       
        ItemStack handItem = player.getInventory().getItemInMainHand();
       
         if(event.getAction().equals(Action.RIGHT_CLICK_AIR) && handItem.getItemMeta().getItemFlags().contains(ItemFlag.HIDE_ENCHANTS))
         {
             int amount = handItem.getAmount();
             player.getInventory().removeItem(handItem);
             player.sendMessage(ChatColor.DARK_AQUA + "You have consumed " + ChatColor.WHITE + amount + ChatColor.DARK_AQUA + " Magic Dust");
             UUID id = player.getUniqueId();
             if (!playerdust.containsKey(id))
             {
                 playerdust.put(id, 0);
             }
             int dustBal = playerdust.get(id) + amount;
             playerdust.put(id, dustBal);
             Main.getPlugin(Main.class).getConfig().set("Users." + player.getUniqueId() + ".Balance", playerdust.get(player.getUniqueId()));
             Main.getPlugin(Main.class).saveConfig();
            
             //
         }
        }
       
    }
    
       
    


    plugin.yml (open)

    Code:
    name: MagicDust
    author: SnappSeal
    version: 1.0
    main: me.alton.MagicDust.Main
    description: Magic Dust
    
    commands:
      getdust:
        description: get dust
        default: false
        permission: dust.get
    
      dbal:
        description: check balance
        default: false
           
    permissions:
      dust.get:
        description: for /getdust
        default: op
    
           


    config.yml (open)

    Code:
    ##This section you can customize the messages
    dustGetMessage: '&3You have received &f1 &3Magical Dust' ##when a player uses heal block
    getDustNoPermission: '&cYou dont have the correct permissions' ##when a player is at full health when trying to use heal block
    consumeDust: '&3You Have Consumed Dust'
    
    ##This section is for customizing the item of magical dust
    magicDustType: 'GLOWSTONE_DUST'
    
     
  2. Online

    KarimAKL

    @ReflectionCraft Doesn't look to me like you are loading the config values into the 'playerdust' map.
     
  3. Offline

    ReflectionCraft

    Code:
    UUID id = player.getUniqueId();
             if (!playerdust.containsKey(id))
             {
                 playerdust.put(id, 0);
             }
             int dustBal = playerdust.get(id) + amount;
             playerdust.put(id, dustBal);
    This is where I put the value into the Map
    Code:
    Main.getPlugin(Main.class).getConfig().set("Users." + player.getUniqueId() + ".Balance", playerdust.get(player.getUniqueId()));
    Main.getPlugin(Main.class).saveConfig();
             
    This is where I save the map into the config
    Shouldn't that save the values?
     
  4. Online

    KarimAKL

    @ReflectionCraft You are saving the amount of dust the player has to the config, but you are only getting it back in your PlayerInteractEvent, load them onEnable or in the PlayerJoinEvent instead.
    You are also not getting the amount of dust from the config, only from the item in their hand, change that as well.
     
  5. Offline

    ReflectionCraft

    So I need to load
    Code:
    Main.getPlugin(Main.class).getConfig().set("Users." + player.getUniqueId() + ".Balance", playerdust.get(player.getUniqueId()));
    in my onEnable
    And as for getting the amount of dust in their hand, that Is how im determining how much to add to the balance.
    I get the amount in hand lets say they have 3 in their hand, and add 3 to the balance

    EDIT: The line I added to my onEnable is pretty much the same thing I use in my EventsClass
    Code:
    getConfig().get("Users." + player.getUniqueId() + ".Balance", ec.getDustMap().get(player.getUniqueId()));
     
    Last edited: Jul 8, 2019
  6. Online

    KarimAKL

    @ReflectionCraft That line requires you to have a 'player' variable, instead you should loop the keys inside the 'Users' section in your config, then get the 'Balance' value of that key.
     
  7. Offline

    ReflectionCraft

    My key is player.getUniqueId
     
  8. Online

    KarimAKL

    @ReflectionCraft But how are you getting 'player' inside your onEnable? You would have to loop the players or something, which isn't exactly what you want, instead loop the keys inside the configuration section 'Users' inside your config.
     
  9. Offline

    ReflectionCraft

    Im confused what you mean by "configuration section 'Users' inside your config"
     
  10. Online

    KarimAKL

    @ReflectionCraft Use this method to get the configuration section 'Users', then use this method to get the keys. You only need the direct keys, so you can set 'deep' to 'false' in the second method.
     

Share This Page