Posted June 9, 201312 yr Hello, as of recent I have been working with basic tileentities and containers. Since I am new to them, it has been very difficult, however I have been starting to get the hang of containers. Everything with them is almost done expect for the annoying fact that my larger container has slot/stack transfer issues, and I can't seem to pinpoint why. All my code for my larger container came off of the smaller one, but I changed all the values to fit the size. But Whenever I try to transfer items via shiftclicking OR just plain drag-n-drop one item is ejected into the world and the stack size I click goes down by 1. My code is below Below in this spoiler is the smaller container that works, it has 36 slots per inventory BlockSmallChest.java package mod_Rareores; import java.util.Random; import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.creativetab.CreativeTabs; 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.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; public class BlockSmallChest extends BlockContainer { public BlockSmallChest (int id) { super(id, Material.wood); setCreativeTab(CreativeTabs.tabDecorations); } @Override public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int idk, float what, float these, float are) { TileEntity tileEntity = world.getBlockTileEntity(x, y, z); if (tileEntity == null || player.isSneaking()) { return false; } player.openGui(mod_rareores.instance, 0, world, x, y, z); return true; } @Override public void breakBlock(World world, int x, int y, int z, int par5, int par6) { dropItems(world, x, y, z); super.breakBlock(world, x, y, z, par5, par6); } private void dropItems(World world, int x, int y, int z){ Random rand = new Random(); TileEntity tileEntity = world.getBlockTileEntity(x, y, z); if (!(tileEntity instanceof IInventory)) { return; } IInventory inventory = (IInventory) tileEntity; for (int i = 0; i < inventory.getSizeInventory(); i++) { ItemStack item = inventory.getStackInSlot(i); if (item != null && item.stackSize > 0) { float rx = rand.nextFloat() * 0.8F + 0.1F; float ry = rand.nextFloat() * 0.8F + 0.1F; float rz = rand.nextFloat() * 0.8F + 0.1F; EntityItem entityItem = new EntityItem(world, x + rx, y + ry, z + rz, new ItemStack(item.itemID, item.stackSize, item.getItemDamage())); if (item.hasTagCompound()) { entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy()); } float factor = 0.05F; entityItem.motionX = rand.nextGaussian() * factor; entityItem.motionY = rand.nextGaussian() * factor + 0.2F; entityItem.motionZ = rand.nextGaussian() * factor; world.spawnEntityInWorld(entityItem); item.stackSize = 0; } } } @Override public TileEntity createNewTileEntity(World world) { return new TileEntitySmallChest(); } } TileEntitySmallChest.java package mod_Rareores; 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; public class TileEntitySmallChest extends TileEntity implements IInventory { private ItemStack[] inventorysize; public TileEntitySmallChest(){ inventorysize = new ItemStack[36]; } @Override public int getSizeInventory() { return inventorysize.length; } @Override public ItemStack getStackInSlot(int slot) { return inventorysize[slot]; } @Override public void setInventorySlotContents(int slot, ItemStack stack) { inventorysize[slot] = stack; if (stack != null && stack.stackSize > getInventoryStackLimit()) { stack.stackSize = getInventoryStackLimit(); } } @Override public ItemStack decrStackSize(int slot, int amt) { ItemStack stack = getStackInSlot(slot); if (stack != null) { if (stack.stackSize <= amt) { setInventorySlotContents(slot, null); } else { stack = stack.splitStack(amt); if (stack.stackSize == 0) { setInventorySlotContents(slot, null); } } } return stack; } @Override public ItemStack getStackInSlotOnClosing(int slot) { ItemStack stack = getStackInSlot(slot); if (stack != null) { setInventorySlotContents(slot, null); } return stack; } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer player) { return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64; } @Override public void openChest() {} @Override public void closeChest() {} @Override public void readFromNBT(NBTTagCompound tagCompound) { super.readFromNBT(tagCompound); NBTTagList tagList = tagCompound.getTagList("Inventory"); for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i); byte slot = tag.getByte("Slot"); if (slot >= 0 && slot < inventorysize.length) { inventorysize[slot] = ItemStack.loadItemStackFromNBT(tag); } } } @Override public void writeToNBT(NBTTagCompound tagCompound) { super.writeToNBT(tagCompound); NBTTagList itemList = new NBTTagList(); for (int i = 0; i < inventorysize.length; i++) { ItemStack stack = inventorysize[i]; if (stack != null) { NBTTagCompound tag = new NBTTagCompound(); tag.setByte("Slot", (byte) i); stack.writeToNBT(tag); itemList.appendTag(tag); } } tagCompound.setTag("Inventory", itemList); } @Override public String getInvName() { return "mod_rareores.TileEntitySmallChest"; } @Override public boolean isInvNameLocalized() { // TODO Auto-generated method stub return false; } @Override public boolean isStackValidForSlot(int i, ItemStack itemstack) { // TODO Auto-generated method stub return false; } } SmallChestContainer.java package mod_Rareores; 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 SmallChestContainer extends Container { protected TileEntitySmallChest tileEntity; public SmallChestContainer (InventoryPlayer inventoryPlayer, TileEntitySmallChest TileEntity) { tileEntity = TileEntity; for (int i = 0; i < 4; i++) { for (int j = 0; j < 9; j++) { addSlotToContainer(new Slot(tileEntity, j + i * 9, 8 + j * 18, 10 + i * 18)); } } bindPlayerInventory(inventoryPlayer); } @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, 96 + i * 18)); } } for (int i = 0; i < 9; i++) { addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 154)); } } @Override public ItemStack transferStackInSlot(EntityPlayer player, int slotnumber) { ItemStack stack = null; Slot slot = (Slot) inventorySlots.get(slotnumber); if (slot != null && slot.getHasStack()) { ItemStack stackInSlot = slot.getStack(); stack = stackInSlot.copy(); if (slotnumber < 36) { if (!this.mergeItemStack(stackInSlot, 36, 45, true)) { return null; } } else if (!this.mergeItemStack(stackInSlot, 0, 36, false)) { return null; } if (stackInSlot.stackSize == 0) { slot.putStack(null); } else { slot.onSlotChanged(); } if (stackInSlot.stackSize == stack.stackSize) { return null; } slot.onPickupFromSlot(player, stackInSlot); } return stack; } } Below in this spoiler is the container that is larger and does not work BlockLArgeChest.java package mod_Rareores; import java.util.Random; import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.creativetab.CreativeTabs; 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.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; public class BlockLargeChest extends BlockContainer { public BlockLargeChest (int id) { super(id, Material.iron); setCreativeTab(CreativeTabs.tabDecorations); } @Override public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int a, float b, float c, float d) { TileEntity tileEntity = world.getBlockTileEntity(x, y, z); if (tileEntity == null || player.isSneaking()) { return false; } player.openGui(mod_rareores.instance, 1, world, x, y, z); return true; } @Override public void breakBlock(World world, int x, int y, int z, int par5, int par6) { dropItems(world, x, y, z); super.breakBlock(world, x, y, z, par5, par6); } private void dropItems(World world, int x, int y, int z){ Random rand = new Random(); TileEntity tileEntity = world.getBlockTileEntity(x, y, z); if (!(tileEntity instanceof IInventory)) { return; } IInventory inventory = (IInventory) tileEntity; for (int i = 0; i < inventory.getSizeInventory(); i++) { ItemStack item = inventory.getStackInSlot(i); if (item != null && item.stackSize > 0) { float rx = rand.nextFloat() * 0.8F + 0.1F; float ry = rand.nextFloat() * 0.8F + 0.1F; float rz = rand.nextFloat() * 0.8F + 0.1F; EntityItem entityItem = new EntityItem(world, x + rx, y + ry, z + rz, new ItemStack(item.itemID, item.stackSize, item.getItemDamage())); if (item.hasTagCompound()) { entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy()); } float factor = 0.05F; entityItem.motionX = rand.nextGaussian() * factor; entityItem.motionY = rand.nextGaussian() * factor + 0.2F; entityItem.motionZ = rand.nextGaussian() * factor; world.spawnEntityInWorld(entityItem); item.stackSize = 0; } } } @Override public TileEntity createNewTileEntity(World world) { return new TileEntityLargeChest(); } } TileEntityLargeChest.java package mod_Rareores; 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; public class TileEntityLargeChest extends TileEntity implements IInventory { private ItemStack[] inventorysize; public TileEntityLargeChest(){ inventorysize = new ItemStack[140]; } @Override public int getSizeInventory() { return inventorysize.length; } @Override public ItemStack getStackInSlot(int slot) { return inventorysize[slot]; } @Override public void setInventorySlotContents(int slot, ItemStack stack) { inventorysize[slot] = stack; if (stack != null && stack.stackSize > getInventoryStackLimit()) { stack.stackSize = getInventoryStackLimit(); } } @Override public ItemStack decrStackSize(int slot, int amt) { ItemStack stack = getStackInSlot(slot); if (stack != null) { if (stack.stackSize <= amt) { setInventorySlotContents(slot, null); } else { stack = stack.splitStack(amt); if (stack.stackSize == 0) { setInventorySlotContents(slot, null); } } } return stack; } @Override public ItemStack getStackInSlotOnClosing(int slot) { ItemStack stack = getStackInSlot(slot); if (stack != null) { setInventorySlotContents(slot, null); } return stack; } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer player) { return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64; } @Override public void openChest() {} @Override public void closeChest() {} @Override public void readFromNBT(NBTTagCompound tagCompound) { super.readFromNBT(tagCompound); NBTTagList tagList = tagCompound.getTagList("Inventory"); for (int i = 0; i < tagList.tagCount(); i++) { NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i); byte slot = tag.getByte("Slot"); if (slot >= 0 && slot < inventorysize.length) { inventorysize[slot] = ItemStack.loadItemStackFromNBT(tag); } } } @Override public void writeToNBT(NBTTagCompound tagCompound) { super.writeToNBT(tagCompound); NBTTagList itemList = new NBTTagList(); for (int i = 0; i < inventorysize.length; i++) { ItemStack stack = inventorysize[i]; if (stack != null) { NBTTagCompound tag = new NBTTagCompound(); tag.setByte("Slot", (byte) i); stack.writeToNBT(tag); itemList.appendTag(tag); } } tagCompound.setTag("Inventory", itemList); } @Override public String getInvName() { return "mod_rareores.TileEntityLargeChest"; } @Override public boolean isInvNameLocalized() { // TODO Auto-generated method stub return false; } @Override public boolean isStackValidForSlot(int i, ItemStack itemstack) { // TODO Auto-generated method stub return false; } } LargeChestContainer.java package mod_Rareores; 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 LargeChestContainer extends Container { protected TileEntityLargeChest tileEntity; public LargeChestContainer (InventoryPlayer PlayerInventory, TileEntityLargeChest te){ tileEntity = te; for (int i = 0; i < 10; i++) { for (int j = 0; j < 14; j++) { addSlotToContainer(new Slot(tileEntity, j + i * 14, -54 + 10 + j * 18, -54 + 12 + i * 18)); } } bindPlayerInventory(PlayerInventory); } @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, 1 + j * 18, 139 + i * 18)); } } for (int i = 0; i < 9; i++) { addSlotToContainer(new Slot(inventoryPlayer, i, 1 + i * 18, 193)); } } @Override public ItemStack transferStackInSlot(EntityPlayer player, int slotnumber) { ItemStack slotstack = null; Slot slot = (Slot) inventorySlots.get(slotnumber); if (slot != null && slot.getHasStack()) { ItemStack stackInSlot = slot.getStack(); slotstack = stackInSlot.copy(); if (slotnumber < 140) { if (!this.mergeItemStack(stackInSlot, 140, this.inventorySlots.size() , true)) { return null; } } //places it into the tileEntity is possible since its in the player inventory else if (!this.mergeItemStack(stackInSlot, 0, 140, false)) { return null; } if (stackInSlot.stackSize == 0) { slot.putStack(null); } else { slot.onSlotChanged(); } if (stackInSlot.stackSize == slotstack.stackSize) { return null; } slot.onPickupFromSlot(player, stackInSlot); } return slotstack; } } I really hope someone can figure this out, I've just about had it... This is the creator of the Rareores mod! Be sure to check it out at
June 10, 201312 yr Author I'm not sure if the problem is in the container, or the tileentity, btw. I've looked at the container 100 times now and couldn't find anything. This is the creator of the Rareores mod! Be sure to check it out at
June 10, 201312 yr Author Never mind, it was a GUI problem, completely unrelated to the container. I just needed to set the X and Y size in the constructor. This is the creator of the Rareores mod! Be sure to check it out at
June 10, 201312 yr QUADRA POST! Sorry I couldn't help; I couldn't find any problems either. BEWARE OF GOD --- Co-author of Pentachoron Labs' SBFP Tech.
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.