package com.mrcrayfish.furniture.tileentity; import javax.annotation.Nullable; import com.mrcrayfish.furniture.Reference; import net.minecraft.client.resources.I18n; 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.ItemStackHelper; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.NetworkManager; import net.minecraft.network.play.server.SPacketUpdateTileEntity; import net.minecraft.tileentity.TileEntityLockable; import net.minecraft.util.NonNullList; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentTranslation; public abstract class TileEntityFurniture extends TileEntityLockable implements IInventory { protected NonNullList<ItemStack> inventory; private final String ID; private final int INVENTORY_SIZE; protected String customName; public TileEntityFurniture(String id, int inventorySize) { this.ID = id; this.INVENTORY_SIZE = inventorySize; this.inventory = NonNullList.<ItemStack>withSize(inventorySize, ItemStack.EMPTY); } @Override public String getName() { return hasCustomName() ? this.customName : "container." + ID; } @Override public boolean hasCustomName() { return this.customName != null && !this.customName.isEmpty(); } public void setCustomName(String customName) { this.customName = customName; } @Override public ITextComponent getDisplayName() { return (ITextComponent) (this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName(), new Object[0])); } @Override public String getGuiID() { return Reference.MOD_ID + ":" + ID; } @Override public int getSizeInventory() { return INVENTORY_SIZE; } @Override public boolean isEmpty() { for(ItemStack itemstack : this.inventory) { if(!itemstack.isEmpty()) { return false; } } return true; } @Override public ItemStack getStackInSlot(int index) { return (ItemStack) this.inventory.get(index); } @Override public ItemStack decrStackSize(int index, int count) { ItemStack itemstack = ItemStackHelper.getAndSplit(this.inventory, index, count); if (!itemstack.isEmpty()) { this.markDirty(); } return itemstack; } @Override public ItemStack removeStackFromSlot(int index) { return ItemStackHelper.getAndRemove(this.inventory, index); } @Override public void setInventorySlotContents(int index, @Nullable ItemStack stack) { this.inventory.set(index, stack); if (stack.getCount() > this.getInventoryStackLimit()) { stack.setCount(this.getInventoryStackLimit()); } this.markDirty(); } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUsableByPlayer(EntityPlayer player) { return this.world.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D, (double) this.pos.getZ() + 0.5D) <= 64.0D; } @Override public void openInventory(EntityPlayer player) {} @Override public void closeInventory(EntityPlayer player) {} @Override public boolean isItemValidForSlot(int index, ItemStack stack) { return true; } @Override public int getField(int id) { return 0; } @Override public void setField(int id, int value) {} @Override public int getFieldCount() { return 0; } @Override public void clear() { this.inventory.clear(); } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); this.inventory = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY); ItemStackHelper.loadAllItems(compound, this.inventory); if (compound.hasKey("CustomName", 8)) { this.customName = compound.getString("CustomName"); } } @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); ItemStackHelper.saveAllItems(compound, this.inventory); if (this.hasCustomName()) { compound.setString("CustomName", this.customName); } return compound; } @Override public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) { this.readFromNBT(pkt.getNbtCompound()); } @Override public SPacketUpdateTileEntity getUpdatePacket() { return new SPacketUpdateTileEntity(pos, getBlockMetadata(), this.writeToNBT(new NBTTagCompound())); } @Override public NBTTagCompound getUpdateTag() { return this.writeToNBT(new NBTTagCompound()); } }