Help with listing players from highest to lowest

Discussion in 'Plugin Development' started by OhYes, Mar 9, 2016.

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

    OhYes

    Hi, so for my KitPVP server I want to have sort of a Hierarchy from top to bottom listing the players with the most kills at #1 and least at #10, kind of like how essentials does BalTop. I've looked everywhere and I really cannot find how to make a decending order list. Any pointers?

    Example:
    -----[ Top Players ]-----
    1. Oh_Yes - 169 Kills
    2. Oh_No - 154 Kills
    3. Oh_Maybe - 96 Kills
    --------------------------
     
    WolfMage1 likes this.
  2. Offline

    mine-care

    Hehe i like your example names!
    What datatype are you using to store your data in?
     
  3. Offline

    timtower Administrator Administrator Moderator

    @OhYes Loop through the values, get the highest, save that, remove from original list.
    Repeat till correct amount is reached.
     
  4. Offline

    OhYes

    @mine-care Integers I would imagine. Well, I'm using levels to determine kills.
    @timtower so something along the lines of:
    (levels are kills btw)
    for(int i : Bukkit.getOnlinePlayers().getLevel()){
    i.... -.. I don't know.. :c
    }
     
  5. Offline

    Gonmarte

    Credits to @Zombie_Striker in the thread: https://bukkit.org/threads/how-i-get-the-10-players-with-highers-values-in-config.381268/

    1. Create a map where the keys are Integers, and the Values or UUID's
    2. Create a for lop to loop through all the players
    3. Get the players score from the config.
    4. Create a loop for integers, starting at 1 and going upto 10
    5. If the map does not have that values saved (e.g. !map.contains(int)) or if the value it returns is less than the score (map.get(int) < score) do the following
    6. Create a backwards loop which will set each key up down by one (map.put(number-1,map.get(number)))
    7. After setting all the values down by one, put your score into the map with the and assign the key to be that of the ranking (the now open slot in the map)
    8. One this is done for all players, the player with the highest score will have a key of 1,the player with second highest score will be 2 ect.
    This is the process of creating a map contain the players with the highest scores. This is as simple as I can explain it without posting code.
     
    mine-care likes this.
  6. Offline

    mine-care

  7. Offline

    HoeMC

    A map of Integers -> UUIDs wouldn't work if multiple people had the same number of kills. You'd need a multimap (Map of Integers -> Collection) for one-to-many mapping. To do what you described above would only require a sorted map such as a TreeMap anyway; and you'd only have to get the last 3 values in the map. Sorting it yourself wouldn't be required.

    The best way I see of doing this is using a Map of UUIDs to Integers and creating your own comparator that sorts by value. You can then take the top/bottom 3 entries. There are some good solutions/comparators in this thread on StackOverflow.

    @mine-care is there a reason for choosing a TreeMap over a HashMap? If you're using a custom comparator to sort by value why bother using a key sorted map?
     
    Last edited: Mar 10, 2016
  8. Offline

    OhYes

    Thanks, but even as you said in the forum post, it's hard to understand, at least for me it is. I know what loops are and stuff but my brain just decides to fart sometimes. And I'm derping right now. ._. Any code perhaps for roughly what I'd want to do?
     
  9. Offline

    Irantwomiles

    Don't treemaps keep the values in the order they were put in? Or I'm dead wrong :p
     
  10. Offline

    HoeMC

    TreeMaps iterate by key value. You're thinking of LinkedHashMaps which retain the order the pairs were inserted.
     
    Last edited: Mar 10, 2016
    mine-care likes this.
  11. Offline

    Gonmarte

    I have not many experience on this, and even if i did i wouldnt spoonfeed. Why you dont start for the first step? I bet you know how to do a hashmap. Then follow all the steps, and if u dont understand one, just tell us. We cant help you if u dont show us some start. If you feel that u dont understand nothing of that, you should re-learn java.
     
  12. Offline

    HoeMC

    Problem is what you suggested wouldn't work; even if it did there are much cleaner ways to do that using TreeMaps. Writing a comparator for value sorting is the easiest way I see of achieving this with maps.
     
  13. Offline

    Gonmarte

    Why it wouldnt work?
     
  14. Offline

    HoeMC

    Because standard Java map implementations are many-to-one mappings. If two players have the same number of kills, you can't represent that as a key value pair in an Integer -> UUID map. You can, however, instantiate a map of Integer -> Collection<UUID>.
     
Thread Status:
Not open for further replies.

Share This Page