Player Skulls

Discussion in 'Spigot Plugin Development' started by _Zaboomafoo_, Jun 18, 2015.

  1. Hi below I have my code where I am creating an array of players and adding their skulls to an inventory GUI but I really would like to know what is wrong with it. Thanks.
    Code (Text):
        @SuppressWarnings("deprecation")
        public static void openList(Player p)
          {
            int onlinePlayers = Bukkit.getServer().getOnlinePlayers().length;
            int counter = 0;
            Inventory i = Bukkit.getServer().createInventory(p, rows, ChatColor.DARK_PURPLE + "Total Online Players = " + onlinePlayers);
            Player[] arrayOfPlayers;
            int j = (arrayOfPlayers = Bukkit.getOnlinePlayers()).length;
            for (int o = 0; o < j; o++)
            {
              Player ps = arrayOfPlayers[o];
              String name = ps.getName();
              ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1);
              SkullMeta meta = (SkullMeta) skull.getItemMeta();
              meta.setOwner(ChatColor.GREEN + name);
              meta.setDisplayName(ChatColor.WHITE + name);
              skull.setItemMeta(meta);
              i.setItem(counter, skull);
              counter++;
            }
            p.openInventory(i);
          }
     
    The error I am having can be found here:
    http://hastebin.com/jigelofodi.sm
     
  2. Code (Text):
    Player ps = arrayOfPlayers[o];
    I'm assuming you mean 0 and not o

    (You have the letter O instead of the number 0)
     
  3. sothatsit

    Patron

    o is his index variable.
     
  4. I knew that :oops:


    Also shouldn't the skull itemstack have the durability of 3? That way it's a player skull.
     
  5. Code (Text):
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at org.bukkit.craftbukkit.v1_7_R4.inventory.CraftInventoryCustom$MinecraftInventory.setItem(CraftInventoryCustom.java:103) ~[spigot-1.7.10.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at org.bukkit.craftbukkit.v1_7_R4.inventory.CraftInventory.setItem(CraftInventory.java:82) ~[spigot-1.7.10.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at me.zaboomafoo.online.main.openList(main.java:57) ~[?:?]
        at me.zaboomafoo.online.main.onCommand(main.java:26) ~[?:?]
    Whats in your main class on line 57?
     
  6. On line 57 I have the line i.setItem(counter, skull); found in the threads first post.
     
  7. Don't use ChatColor.GREEN with the owner. Data value of 3 is applied automatically.
    Also arrayOfPlayers should actually be filled. :)
     
  8. sothatsit

    Patron

    Your code is all over the shop.

    Where do you set the value of row? The error you are getting is because the size of the inventory is 0.
    Code (Text):
    ...createInventory(p, rows, ChatColor.DARK_PURPLE...
    The owner cannot have any chat colors, it has to be just the players name.
    Code (Text):
    meta.setOwner(ChatColor.GREEN + name);
    This is what you should be doing:

    Create a Player[] array at the start and set it to the online players.
    Code (Text):
    Player[] players = Bukkit.getOnlinePlayers();
    Create your inventory
    Code (Text):
    Inventory inv = Bukkit.getServer().createInventory(p, (int) Math.ceil(players.length / 9d), ChatColor.DARK_PURPLE + "Total Online Players = " + onlinePlayers);
    Loop through players
    Code (Text):
    for(int i=0; i < players.length; i++) {
       Player player = players[i];
    }
    Create ItemStack
    Code (Text):
    ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
    Set the owner and the display name
    Code (Text):
    SkullMeta meta = (SkullMeta) item.getItemMeta();
    meta.setOwner(player.getName());
    meta.setDisplayName(player.getName());
    item.setItemMeta(meta);
    Set the item in the inventory
    Code (Text):
    inv.setItem(i, item);
    Open the inventory
    Code (Text):
    p.openInventory(inv);
     
  9. This is an the letter "o".
     
  10. Just out of curiosity, would casting a collection of players to an array of players directly mess with the indexes of the entries?
     
  11. Try printing out your rows variable, i'm wondering if maybe it's 0, so when you go to set the item in the inventory it can't be set
     
  12. sothatsit

    Patron

    He does set it.
    Code (Text):
    Player[] arrayOfPlayers;
            int j = (arrayOfPlayers = Bukkit.getOnlinePlayers()).length;
    There is no reason for him to do it this way, but that does work.
     
  13. sothatsit

    Patron

    If you had read the code, o is his index variable.

    Bukkit.getOnlinePlayers() returns a Player[] array, not a Collection.

    This is what is causing his error.
     
  14. OK I see what you mean, so how could I make the inventory bigger if more players were to get on?
     
  15. PHP:
    for(Player player : Bukkit.getOnlinePlayers()) { }
    Also
    Re-define the inventory
    PHP:
    inv = Bukkit.createInventory(.....
     
  16. sothatsit

    Patron

    Code (Text):
    int size = (int) Math.ceil(Bukkit.getOnlinePlayers().length / 9d);
    This will make the inventory as big as it needs to be to hold all the players online.

    Just pass size as the size parameter in createInventory.

    He needs the index variable for setting the item in the inventory. This does not allow that.
     
  17. OK all I figured it out and it works! Thanks for your help.
     
  18. OK I tried this but it doesn't work, and yes I passed the size parameter in CreateInventory. Any other ideas?
     
  19. PHP:
          Player[] arrayOfPlayers = Bukkit.getServer().getOnlinePlayers();
            for (int o = 0; o < arrayOfPlayers.length(); o++)
            {
              Player ps = arrayOfPlayers[o];
              String name = ps.getName();
              ItemStack skull = new ItemStack(Material.SKULL_ITEM, (short)3);
              SkullMeta meta = (SkullMeta) skull.getItemMeta();
              meta.setOwner(name);
              meta.setDisplayName(ChatColor.WHITE + name);
              skull.setItemMeta(meta);
              i.setItem(o, skull);
            }
           
     
  20. Code and what doesn't work?

    EDIT: I believe the player head has a data of 3 and the owner should be set to the player only, no colors (display name doesn't matter)