Advertisement
Guest User

Playerlist implementation

a guest
Feb 22nd, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1.     public void sendList(Player p) {
  2.         //Create a new ArrayList for all the entries
  3.         ArrayList<ListEntry> entries = new ArrayList<>();
  4.        
  5.         for (Player player : Bukkit.getOnlinePlayers()) { //Loop thru players
  6.             if (player.isOp()) continue; //Nowhere in your code you deal with players who are OP? so just skip them?
  7.            
  8.             //Deal with ranks
  9.             if (tryToAdd(entries, 1, player, ChatColor.DARK_RED,    "owner"))       continue; //Try to add, if it added, continue to next player
  10.             if (tryToAdd(entries, 2, player, ChatColor.RED,         "admin"))       continue; //If it didn't, move on to the next rank
  11.             if (tryToAdd(entries, 3, player, ChatColor.DARK_PURPLE, "moderator"))   continue;
  12.             if (tryToAdd(entries, 4, player, ChatColor.GOLD,        "pro"))         continue;
  13.             if (tryToAdd(entries, 5, player, ChatColor.BLUE,        "mvp"))         continue;
  14.             if (tryToAdd(entries, 6, player, ChatColor.GREEN,       "vip"))         continue;
  15.             if (tryToAdd(entries, 7, player, ChatColor.YELLOW,      "default"))     continue;
  16.         }
  17.        
  18.         //Now you should have a list full with entires, since it implements Comparable you can sort it.
  19.         Collections.sort(entries);
  20.        
  21.         //Build the message
  22.         String msg = "Players: ";
  23.        
  24.         boolean first = true;
  25.         for (ListEntry entry : entries) { //Loop thru entries
  26.             if (!first) { //Check if not first player added
  27.                 msg += ChatColor.WHITE + ", "; //Add the ,
  28.             }
  29.            
  30.             first = false;
  31.             msg += entry.color + entry.name; //Add the color + name to the message
  32.         }
  33.        
  34.         //Message is complete
  35.         p.sendMessage(msg);
  36.        
  37.     }
  38.    
  39.     private boolean tryToAdd(ArrayList<ListEntry> entries, int rank, Player player, ChatColor color, String permission) {
  40.         if (player.hasPermission("dkhc." + permission)) {
  41.             entries.add(new ListEntry(rank, player.getName(), color));
  42.             return true;
  43.         }
  44.         return false;
  45.     }
  46.    
  47.    
  48.     private class ListEntry implements Comparable<ListEntry> {
  49.        
  50.         int rank;
  51.         String name;
  52.         ChatColor color;
  53.        
  54.         public ListEntry(int rank, String name, ChatColor color) {
  55.             this.rank = rank;
  56.             this.name = name;
  57.             this.color = color;
  58.         }
  59.        
  60.         @Override
  61.         public int compareTo(ListEntry o) { //First sort on rank -> After that sort on name
  62.             //First check on rank
  63.             int comparison = Integer.valueOf(rank).compareTo(Integer.valueOf(o.rank)); //Compare the rank of this entry to the otherone
  64.            
  65.             //If you want to sort on names, use this: | Otherwise just return the comparison
  66.             if (comparison != 0) return comparison; //Ranks aren't equal, return the difference
  67.             //return comparison;
  68.            
  69.             //Optional: Sort on name
  70.             return name.toLowerCase().compareTo(o.name.toLowerCase()); //Compare names
  71.         }
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement