Problems getting the 10 highest values on the config.

Discussion in 'Plugin Development' started by Gonmarte, Sep 2, 2015.

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

    Gonmarte

  2. Offline

    Gonmarte

  3. Offline

    Tecno_Wizard

    @Gonmarte, i'll give you some pseudo code.

    ConfigurationSection section = (get config section)
    HashMap data = section.getValues(false)


    This will give you a map of the data in get config section. From there on you can get the values, stick them into an array and invoke Arrays.sort
     
    Gonmarte likes this.
  4. Offline

    Gonmarte

    @Tecno_Wizard I got errors
    Here is the code

    Code:
                Player player = (Player) sender;
                                       // There is an error here, it says that i need to put a String inside getConfigurationSection(String);
                ConfigurationSection section = this.getConfig().getConfigurationSection();
                Map<String, Object> data = section.getValues(false);
                                         // Error in sort!
                    Collections.sort(data, new Comparator<String>() {
    
    
    Thank you for helping me!
    It means a lot for me =)

    @Tecno_Wizard I dont know if u see but i created a method getValues in an ArrayList
    Code:
        public ArrayList<String> getValues() {
           
            Map<String, Object> map = this.getConfig().getValues(true);
           
            ArrayList<String> lines = new ArrayList<String>();
           
            for (Entry<String, Object> e : map.entrySet()) {
               
                lines.add(e.getValue() + "|" + e.getKey());
               
            }
            return lines;
           
        }
    
    
    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 4, 2015
  5. Offline

    Tecno_Wizard

    @Gonmarte, it seems that in your case, the configuration section you need isright at the base of the config, so you don't need to get the section. Just getKeys without deep should do it.
     
  6. Offline

    Gonmarte

    @Tecno_Wizard im not at nome right. Só i Need to remove the getConfigurationSection() and should i remove the method getValues()?
     
  7. Offline

    stormneo7

    Wrote this code for you. Hopefully it helps and offers a decent explanation.
    Code:
    import java.util.TreeSet;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Mani extends JavaPlugin {
    
        @Override
        public void onEnable() {
            saveDefaultConfig();
        }
    
        @Override
        public void onDisable() {
            saveDefaultConfig();
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("top")) {
           
                // TreeSets provide total ordering of its keys. We'll be using a TreeSet to order our given data. This way, we can avoid using a custom comparison tool.
                TreeSet<String> sortedSet = new TreeSet<String>();
    
                // Load the TreeSet using the Configuration Values.
                for (String playerName : this.getConfig().getDefaultSection().getKeys(false)) {
                    int playerKills = this.getConfig().getInt(playerName);
    
                    // Notice how we put the Player's Kills first.
                    // This is because we are attempting to order the TreeSet based on Kill Values. 
                    // If we were to put the Player Name first, the TreeSet would output the Player Names based on alphabetical order).
                    // After doing so, we are able to retrieve the Player's Name by splitting the String up.
                    sortedSet.add(playerKills + " " + playerName);
                }
    
                // Check if the Set is Empty
                if (sortedSet.isEmpty())
                    sender.sendMessage(ChatColor.RED + "There is no data stored to show.");
                else {
                    // Since Trees are ordered from LEAST to GREATEST, we will need to get the LAST entry.
                    String entry = sortedSet.descendingIterator().next();
    
                    // Decompile the Entry Values.
                    String[] entryData = entry.split(" ");
                    int playerKills = Integer.parseInt(entryData[0]);
                    String playerName = entryData[1];
    
                    // Tells the Command Sender
                    sender.sendMessage(ChatColor.GREEN + playerName + " has " + playerKills + " kills!");
                }
            }
            return false;
        }
    }
     
    Gonmarte likes this.
  8. Offline

    Gonmarte

    @stormneo7 but with that code i wont get the top 10 values, it will order ALL the values no config
     
  9. Offline

    mythbusterma

    @Gonmarte

    If you have all the values in order, just take the first ten.


    @stormneo7

    The spoonfeeding is unreal.
     
  10. Offline

    stormneo7

    Iterate through
    Code:
    sortedSet.descendingIterator()
    as it is already from GREATEST to LEAST. Afterwards, you can just print the lines and split it like I showed you above.
     
  11. Offline

    Gonmarte

    @stormneo7 ok im going to try, if i need any help ill tell you soon. I i dont i will mark it SOLVED and tell you =)
    Thank you!

    @stormneo7 @mythbusterma My code is not working

    Code:
    package me.gonmarte;
    
    import java.util.TreeSet;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Mani extends JavaPlugin implements Listener{
      
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            this.getConfig().options().copyDefaults(true);
            saveConfig();
        }
      
        @Override
        public void onDisable() {
            saveConfig();
          
        }
      
        @EventHandler
        public void onPlayerDeath(EntityDeathEvent event) {
          
            Entity deadEntity = event.getEntity();
          
            Entity killer = event.getEntity().getKiller();
          
            if(deadEntity instanceof Zombie && killer instanceof Player) {
              
                Player player = (Player) killer;
              
                int totalkillcount = this.getConfig().getInt("Total" + ".Zombies");
              
                int killcount = this.getConfig().getInt(player.getName() + ".Zombies");
              
                this.getConfig().set(player.getName() + ".Zombies", killcount + 1);
              
                this.getConfig().set("Total" + ".Zombies", totalkillcount + 1);
              
                player.sendMessage(ChatColor.DARK_AQUA + "Nice! You have killed an " + ChatColor.DARK_PURPLE + "Ender Dragon!" );
              
            }
          
        }
      
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
               if (command.getName().equalsIgnoreCase("top")) {
            
                   // TreeSets provide total ordering of its keys. We'll be using a TreeSet to order our given data. This way, we can avoid using a custom comparison tool.
                   TreeSet<String> sortedSet = new TreeSet<String>();
        
                   // Load the TreeSet using the Configuration Values.
                   for (String playerName : this.getConfig().getDefaultSection().getKeys(false)) {
                       int playerKills = this.getConfig().getInt(playerName + ".Zombies");
        
                       // Notice how we put the Player's Kills first.
                       // This is because we are attempting to order the TreeSet based on Kill Values.
                       // If we were to put the Player Name first, the TreeSet would output the Player Names based on alphabetical order).
                       // After doing so, we are able to retrieve the Player's Name by splitting the String up.
                       sortedSet.add(playerKills + " " + playerName);
                   }
        
                   // Check if the Set is Empty
                   if (sortedSet.isEmpty())
                       sender.sendMessage(ChatColor.RED + "There is no data stored to show.");
                   else {
                       // Since Trees are ordered from LEAST to GREATEST, we will need to get the LAST entry.
                       String entry = sortedSet.descendingIterator().next();
        
                       // Decompile the Entry Values.
                       String[] entryData = entry.split(" ");
                       int playerKills = Integer.parseInt(entryData[0]);
                       String playerName = entryData[1];
        
                       // Tells the Command Sender
                       sender.sendMessage(ChatColor.GREEN + playerName + " has " + playerKills + " kills!");
                   }
               }
               return false;
           }
    }
    
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  12. Offline

    Gonmarte

  13. Offline

    mythbusterma

  14. Offline

    Gonmarte

  15. Offline

    boomboompower

    Have you added it in the plugin yml?

    Code:
    name: Something
    version: 1.0
    main: me.gonmarte.Mani
    commands:
       top:
          description: Something
          usage: /<command>
     
  16. Offline

    Gonmarte

  17. Offline

    mythbusterma

    @Gonmarte

    You're going to have to say more than "it doesn't work" to get help.
     
  18. Offline

    RoboticPlayer

    @Gonmarte Are there any stack traces in the console when you run the command?
     
Thread Status:
Not open for further replies.

Share This Page