Bukkit.getOnlinePlayers returns error

Discussion in 'Plugin Development' started by Derugo, Aug 13, 2014.

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

    Derugo

    I'm new to coding java and I'm attempting to create a custom list based on permissions plugin for an upcoming server of mine, and ran into this;

    It seems to be the Bukkit.getOnlinePlayers() is causing the issue, but I'm not sure.

    Here's the code and my error log, I've made the part that makes an error, bold and italic.

    Code:
    package me.vadoxlist;
     
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin {
     
        public void onEnable() {
            getLogger().info("Plugin enabled!");
        }
     
        public void onDisable() {
            getLogger().info("Plugin disabled!");
        }
     
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("list")) {
                Player[] player = [B][I][U]Bukkit.getOnlinePlayers[/U][/I][/B]();
                int admins = 0,owners = 0,mods = 0,users = 0;
                for (int i = 0; i < player.length; i++) {
                    if (player[i].hasPermission("BetterList.Admin")) {
                        admins++;
                    } else if (player[i].hasPermission("BetterList.Moderator")) {
                        mods++;
                    } else if (player[i].hasPermission("BetterList.Owner")) {
                        owners++;
                    }
                    else {
                        users++;
                    }
                }
                sender.sendMessage("----------------------------");
                sender.sendMessage("Players ("+""+users+"/"+Bukkit.getServer().getMaxPlayers()+") Online");
                sender.sendMessage("Admins: ("+admins+") Online");
                sender.sendMessage("Moderators: ("+mods+") Online");
                sender.sendMessage("Owners: ("+owners+") Online");
                sender.sendMessage("----------------------------");
            }
            return true;
        }
    }
    
    When I hover over the Bukkit.getOnlinePlayers() in eclipse it shows:
    " Type mismatch: cannot convert from Collection<capture#1-of ? extends Player> to Player[]"

    Error log (from console log):

    Code:
    [05:03:36] [Server thread/INFO]: Derugo issued server command: /list
    [05:03:36] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'list' in plugin VadoxList v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[minecraft_server.jar:git-Spigot-1591]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[minecraft_server.jar:git-Spigot-1591]
        at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[minecraft_server.jar:git-Spigot-1591]
        at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1013) [minecraft_server.jar:git-Spigot-1591]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:850) [minecraft_server.jar:git-Spigot-1591]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [minecraft_server.jar:git-Spigot-1591]
        at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [minecraft_server.jar:git-Spigot-1591]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:184) [minecraft_server.jar:git-Spigot-1591]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [minecraft_server.jar:git-Spigot-1591]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:731) [minecraft_server.jar:git-Spigot-1591]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [minecraft_server.jar:git-Spigot-1591]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [minecraft_server.jar:git-Spigot-1591]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [minecraft_server.jar:git-Spigot-1591]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [minecraft_server.jar:git-Spigot-1591]
    Caused by: java.lang.Error: Unresolved compilation problem:
        Type mismatch: cannot convert from Collection<capture#1-of ? extends Player> to Player[]
     
        at me.vadoxlist.Main.onCommand(Main.java:21) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[minecraft_server.jar:git-Spigot-1591]
        ... 13 more
    [05:03:36] 
    All and any help is appreciated!
     
  2. Offline

    fireblast709

    Derugo CraftBukkit changed the return type of Bukkit.getOnlinePlayers() from a Player[] to a Collection<? extends Player>. Just change this in your code.
     
    Garris0n likes this.
  3. Offline

    Garris0n

    It returns a collection, not an array.
     
  4. Offline

    Derugo

  5. Offline

    fireblast709

    Derugo instead of indexing it yourself, why don't you change it to a foreach loop
    Code:
    for(Player player : Bukkit.getOnlinePlayers())
    {
        // TODO: something with the player
    } 
     
  6. Offline

    Derugo

    How would I go about doing this?
    Doing it for each of the groups, users, moderators, admins, owners, etc?
     
  7. Offline

    fireblast709

    Derugo in that printscreen you posted, it would just be changing player[ i] to player (and the for loop to a foreach as shown in my previous snippet)
     
  8. Offline

    Derugo

    Not sure if this is right but no more errors except for a new error with player.lengh:
    http://prntscr.com/4cmu3q
     
  9. Offline

    fireblast709

    Derugo because you kept your old code and wrapped it in a for loop... First declare the int values 'owners', 'admins', 'mods' and 'users', then loop over the players using
    And in the loop you do the if-statements
     
  10. Offline

    Derugo

    Completely puzzled with what you're telling me to do. Any chance you could show an example of what you mean for one of the int values?
     
  11. Offline

    Necrodoom

    Derugo read Java documentation and tutorials about how Java collections and loops work.
     
Thread Status:
Not open for further replies.

Share This Page