Jump to content

[1.7.2]IndexOutOfBoundsException: Index: 45, Size: 45


Recommended Posts

Posted

OK so I am having a problem when opening the GUI on one of my blocks.

It instantly crashes and displays this every time and I am unsure of what the problem is because I only changed a few method names to update to 1.7.2.

 

I have checked around and have seen this same error on here and the only one that was solved was by changing the code in the GUIHandler when mine is correct so I am completely lost and have been trying to figure this out for awhile now. But I am pretty sure that it has to do with either the Container or Inventory class.

 

If you need anything else I'll be sure to post it ASAP...

Thanks for any help.

 

[05:36:34] [Client thread/FATAL]: Unreported exception thrown!
java.lang.IndexOutOfBoundsException: Index: 45, Size: 45
at java.util.ArrayList.rangeCheck(ArrayList.java:635) ~[?:1.7.0_45]
at java.util.ArrayList.get(ArrayList.java:411) ~[?:1.7.0_45]
at net.minecraft.inventory.Container.getSlot(Container.java:136) ~[Container.class:?]
at net.minecraft.inventory.Container.putStacksInSlots(Container.java:564) ~[Container.class:?]
at net.minecraft.client.network.NetHandlerPlayClient.handleWindowItems(NetHandlerPlayClient.java:1194) ~[NetHandlerPlayClient.class:?]
at net.minecraft.network.play.server.S30PacketWindowItems.processPacket(S30PacketWindowItems.java:70) ~[s30PacketWindowItems.class:?]
at net.minecraft.network.play.server.S30PacketWindowItems.processPacket(S30PacketWindowItems.java:78) ~[s30PacketWindowItems.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:242) ~[NetworkManager.class:?]
at net.minecraft.client.multiplayer.PlayerControllerMP.updateController(PlayerControllerMP.java:339) ~[PlayerControllerMP.class:?]
at net.minecraft.client.Minecraft.runTick(Minecraft.java:1689) ~[Minecraft.class:?]
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1036) ~[Minecraft.class:?]
at net.minecraft.client.Minecraft.run(Minecraft.java:951) [Minecraft.class:?]
at net.minecraft.client.main.Main.main(Main.java:112) [Main.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_45]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_45]
at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_45]
at net.minecraft.launchwrapper.Launch.launch(Launch.java:134) [launchwrapper-1.9.jar:?]
at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.9.jar:?]

 

Inventory Class

package grim3212.mc.morestorage.Tower;

import java.util.ArrayList;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;

public class InventoryItemTower implements IInventory {
public ArrayList<TileEntityItemTower> itemTowers = new ArrayList();
public TileEntityItemTower mainTower;

public InventoryItemTower(ArrayList<TileEntityItemTower> itemTowers, TileEntityItemTower mainTower) {
	this.itemTowers = itemTowers;
	this.mainTower = mainTower;
}

public int getSizeInventory() {
	return this.mainTower.getSizeInventory() * this.itemTowers.size();
}

public ItemStack getStackInSlot(int i) {
	return getInvFromSlot(i).getStackInSlot(getLocalSlot(i));
}

public ItemStack decrStackSize(int i, int j) {
	return getInvFromSlot(i).decrStackSize(getLocalSlot(i), j);
}

public ItemStack getStackInSlotOnClosing(int i) {
	return getInvFromSlot(i).getStackInSlotOnClosing(getLocalSlot(i));
}

public void setInventorySlotContents(int i, ItemStack itemstack) {
	getInvFromSlot(i).setInventorySlotContents(getLocalSlot(i), itemstack);
}

public String getInventoryName() {
	return this.mainTower.getInventoryName();
}

public boolean hasCustomInventoryName() {
	return this.mainTower.hasCustomInventoryName();
}

public int getInventoryStackLimit() {
	return this.mainTower.getInventoryStackLimit();
}

public void markDirty() {
	for (IInventory inventory : this.itemTowers)
		inventory.markDirty();
}

public boolean isUseableByPlayer(EntityPlayer entityplayer) {
	boolean flag = true;

	for (IInventory inventory : this.itemTowers) {
		if (!inventory.isUseableByPlayer(entityplayer))
			flag = false;
	}

	return flag;
}

public void openInventory() {
	for (IInventory inventory : this.itemTowers)
		inventory.openInventory();
}

public void closeInventory() {
	for (IInventory inventory : this.itemTowers)
		inventory.closeInventory();
}

public void setAnimation(int animID) {
	for (TileEntityItemTower inventory : this.itemTowers)
		inventory.setAnimation(animID);
}

private int getLocalSlot(int slot) {
	return slot % this.mainTower.getSizeInventory();
}

private IInventory getInvFromSlot(int slot) {
	int inventoryIndex = (int) Math.floor(slot / this.mainTower.getSizeInventory());
	return (IInventory) this.itemTowers.get(inventoryIndex);
}

public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	return getInvFromSlot(i).isItemValidForSlot(getLocalSlot(i), itemstack);
}
}

 

Container Class

package grim3212.mc.morestorage.Tower;

import grim3212.mc.morestorage.SlotMoveable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ContainerItemTower extends Container {
private IInventory itemTowers;
private int numRows;

public ContainerItemTower(IInventory playerInventory, IInventory itemTowers) {
	this.itemTowers = itemTowers;
	this.numRows = (itemTowers.getSizeInventory() / 9);

	itemTowers.openInventory();

	for (int j = 0; j < this.numRows; j++) {
		for (int k = 0; k < 9; k++) {
			addSlotToContainer(new SlotMoveable(itemTowers, k + j * 9, 8 + k * 18, 18 + j * 18));
		}

	}

	// Player Inventory
	for (int j = 0; j < 3; j++) {
		for (int k = 0; k < 9; k++) {
			addSlotToContainer(new Slot(playerInventory, k + j * 9 + 9, 8 + k * 18, 50 + j * 18));
		}

	}

	// Player Hotbar Inventory
	for (int j = 0; j < 9; j++) {
		addSlotToContainer(new Slot(playerInventory, j, 8 + j * 18, 108));
	}

	if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
		setDisplayRow(0);
}

@SideOnly(Side.CLIENT)
public void setDisplayRow(int row) {
	int minSlot = row * 9;
	int maxSlot = (row + 1) * 9;

	for (int slotIndex = 0; slotIndex < this.numRows * 9; slotIndex++) {
		if ((slotIndex >= minSlot) && (slotIndex < maxSlot)) {
			int modRow = (int) Math.floor((slotIndex - minSlot) / 9.0D);
			int modColumn = slotIndex % 9;
			((SlotMoveable) this.inventorySlots.get(slotIndex)).setSlotPosition(8 + modColumn * 18, 18 + modRow * 18);
		} else {
			((SlotMoveable) this.inventorySlots.get(slotIndex)).setSlotPosition(-9999, -9999);
		}
	}
}

public boolean canInteractWith(EntityPlayer par1EntityPlayer) {
	return this.itemTowers.isUseableByPlayer(par1EntityPlayer);
}

public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int slotID) {
	ItemStack itemstack = null;
	Slot slot = (Slot) this.inventorySlots.get(slotID);

	if ((slot != null) && (slot.getHasStack())) {
		ItemStack itemstack1 = slot.getStack();
		itemstack = itemstack1.copy();

		if (slotID < this.numRows * 9) {
			if (!mergeItemStack(itemstack1, this.numRows * 9, this.inventorySlots.size(), true)) {
				return null;
			}
		} else if (!mergeItemStack(itemstack1, 0, this.numRows * 9, false)) {
			return null;
		}

		if (itemstack1.stackSize == 0) {
			slot.putStack((ItemStack) null);
		} else {
			slot.onSlotChanged();
		}
	}

	return itemstack;
}

public void onContainerClosed(EntityPlayer par1EntityPlayer) {
	super.onContainerClosed(par1EntityPlayer);
	this.itemTowers.closeInventory();
}
}

 

Block

package grim3212.mc.morestorage.Tower;

import grim3212.mc.morestorage.BlockMoreStorage;
import grim3212.mc.morestorage.MoreStorageCore;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.internal.FMLNetworkHandler;

