Jump to content

[SOLVED] IndexOutOfBoundsException: Index: 45, Size: 45 when opening container


xandayn

Recommended Posts

Hey, I've been stuck on a bug for a few days here and was wondering if anyone had any ideas.

 

EDIT: I've made a Git for my project, if that's easier to see https://github.com/xandayn/StorageCraft

 

EDIT2: I ended up just rewriting all the code for the diamond chest and now it works, never found out the exact issue of the bug though.

 

EDIT3: So looking through my old code and comparing to my new code I realized my mistake, in the GuiHandler class I'm checking for a TileEntityGoldChest where I should be checking for a TileEntityDiamondChest, just thought I'd make that note incase anyone was having a similar issue.

 

Crash Report

Exception in world tick

java.lang.IndexOutOfBoundsException: Index: 45, Size: 45
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at net.minecraft.inventory.Container.getSlot(Container.java:133)
at net.minecraft.inventory.Container.putStacksInSlots(Container.java:562)
at net.minecraft.client.multiplayer.NetClientHandler.handleWindowItems(NetClientHandler.java:1284)
at net.minecraft.network.packet.Packet104WindowItems.processPacket(Packet104WindowItems.java:67)
at net.minecraft.network.MemoryConnection.processReadPackets(MemoryConnection.java:89)
at net.minecraft.client.multiplayer.NetClientHandler.processReadPackets(NetClientHandler.java:281)
at net.minecraft.client.multiplayer.WorldClient.tick(WorldClient.java:99)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:1930)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:910)
at net.minecraft.client.Minecraft.run(Minecraft.java:838)
at net.minecraft.client.main.Main.main(Main.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.minecraft.launchwrapper.Launch.launch(Launch.java:131)
at net.minecraft.launchwrapper.Launch.main(Launch.java:27)

 

 

My block code:

package xan.storagecraft.block;

import static net.minecraftforge.common.ForgeDirection.DOWN;

import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import xan.storagecraft.StorageCraft;
import xan.storagecraft.tileentity.TileEntityDiamondChest;
import xan.storagecraft.tileentity.TileEntityGoldChest;
import xan.storagecraft.tileentity.TileEntityIronChest;
import xan.storagecraft.tileentity.TileEntitySC;
import cpw.mods.fml.common.network.FMLNetworkHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class BlockChestMulti extends Block{


public BlockChestMulti(int par1) {
	super(par1, Material.iron);
	this.setCreativeTab(CreativeTabs.tabDecorations);
	this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
}

@Override
public boolean isOpaqueCube(){
	return false;
}

@Override
public boolean renderAsNormalBlock(){
	return false;
}

@Override
public int getRenderType()
{
	return -1;
}

@Override
public boolean hasTileEntity(int meta) {
	return true;
}

@Override
public TileEntity createTileEntity(World world, int metadata) {
	switch(metadata % 4){
	case 0:
		return new TileEntityIronChest();
	case 1:
		return new TileEntityGoldChest();
	case 2:
		return new TileEntityDiamondChest();
	default:
		return new TileEntitySC();
	}

}

@Override
public int damageDropped(int par1) {
	return par1 % 4;
}

@SideOnly(Side.CLIENT)
private Icon[] icons;

@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister iconReg){
	icons = new Icon[4];
	icons[0] = iconReg.registerIcon("iron_block");
	icons[1] = iconReg.registerIcon("gold_block");
	icons[2] = iconReg.registerIcon("diamond_block");
	icons[3] = iconReg.registerIcon("quartz_block_top");
}

@Override
@SideOnly(Side.CLIENT)
public Icon getIcon(int par1, int meta){
	return icons[meta % 4];
}

@Override
@SideOnly(Side.CLIENT)
public void getSubBlocks(int par1, CreativeTabs tab, List list){
	for(int i = 0; i < 4; i++){
		list.add(new ItemStack(par1, 1, i));
	}
}

 /**
     * Called upon block activation (right click on the block.)
     */
@Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
    {
	if(!world.isRemote){
		if(world.getBlockMetadata(x, y, z) % 4 == 0 && !world.isBlockSolidOnSide(x, y + 1, z, DOWN))
			FMLNetworkHandler.openGui(player, StorageCraft.instance, 0, world, x, y, z);
		else if(world.getBlockMetadata(x, y, z) % 4 == 1 && !world.isBlockSolidOnSide(x, y + 1, z, DOWN))
			FMLNetworkHandler.openGui(player, StorageCraft.instance, 1, world, x, y, z);
		else if(world.getBlockMetadata(x, y, z) % 4 == 2 && !world.isBlockSolidOnSide(x, y + 1, z, DOWN))
			FMLNetworkHandler.openGui(player, StorageCraft.instance, 2, world, x, y, z);
	}

	return true;
    }

@Override
public void breakBlock(World world, int x, int y, int z, int id, int meta) {

	TileEntity te = world.getBlockTileEntity(x, y, z);

	if(te != null && te instanceof IInventory){
		IInventory inventory = (IInventory)te;

		for(int i = 0; i < inventory.getSizeInventory(); i++){
			ItemStack stack = inventory.getStackInSlotOnClosing(i);

			if(stack != null){
				float spawnX = x + world.rand.nextFloat();
				float spawnY = y + world.rand.nextFloat();
				float spawnZ = z + world.rand.nextFloat();

				EntityItem droppedItem = new EntityItem(world, spawnX, spawnY, spawnZ, stack);

				float mult = 0.05f;

				droppedItem.motionX = (-0.5 + world.rand.nextFloat()) * mult;
				droppedItem.motionY = (4 + world.rand.nextFloat()) * mult;
				droppedItem.motionZ = (-0.5 + world.rand.nextFloat()) * mult;

				world.spawnEntityInWorld(droppedItem);
			}
		}
	}

	super.breakBlock(world, x, y, z, id, meta);
}

@Override
public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLivingBase par5EntityLivingBase, ItemStack par6ItemStack) {

	super.onBlockPlacedBy(par1World, par2, par3, par4, par5EntityLivingBase, par6ItemStack);

        int rotate = ((MathHelper.floor_double((double)(par5EntityLivingBase.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3) + 2) % 4;
	par1World.setBlockMetadataWithNotify(par2, par3, par4, (rotate * 4) + par1World.getBlockMetadata(par2, par3, par4), 3);
}
}

 

My Container class

package xan.storagecraft.client.interfaces.container;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import xan.storagecraft.tileentity.TileEntityDiamondChest;
import xan.storagecraft.tileentity.TileEntityGoldChest;
import xan.storagecraft.tileentity.TileEntityIronChest;
import xan.storagecraft.tileentity.TileEntitySC;

public class ContainerSC extends Container{

private TileEntitySC chest;
private InventoryPlayer invPlayer;

public ContainerSC(InventoryPlayer invPlayer, TileEntitySC chest){
	this.chest = chest;
	this.invPlayer = invPlayer;
	//chest.openChest();
	if(chest instanceof TileEntityIronChest){
		for (int x = 0; x < 9; x++){
			addSlotToContainer(new Slot(invPlayer, x, 8 + 18 * x, 198));
		}

		for (int y = 0; y < 3; y++){
			for (int x = 0; x < 9; x++){
				addSlotToContainer(new Slot(invPlayer, x + y * 9 + 9, 8 + 18 * x, 140 + y * 18));
			}
		}
		for (int y = 0; y < 6; y++){
			for(int x = 0; x < 9; x++){
				addSlotToContainer(new Slot(chest, x + (y * 9), 8 + 18 * x, 18 + y * 18));
			}
		}
	}
	else if(chest instanceof TileEntityGoldChest){
		for (int x = 0; x < 9; x++){
			addSlotToContainer(new Slot(invPlayer, x, 35 + 18 * x, 198));
		}

		for (int y = 0; y < 3; y++){
			for (int x = 0; x < 9; x++){
				addSlotToContainer(new Slot(invPlayer, x + y * 9 + 9, 35 + 18 * x, 140 + y * 18));
			}
		}
		for (int y = 0; y < 6; y++){
			for(int x = 0; x < 12; x++){
				addSlotToContainer(new Slot(chest, x + (y * 12), 8 + 18 * x, 18 + y * 18));
			}
		}
	}
	else if(chest instanceof TileEntityDiamondChest){
		for (int y = 0; y < 9; y++){
			for(int x = 0; x < 12; x++){
				addSlotToContainer(new Slot(chest, x + (y * 12), 8 + 18 * x, 18 + y * 18));
			}
		}
	}
	chest.openChest();
}

@Override
public boolean canInteractWith(EntityPlayer entityplayer) {
	return chest.isUseableByPlayer(entityplayer);
}

@Override
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) {
	return null;
}

public IInventory getInv(){
	return (IInventory)chest;
}

@Override
public void onContainerClosed(EntityPlayer par1EntityPlayer) {
	super.onContainerClosed(par1EntityPlayer);
	chest.closeChest();
}

}

 

