Posted November 19, 201411 yr I been working on a crafting bench that has slots that extends TileEntity. The issue is that even with those the items still get removed when the bench is closed. So I must be missing what makes the items stay/remove from the TileEntity vs. Container. Anyone care to enlighten me?
November 19, 201411 yr Author I provided code in my last thread http://www.minecraftforge.net/forum/index.php/topic,25281.msg128794.html#msg128794 ย before that code I had none to show. Just asking concept designs. ย Although I do admit I failed providing the code this time around. ย Here is the Container ย ย package com.bigbaddevil7.rustic.client.interfaces; import com.bigbaddevil7.rustic.init.ModBlocks; import com.bigbaddevil7.rustic.tileentity.TileEntityTable; import com.bigbaddevil7.rustic.utility.LogHelper; 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.InventoryCraftResult; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.inventory.Slot; import net.minecraft.inventory.SlotCrafting; import net.minecraft.item.ItemStack; import net.minecraft.world.World; public class ContainerTable extends Container { ย ย public InventoryCrafting craftMatrix; ย ย //public static InventoryCrafting toolSlot; ย ย public IInventory craftResult; ย ย private static TileEntityTable toolSlot; ย ย private World worldObj; ย ย private int posX, posY, posZ; ย ย public ContainerTable(InventoryPlayer invPlayer, World world, int x, int y, int z){ ย ย ย ย craftMatrix = new InventoryCrafting(this,5 ,5); ย ย ย ย toolSlot = new TileEntityTable(this, 2, 2); ย ย ย ย craftResult = new InventoryCraftResult(); ย ย ย ย worldObj = world; ย ย ย ย posX = x; ย ย ย ย posY = y; ย ย ย ย posZ = z; ย ย ย ย this.addSlotToContainer(new SlotCrafting(invPlayer.player, craftMatrix, craftResult, 0, 141, 11)); ย ย ย ย for(int i = 0; i < 4; i++){ ย ย ย ย ย ย for(int k = 0; k < 4; k++){ ย ย ย ย ย ย ย ย this.addSlotToContainer(new SlotTool(toolSlot, k + i * 4, 115ย + k * 18, 43 + i * 18)); ย ย ย ย ย ย } ย ย ย ย } ย ย ย ย for(int i = 0; i < 5; i++){ ย ย ย ย ย ย for(int k = 0; k < 5; k++){ ย ย ย ย ย ย ย ย this.addSlotToContainer(new Slot(craftMatrix, k + i * 5, 8 + k * 18, 7 + i * 18)); ย ย ย ย ย ย } ย ย ย ย } ย ย ย ย for(int i = 0; i < 3; i++){ ย ย ย ย ย ย for(int kย = 0; k < 9; k++){ ย ย ย ย ย ย ย ย this.addSlotToContainer(new Slot(invPlayer, k + i * 9 + 9, 8 + k * 18, 106 + i * 18)); ย ย ย ย ย ย } ย ย ย ย } ย ย ย ย for(int i = 0; i < 9; i++){ ย ย ย ย ย ย this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 164)); ย ย ย ย } ย ย ย ย onCraftMatrixChanged(craftMatrix); ย ย } ย ย 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 == 0) ย ย ย ย ย ย { ย ย ย ย ย ย ย ย if (!this.mergeItemStack(itemstack1, 10, 46, true)) ย ย ย ย ย ย ย ย { ย ย ย ย ย ย ย ย ย ย return null; ย ย ย ย ย ย ย ย } ย ย ย ย ย ย ย ย slot.onSlotChange(itemstack1, itemstack); ย ย ย ย ย ย } ย ย ย ย ย ย else if (par2 >= 10 && par2 < 37) ย ย ย ย ย ย { ย ย ย ย ย ย ย ย if (!this.mergeItemStack(itemstack1, 37, 46, false)) ย ย ย ย ย ย ย ย { ย ย ย ย ย ย ย ย ย ย return null; ย ย ย ย ย ย ย ย } ย ย ย ย ย ย } ย ย ย ย ย ย else if (par2 >= 37 && par2 < 46) ย ย ย ย ย ย { ย ย ย ย ย ย ย ย if (!this.mergeItemStack(itemstack1, 10, 37, false)) ย ย ย ย ย ย ย ย { ย ย ย ย ย ย ย ย ย ย return null; ย ย ย ย ย ย ย ย } ย ย ย ย ย ย } ย ย ย ย ย ย else if (!this.mergeItemStack(itemstack1, 10, 46, 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 void onContainerClosed(EntityPlayer par1EntityPlayer) ย ย { ย ย ย ย //super.onContainerClosed(par1EntityPlayer); ย ย ย ย if (!this.worldObj.isRemote) ย ย ย ย { ย ย ย ย ย ย for (int i = 0; i < 25; ++i) ย ย ย ย ย ย { ย ย ย ย ย ย ย ย ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i); ย ย ย ย ย ย ย ย if (itemstack != null) ย ย ย ย ย ย ย ย { ย ย ย ย ย ย ย ย ย ย par1EntityPlayer.dropPlayerItemWithRandomChoice(itemstack, false); ย ย ย ย ย ย ย ย } ย ย ย ย ย ย } ย ย ย ย ย ย /*for(int i = 0; i < 4; i++) { ย ย ย ย ย ย ย ย ItemStack itemStack2 = this.toolSlot.getStackInSlotOnClosing(i); ย ย ย ย ย ย ย ย if (itemStack2 != null) { ย ย ย ย ย ย ย ย ย ย par1EntityPlayer.dropPlayerItemWithRandomChoice(itemStack2, false); ย ย ย ย ย ย ย ย } ย ย ย ย ย ย }*/ ย ย ย ย } ย ย } ย ย public void onCraftMatrixChanged(IInventory inventory){ ย ย ย ย craftResult.setInventorySlotContents(0, CraftingManagerTable.getInstance().findMatchingRecipe(craftMatrix, worldObj)); ย ย } ย ย @Override ย ย public boolean canInteractWith(EntityPlayer player) { ย ย ย ย if(worldObj.getBlock(posX, posY, posZ) != ModBlocks.blockTable){ ย ย ย ย ย ย return false; ย ย ย ย }else{ ย ย ย ย ย ย return player.getDistanceSq((double)posX + 0.5D, (double)posY + 0.5D, (double)posZ + 0.5D) <= 64.0D; ย ย ย ย } ย ย } ย ย public static boolean toolInSlot(){ ย ย ย ย boolean hasItem = true; ย ย ย ย for(int i = 0; i < 4; i++) { ย ย ย ย ย ย ItemStack slot = toolSlot.getStackInSlot(i); ย ย ย ย ย ย if (slot == null) { ย ย ย ย ย ย ย ย hasItem = false; ย ย ย ย ย ย } ย ย ย ย } ย ย ย ย return hasItem; ย ย } } ย ย ย Here is the TileEntity that acts for the slots ย ย package com.bigbaddevil7.rustic.tileentity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; /** * Created by bigbaddevil7 on 11/15/2014. */ public class TileEntityTable extends TileEntity implements IInventory { ย ย private ItemStack[] stackList; ย ย private int inventoryWidth; ย ย private static Container eventHandler; ย ย public TileEntityTable(){}; ย ย public TileEntityTable(Container container, int x, int y){ ย ย ย ย int k = x * y; ย ย ย ย this.stackList = new ItemStack[k]; ย ย ย ย this.eventHandler = container; ย ย ย ย this.inventoryWidth = x; ย ย } ย ย @Override ย ย public int getSizeInventory() { ย ย ย ย return this.stackList.length; ย ย } ย ย @Override ย ย public ItemStack getStackInSlot(int slot) { ย ย ย ย return slot >= this.getSizeInventory() ? null : this.stackList[slot]; ย ย } ย ย /** ย ย * Returns the itemstack in the slot specified (Top left is 0, 0). Args: row, column ย ย */ ย ย public ItemStack getStackInRowAndColumn(int par1, int par2) ย ย { ย ย ย ย if (par1 >= 0 && par1 < this.inventoryWidth) ย ย ย ย { ย ย ย ย ย ย int k = par1 + par2 * this.inventoryWidth; ย ย ย ย ย ย return this.getStackInSlot(k); ย ย ย ย } ย ย ย ย else ย ย ย ย { ย ย ย ย ย ย return null; ย ย ย ย } ย ย } ย ย @Override ย ย public ItemStack decrStackSize(int par1, int par2) ย ย { ย ย ย ย if (this.stackList[par1] != null) ย ย ย ย { ย ย ย ย ย ย ItemStack itemstack; ย ย ย ย ย ย if (this.stackList[par1].stackSize <= par2) ย ย ย ย ย ย { ย ย ย ย ย ย ย ย itemstack = this.stackList[par1]; ย ย ย ย ย ย ย ย this.stackList[par1] = null; ย ย ย ย ย ย ย ย this.eventHandler.onCraftMatrixChanged(this); ย ย ย ย ย ย ย ย return itemstack; ย ย ย ย ย ย } ย ย ย ย ย ย else ย ย ย ย ย ย { ย ย ย ย ย ย ย ย itemstack = this.stackList[par1].splitStack(par2); ย ย ย ย ย ย ย ย if (this.stackList[par1].stackSize == 0) ย ย ย ย ย ย ย ย { ย ย ย ย ย ย ย ย ย ย this.stackList[par1] = null; ย ย ย ย ย ย ย ย } ย ย ย ย ย ย ย ย this.eventHandler.onCraftMatrixChanged(this); ย ย ย ย ย ย ย ย return itemstack; ย ย ย ย ย ย } ย ย ย ย } ย ย ย ย else ย ย ย ย { ย ย ย ย ย ย return null; ย ย ย ย } ย ย } ย ย @Override ย ย public ItemStack getStackInSlotOnClosing(int par1) { ย ย ย ย if (this.stackList[par1] != null) ย ย ย ย { ย ย ย ย ย ย ItemStack itemstack = this.stackList[par1]; ย ย ย ย ย ย this.stackList[par1] = null; ย ย ย ย ย ย return itemstack; ย ย ย ย } ย ย ย ย else ย ย ย ย { ย ย ย ย ย ย return null; ย ย ย ย } ย ย } ย ย @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", 10); ย ย ย ย for(int i = 0; i < items.tagCount(); i++){ ย ย ย ย ย ย NBTTagCompound item = (NBTTagCompound)items.getCompoundTagAt(i); ย ย ย ย ย ย int slot = item.getByte("Slot"); ย ย ย ย ย ย if(slot >= 0 && slot <getSizeInventory()){ ย ย ย ย ย ย ย ย setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item)); ย ย ย ย ย ย } ย ย ย ย } ย ย } ย ย @Override ย ย public void setInventorySlotContents(int par1, ItemStack itemStack) { ย ย ย ย this.stackList[par1] = itemStack; ย ย ย ย this.eventHandler.onCraftMatrixChanged(this); ย ย } ย ย @Override ย ย public String getInventoryName() { ย ย ย ย return "container.prototyping table"; ย ย } ย ย @Override ย ย public boolean hasCustomInventoryName() { ย ย ย ย return false; ย ย } ย ย @Override ย ย public int getInventoryStackLimit() { ย ย ย ย return 64; ย ย } ย ย @Override ย ย public boolean isUseableByPlayer(EntityPlayer var1) { ย ย ย ย return true; ย ย } ย ย @Override ย ย public void openInventory() {} ย ย @Override ย ย public void closeInventory() {} ย ย @Override ย ย public boolean isItemValidForSlot(int var1, ItemStack var2) { ย ย ย ย return false; ย ย } } [/spoiler] ย
November 20, 201411 yr Author Oh so I would get the world for it in the GuiHandler. Like how you would for a furnace or machine.
November 20, 201411 yr Author sorry... just throwing me off cause its only partially acts as a tile entity. ย
November 20, 201411 yr Author The overall block has only 4 slots out of 30 that are TileEntity. When the block was originally made it acted like a normal 5X5 crafting table that spewed out the items. Now I have 4 extra slots that are a TileEntity
November 20, 201411 yr Author So I take it the method I was trying wont work then. Hmmm two inventories, that sound tricky. ย
November 20, 201411 yr Author if you know what you are doing. ย Yea... sounds tricky, although I think I may have an understanding of what you mean by it, would it be something along the lines of making two methods in the Container called buildContainer() and buildTileEntity() to handle the different types and making of different slots then call them accordingly?
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.