equivalent-exchange-3/ee3_common/com/pahimar/ee3/tileentity/TileAlchemicalChest.java

244 lines
6.7 KiB
Java
Raw Normal View History

2013-02-19 02:13:24 +01:00
package com.pahimar.ee3.tileentity;
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;
import com.pahimar.ee3.block.ModBlocks;
import com.pahimar.ee3.lib.Sounds;
2013-02-19 02:13:24 +01:00
import com.pahimar.ee3.lib.Strings;
/**
* 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 {
/** 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;
/** The number of players currently using this chest */
public int numUsingPlayers;
/** Server sync counter (once per 20 ticks) */
private int ticksSinceSync;
2013-02-26 22:08:43 +01:00
private final int INVENTORY_SIZE = 13 * 4;
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() {
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() {
return inventory.length;
2013-02-19 02:13:24 +01:00
}
@Override
public ItemStack getStackInSlot(int slot) {
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) {
if (inventory[slot] != null) {
ItemStack itemStack = inventory[slot];
inventory[slot] = null;
2013-02-26 22:08:43 +01:00
return itemStack;
}
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) {
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() {
return this.hasCustomName() ? this.getCustomName() : Strings.CONTAINER_ALCHEMICAL_CHEST_NAME;
2013-02-19 02:13:24 +01:00
}
@Override
public int getInventoryStackLimit() {
return 64;
}
/**
* Called when a client event is received with the event number and
* argument, see World.sendClientEvent
*/
@Override
public boolean receiveClientEvent(int eventID, int numUsingPlayers) {
if (eventID == 1) {
this.numUsingPlayers = numUsingPlayers;
return true;
}
else
return super.receiveClientEvent(eventID, numUsingPlayers);
}
2013-02-19 02:13:24 +01:00
@Override
public void openChest() {
++numUsingPlayers;
worldObj.addBlockEvent(xCoord, yCoord, zCoord, ModBlocks.alchemicalChest.blockID, 1, numUsingPlayers);
2013-02-19 02:13:24 +01:00
}
@Override
public void closeChest() {
--numUsingPlayers;
worldObj.addBlockEvent(xCoord, yCoord, zCoord, ModBlocks.alchemicalChest.blockID, 1, numUsingPlayers);
}
/**
* 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.
*/
@Override
public void updateEntity() {
super.updateEntity();
if (++ticksSinceSync % 20 * 4 == 0) {
worldObj.addBlockEvent(xCoord, yCoord, zCoord, Block.enderChest.blockID, 1, numUsingPlayers);
}
prevLidAngle = lidAngle;
float angleIncrement = 0.1F;
double adjustedXCoord, adjustedZCoord;
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);
}
if (numUsingPlayers == 0 && lidAngle > 0.0F || numUsingPlayers > 0 && lidAngle < 1.0F) {
float var8 = lidAngle;
if (numUsingPlayers > 0) {
lidAngle += angleIncrement;
}
else {
lidAngle -= angleIncrement;
}
if (lidAngle > 1.0F) {
lidAngle = 1.0F;
}
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);
}
if (lidAngle < 0.0F) {
lidAngle = 0.0F;
}
}
2013-02-19 02:13:24 +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");
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");
if (slot >= 0 && slot < inventory.length) {
inventory[slot] = ItemStack.loadItemStackFromNBT(tagCompound);
2013-02-19 02:13:24 +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();
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);
inventory[currentIndex].writeToNBT(tagCompound);
2013-02-19 02:13:24 +01:00
tagList.appendTag(tagCompound);
}
}
nbtTagCompound.setTag("Items", tagList);
2013-02-19 02:13:24 +01:00
}
@Override
public boolean isInvNameLocalized() {
return this.hasCustomName();
}
@Override
public boolean isStackValidForSlot(int side, ItemStack itemStack) {
return true;
}
2013-02-19 02:13:24 +01:00
}