Getting all Players

Discussion in 'Spigot Plugin Development' started by Koekjeee, May 15, 2016.

  1. I have this code:
    Code (Text):

    package me.koekjeee.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.SkullType;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.SkullMeta;

    import me.koekjeee.admin.Main;

    public class PlayersCommand implements CommandExecutor {

       public PlayersCommand(Main main) {

       }

       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         if (!(sender instanceof Player)) {
           return true;
         }
         Player player = (Player) sender;

         if (cmd.getName().equalsIgnoreCase("players")) {
           for (World w : Main.getPlugin().getServer().getWorlds()) {
             for (Player p : w.getPlayers()) {
               ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal());
               SkullMeta meta = (SkullMeta) skull.getItemMeta();
               meta.setOwner(p.getName());
               meta.setDisplayName(ChatColor.LIGHT_PURPLE + p.getName());
               skull.setItemMeta(meta);

               Main.players().addItem(skull);
             }
           }
           player.openInventory(Main.players());
         }
         return false;
       }
    }
     
    Error:
    http://imgur.com/Onslrlt
     
  2. w.getPlayers() returns null.
    Rather just use Bukkit.getOnlinePlayers().
     
  3. Now he opens this inventory lol:
    http://imgur.com/Kr0vdrp

    This isn't good. He doens't show my head and its weird xD.

    Main:
    Code (Text):

    package me.koekjeee.admin;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.koekjeee.commands.PlayersCommand;

    public class Main extends JavaPlugin {
       
       private static Inventory players;

       private static Plugin plugin;
       private static Logger log;
       
       private int lengthInv;
       
       public void onEnable() {
         log = getServer().getLogger();
         plugin = getPlugin();
         registerCommands();
         
         players = Bukkit.createInventory(null, lengthInv, ChatColor.RED + "Online Players");
       
         if(Bukkit.getOnlinePlayers().size() < 9) {
           lengthInv = 9;
         } else {
           lengthInv = Bukkit.getOfflinePlayers().length;
         }
       }
       
       private void registerCommands() {
         getCommand("players").setExecutor(new PlayersCommand(this));
       }
       
       public static Logger getLog() {
         return log;
       }
       
       public static Plugin getPlugin() {
         return plugin;
       }
       
       public static Inventory players() {
         return players;
       }
    }