I am part of a mod group making a mod, however I'm having problems. Whenever I open the GUI, I can't move anything; If I left click it, it decreases the stack size, which lead me to thing it might be part of decrStackSize, but it wasn't, since that wasn't even called. If anyone has experienced this before, or knows what the problem is that would be great. Thanks guys. (There's a lot of unused code by the previous coder)
Your IInventory implementation is a bit off - you need to call markDirty() whenever a slot changes. Here's a base class I use whenever I need a TileEntity with an inventory, you would just extend this from your TileEntity class. Note specifically the decrStackSize and setInventorySlotContents methods:
/**
*
* Base class for any tile entity that needs an inventory;
* disallows update ticks by default, so re-override canUpdate if needed
*
*/
public abstract class TileEntityInventory extends TileEntity implements IInventory
{
/** The tile entity's inventory slots need to be initialized during construction */
protected ItemStack[] inventory;
@Override
public boolean canUpdate() {
return false;
}
@Override
public int getSizeInventory() {
return inventory.length;
}
@Override
public ItemStack getStackInSlot(int slot) {
return inventory[slot];
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
ItemStack stack = getStackInSlot(slot);
if (stack != null) {
if(stack.stackSize > amount) {
stack = stack.splitStack(amount);
markDirty();
} else {
setInventorySlotContents(slot, null);
}
}
return stack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
ItemStack stack = getStackInSlot(slot);
setInventorySlotContents(slot, null);
return stack;
}
@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) {
inventory[slot] = itemstack;
if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
itemstack.stackSize = getInventoryStackLimit();
}
markDirty();
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
NBTTagList items = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < items.tagCount(); ++i) {
NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
byte slot = item.getByte("Slot");
if (slot >= 0 && slot < inventory.length) {
inventory[slot] = ItemStack.loadItemStackFromNBT(item);
}
}
}
@Override
public void writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
NBTTagList items = new NBTTagList();
for (int i = 0; i < inventory.length; ++i) {
if (inventory[i] != null) {
NBTTagCompound item = new NBTTagCompound();
item.setByte("Slot", (byte) i);
inventory[i].writeToNBT(item);
items.appendTag(item);
}
}
compound.setTag("Items", items);
}
@Override
public void openInventory() {}
@Override
public void closeInventory() {}
}
Your IInventory implementation is a bit off - you need to call markDirty() whenever a slot changes. Here's a base class I use whenever I need a TileEntity with an inventory, you would just extend this from your TileEntity class. Note specifically the decrStackSize and setInventorySlotContents methods:
/**
*
* Base class for any tile entity that needs an inventory;
* disallows update ticks by default, so re-override canUpdate if needed
*
*/
public abstract class TileEntityInventory extends TileEntity implements IInventory
{
/** The tile entity's inventory slots need to be initialized during construction */
protected ItemStack[] inventory;
@Override
public boolean canUpdate() {
return false;
}
@Override
public int getSizeInventory() {
return inventory.length;
}
@Override
public ItemStack getStackInSlot(int slot) {
return inventory[slot];
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
ItemStack stack = getStackInSlot(slot);
if (stack != null) {
if(stack.stackSize > amount) {
stack = stack.splitStack(amount);
markDirty();
} else {
setInventorySlotContents(slot, null);
}
}
return stack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
ItemStack stack = getStackInSlot(slot);
setInventorySlotContents(slot, null);
return stack;
}
@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) {
inventory[slot] = itemstack;
if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
itemstack.stackSize = getInventoryStackLimit();
}
markDirty();
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
NBTTagList items = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < items.tagCount(); ++i) {
NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
byte slot = item.getByte("Slot");
if (slot >= 0 && slot < inventory.length) {
inventory[slot] = ItemStack.loadItemStackFromNBT(item);
}
}
}
@Override
public void writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
NBTTagList items = new NBTTagList();
for (int i = 0; i < inventory.length; ++i) {
if (inventory[i] != null) {
NBTTagCompound item = new NBTTagCompound();
item.setByte("Slot", (byte) i);
inventory[i].writeToNBT(item);
items.appendTag(item);
}
}
compound.setTag("Items", items);
}
@Override
public void openInventory() {}
@Override
public void closeInventory() {}
}
[/i][/i]
Sorry for taking so long to respond; I started a clean project, just to figure this out as it is still happening. Whenever I left click, one item disappears, and when I right click, the stack disappears, and I don't know where it's going to. When I relog, the items are back in my inventory though.
I am part of a mod group making a mod, however I'm having problems. Whenever I open the GUI, I can't move anything; If I left click it, it decreases the stack size, which lead me to thing it might be part of decrStackSize, but it wasn't, since that wasn't even called. If anyone has experienced this before, or knows what the problem is that would be great. Thanks guys. (There's a lot of unused code by the previous coder)
TileEntityCarpenter: http://pastebin.com/LXhrBvy7
BlockCarpentersBench: http://pastebin.com/MmQXr2Vh
ContainerCarpenterBench: http://pastebin.com/aqWA50QM
GUICarpenterBenchL http://pastebin.com/3Qeyx05i
Just to be clear, the GUI shows up; It's the container/tile entity that's being weird.
Thanks in advance guys
Jay
Wow! Extremely useful! I'll have to steal this!
Hello!
For whatever reason I fixed this by changing my @Instance name to the same thing as my modid.
WAS:
NOW: