Solved Highest value on Config/Map ?

Discussion in 'Plugin Development' started by Gonmarte, Apr 3, 2016.

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

    Gonmarte

    Code:
    private HashMap<String, Integer> kills = new HashMap<>();
       
        @Override
        public void onEnable() {
            addMap();
            addConfig();
            this.getConfig().options().copyDefaults(true);
        }
        @Override
        public void onDisable() {
            saveConfig();
        }
       
        public void addMap(){
    //Ik it doesnt make sense but its just a test.
            kills.put("Players." + "Ola", 176);
            kills.put("Players." + "Popo", 112);
            kills.put("Players." + "Fala", 165);
            kills.put("Players." + "Rita", 1123);
            kills.put("Players." + "Rafa", 142);
            kills.put("Players." + "Quim", 126);
            kills.put("Players." + "Armindo", 16);
            kills.put("Players." + "Vski", 12);
            kills.put("Players." + "Gonmarte", 143);
            kills.put("Players." + "Calate", 134);
        }
       
        public void addConfig(){
            for(Entry<String, Integer> e : kills.entrySet()){
                this.getConfig().set(e.getKey(), e.getValue().toString());
            }
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if(cmd.getName().equalsIgnoreCase("topkills") && sender instanceof Player){
                Player player = (Player) sender;
                ConfigurationSection cs = this.getConfig().getConfigurationSection("Players.");
                int max = 0;
               
                for(String key : cs.getKeys(false)){
                    int kills = this.getConfig().getInt("Players." + key);
                    if(kills >= max){
                        max = kills;
                        //Attempt to debug, its printing 0 in every loop
                        player.sendMessage("" + max);
                    }
                }
                // max is printed with the value of 0
                player.sendMessage("O player com mais kills é o que tem " + max);
                return true;
            }
            return false;
        }
    
     
    Last edited: Apr 3, 2016
  2. Offline

    teej107

    Ninja'd but I'll finish anyway :p

    You declare and assign 'max' as 0

    You declare and assign 'kills' to the value of the path in the config. Kills must be equal or more than 'max'.
    The code in that if statement is running because you are getting messages from your sendMessage() method.
    You assign 'max' to the value of 'kills'
    You say 'max' always prints 0?
    Therefore I've concluded that 'kills' is also 0.

    Make sure your path is correct because the JavaDocs states for the getInt method:
     
  3. Offline

    Gonmarte

    Solved.
    You are right. There arent any int on my paths because i converted it to strings....
    I removed the toString() method and its getting the highest value.
    Ty
    Error Solved:~
    Code:
    
        public void addConfig(){
            for(Entry<String, Integer> e : kills.entrySet()){
                                  //toString() method removed
                this.getConfig().set(e.getKey(), e.getValue().toString());
            }
        }
    
     
Thread Status:
Not open for further replies.

Share This Page