2014-04-30 03:46:59 +02:00
|
|
|
package com.pahimar.ee3.tileentity;
|
|
|
|
|
|
|
|
import com.pahimar.ee3.network.PacketHandler;
|
|
|
|
import com.pahimar.ee3.network.message.MessageTileEntityGlassBell;
|
|
|
|
import com.pahimar.ee3.reference.Names;
|
|
|
|
import cpw.mods.fml.common.network.NetworkRegistry;
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
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.network.Packet;
|
|
|
|
|
2014-07-03 21:54:14 +02:00
|
|
|
public class TileEntityGlassBell extends TileEntityEE implements IInventory {
|
2014-04-30 03:46:59 +02:00
|
|
|
public static final int INVENTORY_SIZE = 1;
|
|
|
|
public static final int DISPLAY_SLOT_INVENTORY_INDEX = 0;
|
|
|
|
public ItemStack outputItemStack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ItemStacks that hold the items currently being used in the Glass Bell
|
|
|
|
*/
|
|
|
|
private ItemStack[] inventory;
|
|
|
|
|
2014-07-03 21:54:14 +02:00
|
|
|
public TileEntityGlassBell() {
|
2014-04-30 03:46:59 +02:00
|
|
|
inventory = new ItemStack[INVENTORY_SIZE];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public int getSizeInventory() {
|
2014-04-30 03:46:59 +02:00
|
|
|
return inventory.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public ItemStack getStackInSlot(int slotIndex) {
|
2014-04-30 03:46:59 +02:00
|
|
|
return inventory[slotIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public ItemStack decrStackSize(int slotIndex, int decrementAmount) {
|
2014-04-30 03:46:59 +02:00
|
|
|
ItemStack itemStack = getStackInSlot(slotIndex);
|
2014-07-03 21:54:14 +02:00
|
|
|
if (itemStack != null) {
|
|
|
|
if (itemStack.stackSize <= decrementAmount) {
|
2014-04-30 03:46:59 +02:00
|
|
|
setInventorySlotContents(slotIndex, null);
|
2014-07-03 21:54:14 +02:00
|
|
|
} else {
|
2014-04-30 03:46:59 +02:00
|
|
|
itemStack = itemStack.splitStack(decrementAmount);
|
2014-07-03 21:54:14 +02:00
|
|
|
if (itemStack.stackSize == 0) {
|
2014-04-30 03:46:59 +02:00
|
|
|
setInventorySlotContents(slotIndex, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public ItemStack getStackInSlotOnClosing(int slotIndex) {
|
2014-04-30 03:46:59 +02:00
|
|
|
ItemStack itemStack = getStackInSlot(slotIndex);
|
2014-07-03 21:54:14 +02:00
|
|
|
if (itemStack != null) {
|
2014-04-30 03:46:59 +02:00
|
|
|
setInventorySlotContents(slotIndex, null);
|
|
|
|
}
|
|
|
|
return itemStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public void setInventorySlotContents(int slotIndex, ItemStack itemStack) {
|
2014-04-30 03:46:59 +02:00
|
|
|
inventory[slotIndex] = itemStack;
|
|
|
|
|
2014-07-03 21:54:14 +02:00
|
|
|
if (itemStack != null && itemStack.stackSize > getInventoryStackLimit()) {
|
2014-04-30 03:46:59 +02:00
|
|
|
itemStack.stackSize = getInventoryStackLimit();
|
|
|
|
}
|
|
|
|
|
2014-07-03 21:54:14 +02:00
|
|
|
if (!this.worldObj.isRemote) {
|
2014-04-30 03:46:59 +02:00
|
|
|
ItemStack displayStack = this.inventory[DISPLAY_SLOT_INVENTORY_INDEX];
|
|
|
|
|
2014-07-03 21:54:14 +02:00
|
|
|
if (displayStack != null) {
|
2014-04-30 03:46:59 +02:00
|
|
|
this.state = (byte) Block.getBlockFromItem(displayStack.getItem()).getLightValue();
|
2014-07-03 21:54:14 +02:00
|
|
|
} else {
|
2014-04-30 03:46:59 +02:00
|
|
|
this.state = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PacketHandler.INSTANCE.sendToAllAround(new MessageTileEntityGlassBell(this, displayStack), new NetworkRegistry.TargetPoint(this.worldObj.provider.dimensionId, this.xCoord, this.yCoord, this.zCoord, 128d));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.markDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public String getInventoryName() {
|
2014-04-30 03:46:59 +02:00
|
|
|
return this.hasCustomName() ? this.getCustomName() : Names.Containers.GLASS_BELL;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public boolean hasCustomInventoryName() {
|
|
|
|
return this.hasCustomName();
|
2014-04-30 03:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public int getInventoryStackLimit() {
|
2014-04-30 03:46:59 +02:00
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public boolean isUseableByPlayer(EntityPlayer entityPlayer) {
|
2014-04-30 03:46:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public void openInventory() {
|
2014-04-30 03:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public void closeInventory() {
|
2014-04-30 03:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public boolean isItemValidForSlot(int slotIndex, ItemStack itemStack) {
|
2014-04-30 03:46:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public void readFromNBT(NBTTagCompound nbtTagCompound) {
|
2014-04-30 03:46:59 +02:00
|
|
|
super.readFromNBT(nbtTagCompound);
|
|
|
|
|
|
|
|
// Read in the ItemStacks in the inventory from NBT
|
|
|
|
NBTTagList tagList = nbtTagCompound.getTagList("Items", 10);
|
|
|
|
inventory = new ItemStack[this.getSizeInventory()];
|
2014-07-03 21:54:14 +02:00
|
|
|
for (int i = 0; i < tagList.tagCount(); ++i) {
|
2014-04-30 03:46:59 +02:00
|
|
|
NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
|
|
|
|
byte slotIndex = tagCompound.getByte("Slot");
|
2014-07-03 21:54:14 +02:00
|
|
|
if (slotIndex >= 0 && slotIndex < inventory.length) {
|
2014-04-30 03:46:59 +02:00
|
|
|
inventory[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public void writeToNBT(NBTTagCompound nbtTagCompound) {
|
2014-04-30 03:46:59 +02:00
|
|
|
super.writeToNBT(nbtTagCompound);
|
|
|
|
|
|
|
|
// Write the ItemStacks in the inventory to NBT
|
|
|
|
NBTTagList tagList = new NBTTagList();
|
2014-07-03 21:54:14 +02:00
|
|
|
for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex) {
|
|
|
|
if (inventory[currentIndex] != null) {
|
2014-04-30 03:46:59 +02:00
|
|
|
NBTTagCompound tagCompound = new NBTTagCompound();
|
|
|
|
tagCompound.setByte("Slot", (byte) currentIndex);
|
|
|
|
inventory[currentIndex].writeToNBT(tagCompound);
|
|
|
|
tagList.appendTag(tagCompound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nbtTagCompound.setTag("Items", tagList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-03 21:54:14 +02:00
|
|
|
public Packet getDescriptionPacket() {
|
|
|
|
if (getStackInSlot(DISPLAY_SLOT_INVENTORY_INDEX) != null && getStackInSlot(DISPLAY_SLOT_INVENTORY_INDEX).stackSize > 0) {
|
2014-04-30 03:46:59 +02:00
|
|
|
return PacketHandler.INSTANCE.getPacketFrom(new MessageTileEntityGlassBell(this, getStackInSlot(DISPLAY_SLOT_INVENTORY_INDEX)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return PacketHandler.INSTANCE.getPacketFrom(new MessageTileEntityGlassBell(this, null));
|
|
|
|
}
|
|
|
|
}
|