Jump to content

[1.7.10] IndexOutOfBounds exception when clicking on a new inventory slot?


Ms_Raven

Recommended Posts

This is my error:

 

 

net.minecraft.util.ReportedException: Ticking memory connection

at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:198) ~[NetworkSystem.class:?]

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) ~[MinecraftServer.class:?]

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) ~[MinecraftServer.class:?]

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) ~[integratedServer.class:?]

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]

Caused by: java.lang.IndexOutOfBoundsException: Index: 63, Size: 45

at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_51]

at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_51]

at net.minecraft.inventory.Container.slotClick(Container.java:300) ~[Container.class:?]

at net.minecraft.network.NetHandlerPlayServer.processClickWindow(NetHandlerPlayServer.java:955) ~[NetHandlerPlayServer.class:?]

at net.minecraft.network.play.client.C0EPacketClickWindow.processPacket(C0EPacketClickWindow.java:41) ~[C0EPacketClickWindow.class:?]

at net.minecraft.network.play.client.C0EPacketClickWindow.processPacket(C0EPacketClickWindow.java:113) ~[C0EPacketClickWindow.class:?]

at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) ~[NetworkManager.class:?]

at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) ~[NetworkSystem.class:?]

... 5 more

 

 

I'm assuming it's only recognising the 45 slots in the Vanilla inventory, not the additional 20 slots in my new inventory?

Each time I click on a new slot I get this crash report. But I can't figure out where that goes wrong. How do I fix this?

 

My inventory container:

public class ContainerExtendedInventory extends Container
{

public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
public IInventory craftResult = new InventoryCraftResult();

public ExtendedInventory inventory;
private EntityPlayer player;

private int slotAmount;

public ContainerExtendedInventory(EntityPlayer player, InventoryPlayer inventory, ExtendedInventory inventoryCustom)
{
	this.player = player;
	this.inventory = inventoryCustom;

	int i;
	int j;

	// Accessories
	this.addSlotToContainer(new SlotHead(inventoryCustom, 0, 188, );
	this.addSlotToContainer(new SlotNeck(inventoryCustom, 1, 188, 26));
	this.addSlotToContainer(new SlotBody(inventoryCustom, 2, 188, 44));
	this.addSlotToContainer(new SlotHandL(inventoryCustom, 3, 170, 35));
	this.addSlotToContainer(new SlotHandR(inventoryCustom, 4, 188, 35));
	this.addSlotToContainer(new SlotBeltL(inventoryCustom, 5, 170, 53));
	this.addSlotToContainer(new SlotBeltR(inventoryCustom, 6, 188, 53));
	this.addSlotToContainer(new SlotFeet(inventoryCustom, 7, 188, 63));

	// (Future Accessory Slots)
	this.addSlotToContainer(new SlotNull(inventoryCustom, 8, 170, 17));
	this.addSlotToContainer(new SlotNull(inventoryCustom, 9, 206, 17));

	// (Future Slots)
	this.addSlotToContainer(new SlotNull(inventoryCustom, 10, 179, 93));
	this.addSlotToContainer(new SlotNull(inventoryCustom, 11, 197, 93));
	this.addSlotToContainer(new SlotNull(inventoryCustom, 12, 179, 111));
	this.addSlotToContainer(new SlotNull(inventoryCustom, 13, 197, 111));

	// (Future Slots)
	this.addSlotToContainer(new SlotNull(inventoryCustom, 14, 180, 132));
	this.addSlotToContainer(new SlotNull(inventoryCustom, 15, 198, 132));

	// Ammo
	this.addSlotToContainer(new SlotAmmo(inventoryCustom, 16, 80, );
	this.addSlotToContainer(new SlotAmmo(inventoryCustom, 17, 80, 26));
	this.addSlotToContainer(new SlotAmmo(inventoryCustom, 18, 80, 44));
	this.addSlotToContainer(new SlotAmmo(inventoryCustom, 19, 80, 62));

	slotAmount = 20;
	Tools.print("Custom slots ended at " + slotAmount);

	for (i = 0; i < 4; ++i)
	{
		this.addSlotToContainer(
				new SlotArmourDefault(player, inventory, inventoryCustom.getSizeInventory() - 1 - i, 8, 8 + i * 18, i));
		slotAmount++;
	}
	Tools.print("Armour ended at " + slotAmount);

	for (i = 0; i < 3; ++i)
	{
		for (int k = 0; k < 9; ++k)
		{
			this.addSlotToContainer(new Slot(inventory, k + i * 9 + 9, 8 + k * 18, 84 + i * 18));
			slotAmount++;
		}
	}
	Tools.print("Inventory ended at " + slotAmount);

	for (i = 0; i < 9; ++i)
	{
		this.addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 142));
		slotAmount++;
	}
	Tools.print("Hotbar ended at " + slotAmount);

	for (i = 0; i < 2; ++i)
	{
		for (j = 0; j < 2; ++j)
		{
			this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 2, 125 + j * 18, 17 + i * 18));
			slotAmount++;
		}
	}
	Tools.print("Crafting ended at " + slotAmount);

	this.addSlotToContainer(new SlotCrafting(player, this.craftMatrix, this.craftResult, 0, 134, 60));
	slotAmount++;
	Tools.print("Craft result ended at " + slotAmount);

}

public ItemStack slotClick(int slot, int button, int flag, EntityPlayer player)
{
	if ((slot >= 0) && (getSlot(slot) != null) && (getSlot(slot).getStack() == player.getHeldItem()))
	{
		return null;
	}
	Tools.print("Clicked on slot " + slot);
	return super.slotClick(slot, button, flag, player);
}

@Override
public boolean canInteractWith(EntityPlayer player)
{
	return true;
}

@Override
public ItemStack transferStackInSlot(EntityPlayer player, int par2)
{
	// Accessories 0-19
	// Armour 20-23
	// Inventory 24-50
	// Hotbar 51-59
	// Crafting 60-63
	// Craft result 64

	ItemStack itemstack = null;
	Slot slot = (Slot) this.inventorySlots.get(par2);
	if ((slot != null) && (slot.getHasStack()))
	{
		ItemStack itemstack1 = slot.getStack();
		itemstack = itemstack1.copy();

		// Accessories to inventory
		if (par2 < 20)
		{
			if (!mergeItemStack(itemstack1, 24, 51, true))
			{
				return null;
			}
			slot.onSlotChange(itemstack1, itemstack);
		}
		// Armour to inventory
		else if ((par2 >= 20) && (par2 < 24))
		{
			if (!mergeItemStack(itemstack1, 24, 51, false))
			{
				return null;
			}
		}
		// Hotbar to inventory
		else if ((par2 >= 51) && (par2 < 60))
		{
			if (!mergeItemStack(itemstack1, 24, 51, false))
			{
				return null;
			}
		}
		else if ((par2 >= 24) && par2 < 51)
		{
			if (itemstack.getItem() instanceof CoreAccessory)
			{
				Place place = ((CoreAccessory) itemstack.getItem()).getPlacement();

				switch (place)
				{
					case HEAD:
						if (inventory.getStackInSlot(0) != null)
						{
							return null;
						}
					case NECK:
						if (inventory.getStackInSlot(1) != null)
						{
							return null;
						}
					case BODY:
						if (inventory.getStackInSlot(2) != null)
						{
							return null;
						}
					case HANDS:
						if ((inventory.getStackInSlot(3) != null) || inventory.getStackInSlot(4) != null)
						{
							return null;
						}
					case BELT:
						if ((inventory.getStackInSlot(5) != null) || inventory.getStackInSlot(6) != null)
						{
							return null;
						}
					case FEET:
						if (inventory.getStackInSlot(7) != null)
						{
							return null;
						}
					case AMMO:
						if (inventory.getStackInSlot(16) != null)
						{
							return null;
						}
				}
			}
			else if (!mergeItemStack(itemstack1, 51, 60, false))
			{
				return null;
			}
		}
		if (itemstack1.stackSize == 64)
		{
			slot.putStack((ItemStack) null);
		}
		else
		{
			slot.onSlotChanged();
		}
		if (itemstack1.stackSize == itemstack.stackSize)
		{
			return null;
		}
		slot.onPickupFromSlot(player, itemstack1);
	}
	return itemstack;
}

