Jump to content

Adding a new slot to container returns an IndexOutOfBounds exception


Insane96MCP

Recommended Posts

I'm trying to copy the anvil container. But as soon as I try to make 

this.addSlotToContainer(new Slot(...));

and I open the GUI, I get a java.lang.IndexOutOfBoundsException: Index: 39, Size: 39:

[11:27:18] [Client thread/FATAL]: Error executing task
java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException: Index: 39, Size: 39
	at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_141]
	at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_141]
	at net.minecraft.util.Util.runTask(Util.java:30) [Util.class:?]
	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1109) [Minecraft.class:?]
	at net.minecraft.client.Minecraft.run(Minecraft.java:407) [Minecraft.class:?]
	at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_141]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_141]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_141]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_141]
	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_141]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_141]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_141]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_141]
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]
	at GradleStart.main(GradleStart.java:26) [start/:?]
Caused by: java.lang.IndexOutOfBoundsException: Index: 39, Size: 39
	at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_141]
	at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_141]
	at net.minecraft.inventory.Container.getSlot(Container.java:135) ~[Container.class:?]
	at net.minecraft.inventory.Container.setAll(Container.java:554) ~[Container.class:?]
	at net.minecraft.client.network.NetHandlerPlayClient.handleWindowItems(NetHandlerPlayClient.java:1306) ~[NetHandlerPlayClient.class:?]
	at net.minecraft.network.play.server.SPacketWindowItems.processPacket(SPacketWindowItems.java:72) ~[SPacketWindowItems.class:?]
	at net.minecraft.network.play.server.SPacketWindowItems.processPacket(SPacketWindowItems.java:13) ~[SPacketWindowItems.class:?]
	at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:21) ~[PacketThreadUtil$1.class:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_141]
	at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_141]
	at net.minecraft.util.Util.runTask(Util.java:29) ~[Util.class:?]
	... 15 more

 

Here's the code

Another problem is that seems like the server is not getting input properly so I can't output nothing from the anvil. But I think that fixing the excpetion will solve this too.

 

 

Edited by Insane96MCP
Link to comment
Share on other sites

When you stepped into and through

this.addSlotToContainer(new Slot(...));

  in the debugger, did you see the array being extended?

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

11 hours ago, jeffryfisher said:

When you stepped into and through


this.addSlotToContainer(new Slot(...));

  in the debugger, did you see the array being extended?

Nope, no exception is given going through addSlotToContainer in the debugger It happens later

Edited by Insane96MCP
Link to comment
Share on other sites

Well then, if the array isn't getting bigger, but you try to access more elements than were allocated, then you're going to run off the end. Fix it.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

You're using Containers with a different number of Slots on the client and server. The client uses ContainerRepair; but the server uses GuiServer, which extends ContainerRepair and adds an extra Slot. The client-side Container doesn't have a Slot at this index, so the update packets throw an IndexOutOfBoundsException.

 

You need to use Containers with the same number of slots on both sides, usually instances of the same class. GuiRepair doesn't have a constructor that lets you provide your own Container, so you'll probably need to copy rather than extend it.

 

If you extend a class, you don't need to copy all of its fields and methods; you only need to override the methods that should have different behaviour. To access the private IInventory fields in ContainerRepair, either use reflection or get the corresponding Slots from Container#inventorySlots and get the inventory from the Slot#inventory field.

 

GuiServer isn't a good name, since it's not a GUI and the server doesn't have GUIs. I recommend following the MCP naming conventions and naming it something like ContainerGoldenAnvil.

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.