Solved Ordering a list of Int's from a config

Discussion in 'Plugin Development' started by ProSl3nderMan, Aug 20, 2015.

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

    Zombie_Striker

    @ProSl3nderMan
    Since I did not order my comments as you did, let me see if I get this correct:
    then fix your code to add a delay. Use schedulers for the delay.

    Alright, this doesn't really matter, but you could add it if you want the your code to not be that specific.

    The break means that if i is equal to the players name, it will stop looking for the players name. That should not cause your code to light up in you IDE.

    Those brackets do nothing but will break your plugin if it reaches that point. I don't know what would happen since I never actually tried putting brackets around methods, but it most likely it would not do anything good. You would have to remove the open bracket and the closed bracket and everything should be fine after that.

    As for this, you are probibly printing the UUID (sendmessage(uuid)) instead of sending their name (sendmessage(bukkit.getofflineplayer(uuid).getname) )
     
  2. Offline

    ProSl3nderMan

    Lol, I didn't see the first comment. 1: was to the equalsignorecase, 2: was to the break;, 3: was to the bracket. Although I totally forgot that it was one method, which is why you suggested removing it. Thanks.

    Now, I shall try the sendmessage thing.

    @Zombie_Striker
    Idk understand how to put that sendmessage thing in with this code:
    Code:
    if (args[0].equalsIgnoreCase("top")) {
                for(String s : AccessControl.plugin.getConfig().getConfigurationSection("players.").getKeys(false)) {
                    hm.put(s, Integer.valueOf(AccessControl.plugin.getConfig().getInt("players." + s + ".GamesWon")));
                }
                List<String> sorted = new ArrayList<String>(hm.keySet());
                Collections.sort(sorted, new Comparator<String>() {
                    public int compare(String s1, String s2) {
                        return Integer.valueOf(hm.get(s2)).compareTo(hm.get(s1));
                    }
                });
               
                for (int i = 0; i < hm.size(); i++) {
                        if (i > 10) break;
                            p.sendMessage((i + 1) + ". " + sorted.get(i) + ": " + hm.get(sorted.get(i)));
                }
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  3. Offline

    Zombie_Striker

    If (sorted.get(i)) returns the UUID, use getServer().getPlayer(sorted.get(i)).getname

    If it's (hm.get(sorted.get(i))), then use getServer().getPlayer(hm.get(sorted.get(i))).getname()
     
  4. Offline

    ProSl3nderMan

    getServer() underfined for class? Put Bukkit.getServer().
    Anyways, this line doesn't work: getServer().getPlayer(hm.get(sorted.get(i))).getname() Says for getPlayer "The method getPlayer(String) in the type Server is not applicable for the arguments (Integer)"
     
    Last edited: Aug 26, 2015
  5. Offline

    Zombie_Striker

    @ProSl3nderMan
    Then try getting the offline player (.getOfflinePlayer(String))

    [edit] this should work:
    Bukkit.getServer().getPlayer(String or UUID)
     
  6. Offline

    ProSl3nderMan

    @Zombie_Striker
    Is this what you had in mind?: Bukkit.getOfflinePlayer(hm.get(sorted.get(i))).getName()
    Doesn't work, same error as before. i is an int btw.
     
  7. I think Collection#sort(list) sorts it in ascending order so I'd do a for loop like this:
    List<String> toShow = new ArrayList<String>();
    for(int i = 0; i < 10; i++) {
    toShow.add(i + ":" + ( list#get(list#size() - i) ) );
    }
     
  8. Offline

    ProSl3nderMan

    @TheGamesHawk2001
    The code I already have works. What I need help on is displaying the IGN instead of the UUID
     
  9. You can get the player from a UUID by doing Bukkit#getPlayer(UUID uuid) and then their name by Player#getName() :)
     
  10. Offline

    ProSl3nderMan

    Doesn't work, my code:
    Code:
    if (args[0].equalsIgnoreCase("top")) {
                for(String s : AccessControl.plugin.getConfig().getConfigurationSection("players.").getKeys(false)) {
                    hm.put(s, Integer.valueOf(AccessControl.plugin.getConfig().getInt("players." + s + ".GamesWon")));
                }
                List<String> sorted = new ArrayList<String>(hm.keySet());
                Collections.sort(sorted, new Comparator<String>() {
                    public int compare(String s1, String s2) {
                        return Integer.valueOf(hm.get(s2)).compareTo(hm.get(s1));
                    }
                });
               
                for (int i = 0; i < hm.size(); i++) {
                        if (i > 10) break;
                            p.sendMessage((i + 1) + ". " + Bukkit.getPlayer(sorted.get(i)).getName() + ": " +  hm.get(sorted.get(i)));
                }
            }
    
    This is the error I get in the console:
    I'm sorry for sounding desperate, but I really would like to get this code to work.
    This is what I have and thought should work:
    Code:
            if (args[0].equalsIgnoreCase("top")) {
                for(String s : AccessControl.plugin.getConfig().getConfigurationSection("players.").getKeys(false)) {
                    hm.put(s, Integer.valueOf(AccessControl.plugin.getConfig().getInt("players." + s + ".GamesWon")));
                }
                List<String> sorted = new ArrayList<String>(hm.keySet());
                Collections.sort(sorted, new Comparator<String>() {
                    public int compare(String s1, String s2) {
                        return Integer.valueOf(hm.get(s2)).compareTo(hm.get(s1));
                    }
                });
              
                for (int i = 0; i < hm.size(); i++) {
                        if (i > 10) break;
                            p.sendMessage((i + 1) + ". " + Bukkit.getServer().getPlayer(sorted.get(i)).getName() + ": " +  hm.get(sorted.get(i)));
                }
            }
    Correct me if I did anything wrong.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  11. Offline

    Zombie_Striker

    Caused by: java.lang.NullPointerException
    at me.ProSl3nderMan.AccessControl.CrExecutor.onCommand(CrExecutor.java:527) ~[?:?]

    What is line 527? Something on that line is null.
     
  12. Offline

    ProSl3nderMan

    This was line 527: " p.sendMessage((i +1)+". "+ Bukkit.getPlayer(sorted.get(i)).getName()+": "+ hm.get(sorted.get(i)));"
     
  13. Offline

    Zombie_Striker

    Okay so after each +, create a new line so it looks like the following:
    p.sendMessage((i +1)+
    ". "+
    Bukkit.getPlayer(sorted.get(i)).getName()+
    ": "+
    hm.get(sorted.get(i)));"
    This will help sort out which statement is causing the error. Most likely its that the player is returning null.
     
  14. Offline

    ProSl3nderMan

    Just did it, this is the problem: "(Bukkit.getPlayer(sorted.get(i)).getName())" although I see nothing wrong.
     
  15. Offline

    Zombie_Striker

    Well, the player most likely is null, meaning the the UUID that is being returned is not correct/does not exist. Check what sorted.get(i) is equal to and check if the player is null before you send the message.
     
  16. Offline

    SuperOriginal

    Bukkit.getPlayer(uuid) returns null if they're not online...

    You need to get the OfflinePlayer
     
  17. Offline

    ProSl3nderMan

    @SuperOriginal
    Doesn't work, my code:
    Code:
    if (args[0].equalsIgnoreCase("top")) {
                for(String s : AccessControl.plugin.getConfig().getConfigurationSection("players").getKeys(false)) {
                    hm.put(s, Integer.valueOf(AccessControl.plugin.getConfig().getInt("players." + s + ".GamesWon")));
                }
                List<String> sorted = new ArrayList<String>(hm.keySet());
                Collections.sort(sorted, new Comparator<String>() {
                    public int compare(String s1, String s2) {
                        return Integer.valueOf(hm.get(s2)).compareTo(hm.get(s1));
                    }
                });
              
                for (int i = 0; i < hm.size(); i++) {
                        if (i > 10) break;
                            p.sendMessage((i + 1) + ". " + Bukkit.getOfflinePlayer(sorted.get(i)).getName() + ": " +  hm.get(sorted.get(i)));
                }
            }
    @Zombie_Striker
    If I do check if player is null, what do I do then?
     
  18. Offline

    Zombie_Striker

    @ProSl3nderMan
    if(Player == null) // if this returns true, the player doesn't exist.
     
  19. Offline

    ProSl3nderMan

    @SuperOriginal
    So apparently my code wasn't updating each time I exported it. Now that I tried it, it stills gives me the UUID?
     
  20. Offline

    Zombie_Striker

  21. Offline

    ProSl3nderMan

    This is the picture:
     

    Attached Files:

  22. Offline

    Zombie_Striker

    @ProSl3nderMan
    I have no idea then. .getName() should return the name.
     
  23. Offline

    au2001

    @ProSl3nderMan This code will return the name, you must have exported your JAR at a wrong location.
    Or maybe you forgot to reload/restart? And check you don't have to versions of the plugin installed.
     
  24. Offline

    ProSl3nderMan

    Well, exporting has been acting funny recently, when I get home I'll check it.
     
  25. or isn't online.
     
  26. Offline

    ProSl3nderMan

    Alright! The getOfflinePlayer didn't work with the sorted.get(i), so right when I was about to quit, I prayed. Then I look at the code and God gave me a clue that made me figure everything out! So, instead of looking up the UUID with getOfflinePlayer, I looked it up in the config and got the UUID with sorted.get(i) and ".IGN". Isn't God awesome and isn't coding so fun? Here is my code to help you'all understand what I did, would be line 15:
    Code:
            if (args[0].equalsIgnoreCase("top")) {
                for(String s : AccessControl.plugin.getConfig().getConfigurationSection("players").getKeys(false)) {
                    hm.put(s, Integer.valueOf(AccessControl.plugin.getConfig().getInt("players." + s + ".GamesWon")));
                }
            List<String> sorted = new ArrayList<String>(hm.keySet());
            Collections.sort(sorted, new Comparator<String>() {
                public int compare(String s1, String s2) {
                        return Integer.valueOf(hm.get(s2)).compareTo(hm.get(s1));
                }
                });
              
                for (int i = 0; i < hm.size(); i++) {
                        if (i > 10) break;
                            p.sendMessage((i + 1) + ". " + AccessControl.plugin.getConfig().getString("players." + sorted.get(i) + ".IGN") + ": " +  hm.get(sorted.get(i)));
                }
            }
    Thanks all for the help on this LONG thread. Really appreciate it.
     
  27. Offline

    au2001

    Code:
    for (int i = 0; i < hm.size(); i++) {
       if (i > 10) break;
        // Code
    }
    Well you can replace that with:
    Code:
    for (int i = 0; i <= 10; i++)
        // Code
    
     
  28. Offline

    ProSl3nderMan

    @au2001
    True ;P kinda weird the way I did it.
     
  29. Offline

    au2001

    @ProSl3nderMan Also, why do you always use Integer.valueOf()?

    If you have an Integer, you can set it to a plain int without any problem ;)
     
  30. Offline

    ProSl3nderMan

    @au2001
    it's just a tiny little thing hehe.
     
Thread Status:
Not open for further replies.

Share This Page