TOP 10 Players with highest kills

Discussion in 'Plugin Development' started by Edmond506, Jan 19, 2017.

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

    Edmond506

    Hello,
    I'm trying to make a plugin for my server that the players with kills can see who's the top 10 on the leaderboard like a message.
    I tried Collection.sort but it isn't working.
    I have the kills with the players stored in a config.
    Can anyone help me?
     
  2. Offline

    Zombie_Striker

    @Edmond506
    Have you use google yet?
    https://bukkit.org/threads/top-kills-scoreboard.290383/

    1. Keep a hashmap of all the scores/kills.
    2. Using Collection.sort, sort all the values (scores/kills) using the "Collections.sort" method.
    3. Get the top 10 values in the collection. Go back to the hashmap and find all the players with those scores.
    4. You now have the top 10 players.
     
  3. Offline

    kameronn

    @Zombie_Striker @Edmond506
    You would also have to do Collections.reverse to get it in the order from greatest to least. Also if he were using a hashmap in order to use Collections.sort and Collections.reverse he would to need to use a List, and since map.values(); returns a Set he would also need to put it in a list then loop through the list and all the players in the hashmap and check their values.

    So here's what I mean and what I would do:
    1. Put the names of the players and their integers in the hashmap.
    2. Create a LinkedList (example below).
    3. Collections.sort and Collections.reverse the LinkedList you created.
    4. Make a for loop and loop through all the values in the LinkedList.
    5. In this for loop make another for loop and loop through all the names of the players in the hashmap.
    6. Get the value of each player and check if its equal to the value that is currently being check in the first for loop.
    7. Send player message or do whatever you want with the values
    Code:
    List<Integer> sortmap = new LinkedList<Integer>(map.values());
     
    Zombie_Striker likes this.
  4. Offline

    Edmond506

    @Zombie_Striker I searched for those, but I can't figure out how to do it. It doesn't work for me.
    @kameronn I'm getting a problem there...The kills are stored in a config file and when I try to loop thru them and add them to a HashMap I get an error.
     
  5. Offline

    timtower Administrator Administrator Moderator

    Code?
     
  6. Offline

    kameronn

    @Edmond506
    Code:
        HashMap<String, Integer> map = new HashMap<>();
      
        public void add() {
            for(config...) {
                int kills = config.getInt(uuid + ".kills");
                String name = config.getString(uuid + ".currentname");
              
                map.put(name, kills);
              
            }
          
        }
    What do you mean you're getting an error
     
  7. Offline

    Edmond506

    This is my code so far!
    Code:
        static HashMap<String, Integer> map = new HashMap<>();
       
        public static int top1kill;
        public static String top1name;
       
       
        public void add() {
            for(String i : Main.database.getConfig().getConfigurationSection("Players").getKeys(false)) {
                int kills = Main.database.getConfig().getConfigurationSection("Players").getInt(i);
                String name = Main.database.getConfig().getConfigurationSection("Players").getString(i);
             
                map.put(name, kills);
             
            }
         
        }
        public static void top () {
            List<Integer> sortmap = new LinkedList<Integer>(map.values());
            Collections.sort(sortmap);
            Collections.reverse(sortmap);
            //Here I want to add the top name and the kill to top1kill and top1name
            //like top1kill = sortmap[0]
        }
     
  8. Offline

    Edmond506

  9. Offline

    Zombie_Striker

    Can you post the error?
     
  10. Offline

    I Al Istannen

    @Edmond506
    Code:java
    1. List<Entry<String, Integer>> entries = Map
    2. #entrySet // turn it into a collection to be able to use the stream method
    3. #stream // get a stream
    4. #sorted(Entry#comparingByValue) // sort by the value
    5. #limit(10) // we only want the first ten
    6. #collect(Collectors#toList()) // make it a list again

    This list is in ascending order, meaning the smallest int comes first. You need to modify the "sorted" call to suit your needs. I will leave that up to you to figure out :)
     
    JanTuck and Zombie_Striker like this.
Thread Status:
Not open for further replies.

Share This Page