protected boolean mergeItemStack(ItemStack par1ItemStack, int par2, int par3, boolean par4)
{
	boolean flag1 = false;
	int k = par2;
	if (par4)
	{
		k = par3 - 1;
	}
	if (par1ItemStack.isStackable())
	{
		while ((par1ItemStack.stackSize > 0) && (((!par4) && (k < par3)) || ((par4) && (k >= par2))))
		{
			Slot slot = (Slot) this.inventorySlots.get(k);
			ItemStack itemstack1 = slot.getStack();
			if ((itemstack1 != null) && (itemstack1 == par1ItemStack)
					&& ((!par1ItemStack.getHasSubtypes()) || (par1ItemStack.getItemDamage() == itemstack1.getItemDamage()))
					&& (ItemStack.areItemStackTagsEqual(par1ItemStack, itemstack1)))
			{
				int l = itemstack1.stackSize + par1ItemStack.stackSize;
				if ((l <= par1ItemStack.getMaxStackSize()) && (l <= slot.getSlotStackLimit()))
				{
					par1ItemStack.stackSize = 0;
					itemstack1.stackSize = l;
					this.inventory.markDirty();
					flag1 = true;
				}
				else if ((itemstack1.stackSize < par1ItemStack.getMaxStackSize()) && (l < slot.getSlotStackLimit()))
				{
					par1ItemStack.stackSize -= par1ItemStack.getMaxStackSize() - itemstack1.stackSize;
					itemstack1.stackSize = par1ItemStack.getMaxStackSize();
					this.inventory.markDirty();
					flag1 = true;
				}
			}
			if (par4)
			{
				k--;
			}
			else
			{
				k++;
			}
		}
	}
	if (par1ItemStack.stackSize > 0)
	{
		if (par4)
		{
			k = par3 - 1;
		}
		else
		{
			k = par2;
		}
		while (((!par4) && (k < par3)) || ((par4) && (k >= par2)))
		{
			Slot slot = (Slot) this.inventorySlots.get(k);
			ItemStack itemstack1 = slot.getStack();
			if (itemstack1 == null)
			{
				int l = par1ItemStack.stackSize;
				if (l <= slot.getSlotStackLimit())
				{
					slot.putStack(par1ItemStack.copy());
					par1ItemStack.stackSize = 0;
					this.inventory.markDirty();
					flag1 = true;
					break;
				}
				putStackInSlot(k, new ItemStack(par1ItemStack.getItem(), slot.getSlotStackLimit()));
				par1ItemStack.stackSize -= slot.getSlotStackLimit();
				this.inventory.markDirty();
				flag1 = true;
			}
			if (par4)
			{
				k--;
			}
			else
			{
				k++;
			}
		}
	}
	return flag1;
}
}

Link to comment
Share on other sites

  • 2 weeks later...

No? All my new inventory slots use inventoryCustom.

I'm adding slots the same exact way here as I did with my backpack inventory which works perfectly fine. Only difference is that I'm now including crafting and armour slots, the addition of which would not result in the error I'm getting.

 

This is the backpack that works fine:

package skye.gamecube.core.inventories;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerBackpack extends Container
{

public final InventoryBackpack inventory;

public ContainerBackpack(EntityPlayer par1Player, InventoryPlayer inventoryPlayer, InventoryBackpack inventoryItem)
{
	this.inventory = inventoryItem;
	for (int i = 0; i < 20; i++)
	{
		addSlotToContainer(new SlotBackpack(this.inventory, i, 80 + 18 * (i / 4), 8 + 18 * (i % 4)));
	}
	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));
	}
}

public boolean canInteractWith(EntityPlayer player)
{
	return this.inventory.isUseableByPlayer(player);
}

public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
{
	ItemStack itemstack = null;
	Slot slot = (Slot) this.inventorySlots.get(par2);
	if ((slot != null) && (slot.getHasStack()))
	{
		ItemStack itemstack1 = slot.getStack();
		itemstack = itemstack1.copy();
		if (par2 < 20)
		{
			if (!mergeItemStack(itemstack1, 20, 56, true))
			{
				return null;
			}
			slot.onSlotChange(itemstack1, itemstack);
		}
		else if ((par2 >= 20) && (par2 < 47))
		{
			if (!mergeItemStack(itemstack1, 47, 56, false))
			{
				return null;
			}
		}
		else if ((par2 >= 47) && (par2 < 56))
		{
			if (!mergeItemStack(itemstack1, 20, 47, false))
			{
				return null;
			}
		}
		if (itemstack1.stackSize == 0)
		{
			slot.putStack((ItemStack) null);
		}
		else
		{
			slot.onSlotChanged();
		}
		if (itemstack1.stackSize == itemstack.stackSize)
		{
			return null;
		}
		slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
	}
	return itemstack;
}

