Solved Can't save data on a HashMap

Discussion in 'Plugin Development' started by MrBakbuki, Apr 28, 2021.

  1. Offline

    MrBakbuki

    Hey, I'm trying to create a stat plugin, but I can't seem to find a way to put the data into a HashMap inside of another class, I get no errors, it just doesn't work. Any help appreciated :)

    Update stats test command:
    Code:
    public class UpdateStatsCommand implements CommandExecutor {
    
        private Main plugin;
        DataStorage data = new DataStorage();
    
        public UpdateStatsCommand(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("updatestats").setExecutor(this);
         //   plugin.getCommand("updatestats").setTabCompleter(this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
            if(p.getInventory().getBoots() != null){
                ItemStack Boots =  p.getPlayer().getInventory().getBoots();
                ItemMeta BootsMeta = Boots.getItemMeta();
                PersistentDataContainer BootsData = BootsMeta.getPersistentDataContainer();
                if(BootsData.has(new NamespacedKey(Main.getPlugin(),"Defense"), PersistentDataType.INTEGER)){
    
                    data.setDefense(p.getUniqueId(),BootsData.get(new NamespacedKey(Main.getPlugin(), "Defense"), PersistentDataType.INTEGER));
    
    
                }
            }
    
    
    
            return false;
        }
    
    
    
    
    }
    Get stat command:
    Code:
    public class StatCommand implements CommandExecutor, TabCompleter {
    
    
        private Main plugin;
        DataStorage data = new DataStorage();
        Utils utils = new Utils();
    
    
        public StatCommand(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("stat").setExecutor(this);
            plugin.getCommand("stat").setTabCompleter(this);
        }
    
    
    
    
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    
            Player p = (Player) sender;
            utils.addDefense(p);
            if(args.length == 0){
                p.sendMessage(Utils.chat("&cbruh pls use this liek this omg!1!! -> Usage: /stat <Strength/Health/Defense/Damage/Intelligence>"));
            }else{
                if(args[0].equalsIgnoreCase("Strength")){
                    p.sendMessage(Utils.chat("&aYou have " + data.getStrength(p.getUniqueId()) + " Strength."));
                }else if(args[0].equalsIgnoreCase("Health")) {
    
                    p.sendMessage(Utils.chat("&aYou have " + data.getHealth(p.getUniqueId()) + " Health."));
    
                }else if(args[0].equalsIgnoreCase("Defense")){
    
                    p.sendMessage(Utils.chat("&aYou have " + data.getDefense(p.getUniqueId()) + " Defense."));
    
    
                }else{
                    p.sendMessage(Utils.chat("&cCouldn't find your stat :( Did you spell it correctly? Please note not all of the stats are currently implemented into the game."));
                }
            }
    
    
    
    
    
    
    
    
            return false;
        }
    
    
    
        @Override
        public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
    
            List<String> tabComplete = Arrays.asList("HEALTH","DEFENSE","INTELLIGENCE","STRENGTH","DAMAGE");
            List<String> tabCompleteFinal = new ArrayList<>();
            String input = args[0].toUpperCase();
    
    
    
            for(String s : tabComplete){
                if(s.startsWith(input)){
                    tabCompleteFinal.add(s);
                }
    
            }
    
    
            return tabCompleteFinal;
    
    
        }
    }
    Storage class:
    Code:
    public class DataStorage {
    
        private HashMap<UUID,Integer> Strength = new HashMap<UUID,Integer>();
        private HashMap<UUID,Integer> Health = new HashMap<UUID,Integer>();
        private HashMap<UUID,Integer> Defense = new HashMap<UUID,Integer>();
        private HashMap<UUID,Integer> cHealth = new HashMap<UUID,Integer>();
    
    
        public int getStrength(UUID uuid){
            if(Strength.get(uuid)!=null){
                return Strength.get(uuid);
            }else{
                return 0;
            }
        }
        public int getHealth(UUID uuid){
            if(Health.get(uuid)!=null){
                return Health.get(uuid);
            }else{
                return 0;
            }
        }
        public int getDefense(UUID uuid){
            if(Defense.get(uuid)!=null){
                return Defense.get(uuid);
            }else{
                return 0;
            }
        }
        public int getcHealth(UUID uuid){
            return cHealth.get(uuid);
        }
        public boolean iscHealthNull(UUID uuid){
            if(cHealth.get(uuid)==null){
                return true;
            }else{
                return false;
            }
        }
        public void e(UUID uuid){
            Defense.put(uuid, 10);
        }
    
        public void setStrength(UUID uuid, int value){
            Strength.put(uuid, value);
        }
        public void setHealth(UUID uuid, int value){
            Health.put(uuid, value);
        }
        public void setDefense(UUID uuid, int value){
            Defense.put(uuid, value);
        }
        public void setcHealth(UUID uuid, int value){
            cHealth.put(uuid, value);
        }
    
    }
    upload_2021-4-28_17-26-31.png

    If anything is not clear, feel free to ask me :)
     
  2. Offline

    timtower Administrator Administrator Moderator

    @MrBakbuki They all have their own datastorage instance.
     
  3. Offline

    MrBakbuki

    Thanks for replying, how would you recommend fixing this?
     
  4. Offline

    MrBakbuki

Share This Page