Jump to content

[Unsolved] Array-related crash when opening container GUI


Qwertygiy

Recommended Posts

I have a block with a tile entity inventory, and after spending a couple hours fixing some rookie mistakes, I have run into an actual crash that does not mention any of my code anywhere.

 

Here's the direct crash trace:

 

 

 

2012-09-19 19:29:57 [iNFO] [sTDERR] java.lang.IndexOutOfBoundsException: Index: 45, Size: 45
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at java.util.ArrayList.rangeCheck(ArrayList.java:604)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at java.util.ArrayList.get(ArrayList.java:382)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at net.minecraft.src.Container.getSlot(Container.java:123)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at net.minecraft.src.Container.putStacksInSlots(Container.java:341)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at net.minecraft.src.NetClientHandler.handleWindowItems(NetClientHandler.java:985)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at net.minecraft.src.Packet104WindowItems.processPacket(Packet104WindowItems.java:69)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at net.minecraft.src.MemoryConnection.processReadPackets(MemoryConnection.java:75)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at net.minecraft.src.NetClientHandler.processReadPackets(NetClientHandler.java:103)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at net.minecraft.src.WorldClient.tick(WorldClient.java:66)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at net.minecraft.client.Minecraft.runTick(Minecraft.java:1777)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:834)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at net.minecraft.client.Minecraft.run(Minecraft.java:764)
2012-09-19 19:29:57 [iNFO] [sTDERR] 	at java.lang.Thread.run(Thread.java:722)

 

 

 

Now, the first three lines appear to be rather obvious; since 0 is inclusive, something is looking for the 46th item in a 45-item array.

 

The next line isn't too helpful, but the fifth is slightly enlightening. Here's the relevant functions in Container.java:

 

public Slot getSlot(int par1)
    {
        return (Slot)this.inventorySlots.get(par1); //line 123
    }

public void putStacksInSlots(ItemStack[] par1ArrayOfItemStack)
    {
        for (int var2 = 0; var2 < par1ArrayOfItemStack.length; ++var2)
        {
            this.getSlot(var2).putStack(par1ArrayOfItemStack[var2]); //line 341
        }
    }

 

So, to me, putStacksInSlots would appear to be the 'true culprit' of the stack trace. The array here apparently has more than 45 slots, while the Container does not, and so it crashes. I'm using the Recommended Forge, #251; is it a bug with the API or am I doing something incorrectly to set it off? I looked through the change log on Jenkins to make sure it wasn't a bug fixed in a recent release, and it wasn't mentioned.

 

Here's my Container class:

 

 

 

package KBI.agricrafture.core;

import net.minecraft.src.*;
import KBI.agricrafture.api.*;
import java.util.logging.Level;
import cpw.mods.fml.common.FMLLog;

public class ContainerCulinary extends Container 
{

        protected TileEntityCulinary tileEntity;
       
        public ContainerCulinary (InventoryPlayer inventoryPlayer, TileEntityCulinary te)
	{
			FMLLog.log(Level.FINE, "ContainerCulinary intitiated");
                tileEntity = te;

                //the Slot constructor takes the IInventory and the slot number in that it binds to
                //and the x-y coordinates it resides on-screen
			//0-5 are ingredients
                addSlotToContainer(new Slot(tileEntity, 0, 7, 15));
			addSlotToContainer(new Slot(tileEntity, 1, 25, 15));
			addSlotToContainer(new Slot(tileEntity, 2, 7, 33));
			addSlotToContainer(new Slot(tileEntity, 3, 25, 33));
			addSlotToContainer(new Slot(tileEntity, 4, 7, 51));
			addSlotToContainer(new Slot(tileEntity, 5, 25, 51));
			//tool slot, for knife, spoon, etc.
			addSlotToContainer(new Slot(tileEntity, 6, 60, 7));
			//dish slot, for plate, bowl, jar, etc.
			addSlotToContainer(new Slot(tileEntity, 7, 60, 61));
			//main output
			addSlotToContainer(new Slot(tileEntity, 8, 147, 35));
			//secondary output 1
			addSlotToContainer(new Slot(tileEntity, 9, 147, 7));
			//secondary output 2
			addSlotToContainer(new Slot(tileEntity, 10, 147, 61));

                //commonly used vanilla code that adds the player's inventory
                bindPlayerInventory(inventoryPlayer);
			FMLLog.log(Level.FINE, "ContainerCulinary initiation complete.");
        }

        @Override
        public boolean canInteractWith(EntityPlayer player) 
	{
                return tileEntity.isUseableByPlayer(player);
        }


        protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) 
	{
                for (int i = 0; i < 3; i++) 
			{
                        for (int j = 0; j < 9; j++) 
					{
                                addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
                        }
                }

                for (int i = 0; i < 9; i++) 
			{
                        addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
                }
        }

	@Override
        public ItemStack transferStackInSlot(int slot) 
	{
                ItemStack stack = null;
                Slot slotObject = (Slot) inventorySlots.get(slot);

                //null checks and checks if the item can be stacked (maxStackSize > 1)
                if (slotObject != null && slotObject.getHasStack()) 
			{
                        ItemStack stackInSlot = slotObject.getStack();
                        stack = stackInSlot.copy();

                        //merges the item into player inventory since its in the tileEntity
                        if (slot >= 0 && slot <= 10)
					{
                                if (!mergeItemStack(stackInSlot, 11, inventorySlots.size(), true)) 
							{
                                        return null;
                                }
                        //places it into the tileEntity is possible since its in the player inventory
                        } 
					else if (!mergeItemStack(stackInSlot, 0, 7, false)) 
					{
                                return null;
                        }

                        if (stackInSlot.stackSize == 0) 
					{
                                slotObject.putStack(null);
                        } 
					else {
                                slotObject.onSlotChanged();
                        }
                }

                return stack;
        }
}

 

 

 

Counting slots, there should be 47 of them -- 36 for the player inventory, and 11 for the tile entity.

 

Any ideas what's going on and how to fix it?

Link to comment
Share on other sites

Are you sending any packets for this container of yours? how are you sending them? I'm asking since right before the crash it is working with packets so I assume there might be from there the array which is out of bounds comes from?

 

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

I am not, so any packet handling is being done either by Forge or by vanilla code, but I may have to use packets in the future. I'm 99% sure of that, and my utter lack of knowledge on how to do so may somehow be involved with the crash, but I don't want to add 300 more lines of code that won't fix the crash.

Link to comment
Share on other sites

It's when I right-click the block to open the GUI.

 

Here's TileEntityCulinary.java: http://pastebin.com/h3Qm3LDY

 

Note: everything after line 174 is code involving CulinaryRecipe.java.

And yes, before it's a true API I need to remove that import core.* line.

try setting the size of the array directly private ItemStack[] inv = new ItemStack[11]; might not be the issue but could. Also add an if statement to getSlot too check if requrest slot is not over the length.

Link to comment
Share on other sites

Added this function to ContainerCulinary:

 

@Override
	public Slot getSlot(int par1)
	{
		if(par1 < this.inventorySlots.size())
		{
			return (Slot)this.inventorySlots.get(par1);
		}
		else
		{
			return (Slot)null;
		}
	}

 

and also defined inv in TileEntityCulinary immediately instead of in the constructor.

 

I get the same crash as before upon right-clicking the block.

Link to comment
Share on other sites

still reading threw the code but i noticed in you block class that on activate you don't check to make sure the world isn't remote. You need to do this so the server tells the client to open a GUI with the correct info.

if (par1World.isRemote)
        {
            return true;
        }
        else
        {//rest of your code here

 

Also why is your GUI id so high. If i'm not mistaken GUI ids are used only per mod meaning several mods can have GUI id 0 or 1.

Link to comment
Share on other sites

The high ID was an attempt at bug fixing mostly; because I think I found that the ID I was using (either 0 or 1, I forget) might have been the one used by the vanilla Crafting Table GUI, causing confusion in the code. As it still happened with the high ID, that must not be the case and I'll change it back.

 

I added the remote world check, no obvious difference in the result.

Link to comment
Share on other sites

Okay, I did some further work. This time, I basically just copied the vanilla Furnace class and modified it to have 2 burn slots and 4 recipe slots. Same problem as before -- upon right-clicking the block, something tries to access the 46th slot in a 45-slot Array. Since both Containers have more than 45 slots, I suspect I am missing some code somewhere.

Link to comment
Share on other sites

I've put my code up on GitHub at https://github.com/Qwertygiy/Agricrafture to make it easier to get help figuring out how to get this working. In addition to it really not liking containers with over 45 slots, my packets and crafting don't appear to be working properly. All in all it's a big mess and I've looked through other code to try to figure out how to fix it but so far am getting nowhere.

Link to comment
Share on other sites

You can take a look at a couple of the mods that I've written that have working GUI's if you want an idea of how to do it correctly. I'm at work at the moment so I don't have time to look over your code for any specific details or help. I'll try to get something more up here to help you out later. In the mean time, take a look at the mods below. The Charging Bench code shows off how to transfer variables from server -> client using the container and the Inventory Stocker has a full on GUI with custom packets implemented.

 

https://github.com/pantheis/ChargingBench and https://github.com/pantheis/InventoryStocker

 

 

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.

Announcements



×
×
  • Create New...

Important Information

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