The Meaning of Life, the Universe, and Everything.
Join Date:
11/11/2016
Posts:
45
Member Details
I have two containers for two tile entities. They're almost exactly the same thing except that one has 4 slots and the other as 8 slots. The one with 4 slots is working completely fine, however the one with 8 slots isn't working so well.
Opening the container gives an IndexOutOfBoundsException:
java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException: Index: 43, Size: 40
at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_111]
at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_111]
at net.minecraft.util.Util.runTask(Util.java:26) [Util.class:?]
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1108) [Minecraft.class:?]
at net.minecraft.client.Minecraft.run(Minecraft.java:406) [Minecraft.class:?]
at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]
at GradleStart.main(GradleStart.java:26) [start/:?]
Caused by: java.lang.IndexOutOfBoundsException: Index: 43, Size: 40
at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_111]
at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_111]
at net.minecraft.inventory.Container.getSlot(Container.java:127) ~[Container.class:?]
at net.minecraft.inventory.Container.putStackInSlot(Container.java:579) ~[Container.class:?]
at net.minecraft.client.network.NetHandlerPlayClient.handleSetSlot(NetHandlerPlayClient.java:1224) ~[NetHandlerPlayClient.class:?]
at net.minecraft.network.play.server.SPacketSetSlot.processPacket(SPacketSetSlot.java:34) ~[SPacketSetSlot.class:?]
at net.minecraft.network.play.server.SPacketSetSlot.processPacket(SPacketSetSlot.java:12) ~[SPacketSetSlot.class:?]
at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:15) ~[PacketThreadUtil$1.class:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_111]
at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_111]
at net.minecraft.util.Util.runTask(Util.java:25) ~[Util.class:?]
At first, it seems obvious what could be causing it: I'm trying to access slots that don't exist. But it seems that's not the case. I added a simple check after all the slots have been added to check for inventorySlots.size(), and it's saying that there are 44 slots, not 40. Yet when the container attempts to access those slots it seems to think inventorySlots.size is 40. Actually, it tells me there are 44 slots and then the error pops up that tells me there are 40, so there's clearly some weird conflict happening here.
Here's the code where I add in the slots
for(int row = 0; row < 2; row++)
for(int col = 0; col < 4; col++)
addSlotToContainer(new Slot(inv, col + row * 4, 53 + col * 18, 49 - row * 18));
//player inv
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 9; ++j)
addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
for (int k = 0; k < 9; ++k)
this.addSlotToContainer(new Slot(playerInv, k, 8 + k * 18, 142));
System.out.println(this.inventorySlots.size());
The Meaning of Life, the Universe, and Everything.
Join Date:
11/11/2016
Posts:
45
Member Details
I fixed the problem, though I'm not entirely sure how. The inventory array is initialized in the constructor in the tile entity (which implements IInventory)
The Meaning of Life, the Universe, and Everything.
Join Date:
11/11/2016
Posts:
45
Member Details
The array has a size of 8, because my container has 8 slots. Minecraft does inventories where it adds up all the IInventories used in a container through the function in the inventory interface that gets the size of the array.
You can find an example in ContainerFurnace. It assigns slot ids starting from 0 for each inventory (furnace and player), but in the function for handling shift-clicking, it uses 0 to the total inventory size.
I have two containers for two tile entities. They're almost exactly the same thing except that one has 4 slots and the other as 8 slots. The one with 4 slots is working completely fine, however the one with 8 slots isn't working so well.
Opening the container gives an IndexOutOfBoundsException:
At first, it seems obvious what could be causing it: I'm trying to access slots that don't exist. But it seems that's not the case. I added a simple check after all the slots have been added to check for inventorySlots.size(), and it's saying that there are 44 slots, not 40. Yet when the container attempts to access those slots it seems to think inventorySlots.size is 40. Actually, it tells me there are 44 slots and then the error pops up that tells me there are 40, so there's clearly some weird conflict happening here.
Here's the code where I add in the slots
I'll be happy to provide more code if needed.
Instead of assigning indices like this -
addSlotToContainer(new Slot(inv, col + row * 4, 53 + col * 18, 49 - row * 18));
Make a separate integer for assigning indices:
Hm, yeah that makes it easier to understand. It still doesn't fix the error, though...I still have no clue what's causing it
could you show where you initialize your inventory array?
I fixed the problem, though I'm not entirely sure how. The inventory array is initialized in the constructor in the tile entity (which implements IInventory)
I think you might have initialized your inventory array with a size 0f 40, which is too small....
The array has a size of 8, because my container has 8 slots. Minecraft does inventories where it adds up all the IInventories used in a container through the function in the inventory interface that gets the size of the array.
You can find an example in ContainerFurnace. It assigns slot ids starting from 0 for each inventory (furnace and player), but in the function for handling shift-clicking, it uses 0 to the total inventory size.