Solved PrepareItemCraftEvent - Array out of bounds

Discussion in 'Spigot Plugin Development' started by gordanb740, Apr 12, 2021.

  1. I made a plugin that prevents people from crafting certain items using the PrepareItemCraftEvent, but the console randomly gave an error when a player was crafting something. I'm not sure what the player did exactly but this is the code that caused the error:

    Code (Java):
    @EventHandler
        public void onCraft(PrepareItemCraftEvent e) {
            for (HumanEntity entity : e.getViewers()) {
                if(entity instanceof Player) {
                 
                    if (e.getRecipe() == null) return;
                    CraftingInventory inventory = e.getInventory();
                    ItemStack[] items = inventory.getMatrix();
                 
                    for(int i = 0; i < 9; i++) {
                        if(items[i] != null && items[i].getType() == Material.GOLD_NUGGET && items[i].getItemMeta().getDisplayName().equals("Nugget")) {
                            e.getInventory().setResult(new ItemStack(Material.AIR));
                            return;
                        }
                    }
                }
            }
        }

    And this is the error that showed up:

    Code (Text):
    [Error] Could not pass event PrepareItemCraftEvent to TestPlugin v1.0
    java.lang.ArrayIndexOutOfBoundsException: 4
    at com.User7.TestPlugin.OnEntityHit.onCraft(OnEntityHit.java:220) ~[?:?]
    at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor73.execute(Unknown Source) ~[?:?]
    at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[patched_1.16.4.jar:git-Paper-260]
    at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[patched_1.16.4.jar:git-Paper-260]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[patched_1.16.4.jar:git-Paper-260]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:607) ~[patched_1.16.4.jar:git-Paper-260]
    at org.bukkit.craftbukkit.v1_16_R3.event.CraftEventFactory.callPreCraftEvent(CraftEventFactory.java:1259) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.ContainerWorkbench.a(ContainerWorkbench.java:70) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.ContainerPlayer.a(ContainerPlayer.java:106) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.InventoryCrafting.splitStack(InventoryCrafting.java:126) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.SlotResult.a(SourceFile:66) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.ContainerPlayer.shiftClick(ContainerPlayer.java:180) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.Container.b(Container.java:334) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.Container.a(Container.java:183) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:2532) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.PacketPlayInWindowClick.a(SourceFile:32) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.PacketPlayInWindowClick.a(SourceFile:10) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda$ensureMainThread$1(PlayerConnectionUtils.java:23) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:1135) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:1128) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.MinecraftServer.sleepForTick(MinecraftServer.java:1089) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1003) ~[patched_1.16.4.jar:git-Paper-260]
    at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:177) ~[patched_1.16.4.jar:git-Paper-260]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_211]
     
    #1 gordanb740, Apr 12, 2021
    Last edited: Apr 14, 2021
  2. What is in the line 220 of your OnEntityHit class? I think the problems starts there

    EDIT:
    I think they are crafting inside their inventory not the crafting bench, since the inventory craft only has 4 slot, your loop will break because you are trying to loop more than four, so instead just do

    for (ItemStack item : items ) {
    // stuff
    }

    This will only loop only the items present in the crafting slot.
     
    #2 Peepersoak, Apr 12, 2021
    Last edited: Apr 12, 2021
    • Winner Winner x 2
  3. Of course, you are so right! I just couldn't for the life of me understand why it was out of bounds, but I completely forgot about inventory crafting that only has 4 slots. This should definetely solve it. Thank you!
     
    • Friendly Friendly x 1
  4. I might be wrong but I don't think you need to loop all viewers and get their view, unless you're opening the inventory to more than one person that is. If that's not the case, just use the provided InventoryView by all inventory events through InventoryEvent#getView
     
    • Useful Useful x 1