Solved Issue with Inventory

Discussion in 'Spigot Plugin Development' started by xSinclare, Mar 20, 2017.

Thread Status:
Not open for further replies.
  1. Hey, guys! So recently I started creating an inventory and this inventory is only part one of the whole aspects of the plugin I'm working. I have two aspects for this feature of my command. The class for the inventory and the class for the command. I have the command all sorted out, so I don't think that'll be an issue here. However, I am having issues with the inventory class. No errors seem to pop up in IntelliJ, but when the plugin is enabled at server startup that when issues start happening. First off here is the console error:

    Note: Some parts in the console message and the class below are omitted due to privacy reasons.

    Code (Text):
    [16:41:05 INFO]: [PLUGIN NAME] Enabling [PLUGIN NAME] v1.4.4
    [16:41:05 INFO]: Plugin enabled!
    [16:41:05 ERROR]: Error occurred while enabling [PLUGIN NAME] v1.4.4 (Is it up to date?)
    java.lang.ExceptionInInitializerError
            at [TLD].[DOMAIN].Core.onEnable(Core.java:34) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_45]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 9
            at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventoryCustom$MinecraftInventory.setItem(CraftInventoryCustom.java:105) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventory.setItem(CraftInventory.java:79) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at [TLD].[DOMAIN].Events.OresMenu.<clinit>(OresMenu.java:32) ~[?:?]
            ... 12 more

    Now here's the class for the inventory. No issues showed up in IntelliJ when I finished the class:
    Code (Text):
    package [TLD].[DOMAIN].Events;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.Wool;

    import java.util.ArrayList;
    import java.util.Arrays;

    /**
    * Created by [USER] on 3/18/17.
    */
    public class OresMenu implements Listener {

        //public OresMenu() {
            public static Inventory OreMenu = Bukkit.getServer().createInventory(null, 9, "Mined Ores");

            static {
                OreMenu.setItem(1, createItem(Material.QUARTZ_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(2, createItem(Material.COAL_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(3, createItem(Material.REDSTONE_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(4, createItem(Material.LAPIS_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(5, createItem(Material.STAINED_GLASS_PANE, ChatColor.GRAY + "{}"));
                OreMenu.setItem(6, createItem(Material.IRON_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(7, createItem(Material.GOLD_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(8, createItem(Material.DIAMOND_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(9, createItem(Material.EMERALD_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
            }
        //}

        private static ItemStack createItem(Material mat, String name) {
            ItemStack i = new Wool(mat).toItemStack(1);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList("Your mined ores."));
            i.setItemMeta(im);
            return i;
        }
    }
    The main thing that is grabbing my attention as well as confusing me is this part:
    Code (Text):
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 9
    Does this have to do with this?
    Code (Text):
    public static Inventory OreMenu = Bukkit.getServer().createInventory(null, 9, "Mined Ores");
    Not sure if I answered by own question, but any and all help would be appreciated!
     
  2. When setting the items, use the numbers 0-8 instead of 1-9. ie:
    Code (Text):

    static {
                OreMenu.setItem(0, createItem(Material.QUARTZ_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(1, createItem(Material.COAL_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(2, createItem(Material.REDSTONE_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(3, createItem(Material.LAPIS_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(4, createItem(Material.STAINED_GLASS_PANE, ChatColor.GRAY + "{}"));
                OreMenu.setItem(5, createItem(Material.IRON_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(6, createItem(Material.GOLD_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(7, createItem(Material.DIAMOND_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
                OreMenu.setItem(8, createItem(Material.EMERALD_ORE, ChatColor.GOLD + "Mined: " + ChatColor.GRAY + "{MINED: " + ChatColor.RED + "#" + ChatColor.GRAY + "}"));
            }
     
    Edit: What is with all the static?
     
  3. Static was just me messing around. The final version will be more fixed up haha. I'll edit this post or respond if I have fixed the issue.

    EDIT: Setting it as 0-8 worked :) What do you suggest I do with the static anyways?
     
  4. I believe the static block is fine, all it does is run the code inside once when the object is initialized... if that's what you intended.
     
  5. Ahh. Yeah, that's what I'm gonna stick with for now. Anyways I do appreciate the help. I shall be marking this thread as Solved and Locking it.
     
Thread Status:
Not open for further replies.

Share This Page