2013-02-19 02:13:24 +01:00
|
|
|
package com.pahimar.ee3.tileentity;
|
|
|
|
|
2013-02-25 22:03:45 +01:00
|
|
|
import net.minecraft.block.Block;
|
2013-02-19 02:13:24 +01:00
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
|
2013-02-25 22:03:45 +01:00
|
|
|
import com.pahimar.ee3.block.ModBlocks;
|
2013-02-26 04:00:57 +01:00
|
|
|
import com.pahimar.ee3.lib.Sounds;
|
2013-02-19 02:13:24 +01:00
|
|
|
import com.pahimar.ee3.lib.Strings;
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
/**
|
|
|
|
* Equivalent-Exchange-3
|
|
|
|
*
|
|
|
|
* TileAlchemicalChest
|
|
|
|
*
|
|
|
|
* @author pahimar
|
|
|
|
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
|
|
|
|
*
|
|
|
|
*/
|
2013-02-19 02:13:24 +01:00
|
|
|
public class TileAlchemicalChest extends TileEE implements IInventory {
|
|
|
|
|
2013-02-25 06:33:21 +01:00
|
|
|
/** The current angle of the chest lid (between 0 and 1) */
|
|
|
|
public float lidAngle;
|
|
|
|
|
|
|
|
/** The angle of the chest lid last tick */
|
|
|
|
public float prevLidAngle;
|
2013-02-26 04:00:57 +01:00
|
|
|
|
2013-02-25 22:03:45 +01:00
|
|
|
/** The number of players currently using this chest */
|
|
|
|
public int numUsingPlayers;
|
|
|
|
|
|
|
|
/** Server sync counter (once per 20 ticks) */
|
|
|
|
private int ticksSinceSync;
|
2013-03-08 19:40:59 +01:00
|
|
|
|
2013-02-26 22:08:43 +01:00
|
|
|
private final int INVENTORY_SIZE = 13 * 4;
|
2013-02-26 04:00:57 +01:00
|
|
|
|
2013-02-19 02:13:24 +01:00
|
|
|
/**
|
|
|
|
* The ItemStacks that hold the items currently being used in the Alchemical
|
|
|
|
* Chest
|
|
|
|
*/
|
|
|
|
private ItemStack[] inventory;
|
|
|
|
|
|
|
|
public TileAlchemicalChest() {
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
super();
|
2013-02-26 22:08:43 +01:00
|
|
|
inventory = new ItemStack[INVENTORY_SIZE];
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSizeInventory() {
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
return inventory.length;
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlot(int slot) {
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
return inventory[slot];
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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) {
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
if (inventory[slot] != null) {
|
|
|
|
ItemStack itemStack = inventory[slot];
|
|
|
|
inventory[slot] = null;
|
2013-02-26 22:08:43 +01:00
|
|
|
return itemStack;
|
|
|
|
}
|
2013-03-08 19:40:59 +01:00
|
|
|
else
|
2013-02-26 22:08:43 +01:00
|
|
|
return null;
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInventorySlotContents(int slot, ItemStack itemStack) {
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
inventory[slot] = itemStack;
|
2013-02-26 22:08:43 +01:00
|
|
|
|
|
|
|
if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit()) {
|
|
|
|
itemStack.stackSize = this.getInventoryStackLimit();
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|
2013-02-26 22:08:43 +01:00
|
|
|
|
|
|
|
this.onInventoryChanged();
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getInvName() {
|
|
|
|
|
2013-03-07 03:46:19 +01:00
|
|
|
return this.hasCustomName() ? this.getCustomName() : Strings.CONTAINER_ALCHEMICAL_CHEST_NAME;
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInventoryStackLimit() {
|
|
|
|
|
|
|
|
return 64;
|
|
|
|
}
|
2013-02-26 04:00:57 +01:00
|
|
|
|
2013-02-25 22:03:45 +01:00
|
|
|
/**
|
2013-02-26 04:00:57 +01:00
|
|
|
* Called when a client event is received with the event number and
|
|
|
|
* argument, see World.sendClientEvent
|
2013-02-25 22:03:45 +01:00
|
|
|
*/
|
|
|
|
@Override
|
2013-03-04 17:40:40 +01:00
|
|
|
public boolean receiveClientEvent(int eventID, int numUsingPlayers) {
|
2013-02-26 04:00:57 +01:00
|
|
|
|
|
|
|
if (eventID == 1) {
|
2013-02-25 22:03:45 +01:00
|
|
|
this.numUsingPlayers = numUsingPlayers;
|
2013-03-04 17:40:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-03-08 19:40:59 +01:00
|
|
|
else
|
2013-03-04 17:40:40 +01:00
|
|
|
return super.receiveClientEvent(eventID, numUsingPlayers);
|
2013-02-25 22:03:45 +01:00
|
|
|
}
|
2013-02-19 02:13:24 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void openChest() {
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
++numUsingPlayers;
|
|
|
|
worldObj.addBlockEvent(xCoord, yCoord, zCoord, ModBlocks.alchemicalChest.blockID, 1, numUsingPlayers);
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void closeChest() {
|
2013-02-26 04:00:57 +01:00
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
--numUsingPlayers;
|
|
|
|
worldObj.addBlockEvent(xCoord, yCoord, zCoord, ModBlocks.alchemicalChest.blockID, 1, numUsingPlayers);
|
2013-02-25 22:03:45 +01:00
|
|
|
}
|
2013-02-26 04:00:57 +01:00
|
|
|
|
2013-02-25 22:03:45 +01:00
|
|
|
/**
|
2013-02-26 04:00:57 +01:00
|
|
|
* Allows the entity to update its state. Overridden in most subclasses,
|
|
|
|
* e.g. the mob spawner uses this to count ticks and creates a new spawn
|
|
|
|
* inside its implementation.
|
2013-02-25 22:03:45 +01:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void updateEntity() {
|
2013-02-26 04:00:57 +01:00
|
|
|
|
2013-02-25 22:03:45 +01:00
|
|
|
super.updateEntity();
|
2013-02-26 04:00:57 +01:00
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
if (++ticksSinceSync % 20 * 4 == 0) {
|
|
|
|
worldObj.addBlockEvent(xCoord, yCoord, zCoord, Block.enderChest.blockID, 1, numUsingPlayers);
|
2013-02-26 04:00:57 +01:00
|
|
|
}
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
prevLidAngle = lidAngle;
|
2013-02-26 04:00:57 +01:00
|
|
|
float angleIncrement = 0.1F;
|
|
|
|
double adjustedXCoord, adjustedZCoord;
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
if (numUsingPlayers > 0 && lidAngle == 0.0F) {
|
|
|
|
adjustedXCoord = xCoord + 0.5D;
|
|
|
|
adjustedZCoord = zCoord + 0.5D;
|
|
|
|
worldObj.playSoundEffect(adjustedXCoord, yCoord + 0.5D, adjustedZCoord, Sounds.CHEST_OPEN, 0.5F, worldObj.rand.nextFloat() * 0.1F + 0.9F);
|
2013-02-26 04:00:57 +01:00
|
|
|
}
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
if (numUsingPlayers == 0 && lidAngle > 0.0F || numUsingPlayers > 0 && lidAngle < 1.0F) {
|
|
|
|
float var8 = lidAngle;
|
2013-02-26 04:00:57 +01:00
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
if (numUsingPlayers > 0) {
|
|
|
|
lidAngle += angleIncrement;
|
2013-02-26 04:00:57 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-03-08 19:40:59 +01:00
|
|
|
lidAngle -= angleIncrement;
|
2013-02-26 04:00:57 +01:00
|
|
|
}
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
if (lidAngle > 1.0F) {
|
|
|
|
lidAngle = 1.0F;
|
2013-02-26 04:00:57 +01:00
|
|
|
}
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
if (lidAngle < 0.5F && var8 >= 0.5F) {
|
|
|
|
adjustedXCoord = xCoord + 0.5D;
|
|
|
|
adjustedZCoord = zCoord + 0.5D;
|
|
|
|
worldObj.playSoundEffect(adjustedXCoord, yCoord + 0.5D, adjustedZCoord, Sounds.CHEST_CLOSE, 0.5F, worldObj.rand.nextFloat() * 0.1F + 0.9F);
|
2013-02-26 04:00:57 +01:00
|
|
|
}
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
if (lidAngle < 0.0F) {
|
|
|
|
lidAngle = 0.0F;
|
2013-02-26 04:00:57 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
@Override
|
2013-02-19 02:13:24 +01:00
|
|
|
public void readFromNBT(NBTTagCompound nbtTagCompound) {
|
|
|
|
|
|
|
|
super.readFromNBT(nbtTagCompound);
|
|
|
|
|
|
|
|
// Read in the ItemStacks in the inventory from NBT
|
|
|
|
NBTTagList tagList = nbtTagCompound.getTagList("Items");
|
2013-03-08 19:40:59 +01:00
|
|
|
inventory = new ItemStack[this.getSizeInventory()];
|
2013-02-19 02:13:24 +01:00
|
|
|
for (int i = 0; i < tagList.tagCount(); ++i) {
|
|
|
|
NBTTagCompound tagCompound = (NBTTagCompound) tagList.tagAt(i);
|
|
|
|
byte slot = tagCompound.getByte("Slot");
|
2013-03-08 19:40:59 +01:00
|
|
|
if (slot >= 0 && slot < inventory.length) {
|
|
|
|
inventory[slot] = ItemStack.loadItemStackFromNBT(tagCompound);
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-08 19:40:59 +01:00
|
|
|
@Override
|
2013-02-19 02:13:24 +01:00
|
|
|
public void writeToNBT(NBTTagCompound nbtTagCompound) {
|
|
|
|
|
|
|
|
super.writeToNBT(nbtTagCompound);
|
|
|
|
|
|
|
|
// Write the ItemStacks in the inventory to NBT
|
|
|
|
NBTTagList tagList = new NBTTagList();
|
2013-03-08 19:40:59 +01:00
|
|
|
for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex) {
|
|
|
|
if (inventory[currentIndex] != null) {
|
2013-02-19 02:13:24 +01:00
|
|
|
NBTTagCompound tagCompound = new NBTTagCompound();
|
|
|
|
tagCompound.setByte("Slot", (byte) currentIndex);
|
2013-03-08 19:40:59 +01:00
|
|
|
inventory[currentIndex].writeToNBT(tagCompound);
|
2013-02-19 02:13:24 +01:00
|
|
|
tagList.appendTag(tagCompound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nbtTagCompound.setTag("Items", tagList);
|
2013-03-08 19:40:59 +01:00
|
|
|
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|
2013-03-04 17:40:40 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
// public boolean hasCustomName()
|
|
|
|
public boolean func_94042_c() {
|
|
|
|
|
2013-03-07 03:46:19 +01:00
|
|
|
return this.hasCustomName();
|
2013-03-04 17:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
// public boolean canInsertSide(int i, ItemStack itemStack)
|
|
|
|
public boolean func_94041_b(int side, ItemStack itemStack) {
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-19 02:13:24 +01:00
|
|
|
}
|