public class BlockItemTower extends BlockMoreStorage {
public BlockItemTower(Material material) {
	super(material);
}

public TileEntity createNewTileEntity(World world, int metadata) {
	return new TileEntityItemTower();
}

public boolean hasRotatedDoor() {
	return true;
}

public boolean isDoorBlocked(World world, int i, int j, int k) {
	return false;
}

public void onBlockClicked(World world, int i, int j, int k, EntityPlayer entityplayer) {
}

public boolean onBlockActivated(World world, int i, int j, int k, EntityPlayer entityplayer, int par6, float par7, float par8, float par9) {
	TileEntity tileentity = world.getTileEntity(i, j, k);

	if ((tileentity == null) || (entityplayer.isSneaking()))
		return false;

	entityplayer.openGui(MoreStorageCore.instance, 4, world, i, j, k);

	return true;
}
}

 

And GUI Handler just in case

public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	TileEntity tileentity = world.getTileEntity(x, y, z);
	TileEntity tileentitytop = world.getTileEntity(x, y + 1, z);

	switch (ID) {
	// Removed the other cases since they are unneeded.
	case 4:
		return getItemTowerContainer(player, world, x, y, z);
	}
	return null;
}

public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	TileEntity tileentity = world.getTileEntity(x, y, z);
	TileEntity tileentitytop = world.getTileEntity(x, y + 1, z);

	switch (ID) {
	// Removed the other cases since they are unneeded.
	case 4:
		return getItemTowerGui(player, world, x, y, z);
                 }
	return null;
}

private Object getItemTowerGui(EntityPlayer player, World world, int x, int y, int z) {
	TileEntity tileentity = world.getTileEntity(x, y, z);

	if (checkUsable(tileentity, player)) {
		int ymod = y;
		while (world.getBlock(x, ymod - 1, z) == world.getBlock(x, y, z))
			ymod--;

		ArrayList itemTowers = new ArrayList();
		while (world.getBlock(x, ymod, z) == world.getBlock(x, y, z)) {
			itemTowers.add((TileEntityItemTower) world.getTileEntity(x, ymod, z));
			ymod++;
		}

		InventoryItemTower towerInv = new InventoryItemTower(itemTowers, (TileEntityItemTower) tileentity);
		return new GuiItemTower(player.inventory, towerInv);
	}

	return null;
}

private Object getItemTowerContainer(EntityPlayer player, World world, int x, int y, int z) {
	TileEntity tileentity = world.getTileEntity(x, y, z);

	if (checkUsable(tileentity, player)) {
		int ymod = y;
		while (world.getBlock(x, ymod - 1, z) == world.getBlock(x, y, z))
			ymod--;

		ArrayList itemTowers = new ArrayList();
		while (world.getBlock(x, ymod, z) == world.getBlock(x, y, z)) {
			itemTowers.add((TileEntityItemTower) world.getTileEntity(x, ymod, z));
			ymod++;
		}

		InventoryItemTower towerInv = new InventoryItemTower(itemTowers, (TileEntityItemTower) tileentity);
		return new ContainerItemTower(player.inventory, towerInv);
	}

	return null;
}

Posted

Hi

 

I don't know exactly what the cause is but I can suggest a couple of ways to troubleshoot it.

The problem is that the packet is trying to send 46 items (0 - 45) when you only have room in your inventory for 45.

 

Perhaps if you put a breakpoint in the vanilla code at the point it is about to send the packet, and look at the list of inventory items it is about to send, you might gain a useful clue.

 

In EntityPlayerMP.sendContainerAndContentsToPlayer

        this.playerNetServerHandler.sendPacket(new S30PacketWindowItems(par1Container.windowId, par2List));

 

-TGG

 

 

Posted

Hi

 

I don't know exactly what the cause is but I can suggest a couple of ways to troubleshoot it.

The problem is that the packet is trying to send 46 items (0 - 45) when you only have room in your inventory for 45.

 

Perhaps if you put a breakpoint in the vanilla code at the point it is about to send the packet, and look at the list of inventory items it is about to send, you might gain a useful clue.

 

