Solved Player[] to iterable

Discussion in 'Spigot Plugin Development' started by mrkino99, Feb 18, 2020.

  1. Pls i need help, i got this error:
    Error:(36, 78) java: incompatible types: org.bukkit.entity.Player[] cannot be converted to java.lang.Iterable<? extends org.bukkit.entity.Player>
    And im using this method
    Code (Java):
    Collection<? extends Player> players = Bukkit.getServer().getOnlinePlayers();
            for (Player player : players) {
                applyBoard(player).addUpdates(players);
            }
    and the method addUpdates is this
    Code (Java):
    public void addUpdates(Iterable<? extends Player> updates) {
            if (this.removed.get()) {
                throw new IllegalStateException("Cannot update whilst board is removed");
            }

            new BukkitRunnable() {
                @Override
                public void run() {
                    if (removed.get()) {
                        cancel();
                        return;
                    }

                    // Lazy load - don't lookup this in every iteration
                    PlayerFaction playerFaction = null;
                    boolean firstExecute = false;
                    //

                    for (Player update : updates) {
                        if (player.equals(update)) {
                            members.addPlayer(update);
                            continue;
                        }

                        if (!firstExecute) {
                            playerFaction = plugin.getFactionManager().getPlayerFaction(player);
                            firstExecute = true;
                        }

                        // Lazy loading for performance increase.
                        PlayerFaction targetFaction;
                        if (playerFaction == null || (targetFaction = plugin.getFactionManager().getPlayerFaction(update)) == null) {
                            neutrals.addPlayer(update);
                        } else if (playerFaction == targetFaction) {
                            members.addPlayer(update);
                        } else if (playerFaction.getAllied().contains(targetFaction.getUniqueID())) {
                            allies.addPlayer(update);
                        } else {
                            neutrals.addPlayer(update);
                        }
                    }
                }
            }.runTaskAsynchronously(plugin);
        }
    what i have to do??
     
  2. If you want to transform a Player-array to an Iterable, you can use Arrays#asList(Array), however, I don't see where you pass an array to that method since players should be a Collection...
     
  3. i want to transform the collection of players into an iterable. how?
     
  4. A Collection already implements iterable, so you are probably passing an array somewhere to that method.
     
  5. Bukkit.getServer().getOnlinePlayers(), yes is an array, but how can i make it an iterable
     
  6. As I said, use Arrays.asList(...getOnlinePlayers());
     
  7. Required type:
    Iterable
    <? extends Player>
    Provided:
    List
    <Collection<capture of ? extends Player>>
    no instance(s) of type variable(s) exist so that Collection<capture of ? extends Player> conforms to Player
     
  8. Why don't you just use a forEach?
    Code (Java):
    Bukkit.getServer().getOnlinePlayers().forEach(player -> {
      applyBoard(player).addUpdates(players);
    })
     
    • Agree Agree x 1
  9. ok nvm solved, i had to cast it but it didnt give me the option to do it
     
  10. Don't just cast stuff...
    Bukkit.getServer().getOnlinePlayers() does actually not return an array but a collection. Casting won't solve your problem. If you are passing the Collection that it returns (an Array is not a collection), then you should be good and the error is at some other point
     
  11. How's that gonna solve it?
    the value of players is Bukkit.getServer().getOnlinePlayers()
     
  12. Bukkit.getServer().getOnlinePlayers() is a Collection<E>, which also implements Iterable<E> the update method wants an Iterable, so what's the problem? just pass in that Collection to the method?
     
  13. I think when using outdated mc versions you will get an Player[] instead. I can't remember when this was the case pre 1.9? pre 1.8?
     
  14. Im using 1.7.10 xD
     
  15. Could be the issue. You need to use an API that fits your outdated version of Minecraft. E.g using a 1.13+ or even a 1.8+ API does not fit your needs.