equivalent-exchange-3/src/main/java/com/pahimar/ee3/tileentity/TileAlchemicalChest.java
2014-01-05 12:16:48 -05:00

316 lines
8.4 KiB
Java

package com.pahimar.ee3.tileentity;
import com.pahimar.ee3.block.ModBlocks;
import com.pahimar.ee3.helper.LogHelper;
import com.pahimar.ee3.lib.Sounds;
import com.pahimar.ee3.lib.Strings;
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;
/**
* Equivalent-Exchange-3
* <p/>
* TileAlchemicalChest
*
* @author pahimar
*/
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;
public static final int INVENTORY_SIZE = 13 * 9;
public static final int SMALL_INVENTORY_SIZE = 12 * 4;
public static final int MEDIUM_INVENTORY_SIZE = 12 * 7;
public static final int LARGE_INVENTORY_SIZE = 13 * 9;
/**
* The ItemStacks that hold the items currently being used in the Alchemical Chest
*/
private ItemStack[] inventory;
public TileAlchemicalChest(int metaData)
{
super();
this.state = (byte) metaData;
if (metaData == 0)
{
inventory = new ItemStack[SMALL_INVENTORY_SIZE];
}
else if (metaData == 1)
{
inventory = new ItemStack[MEDIUM_INVENTORY_SIZE];
}
else if (metaData == 2)
{
inventory = new ItemStack[LARGE_INVENTORY_SIZE];
}
}
@Override
public int getSizeInventory()
{
return inventory.length;
}
@Override
public ItemStack getStackInSlot(int slotIndex)
{
LogHelper.debug(inventory.length);
return inventory[slotIndex];
}
@Override
public ItemStack decrStackSize(int slotIndex, int decrementAmount)
{
ItemStack itemStack = getStackInSlot(slotIndex);
if (itemStack != null)
{
if (itemStack.stackSize <= decrementAmount)
{
setInventorySlotContents(slotIndex, null);
}
else
{
itemStack = itemStack.splitStack(decrementAmount);
if (itemStack.stackSize == 0)
{
setInventorySlotContents(slotIndex, null);
}
}
}
return itemStack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slotIndex)
{
if (inventory[slotIndex] != null)
{
ItemStack itemStack = inventory[slotIndex];
inventory[slotIndex] = null;
return itemStack;
}
else
{
return null;
}
}
@Override
public void setInventorySlotContents(int slotIndex, ItemStack itemStack)
{
inventory[slotIndex] = itemStack;
if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit())
{
itemStack.stackSize = this.getInventoryStackLimit();
}
this.onInventoryChanged();
}
@Override
public String getInvName()
{
return this.hasCustomName() ? this.getCustomName() : Strings.CONTAINER_ALCHEMICAL_CHEST_NAME;
}
@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);
}
}
@Override
public void openChest()
{
++numUsingPlayers;
worldObj.addBlockEvent(xCoord, yCoord, zCoord, ModBlocks.alchemicalChest.blockID, 1, numUsingPlayers);
}
@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;
}
}
}
@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 slotIndex = tagCompound.getByte("Slot");
if (slotIndex >= 0 && slotIndex < inventory.length)
{
inventory[slotIndex] = 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 isItemValidForSlot(int slotIndex, ItemStack itemStack)
{
return true;
}
@Override
public String toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(super.toString());
stringBuilder.append("TileAlchemicalChest Data - ");
for (int i = 0; i < inventory.length; i++)
{
if (i != 0)
{
stringBuilder.append(", ");
}
if (inventory[i] != null)
{
stringBuilder.append(String.format("inventory[%d]: %s", i, inventory[i].toString()));
}
else
{
stringBuilder.append(String.format("inventory[%d]: empty", i));
}
}
stringBuilder.append("\n");
return stringBuilder.toString();
}
}