You look thru all the players for each rank, that's really not necessary. Use if else statements for the permissions. Code:java for (Player player : Bukkit.getOnlinePlayers()) { if (player.testPermission("a")) { //Stuff } else if (player.testPermission("b")) { //Stuff } else if ~ //Etc} Also, I'm not sure if you code alone or with someone, but I'd highly recommend naming your objects correct. This prevents confusion for others who read your code, or for yourself when you read it again in like half a year. Here's an implementation that sorts both on rank & on playername. Code:java 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 } } Here's a pastebin link for readability.
elementalgodz11 Not sure if this will work as I'm unable to test it. PHP: private String getOrderedList(){ StringBuilder sb = new StringBuilder(); String delimiter = ChatColor.WHITE + ", "; List<Player> list = Arrays.asList(Bukkit.getOnlinePlayers()); for (Player players:Bukkit.getOnlinePlayers()){ if (list.indexOf(players) == list.size()-1){ delimiter = "."; } if (players.hasPermission("dkhc.owner")){ sb.append(ChatColor.DARK_RED + players.getName() + delimiter); } else if (players.hasPermission("dkhc.admin")){ sb.append(ChatColor.RED + players.getName() + delimiter); } else if (players.hasPermission("dkhc.moderator")){ sb.append(ChatColor.DARK_PURPLE + players.getName() + delimiter); } else if (players.hasPermission("dkhc.pro")){ sb.append(ChatColor.GOLD + players.getName() + delimiter); } else if (players.hasPermission("dkhc.mvp")){ sb.append(ChatColor.BLUE + players.getName() + delimiter); } else if (players.hasPermission("dkhc.vip")){ sb.append(ChatColor.GREEN + players.getName() + delimiter); } else if (players.hasPermission("dkhc.default")){ sb.append(ChatColor.YELLOW + players.getName() + delimiter); } } return sb.toString();}
dont care about this, JDK compiler will create stringbuilder itself. "STRING" + "String" compiled into stringbuilder and appends. it will slowdown you, not JVM at execution time. also you can benchmark yourself with System.nanotime() EDIT by Moderator: merged posts, please use the edit button instead of double posting.