Sort Players in TreeSet

Discussion in 'Plugin Development' started by Daveloper, Dec 25, 2023.

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

    Daveloper

    Hello, I am new in Plugin development and i hava a problem with my plugin. I want to add Players to a TreeSet, so I tried to write a compareTo(Player p) Method, so the TreeSet knows how to sort the Players, but it doesn't work. Help!

    Source code:

    Code:
    package at.daveloper.bedwars;
    
    import java.util.TreeSet;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    
    public class Test implements Comparable<Player>{
    
        public static void main(String[] args) {
            Player p = Bukkit.getPlayer("ExampleName");
            TreeSet<Player> players = new TreeSet<Player>();
            players.add(p);
            System.out.println(players);   
        }
           
           
        @Override
        public int compareTo(Player player2) {
            String player1Name = //btw what goes in here?
            String player2Name = player2.getName().toString();
            int length1 = player1Name.length(); 
            int length2 = player2Name.length(); 
            int limit = Math.min(length1, length2); 
            char v1[] = player1Name.toCharArray(); 
            char v2[] = player2Name.toCharArray(); 
              
            int i = 0; 
               while (i < limit) { 
                   char ch1 = v1[i]; 
                   char ch2 = v2[i]; 
                   if (ch1 != ch2) { 
                       return ch1 - ch2; 
                   } 
                   i++; 
               } 
               return length1 - length2; 
        }
    }
    
    
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @Daveloper A main function generally does not work in plugins...

    Why do you want to sort them?
     
  3. Offline

    Daveloper

    @timtower The main function is only to show you what I mean. I do not use it in my plugin.
    Because I write the playernames to a file and it should be in alphabetical order. A TreeSet needs a compareTo function or not?
     
  4. Online

    timtower Administrator Administrator Moderator

    Why should it be in alphabetical order? And why are you even using names to start with? UUID's don't change, names do.
     
  5. Offline

    Daveloper

    Because if you look in the file manually you should find the player name quickly. But the main reason why I am using a TreeSet is because playernames can only be once in the "List".
     
  6. Online

    timtower Administrator Administrator Moderator

    ctrl+f ?
    Else: use a Hashmap<String, Player>
    Then loop over the map.

    Still find it weird that you need to look at the file manually though.
     
  7. Offline

    Daveloper

    Ok thanks, but how would I write a compareTo method to compare players? Only because I am interested.
     
  8. Online

    timtower Administrator Administrator Moderator

  9. Offline

    Daveloper

    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page