Help with /who command

Discussion in 'Plugin Development' started by GaaTavares, Jul 27, 2013.

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

    GaaTavares

    How can i do /who command, but when i try to do this, in every line, is only 1 player name. I'm newb on bukkit. Here is my code:

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("quem")) {
    2. player.sendMessage(ChatColor.WHITE + "Atualmente há " + Bukkit.getOnlinePlayers().length + " jogadores online");
    3. player.sendMessage(ChatColor.RED + "Admin" + "" + ChatColor.GRAY + ", " + ChatColor.ITALIC + "" + ChatColor.DARK_PURPLE + "" + ChatColor.ITALIC + "Mod+" + ChatColor.RESET + ChatColor.GRAY + ", " + ChatColor.DARK_PURPLE + "Mod" + ChatColor.GRAY + ", " + ChatColor.GOLD + "Pro" + ChatColor.GRAY + ", " + ChatColor.BLUE + "Mvp" + ChatColor.GRAY + ", " + ChatColor.GREEN + "Vip");
    4. for (Player online : player.getServer().getOnlinePlayers()) {
    5. String playername = online.getDisplayName();
    6. String virgula = ",";
    7. player.sendMessage(virgula + playername);
     
  2. Offline

    cedeel

    Hi there :)

    First of all, your second sendMessage(), the one where you write "Admin" etc. what's that for?
    It seems like it's a static string that won't change, so create a variable for that, and send the variable instead.

    Also, the way you're sending player names, it will be one per line. I'm not sure if it's what you intended.

    How many players are on your server when you try the command?
     
  3. Offline

    TheE

    You are sending each player as an individual message since you are doing it in the for-loop. What you want to do is add the name to a StringBuilder and send the StringBuilder's String to the client:
    Code:java
    1.  
    2. StrBuilder message = new StrBuilder(); //StringBuilder implementation out of apache commons (bundled with bukkit), provides some additional stuff
    3. for (Player online : player.getServer().getOnlinePlayers()) {
    4. ret.appendSeparator(", "); //additional stuff I was talking about
    5. ret.append(online.getName());
    6. }
    7. player.sendMessage(message.toString());
    8.  
     
  4. Offline

    rippin

    You have player.sendMessage() in a loop and that starts a new line every time. You can try adding all names to a single string and then sending the message.
     
  5. Offline

    ERROR372

    GaaTavares

    player.sendMessage sends a line, and each subsequent player.sendMessage will send a new line.

    What you need to do, is have a "String players;" above the for loop. Then go through the for loop and do:

    players = players + playername + ", ";

    Then after the for loop, send the player the string.
     
  6. Offline

    GaaTavares


    The second line is the server ranks: adm,mod+,pro,vip, etc..
     
  7. Offline

    cedeel


    In that case, wouldn't it be more useful to get the groups from your permissions plugin, preferably through Vault?
     
Thread Status:
Not open for further replies.

Share This Page