fd

Discussion in 'Plugin Development' started by elementalgodz11, Feb 22, 2014.

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

    elementalgodz11

    jk
     

    Attached Files:

  2. Offline

    Stoux

    You look thru all the players for each rank, that's really not necessary. Use if else statements for the permissions.

    Code:java
    1. for (Player player : Bukkit.getOnlinePlayers()) {
    2. if (player.testPermission("a")) {
    3. //Stuff
    4. } else if (player.testPermission("b")) {
    5. //Stuff
    6. } else if ~ //Etc
    7. }


    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
    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. }


    Here's a pastebin link for readability.
     
  3. Offline

    Henzz

    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();
    }
     
  4. Offline

    RawCode

    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.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page