Solved Bukkit.getOnlinePlayers type mismatch

Discussion in 'Plugin Development' started by KarimAKL, Mar 5, 2018.

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

    KarimAKL

    I'm trying to make a command that shows a list where you can see how many players are online on the server and how many staff aswell but when i try creating it i get this error 'Type mismatch: cannot convert from Collection<capture#1-of ? extends Player> to Player[]' at 'Bukkit.getOnlinePlayers()', could it be that the way i do it is outdated? I'm new to this so i followed a guide/tutorial. Here is the code:
    View attachment 31338 Thanks in advance.
     
  2. Online

    timtower Administrator Administrator Moderator

    @KarimAKL It is not an array anymore, it is a collection.
    You can also use an enhanced for loop instead of the counter.
     
  3. Offline

    MattTheBeast

    @KarimAKL I got board and coded you a util class for online players, your welcome. Definatly not spoon feeding.

    Code:
    public enum OnlinePlayerList {
    
        OWNERS(0), ADMINS(1), MODS(2), DEFAULTS(3), ALL(-1);
    
        private int id;
        private ArrayList<Player>[] players;
    
        OnlinePlayerList(int id) {
            this.id = id;
            players = new ArrayList[4];
            for (int i = 0; i < 4; i++) {
                players[i] = new ArrayList<Player>();
            }
            getPlayers();
        }
    
        private void getPlayers() {
            for (Player player : Bukkit.getOnlinePlayers()) {
                if (player.hasPermission("KitPvp.Rank.Owner")) players[0].add(player);
                else if (player.hasPermission("KitPvp.Rank.Admin")) players[1].add(player);
                else if (player.hasPermission("KitPvp.Rank.mod")) players[2].add(player);
                else players[3].add(player);
            }
        }
    
        public ArrayList<Player> getPlayerList(){
            if (id >= 0) {
                return players[id];
            }else{
                ArrayList<Player> allPlayers = new ArrayList<Player>();
                allPlayers.addAll(Bukkit.getOnlinePlayers());
                return allPlayers;
            }
        }
    
        public int size() {
            return (id >= 0 ? players[id].size()
                    : (players[0].size() + players[1].size() + players[2].size() + players[3].size()));
        }
    
    }
    
    Use it like this:

    Code:
                //OWNERS, ADMINS, MODS, DEFAULTS, ALL(All players, ignoring rank)
          
                //To get amount of players for specified rank
                OnlinePlayerList.OWNERS.size();
          
                //To get an ArrayList of players for specified rank
                OnlinePlayerList.MODS.getPlayerList();
    
    Note that a collection is a interface, and an arraylist is a class that extends the abstractlist
     
    Last edited: Mar 7, 2018
  4. Offline

    KarimAKL

    Thanks, i'll try looking it up. :)

    Wow, thanks alot. :eek: This is working perfectly as of yet. :D
     
    MattTheBeast likes this.
Thread Status:
Not open for further replies.

Share This Page