My TileEntities

package xan.storagecraft.tileentity;

import java.util.Iterator;
import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import xan.storagecraft.block.BlockChestMulti;
import xan.storagecraft.client.interfaces.container.ContainerSC;

public class TileEntitySC extends TileEntity implements IInventory{

public float prevLidAngle;
public float lidAngle;
    public int numUsingPlayers;

    private int ticksSinceSync;
    private int cachedChestType;

    public ItemStack[] items;
    
@Override
public int getSizeInventory() {
	return items.length;
}

@Override
public ItemStack getStackInSlot(int i) {
	if(i <= items.length)
		return items[i];
	else
		return null;
}

@Override
public ItemStack decrStackSize(int i, int count) {
	ItemStack itemstack = getStackInSlot(i);

	if(itemstack != null){
		if(itemstack.stackSize <= count){
			setInventorySlotContents(i, null);
		}else{
			itemstack = itemstack.splitStack(count);
			onInventoryChanged();
		}
	}

	return itemstack;
}

@Override
public ItemStack getStackInSlotOnClosing(int i) {
	ItemStack item = getStackInSlot(i);
	setInventorySlotContents(i, null);
	return item;
}

@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
	items[i] = itemstack;

	if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
		itemstack.stackSize = getInventoryStackLimit();
	}

	onInventoryChanged();
}

