Solved Error when trying to get item from item manager

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

  1. Offline

    voltywolty

    I created an ItemStack loader (Item Manager) and returned the itemstack after setting the proper values from a config (as seen below):
    Code:
    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)
                itemMeta.setDisplayName(display);
            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 + ")!");
                        continue;
                    }
                }
            }
    
            if (lore.size() > 0)
                itemMeta.setLore(lore);
    
            item.setItemMeta(itemMeta);
            return item;
        }
    }
    However, after I try to run a command:
    Code:
    @Override
        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);
                    player.getInventory().addItem(i);
                }
            }
    
            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:
    Code:
    items:
      test:
        material: diamond
        quantity: 1
        display: 'Test Item'
        lore:
          - This is a test
    Is it in the command itself or something to do with the ItemManager?

    Code:
    [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(PluginCommand.java:47) ~[paper-api-1.20.2-R0.1-SNAPSHOT.jar:?]
       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:155) ~[paper-api-1.20.2-R0.1-SNAPSHOT.jar:?]
       at org.bukkit.craftbukkit.v1_20_R2.CraftServer.dispatchCommand(CraftServer.java:991) ~[paper-1.20.2.jar:git-Paper-309]
       at org.bukkit.craftbukkit.v1_20_R2.command.BukkitCommandWrapper.run(BukkitCommandWrapper.java:64) ~[paper-1.20.2.jar:git-Paper-309]
       at com.mojang.brigadier.CommandDispatcher.execute(CommandDispatcher.java:265) ~[paper-1.20.2.jar:?]
       at net.minecraft.commands.Commands.performCommand(Commands.java:330) ~[?:?]
       at net.minecraft.commands.Commands.performCommand(Commands.java:314) ~[?:?]
       at net.minecraft.server.network.ServerGamePacketListenerImpl.performChatCommand(ServerGamePacketListenerImpl.java:2208) ~[?:?]
       at net.minecraft.server.network.ServerGamePacketListenerImpl.lambda$handleChatCommand$19(ServerGamePacketListenerImpl.java:2168) ~[?:?]
       at net.minecraft.util.thread.BlockableEventLoop.lambda$submitAsync$0(BlockableEventLoop.java:59) ~[?:?]
       at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768) ~[?:?]
       at net.minecraft.server.TickTask.run(TickTask.java:18) ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:153) ~[?:?]
       at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:24) ~[?:?]
       at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:1324) ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:193) ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:126) ~[?:?]
       at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:1301) ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:1294) ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:136) ~[?:?]
       at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:1272) ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1160) ~[paper-1.20.2.jar:git-Paper-309]
       at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:315) ~[paper-1.20.2.jar:git-Paper-309]
       at java.lang.Thread.run(Thread.java:833) ~[?:?]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
       at me.volt.main.dvz.DvZ.onCommand(DvZ.java:56) ~[DvZ.jar:?]
       at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[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

    voltywolty

    Yes I got it fixed, thank you.
     

Share This Page