Solved List online players

Discussion in 'Plugin Development' started by Vextricity, Dec 25, 2014.

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

    Vextricity

    Hello! I'm having a bit of a problem trying to list all online players. I've tried doing a StringBuilder and then appending all online players to it, but this is what I get: http://prntscr.com/5kx3m9

    I've also tried doing an arraylist, but the same thing is happening. I'm not really good with StringBuilders or anything like that, so could somebody give me a hand? Thanks. :)

    My code:
    Code:
            if (cmd.getName().equalsIgnoreCase("list"))
               
            {
               
                StringBuilder players = new StringBuilder();
               
                for (Player user : Bukkit.getOnlinePlayers())
               
                {    
                       
                    players.append(ChatColor.GRAY + user.getName() + ChatColor.DARK_GRAY + ", " + ChatColor.GRAY);
                    sender.sendMessage(ChatColor.BLUE + "Online Players " + ChatColor.DARK_GRAY + "| " + players );   
                   
                }
               
                return true;
    
            }
            
     
  2. Offline

    Konato_K

    Send the message AFTER you're done with the loop.
     
  3. Offline

    sam000

    Isn't there a command in bukkit wat is called list
     
  4. Offline

    SuperOriginal

    @sam000 You can override it if you want...
     
  5. Offline

    sam000

    @SuperOriginal yes i know but if there is already a command for why would you waste your time
     
  6. Offline

    SuperOriginal

    @sam000 For better customization or practice?
     
  7. Offline

    Konato_K

    @sam000 As you can see he uses some fancy colors, it's just aesthetical, but it's always worth, it's not like he is going to spend 359 hours making the command.
     
  8. Offline

    Vextricity

    Yes, that works! Thank you. :)

    Also, does anybody know how I can make it where it doesn't put the comma after the last player?
    http://prntscr.com/5kyhb7

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 17, 2016
  9. Offline

    Konato_K

    @Vextricity An alternative way is append the first name before the loop, then inside the loop is append the comma, then the player name, this way the last player wont have the comma at the end, but of course, you need to check if there is at least one player at the begin and start the loop in the second element (so you'll need a normal for loop, instead of for-each)

    Otherwise I guess you can trim the last part with subString
     
  10. Offline

    Vextricity

    @Konato_K Actually, I figured it out. During the loop, I simply did this:
    Code:
                        players.append(ChatColor.GRAY + user.getName().replaceAll(sender.getName(), "") + ChatColor.DARK_GRAY + ", " + ChatColor.GRAY + sender.getName());
    So yeah, it replaces the sender's name in the player list and adds them to the end, so that way there's no comma at the end and it doesn't repeat twice
     
Thread Status:
Not open for further replies.

Share This Page