In EntityPlayerMP.sendContainerAndContentsToPlayer

        this.playerNetServerHandler.sendPacket(new S30PacketWindowItems(par1Container.windowId, par2List));

 

-TGG

 

Alright thanks for the reply, so I ended up doing as you said and it is strange because the list of inventory items it is about to send is 54 unless I am misreading this.

16gzsio.png

Posted

Yeah I think you're right, it has been given a list of 54 objects for some reason.  The parent container also has 54 slots.

 

But for some reason the container on the client has only 45.

 

How many should it have, based on what you intended?  Can you tell why ?  (perhaps a couple more breakpoints in your container constructor) -?  My guess is some sort of mismatch between your client and server container creation code - perhaps the TileEntity is a different size on both sides?

 

-TGG

 

 

 

 

 

 

Posted

Yeah I think you're right, it has been given a list of 54 objects for some reason.  The parent container also has 54 slots.

 

But for some reason the container on the client has only 45.

 

How many should it have, based on what you intended?  Can you tell why ?  (perhaps a couple more breakpoints in your container constructor) -?  My guess is some sort of mismatch between your client and server container creation code - perhaps the TileEntity is a different size on both sides?

 

-TGG

 

Well each Block itself should start out with 18 inventory slots. Then you place them on top of each other and it adds 18 more in the form of 2 rows.

 

Well right now the TileEntity for this Block extends a TileEntity that all of my Blocks extend and I am fairly certain that everything is correct since the other Blocks are working. The only real things in this Blocks TileEntity class are but, I will keep looking around trying to find the culprit.

public int getSizeInventory() {
	return 18;
}

public int getLowestMetadata() {
	int ymod = 0;

	while (this.worldObj.getBlock(this.xCoord, this.yCoord - ymod - 1, this.zCoord) == this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord))
		ymod++;

	return this.worldObj.getBlockMetadata(this.xCoord, this.yCoord - ymod, this.zCoord);
}

 

 

package grim3212.mc.morestorage;

import grim3212.mc.core.packet.PacketPipeline;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
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 cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class TileEntityStorage extends TileEntity implements IInventory {
private String customName;
private int numUsingPlayers = 0;
public int rotation = 0;
private boolean direction = false;
private ItemStack[] contents;
private int storagePassword = -1;
public boolean hasInitialData = false;
private int hasInitialDataTimeout = 0;

public TileEntityStorage() {
	this.contents = new ItemStack[getSizeInventory()];
}

public void readFromNBT(NBTTagCompound nbttagcompound) {
	super.readFromNBT(nbttagcompound);
	this.storagePassword = nbttagcompound.getInteger("StoragePassword");

	NBTTagList nbttaglist = nbttagcompound.getTagList("Items", 10);
	this.contents = new ItemStack[getSizeInventory()];

	for (int i = 0; i < nbttaglist.tagCount(); i++) {
		NBTTagCompound nbttagcompound1 = (NBTTagCompound) nbttaglist.getCompoundTagAt(i);
		int j = nbttagcompound1.getByte("Slot") & 0xFF;

		if ((j >= 0) && (j < this.contents.length)) {
			this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
		}
	}
}

public void writeToNBT(NBTTagCompound nbttagcompound) {
	super.writeToNBT(nbttagcompound);
	nbttagcompound.setInteger("StoragePassword", this.storagePassword);

	NBTTagList nbttaglist = new NBTTagList();

	for (int i = 0; i < this.contents.length; i++) {
		if (this.contents[i] != null) {
			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound1.setByte("Slot", (byte) i);
			this.contents[i].writeToNBT(nbttagcompound1);
			nbttaglist.appendTag(nbttagcompound1);
		}
	}

	nbttagcompound.setTag("Items", nbttaglist);
}

@SideOnly(Side.CLIENT)
public boolean receiveClientEvent(int eventID, int eventInput) {
	if (eventID == 1) {
		this.numUsingPlayers = eventInput;
		return true;
	}
	if (eventID == 2) {
		this.storagePassword = eventInput;
		return true;
	}

	return super.receiveClientEvent(eventID, eventInput);
}

public void sendStoragePassword() {
	PacketPipeline.sendToServer(new MoreStoragePacket(xCoord, yCoord, zCoord, storagePassword, 2));
}

public void setStoragePassword(int password) {
	this.storagePassword = password;

	if (FMLCommonHandler.instance().getEffectiveSide() != Side.CLIENT)
		sendStoragePassword();
}

public void clearStoragePassword() {
	this.storagePassword = -1;

	if (FMLCommonHandler.instance().getEffectiveSide() != Side.CLIENT)
		sendStoragePassword();
}

public boolean hasPassword() {
	return this.storagePassword > 0;
}

public int getPassword() {
	return this.storagePassword;
}

public void updateEntity() {
	if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) {
		if ((!this.hasInitialData) && (this.hasInitialDataTimeout <= 0)) {

			PacketPipeline.sendToServer(new MoreStoragePacket(xCoord, yCoord, zCoord, 1));

			this.hasInitialDataTimeout = 10;
		}

		if (this.hasInitialDataTimeout > 0)
			this.hasInitialDataTimeout -= 1;
	}

	boolean prevdirection = this.direction;
	this.direction = (this.numUsingPlayers > 0);

	if ((!prevdirection) && (this.direction))
		this.worldObj.playSoundEffect(this.xCoord, this.yCoord, this.zCoord, getOpenSound(), 1.0F, 1.0F);

	int prevrotation = this.rotation;
	int addspeed = (int) (Math.abs(90 - this.rotation) / 10.0F);

	if (this.direction) {
		if (this.rotation < 90) {
			this.rotation += 6 + addspeed;
		}
	} else if (this.rotation > 0) {
		this.rotation -= 6 + addspeed;
	}

	if (this.rotation < 0)
		this.rotation = 0;
	if (this.rotation > 90)
		this.rotation = 90;

	if ((prevrotation > this.rotation) && (this.rotation == 0)) {
		this.worldObj.playSoundEffect(this.xCoord, this.yCoord, this.zCoord, getCloseSound(), 1.0F, 1.0F);
	}
}

