package com.pahimar.ee3.tileentity; import net.minecraft.block.Block; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.network.packet.Packet; import com.pahimar.ee3.core.helper.ItemHelper; import com.pahimar.ee3.lib.Strings; import com.pahimar.ee3.network.PacketTypeHandler; import com.pahimar.ee3.network.packet.PacketTileWithItemUpdate; public class TileGlassBell extends TileEE implements IInventory { /** * The ItemStacks that hold the items currently being used in the Glass Bell */ private ItemStack[] inventory; private final int INVENTORY_SIZE = 1; public static final int DISPLAY_SLOT_INVENTORY_INDEX = 0; public TileGlassBell() { inventory = new ItemStack[INVENTORY_SIZE]; } @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 itemStack = getStackInSlot(slot); if (itemStack != null) { if (itemStack.stackSize <= amount) { setInventorySlotContents(slot, null); } else { itemStack = itemStack.splitStack(amount); if (itemStack.stackSize == 0) { setInventorySlotContents(slot, null); } } } return itemStack; } @Override public ItemStack getStackInSlotOnClosing(int slot) { ItemStack itemStack = getStackInSlot(slot); if (itemStack != null) { setInventorySlotContents(slot, null); } return itemStack; } @Override public void setInventorySlotContents(int slot, ItemStack itemStack) { inventory[slot] = itemStack; if (itemStack != null && itemStack.stackSize > getInventoryStackLimit()) { itemStack.stackSize = getInventoryStackLimit(); } } @Override public String getInvName() { return this.hasCustomName() ? this.getCustomName() : Strings.CONTAINER_GLASS_BELL_NAME; } @Override public int getInventoryStackLimit() { return 64; } @Override public void openChest() { } @Override public void closeChest() { } @Override public void readFromNBT(NBTTagCompound nbtTagCompound) { super.readFromNBT(nbtTagCompound); // Read in the ItemStacks in the inventory from NBT NBTTagList tagList = nbtTagCompound.getTagList("Items"); inventory = new ItemStack[this.getSizeInventory()]; for (int i = 0; i < tagList.tagCount(); ++i) { NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(i); byte slot = tagCompound.getByte("Slot"); if (slot >= 0 && slot < inventory.length) { inventory[slot] = ItemStack.loadItemStackFromNBT(tagCompound); } } } @Override public void writeToNBT(NBTTagCompound nbtTagCompound) { super.writeToNBT(nbtTagCompound); // Write the ItemStacks in the inventory to NBT NBTTagList tagList = new NBTTagList(); for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex) { if (inventory[currentIndex] != null) { NBTTagCompound tagCompound = new NBTTagCompound(); tagCompound.setByte("Slot", (byte) currentIndex); inventory[currentIndex].writeToNBT(tagCompound); tagList.appendTag(tagCompound); } } nbtTagCompound.setTag("Items", tagList); } @Override public boolean isInvNameLocalized() { return this.hasCustomName(); } @Override public boolean isStackValidForSlot(int i, ItemStack itemstack) { return true; } @Override public Packet getDescriptionPacket() { ItemStack itemStack = getStackInSlot(DISPLAY_SLOT_INVENTORY_INDEX); if (itemStack != null && itemStack.stackSize > 0) { return PacketTypeHandler.populatePacket(new PacketTileWithItemUpdate(xCoord, yCoord, zCoord, orientation, state, customName, itemStack.itemID, itemStack.getItemDamage(), itemStack.stackSize, ItemHelper.getColor(itemStack))); } else { return super.getDescriptionPacket(); } } @Override public void onInventoryChanged() { ItemStack itemStack = getStackInSlot(DISPLAY_SLOT_INVENTORY_INDEX); if ((itemStack != null) && (itemStack.itemID < 4096)) { worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, Block.lightValue[itemStack.itemID], 2); } else { worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 2); } } }