@Override
public String getInvName() {
	return "InventoryIronChest";
}

@Override
public boolean isInvNameLocalized() {
	return false;
}

@Override
public int getInventoryStackLimit() {
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
	return entityplayer.getDistanceSq(xCoord + 0.5f, yCoord + 0.5f, zCoord + 0.5f) <= 64;
}

@Override
    public boolean receiveClientEvent(int par1, int par2)
    {
        if (par1 == 1)
        {
            this.numUsingPlayers = par2;
            return true;
        }
        else
        {
            return super.receiveClientEvent(par1, par2);
        }
    }

@Override
public void openChest() {
        if (this.numUsingPlayers < 0)
        {
            this.numUsingPlayers = 0;
        }

        ++this.numUsingPlayers;
        this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType().blockID, 1, this.numUsingPlayers);
        this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType().blockID);
        this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType().blockID);
}

@Override
public void closeChest() {
        if (this.getBlockType() != null && this.getBlockType() instanceof BlockChestMulti)
        {
            --this.numUsingPlayers;
            this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType().blockID, 1, this.numUsingPlayers);
            this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType().blockID);
            this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType().blockID);
        }
}

@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	return true;
}

   
@Override
    public void updateEntity()
    {
        super.updateEntity();
        ++this.ticksSinceSync;
        float f;
        
        if (!this.worldObj.isRemote && this.numUsingPlayers != 0 && (this.ticksSinceSync + this.xCoord + this.yCoord + this.zCoord) % 200 == 0)
        {
            this.numUsingPlayers = 0;
            f = 5.0F;
            List list = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getAABBPool().getAABB((double)((float)this.xCoord - f), (double)((float)this.yCoord - f), (double)((float)this.zCoord - f), (double)((float)(this.xCoord + 1) + f), (double)((float)(this.yCoord + 1) + f), (double)((float)(this.zCoord + 1) + f)));
            Iterator iterator = list.iterator();

            while (iterator.hasNext())
            {
                EntityPlayer entityplayer = (EntityPlayer)iterator.next();
                if (entityplayer.openContainer instanceof ContainerSC)
                {
                    IInventory iinventory = ((ContainerSC)entityplayer.openContainer).getInv();

                    if (iinventory == this)
                    {
                        ++this.numUsingPlayers;
                    }
                }
            }
        }

        this.prevLidAngle = this.lidAngle;
        f = 0.1F;
        double d0;

        if (this.numUsingPlayers > 0 && this.lidAngle == 0.0F)
        {
            this.worldObj.playSoundEffect((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D, "random.chestopen", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
        }

        if ((this.numUsingPlayers == 0 && this.lidAngle > 0.0F) || (this.numUsingPlayers > 0 && this.lidAngle < 1.0F))
        {
            float f1 = this.lidAngle;

            if (this.numUsingPlayers > 0)
            {
                this.lidAngle += f;
            }
            else
            {
                this.lidAngle -= f;
            }

            if (this.lidAngle > 1.0F)
            {
                this.lidAngle = 1.0F;
            }

            float f2 = 0.5F;

            if (this.lidAngle < f2)
            {
                this.worldObj.playSoundEffect((double)this.xCoord + 0.05D, (double)this.yCoord + 0.5D, (double)this.zCoord, "random.chestclosed", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
            }

            if (this.lidAngle < 0.0F)
            {
                this.lidAngle = 0.0F;
            }
        }
    }

@Override
public void writeToNBT(NBTTagCompound compound) {
	super.writeToNBT(compound);

	NBTTagList items = new NBTTagList();

	for (int i = 0; i < getSizeInventory(); i++){
		ItemStack stack = getStackInSlot(i);

		if (stack != null){
			NBTTagCompound item = new NBTTagCompound();

			item.setByte("Slot", (byte)i);
			stack.writeToNBT(item);
			items.appendTag(item);
		}
	}
	compound.setTag("Items", items);
}

@Override
public void readFromNBT(NBTTagCompound compound) {
	super.readFromNBT(compound);
	NBTTagList items = compound.getTagList("Items");

	for (int i = 0; i < items.tagCount(); i++){
		NBTTagCompound item = (NBTTagCompound)items.tagAt(i);
		int slot = item.getByte("Slot");

		if(slot >= 0 && slot < getSizeInventory()){
			setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
		}

	}
}

}

 

package xan.storagecraft.tileentity;

import net.minecraft.item.ItemStack;

public class TileEntityDiamondChest extends TileEntitySC {

public TileEntityDiamondChest(){
	items = new ItemStack[108];
}

}

 

My GUIHandler class

package xan.storagecraft.client.interfaces.gui;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import xan.storagecraft.StorageCraft;
import xan.storagecraft.client.interfaces.container.ContainerSC;
import xan.storagecraft.tileentity.TileEntityDiamondChest;
import xan.storagecraft.tileentity.TileEntityGoldChest;
import xan.storagecraft.tileentity.TileEntityIronChest;
import xan.storagecraft.tileentity.TileEntitySC;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class GuiHandler implements IGuiHandler{

public GuiHandler() {
	NetworkRegistry.instance().registerGuiHandler(StorageCraft.instance, this);
}

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	TileEntity te;
	switch(ID){
	case 0:
		te = world.getBlockTileEntity(x, y, z);
		if(te != null && te instanceof TileEntityIronChest){
			return new ContainerSC(player.inventory, (TileEntitySC)te);
		}
		break;
	case 1:
		te = world.getBlockTileEntity(x, y, z);
		if(te != null && te instanceof TileEntityGoldChest){
			return new ContainerSC(player.inventory, (TileEntitySC)te);
		}
		break;
	case 2:
		te = world.getBlockTileEntity(x, y, z);
		if(te != null && te instanceof TileEntityDiamondChest){
			return new ContainerSC(player.inventory, (TileEntitySC)te);
		}
		break;
	}

	return null;
}

@Override
@SideOnly(Side.CLIENT)
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	TileEntity te;
	switch(ID){
	case 0:
		te = world.getBlockTileEntity(x, y, z);
		if(te != null && te instanceof TileEntitySC){
			return new GuiIronChest(player.inventory, (TileEntitySC)te);
		}
		break;
	case 1:
		te = world.getBlockTileEntity(x, y, z);
		if(te != null && te instanceof TileEntitySC){
			return new GuiGoldChest(player.inventory, (TileEntitySC)te);
		}
		break;
	case 2:
		te = world.getBlockTileEntity(x, y, z);
		if(te != null && te instanceof TileEntityGoldChest){
			return new GuiDiamondChest(player.inventory, (TileEntitySC)te);
		}
		break;
	}

	return null;
}

}

 