protected String getOpenSound() {
	return "random.door_open";
}

protected String getCloseSound() {
	return "random.door_close";
}

public void openInventory() {
	this.numUsingPlayers += 1;
	if (this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord) != Blocks.air)
		this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord), 1, this.numUsingPlayers);
}

public void closeInventory() {
	this.numUsingPlayers -= 1;
	if (this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord) != Blocks.air)
		this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord), 1, this.numUsingPlayers);
}

public void invalidate() {
	super.invalidate();
}

public String getInventoryName() {
	return this.customName != null ? this.customName : getSubInvName();
}

public String getSubInvName() {
	return "NoName";
}

public boolean hasCustomInventoryName() {
	return false;
}

public int getSizeInventory() {
	return 27;
}

public ItemStack decrStackSize(int i, int j) {
	if ((hasPassword()) && (this.numUsingPlayers == 0))
		return null;

	if (this.contents[i] != null) {
		if (this.contents[i].stackSize <= j) {
			ItemStack itemstack = this.contents[i];
			this.contents[i] = null;
			markDirty();
			return itemstack;
		}

		ItemStack itemstack = this.contents[i].splitStack(j);

		if (this.contents[i].stackSize == 0) {
			this.contents[i] = null;
		}

		markDirty();
		return itemstack;
	}

	return null;
}

public ItemStack getStackInSlot(int i) {
	if ((hasPassword()) && (this.numUsingPlayers == 0))
		return null;
	return this.contents[i];
}

public ItemStack getStackInSlotOnClosing(int i) {
	return null;
}

public ItemStack getStackInSlotNoLockBlock(int i) {
	return this.contents[i];
}

public int getInventoryStackLimit() {
	return 64;
}

public void setInventorySlotContents(int i, ItemStack itemstack) {
	this.contents[i] = itemstack;

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

	markDirty();
}

public boolean isUseableByPlayer(EntityPlayer entityplayer) {
	if ((FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) && (!this.hasInitialData))
		return false;
	return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) == this;
}

public boolean isItemValidForSlot(int i, ItemStack itemstack) {
	return !hasPassword();
}
}

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.