Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void sendList(Player p) {
- //Create a new ArrayList for all the entries
- ArrayList<ListEntry> entries = new ArrayList<>();
- for (Player player : Bukkit.getOnlinePlayers()) { //Loop thru players
- if (player.isOp()) continue; //Nowhere in your code you deal with players who are OP? so just skip them?
- //Deal with ranks
- if (tryToAdd(entries, 1, player, ChatColor.DARK_RED, "owner")) continue; //Try to add, if it added, continue to next player
- if (tryToAdd(entries, 2, player, ChatColor.RED, "admin")) continue; //If it didn't, move on to the next rank
- if (tryToAdd(entries, 3, player, ChatColor.DARK_PURPLE, "moderator")) continue;
- if (tryToAdd(entries, 4, player, ChatColor.GOLD, "pro")) continue;
- if (tryToAdd(entries, 5, player, ChatColor.BLUE, "mvp")) continue;
- if (tryToAdd(entries, 6, player, ChatColor.GREEN, "vip")) continue;
- if (tryToAdd(entries, 7, player, ChatColor.YELLOW, "default")) continue;
- }
- //Now you should have a list full with entires, since it implements Comparable you can sort it.
- Collections.sort(entries);
- //Build the message
- String msg = "Players: ";
- boolean first = true;
- for (ListEntry entry : entries) { //Loop thru entries
- if (!first) { //Check if not first player added
- msg += ChatColor.WHITE + ", "; //Add the ,
- }
- first = false;
- msg += entry.color + entry.name; //Add the color + name to the message
- }
- //Message is complete
- p.sendMessage(msg);
- }
- private boolean tryToAdd(ArrayList<ListEntry> entries, int rank, Player player, ChatColor color, String permission) {
- if (player.hasPermission("dkhc." + permission)) {
- entries.add(new ListEntry(rank, player.getName(), color));
- return true;
- }
- return false;
- }
- private class ListEntry implements Comparable<ListEntry> {
- int rank;
- String name;
- ChatColor color;
- public ListEntry(int rank, String name, ChatColor color) {
- this.rank = rank;
- this.name = name;
- this.color = color;
- }
- @Override
- public int compareTo(ListEntry o) { //First sort on rank -> After that sort on name
- //First check on rank
- int comparison = Integer.valueOf(rank).compareTo(Integer.valueOf(o.rank)); //Compare the rank of this entry to the otherone
- //If you want to sort on names, use this: | Otherwise just return the comparison
- if (comparison != 0) return comparison; //Ranks aren't equal, return the difference
- //return comparison;
- //Optional: Sort on name
- return name.toLowerCase().compareTo(o.name.toLowerCase()); //Compare names
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement