Sorry for poor English, I'm not a native English speaker.
I'm using the bukkit API to create a customized anvil inventory, then display it to player, with code like this:
plugin.getServer().createInventory(null, InventoryType.ANVIL);
player.openInventory(anvil);
The API worked and displayed a customized anvil inventory to player. But when I clicked on any slot of the displayed inventory, an ArrayIndexOutOfBoundException was thrown, like
java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[?:?] at java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[?:?] at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:46) [spigot-1.9.2.jar:git-Spigot-e6f93f4-d884ab3] at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [spigot-1.9.2.jar:git-Spigot-e6f93f4-d884ab3] at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.9.2.jar:git-Spigot-e6f93f4-d884ab3] at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot-1.9.2.jar:git-Spigot-e6f93f4-d884ab3] at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot-1.9.2.jar:git-Spigot-e6f93f4-d884ab3] at java.lang.Thread.run(Thread.java:746) [?:?] Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:659) ~[?:?] at java.util.ArrayList.get(ArrayList.java:432) ~[?:?] at net.minecraft.server.v1_9_R1.Container.getSlot(Container.java:109) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-d884ab3] at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1678) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-d884ab3] at net.minecraft.server.v1_9_R1.PacketPlayInWindowClick.a(SourceFile:33) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-d884ab3] at net.minecraft.server.v1_9_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-d884ab3] at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-d884ab3] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[?:?] at java.util.concurrent.FutureTask.run(FutureTask.java:265) ~[?:?] at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-d884ab3] ... 5 more
When set the inventory holder to player, the exception still be thrown:
plugin.getServer().createInventory(player, InventoryType.ANVIL); player.openInventory(anvil);
Here is some of my analyze, though naive but I think will help solving the problem.
I followed the stack trace and found exception handling a PlayInWindowClick packet.
And it was thrown while calling the getSlot method of Container.
The getSlot method invokes get method of field c:java.util.ArrayList.
So I think the inventory info has not yet been imported into the Container properly.
I predicts that either bugs in CraftBukkit or NMS-Patches will cause this bug.
Having read the code of inventories in CraftBukkit, I use reflecting on the inventory I created, in order to check whether there's defect in CraftBukkit.
I first reflect on the field inventory on CraftInventory, retrieving an IInventory object.
The IInventory object is instance of CraftInventoryCustom.MinecraftInventory.
Then I reflect on the array length of items, which is field of the retrieved CraftInventoryCustom.MinecraftInventory object.
The array length is 3, which indicts that defect may be not in CraftBukkit code.
Then I read the code in Container.patch of NMS-Patches, and decompile the patched code. It seems that Container only set the c field in method Container.a, which I think its likely to be a defect in patch for method Container.a.