public ItemStack slotClick(int slot, int button, int flag, EntityPlayer player)
{
	if ((slot >= 0) && (getSlot(slot) != null) && (getSlot(slot).getStack() == player.getHeldItem()))
	{
		return null;
	}
	return super.slotClick(slot, button, flag, player);
}

protected boolean mergeItemStack(ItemStack par1ItemStack, int par2, int par3, boolean par4)
{
	boolean flag1 = false;
	int k = par2;
	if (par4)
	{
		k = par3 - 1;
	}
	if (par1ItemStack.isStackable())
	{
		while ((par1ItemStack.stackSize > 0) && (((!par4) && (k < par3)) || ((par4) && (k >= par2))))
		{
			Slot slot = (Slot) this.inventorySlots.get(k);
			ItemStack itemstack1 = slot.getStack();
			if ((itemstack1 != null) && (itemstack1 == par1ItemStack)
					&& ((!par1ItemStack.getHasSubtypes()) || (par1ItemStack.getItemDamage() == itemstack1.getItemDamage()))
					&& (ItemStack.areItemStackTagsEqual(par1ItemStack, itemstack1)))
			{
				int l = itemstack1.stackSize + par1ItemStack.stackSize;
				if ((l <= par1ItemStack.getMaxStackSize()) && (l <= slot.getSlotStackLimit()))
				{
					par1ItemStack.stackSize = 0;
					itemstack1.stackSize = l;
					this.inventory.markDirty();
					flag1 = true;
				}
				else if ((itemstack1.stackSize < par1ItemStack.getMaxStackSize()) && (l < slot.getSlotStackLimit()))
				{
					par1ItemStack.stackSize -= par1ItemStack.getMaxStackSize() - itemstack1.stackSize;
					itemstack1.stackSize = par1ItemStack.getMaxStackSize();
					this.inventory.markDirty();
					flag1 = true;
				}
			}
			if (par4)
			{
				k--;
			}
			else
			{
				k++;
			}
		}
	}
	if (par1ItemStack.stackSize > 0)
	{
		if (par4)
		{
			k = par3 - 1;
		}
		else
		{
			k = par2;
		}
		while (((!par4) && (k < par3)) || ((par4) && (k >= par2)))
		{
			Slot slot = (Slot) this.inventorySlots.get(k);
			ItemStack itemstack1 = slot.getStack();
			if (itemstack1 == null)
			{
				int l = par1ItemStack.stackSize;
				if (l <= slot.getSlotStackLimit())
				{
					slot.putStack(par1ItemStack.copy());
					par1ItemStack.stackSize = 0;
					this.inventory.markDirty();
					flag1 = true;
					break;
				}
				putStackInSlot(k, new ItemStack(par1ItemStack.getItem(), slot.getSlotStackLimit()));
				par1ItemStack.stackSize -= slot.getSlotStackLimit();
				this.inventory.markDirty();
				flag1 = true;
			}
			if (par4)
			{
				k--;
			}
			else
			{
				k++;
			}
		}
	}
	return flag1;
}
}

 

I cannot figure out what I'm doing wrong now that I did right in the backpack.

Link to comment
Share on other sites

It's been a while since I did inventory containers, but note one thing: When assigning ID to slot you assign it per-inventory, not per-container.

 

Meaning:

If you have 2 inventories in 1 container, you can have slot with same ID in both inventories.

 

You can't just copy code from vanilla: (I mean, it looks like copy-pasta mistake)

net.minecraft.inventory.ContainerPlayer, line 41

this.addSlotToContainer(new Slot(playerInventory, playerInventory.getSizeInventory() - 1 - i, 8, 8 + i * 18)

Your line:

this.addSlotToContainer(new SlotArmourDefault(player, inventory, inventoryCustom.getSizeInventory() - 1 - i, 8, 8 + i * 18, i));

 

Seriously?! :D

 

Anyway: SlotArmourDefault.class would be useful.

 

If you don't get the point:

 

You have 3 inventories there:

*Your player

*Vanilla player

*Crafting

 

In each of those slot ID go from 0 to their size.

What you did is add slot to "Vanilla player" with slot id based on "Your player".

 

Idk if it will fix problem, but that is one of them.

1.7.10 is no longer supported by forge, you are on your own.

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.