[Solved ]Error in " Player[] players = Bukkit.getServer().getOnlinePlayers(); "

Discussion in 'Spigot Plugin Development' started by CrystalDev, May 13, 2015.

  1. Hi, I have an error in: Player[] players = Bukkit.getServer().getOnlinePlayers();

    Code:

    Code (Text):

      @EventHandler(priority=EventPriority.HIGH)
      public void onOnline(InventoryClickEvent event)
      {
        Player [] players = Bukkit.getServer().getOnlinePlayers();
        Player player = (Player)event.getWhoClicked();
        if ((event.getCurrentItem() != null) && (event.getCurrentItem().getType() != Material.AIR) &&
          (event.getInventory().getName().equals("§aCurrent Players §r[" + "§a" + players.length + "§r]")))
        {
          event.setCancelled(true);
          player.updateInventory();
        }
      }
     
     
  2. Can you post your error?
     
  3. Player[ ] is an Array, Bukkit.getOnlinePlayers() is a Collection<? extends Player>
     
  4. How could fix it?
     
  5. Try this (Not tested):
    Code (Text):
    Player[] p = Bukkit.getOnlinePlayers().toArray(new Player[ Bukkit.getOnlinePlayers().size()]);
     
    • Winner Winner x 1
    • Informative Informative x 1
  6. You are the best. Work :)
     
  7. just set the type to Collection<? extends Player>, much faster.