My Chest GUI

package xan.storagecraft.client.interfaces.gui;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;

import xan.storagecraft.client.interfaces.container.ContainerSC;
import xan.storagecraft.lib.Reference;
import xan.storagecraft.tileentity.TileEntitySC;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class GuiDiamondChest extends GuiContainer{

public GuiDiamondChest(InventoryPlayer invPlayer, TileEntitySC chest) {
	super(new ContainerSC(invPlayer, chest));
	xSize = 246;
	ySize = 254;
}

private static final ResourceLocation texture = new ResourceLocation(Reference.MOD_ID, "textures/gui/DiamondChestGUI.png");

@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) {
	GL11.glColor4f(1, 1, 1, 1);

	Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
}
}

 

I'm really baffled by this issue, basically I have a four(two complete and two still in progress) chests sharing the same block id and using metadata to determine which chest it is and which tileentity to use for it only the diamond chest is having issues, the two other chests that I've programmed have all worked perfectly, only the diamond one I've made is having an issue, but it's literally exactly the same as my other previous chests. I'm not sending any custom packets currently, I think I must be overlooking something but I can't find the issue to save my life, I'd really appreciate a little bit of help.

Link to comment
Share on other sites

Hi

 

The basic problem seems to be that you are trying to store 108 ItemStacks into a chest with only 45 slots.  So I would suggest putting a breakpoint into Packet104WindowItems.processPacket and then have a look at the container to see why it only has 45 slots instead of the expected 108.  I'm guessing either it's the wrong container, or you haven't set it up right.

 

-TGG

 

 

 

 

Link to comment
Share on other sites

Hi

 

The basic problem seems to be that you are trying to store 108 ItemStacks into a chest with only 45 slots.  So I would suggest putting a breakpoint into Packet104WindowItems.processPacket and then have a look at the container to see why it only has 45 slots instead of the expected 108.  I'm guessing either it's the wrong container, or you haven't set it up right.

 

-TGG

 

Thanks for the reply, so I did what you suggested and put a break in to check what we'd see, and the ItemStack array is the correct size.21dn7ed.png

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.