Solved Making a list command!

Discussion in 'Plugin Development' started by Astrophylite, Sep 3, 2015.

Thread Status:
Not open for further replies.
  1. Hey all,

    I am working on a plugin that will list all online players.
    Here is the code I have so far:
    Code:
    package me.zircon.zirconessentials.commands.general;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class List implements CommandExecutor {
    
        Object[] onlinePlayers = Bukkit.getServer().getOnlinePlayers().toArray();
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("list")) {
                if(!sender.hasPermission("zirconessentials.list")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to list all online players!");
                    return false;
                } else {
                    StringBuilder sb = new StringBuilder();
                    for(int i = 0; i < Bukkit.getServer().getOnlinePlayers().size(); i++) {
                        if(i == Bukkit.getServer().getOnlinePlayers().size() - 1) {
                            sb.append(onlinePlayers[i]);
                        } else {
                            sb.append(onlinePlayers[i] + ", ");
                        }
                    }
                    String s = sb.toString();
                    sender.sendMessage(ChatColor.AQUA + "Online Players:");
                    sender.sendMessage(ChatColor.AQUA + s);
                }
            }
            return false;
        }
    }
    
    Right now, it is working perfectly, but my only problem is that it appears as "CraftPlayer{Name=_Zircon_}"

    Any help will be appreciated,
    _Zircon_
     
  2. Offline

    timtower Administrator Administrator Moderator

    @_zircon_ That is because you are printing the entire Player object, try player.getName() instead
     
  3. Offline

    Xerox262

    Code:
    sb.append(onlinePlayers[i]);
    Should be
    Code:
    sb.append(onlinePlayers[i].getName());
     
  4. Offline

    teej107

    It's printing the value returned by the toString method.
    @Xerox262 showed the correct method to get the player's name.
     
  5. @Xerox262's method of doing isn't a thing in Eclipse? I'll experiment with it.

    Okay. So I've resorted to using an ArrayList, and everytime someone joins, they get added to the ArrayList, everytime they leave, they get removed from the ArrayList. Not the best idea, if someone has any better ideas, please tell me.. I'm dying over here :p

    YAY! I got it working with the ArrayList, now my only problem is when I reload, the ArrayList gets emptied...

    EDIT: Fixed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
Thread Status:
Not open for further replies.

Share This Page