Solved How to get online players?

Discussion in 'Plugin Development' started by CrazyYoungBro, Jun 12, 2016.

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

    CrazyYoungBro

    Hello,
    I've been working on a plugin that helps communication between staff and players. For this, I need to get the list of online players. I tried this
    Code:
    Player[] list = Bukkit.getServer().getOnlinePlayers();
    But this doesn't work. Please help!
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    CrazyYoungBro

    @timtower The code I listed above returns an error. This is it:
    Type mismatch: cannot convert from Collection<capture#1-of ? extends Player> to Player[]
     
  4. Offline

    timtower Administrator Administrator Moderator

    @CrazyYoungBro I know.
    That is why I asked you what it returns in the first place.
     
  5. Offline

    CrazyYoungBro

    @timtower Is there any solution to this?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @CrazyYoungBro Open the javadocs.
    Find getOnlinePlayers.
    Look at what it returns.
    Put that in your code.
     
    teej107 likes this.
  7. Offline

    CrazyYoungBro

  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    CrazyYoungBro

  10. Offline

    I Al Istannen

    @CrazyYoungBro
    Do the errors vanish if you press CTRL+SHIFT+O? If you use eclipse. This organizes the imports.
    If not, actually saying what the errors are will help.

    You may want to stop Bukkit developing right here and learn a bit more Java instead. You will benefit from it.
     
  11. Offline

    CrazyYoungBro

    @I Al Istannen
    The errors don't vanish.
    I have already mentioned the errors above.
    In short, I want to check if any online player has a specific permission and if so, I want to send all online players with the given permission, a message. Is there any way I could do this? Any code to help me do this?

    Also, I am experienced with Java. I know all the basics.
     
  12. Offline

    I Al Istannen

    @CrazyYoungBro
    You didn't say what the new errors you get are. Just write them, and post your updated code. The whole class, please.
     
  13. Offline

    timtower Administrator Administrator Moderator

    @CrazyYoungBro list = Bukkit.getServer().getOnlinePlayers()
    Will show an error in eclipse.
    Hover mouse over list, hit the quick fix
     
  14. Offline

    CrazyYoungBro

    Thanks

    @timtower Please specify a variable type for list

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  15. Offline

    mine-care

  16. Offline

    sniddunc

    If you're trying to get a collection of players, what do you think the type will be?
     
  17. Offline

    CrazyYoungBro

    Player[] type isn't working. Nor is Collection or List type working.
     
  18. Offline

    mine-care

    @CrazyYoungBro Define "Not working"
    Compilation error?
    Runtime error?
    If it is of the right type then there is no reason for an error.
     
  19. Offline

    CrazyYoungBro

    @mine-care
    Using Player[] gets an compile time error : Type mismatch: cannot convert from Collection<capture#1-of ? extends Player> to Player[]
    This error has a quick fix to change the variable type to Collection<? extends Player> which also returns a compile time error with the method .hasPermission() stating : The method hasPermission(String) is undefined for the type Collection<capture#2-of ? extends Player>
    Using List<Player> also gets the same error as stated for Collection.
     
  20. Offline

    timtower Administrator Administrator Moderator

    @CrazyYoungBro Could you post the full code block then? We just got 1 line from you. We don't know what is around it.
     
  21. Offline

    CrazyYoungBro

    @timtower Here it is
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Command only for players!");
            }
            Player p = (Player) sender;
            String message = "";
           
           
            if(cmd.getName().equalsIgnoreCase("a")) {
               
                for(int i= 1; i <= args.length; i++) {
                    message = message + args[i] + " ";
                }
           
                 list = Bukkit.getServer().getOnlinePlayers();
                if(list.hasPermission("ph.view")) {
                  
                }
               
            }
           
            if(cmd.getName().equalsIgnoreCase("ma")) {
                if(!(p.hasPermission("ph.ma"))) {
                    p.sendMessage(ChatColor.BLUE + "Permissions> " + ChatColor.GRAY + "You do not have access to this command!");
                    return true;
                }
                if(args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Usage: /<command> <player> <message>");
                    return false;
                }
                else if(args.length == 1) {
                    p.sendMessage(ChatColor.RED + "Please provide a message!");
                    return false;
                }
               
                for(int i= 2; i <= args.length; i++) {
                    message = message + args[i] + " ";
                }
               
                if(p.hasPermission("ph.trainee")) {
                  
                }
               
            }
           
            return false;
        }
     
  22. Offline

    timtower Administrator Administrator Moderator

    @CrazyYoungBro Because a list can't have permissions.
    You need to loop through the list.
     
  23. Offline

    mine-care

    So the method returns a Collection and not an array. We've established that.
    as @timtower said, Lists or Collections do not have permission. You need to loop thrugh the elements of the collection in any way you choose, and for each 'element' (Player) check if they have permission.


    Also i noticed in your code:

    Although you check if the sender is not a player and you send the message, you don't stop the execution of code, so it will continue as normal and it will error.
     
  24. Offline

    CrazyYoungBro

    @timtower Oh yeah. So can I do something like
    for(Player x : xyz) {
    if(x)
    }
    Only one doubt remaining. What kind of list should I make? Collection extends Player, List or Player[]?

    Oh Ok Thanks Ill correct it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  25. Offline

    timtower Administrator Administrator Moderator

    @CrazyYoungBro As you experienced many times: Player[] won't work.
    I highly suggest taking another look at collections of any kind in Java.
     
    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page