2014-04-11 01:59:04 +02:00
|
|
|
package com.pahimar.ee3.tileentity;
|
|
|
|
|
2014-04-30 04:26:30 +02:00
|
|
|
import com.pahimar.ee3.init.ModBlocks;
|
2014-04-11 01:59:04 +02:00
|
|
|
import com.pahimar.ee3.inventory.ContainerAlchemicalChest;
|
|
|
|
import com.pahimar.ee3.reference.Names;
|
|
|
|
import com.pahimar.ee3.reference.Sounds;
|
|
|
|
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;
|
|
|
|
|
2014-07-04 21:18:10 +02:00
|
|
|
public class TileEntityAlchemicalChest extends TileEntityEE implements IInventory
|
|
|
|
{
|
2014-04-11 01:59:04 +02: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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of players currently using this chest
|
|
|
|
*/
|
|
|
|
public int numUsingPlayers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Server sync counter (once per 20 ticks)
|
|
|
|
*/
|
|
|
|
private int ticksSinceSync;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ItemStacks that hold the items currently being used in the Alchemical Chest
|
|
|
|
*/
|
|
|
|
private ItemStack[] inventory;
|
|
|
|
|
2014-07-04 21:18:10 +02:00
|
|
|
public TileEntityAlchemicalChest(int metaData)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
super();
|
|
|
|
this.state = (byte) metaData;
|
|
|
|
|
2014-07-04 21:18:10 +02:00
|
|
|
if (metaData == 0)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
inventory = new ItemStack[ContainerAlchemicalChest.SMALL_INVENTORY_SIZE];
|
2014-07-04 21:18:10 +02:00
|
|
|
}
|
|
|
|
else if (metaData == 1)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
inventory = new ItemStack[ContainerAlchemicalChest.MEDIUM_INVENTORY_SIZE];
|
2014-07-04 21:18:10 +02:00
|
|
|
}
|
|
|
|
else if (metaData == 2)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
inventory = new ItemStack[ContainerAlchemicalChest.LARGE_INVENTORY_SIZE];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public void readFromNBT(NBTTagCompound nbtTagCompound)
|
|
|
|
{
|
|
|
|
super.readFromNBT(nbtTagCompound);
|
|
|
|
|
|
|
|
// Read in the ItemStacks in the inventory from NBT
|
2014-07-18 18:58:35 +02:00
|
|
|
NBTTagList tagList = nbtTagCompound.getTagList(Names.NBT.ITEMS, 10);
|
2014-07-04 21:18:10 +02:00
|
|
|
inventory = new ItemStack[this.getSizeInventory()];
|
|
|
|
for (int i = 0; i < tagList.tagCount(); ++i)
|
|
|
|
{
|
|
|
|
NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
|
|
|
|
byte slotIndex = tagCompound.getByte("Slot");
|
|
|
|
if (slotIndex >= 0 && slotIndex < inventory.length)
|
|
|
|
{
|
|
|
|
inventory[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSizeInventory()
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
return inventory.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public ItemStack getStackInSlot(int slotIndex)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
return inventory[slotIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public ItemStack decrStackSize(int slotIndex, int decrementAmount)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
ItemStack itemStack = getStackInSlot(slotIndex);
|
2014-07-04 21:18:10 +02:00
|
|
|
if (itemStack != null)
|
|
|
|
{
|
|
|
|
if (itemStack.stackSize <= decrementAmount)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
setInventorySlotContents(slotIndex, null);
|
2014-07-04 21:18:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
itemStack = itemStack.splitStack(decrementAmount);
|
2014-07-04 21:18:10 +02:00
|
|
|
if (itemStack.stackSize == 0)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
setInventorySlotContents(slotIndex, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public ItemStack getStackInSlotOnClosing(int slotIndex)
|
|
|
|
{
|
|
|
|
if (inventory[slotIndex] != null)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
ItemStack itemStack = inventory[slotIndex];
|
|
|
|
inventory[slotIndex] = null;
|
|
|
|
return itemStack;
|
2014-07-04 21:18:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public void setInventorySlotContents(int slotIndex, ItemStack itemStack)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
inventory[slotIndex] = itemStack;
|
|
|
|
|
2014-07-04 21:18:10 +02:00
|
|
|
if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit())
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
itemStack.stackSize = this.getInventoryStackLimit();
|
|
|
|
}
|
|
|
|
|
2014-04-30 03:46:59 +02:00
|
|
|
|
2014-04-11 01:59:04 +02:00
|
|
|
this.markDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public String getInventoryName()
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
return this.hasCustomName() ? this.getCustomName() : Names.Containers.ALCHEMICAL_CHEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public boolean hasCustomInventoryName()
|
|
|
|
{
|
2014-07-03 21:54:14 +02:00
|
|
|
return this.hasCustomName();
|
2014-04-11 01:59:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public int getInventoryStackLimit()
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do not make give this method the name canInteractWith because it clashes with Container
|
|
|
|
*
|
2014-07-03 21:54:14 +02:00
|
|
|
* @param entityplayer The player we are checking to see if they can use this chest
|
2014-04-11 01:59:04 +02:00
|
|
|
*/
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public boolean isUseableByPlayer(EntityPlayer entityplayer)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public void openInventory()
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
++numUsingPlayers;
|
|
|
|
worldObj.addBlockEvent(xCoord, yCoord, zCoord, ModBlocks.alchemicalChest, 1, numUsingPlayers);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public void closeInventory()
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
--numUsingPlayers;
|
|
|
|
worldObj.addBlockEvent(xCoord, yCoord, zCoord, ModBlocks.alchemicalChest, 1, numUsingPlayers);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public boolean isItemValidForSlot(int slotIndex, ItemStack itemStack)
|
|
|
|
{
|
|
|
|
return true;
|
2014-04-11 01:59:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public void writeToNBT(NBTTagCompound nbtTagCompound)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
super.writeToNBT(nbtTagCompound);
|
|
|
|
|
|
|
|
// Write the ItemStacks in the inventory to NBT
|
|
|
|
NBTTagList tagList = new NBTTagList();
|
2014-07-04 21:18:10 +02:00
|
|
|
for (int currentIndex = 0; currentIndex < inventory.length; ++currentIndex)
|
|
|
|
{
|
|
|
|
if (inventory[currentIndex] != null)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
NBTTagCompound tagCompound = new NBTTagCompound();
|
|
|
|
tagCompound.setByte("Slot", (byte) currentIndex);
|
|
|
|
inventory[currentIndex].writeToNBT(tagCompound);
|
|
|
|
tagList.appendTag(tagCompound);
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 18:58:35 +02:00
|
|
|
nbtTagCompound.setTag(Names.NBT.ITEMS, tagList);
|
2014-04-11 01:59:04 +02: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.
|
|
|
|
*/
|
|
|
|
@Override
|
2014-07-04 21:18:10 +02:00
|
|
|
public void updateEntity()
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
super.updateEntity();
|
|
|
|
|
2014-07-04 21:18:10 +02:00
|
|
|
if (++ticksSinceSync % 20 * 4 == 0)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
worldObj.addBlockEvent(xCoord, yCoord, zCoord, ModBlocks.alchemicalChest, 1, numUsingPlayers);
|
|
|
|
}
|
|
|
|
|
|
|
|
prevLidAngle = lidAngle;
|
|
|
|
float angleIncrement = 0.1F;
|
|
|
|
double adjustedXCoord, adjustedZCoord;
|
|
|
|
|
2014-07-04 21:18:10 +02:00
|
|
|
if (numUsingPlayers > 0 && lidAngle == 0.0F)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-07-04 21:18:10 +02:00
|
|
|
if (numUsingPlayers == 0 && lidAngle > 0.0F || numUsingPlayers > 0 && lidAngle < 1.0F)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
float var8 = lidAngle;
|
|
|
|
|
2014-07-04 21:18:10 +02:00
|
|
|
if (numUsingPlayers > 0)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
lidAngle += angleIncrement;
|
2014-07-04 21:18:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
lidAngle -= angleIncrement;
|
|
|
|
}
|
|
|
|
|
2014-07-04 21:18:10 +02:00
|
|
|
if (lidAngle > 1.0F)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
lidAngle = 1.0F;
|
|
|
|
}
|
|
|
|
|
2014-07-04 21:18:10 +02:00
|
|
|
if (lidAngle < 0.5F && var8 >= 0.5F)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-07-04 21:18:10 +02:00
|
|
|
if (lidAngle < 0.0F)
|
|
|
|
{
|
2014-04-11 01:59:04 +02:00
|
|
|
lidAngle = 0.0F;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-04 21:18:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
2014-04-11 01:59:04 +02:00
|
|
|
}
|