top 10?

Discussion in 'Spigot Plugin Development' started by Yazio, Jun 28, 2015.

  1. hi im wondering on how to get top 10... right now i have this

    PHP:
    p.sendMessage("§71. §d" + tokens.firstKey());
    and it gives me this error
    PHP:
    Caused by: java.util.NoSuchElementException
            at java.util.TreeMap.key(Unknown Source) ~[?:1.8.0_40]
            at java.util.TreeMap.firstKey(Unknown Source) ~[?:1.8.0_40]
     
  2. Tux

    Tux

    The TreeMap is empty. Add a check to make sure it has contents.

    Additionally, your check may be wrong depending on how you're using the TreeMap.
     
  3. i checked if its empty and then if it is.. im adding the player.. but now it says my uuid instead of how many tokens i have

    NEW CODE:
    PHP:
    if (tokens.isEmpty()) {
                            tokens.put(p.getUniqueId(), (Integer) getConfig().get("Players." + p.getUniqueId().toString()));
                        } else {
                            p.sendMessage("§71. §d" + tokens.firstKey());
                        }
     
  4. Code (Text):
    if (!tokens.isEmpty()) {
        // Do some for loop iteration to find the top 10 of either the values or keys in the TreeMap.
    }
    Edit: Opps, I didn't see you reply.
     
  5. Thats why..
     
    • Funny Funny x 2
  6. well how would i change that? i need that p.getUniqueId()
     
  7. PHP:
    p.sendMessage("§71. §d" + Bukkit.getPlayer(tokens.firstKey()).getName);
    and instead of casting it you can do:
    Code (Text):
    getConfig().getInt(Bukkit.getPlayer(tokens.firstKey()));
     
    • Informative Informative x 1
  8. Code (Text):

                        if (tokens.isEmpty()) {
                            tokens.put(p, (Integer) getConfig().get("Players." + p.getUniqueId().toString()));
                        } else {
                            p.sendMessage("§71. §d" + tokens.firstKey().getName());
                        }
     
  9. kind of "worked" it says nothing in my chat
     
  10. I suggest you not store player objects in TreeMaps as it can cause memory leaks.
     
  11. where da hell did u get .getName() from that??
     
  12. So can storing UUIDs. Dont be part of the false information. It is just as safe to store Player objects as any other object.
     
    • Informative Informative x 1
  13. Are there any errors in your console?
    Also are you sure the player you're sending the message to is you?
     
  14. nope
    yup
     
  15. You want a top 10 list, right? What else should it show? Their amount of tokens? Then you just have a list of numbers.. I thought you wanted a list of player names to show the top 10.
     
  16. i mean .getName() shows up in red... but..

    i want it to say
    1. <name> <amount of tokens>
    2. <name> <amount of tokens>
    3. <name> <amount of tokens> etc..
     
  17. PHP:
    if (tokens.isEmpty()) {
        tokens.put(p.getUniqueId(), getConfig().getInt("Players." + p.getUniqueId().toString()));
    } else {
        p.sendMessage("§71. §d" + Bukkit.getPlayer(tokens.firstKey()).getName());
    }
    Does that not send you anything at all? Strange...
     
  18. You need to iterate through the map, then. Right now you are just showing 1 player. Where is the rest of your code to show numbers 2-10?

    The .getName() part wont show up in red if you put your TreeMap as <Player, Double> as I suggested. The key would then return a Player object with .firstKey() and you can easily to .getName() from it.

    I suggest rethinking the map altogether though. Why do you need UUIDs there? Why not just store the relevant information (name, double)?
     
    • Agree Agree x 1
  19. uuid...