Solved Error when trying to get item from item manager

Discussion in 'Plugin Development' started by voltywolty, Dec 2, 2023.

Thread Status:
Not open for further replies.
  1. Offline


    I created an ItemStack loader (Item Manager) and returned the itemstack after setting the proper values from a config (as seen below):
    public class ItemManager {
        private static DvZ plugin;
        public static ItemStack generateItemStack (String code) {
            return generateItemStack(code, null);
        public static ItemStack generateItemStack(String code, Player player) {
            //ConfigurationSection cfg = YamlConfiguration.loadConfiguration(plugin.configManager.itemFile););
            ConfigurationSection cfg = plugin.getConfig().getConfigurationSection("items" + code.toLowerCase());
            if (cfg == null)
                return null;
            Map<String, String> data = new HashMap<>();
            String matStr = cfg.getString("material");
            if (matStr == null) {
                plugin.getLogger().warning(" An item is missing its material ( " + code + ")!");
                return null;
            Material material = Material.matchMaterial(matStr);
            int quantity = cfg.getInt("quantity", 1);
            //int flags = cfg.getInt("flags", 0);
            String display = cfg.getString("display");
            List<String> lore = cfg.getStringList("lore");
            ConfigurationSection enchants = plugin.getConfig().getConfigurationSection("enchants");
            ItemStack item = new ItemStack(material, quantity);
            ItemMeta itemMeta = item.getItemMeta();
            if (display != null)
            if (enchants != null) {
                for (String enchStr : enchants.getKeys(false)) {
                    try {
                        Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(enchStr));
                        int level = enchants.getInt(enchStr, 1);
                        itemMeta.addEnchant(enchantment, level, true);
                    catch (IllegalArgumentException ex) {
                        plugin.getLogger().info("Illegal enchant in item " + code + " (" + enchStr + ")!");
            if (lore.size() > 0)
            return item;
    However, after I try to run a command:
        public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
            if (command.getName().equalsIgnoreCase("test")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    ItemStack i = itemManager.generateItemStack(args[0], player);
            return true;
    I get an error from the error log I put below. Not too sure what could be causing the issue as I thought I did everything properly. I have my item.yml to grab the ItemStack which has the following:
        material: diamond
        quantity: 1
        display: 'Test Item'
          - This is a test
    Is it in the command itself or something to do with the ItemManager?

    [15:39:32] [Server thread/INFO]: AgentMonke_ issued server command: /test
    [15:39:32] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin DvZ v1.0-ALPHA
       at org.bukkit.command.PluginCommand.execute( ~[paper-api-1.20.2-R0.1-SNAPSHOT.jar:?]
       at org.bukkit.command.SimpleCommandMap.dispatch( ~[paper-api-1.20.2-R0.1-SNAPSHOT.jar:?]
       at org.bukkit.craftbukkit.v1_20_R2.CraftServer.dispatchCommand( ~[paper-1.20.2.jar:git-Paper-309]
       at ~[paper-1.20.2.jar:git-Paper-309]
       at com.mojang.brigadier.CommandDispatcher.execute( ~[paper-1.20.2.jar:?]
       at net.minecraft.commands.Commands.performCommand( ~[?:?]
       at net.minecraft.commands.Commands.performCommand( ~[?:?]
       at ~[?:?]
       at$handleChatCommand$19( ~[?:?]
       at net.minecraft.util.thread.BlockableEventLoop.lambda$submitAsync$0( ~[?:?]
       at java.util.concurrent.CompletableFuture$ ~[?:?]
       at ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.util.thread.BlockableEventLoop.doRunTask( ~[?:?]
       at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask( ~[?:?]
       at net.minecraft.server.MinecraftServer.doRunTask( ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.server.MinecraftServer.d( ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.util.thread.BlockableEventLoop.pollTask( ~[?:?]
       at net.minecraft.server.MinecraftServer.pollTaskInternal( ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.server.MinecraftServer.pollTask( ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.util.thread.BlockableEventLoop.managedBlock( ~[?:?]
       at net.minecraft.server.MinecraftServer.waitUntilNextTick( ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.server.MinecraftServer.runServer( ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.server.MinecraftServer.lambda$spin$0( ~[paper-1.20.2.jar:git-Paper-309]
       at ~[?:?]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
       at me.volt.main.dvz.DvZ.onCommand( ~[DvZ.jar:?]
       at org.bukkit.command.PluginCommand.execute( ~[paper-api-1.20.2-R0.1-SNAPSHOT.jar:?]
       ... 23 more
    [15:40:40] [Server thread/INFO]: AgentMonke_ lost connection: Disconnected
    [15:40:40] [Server thread/INFO]: AgentMonke_ left the game
    Last edited by a moderator: Dec 3, 2023
  2. Offline

    timtower Administrator Administrator Moderator

    @voltywolty It is your usage of args[0], you are never checking the length of the array
  3. Offline


    Yes I got it fixed, thank you.
Thread Status:
Not open for further replies.

Share This Page