package net.minecraft.tileentity;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.Iterator;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.BlockChest;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryLargeChest;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.AxisAlignedBB;
public class TileEntityChest extends TileEntity implements IInventory
{
private ItemStack[] chestContents = new ItemStack[36];
/** Determines if the check for adjacent chests has taken place. */
public boolean adjacentChestChecked;
/** Contains the chest tile located adjacent to this one (if any) */
public TileEntityChest adjacentChestZNeg;
/** Contains the chest tile located adjacent to this one (if any) */
public TileEntityChest adjacentChestXPos;
/** Contains the chest tile located adjacent to this one (if any) */
public TileEntityChest adjacentChestXNeg;
/** Contains the chest tile located adjacent to this one (if any) */
public TileEntityChest adjacentChestZPos;
/** The current angle of the lid (between 0 and 1) */
public float lidAngle;
/** The angle of the lid last tick */
public float prevLidAngle;
/** The number of players currently using this chest */
public int numPlayersUsing;
/** Server sync counter (once per 20 ticks) */
private int ticksSinceSync;
private int cachedChestType;
private String customName;
private static final String __OBFID = "CL_00000346";
public TileEntityChest()
{
this.cachedChestType = -1;
}
@SideOnly(Side.CLIENT)
public TileEntityChest(int p_i2350_1_)
{
this.cachedChestType = p_i2350_1_;
}
/**
* Returns the number of slots in the inventory.
*/
public int getSizeInventory()
{
return 27;
}
/**
* Returns the stack in slot i
*/
public ItemStack getStackInSlot(int slotIn)
{
return this.chestContents[slotIn];
}
/**
* Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
* new stack.
*/
public ItemStack decrStackSize(int index, int count)
{
if (this.chestContents[index] != null)
{
ItemStack itemstack;
if (this.chestContents[index].stackSize <= count)
{
itemstack = this.chestContents[index];
this.chestContents[index] = null;
this.markDirty();
return itemstack;
}
else
{
itemstack = this.chestContents[index].splitStack(count);
if (this.chestContents[index].stackSize == 0)
{
this.chestContents[index] = null;
}
this.markDirty();
return itemstack;
}
}
else
{
return null;
}
}
/**
* When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
* like when you close a workbench GUI.
*/
public ItemStack getStackInSlotOnClosing(int index)
{
if (this.chestContents[index] != null)
{
ItemStack itemstack = this.chestContents[index];
this.chestContents[index] = null;
return itemstack;
}
else
{
return null;
}
}
/**
* Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
*/
public void setInventorySlotContents(int index, ItemStack stack)
{
this.chestContents[index] = stack;
if (stack != null && stack.stackSize > this.getInventoryStackLimit())
{
stack.stackSize = this.getInventoryStackLimit();
}
this.markDirty();
}
/**
* Returns the name of the inventory
*/
public String getInventoryName()
{
return this.isCustomInventoryName() ? this.customName : "container.chest";
}
/**
* Returns if the inventory is named
*/
public boolean isCustomInventoryName()
{
return this.customName != null && this.customName.length() > 0;
}
public void setCustomName(String p_145976_1_)
{
this.customName = p_145976_1_;
}
public void readFromNBT(NBTTagCompound compound)
{
super.readFromNBT(compound);
NBTTagList nbttaglist = compound.getTagList("Items", 10);
this.chestContents = new ItemStack[this.getSizeInventory()];
if (compound.hasKey("CustomName", 8))
{
this.customName = compound.getString("CustomName");
}
for (int i = 0; i < nbttaglist.tagCount(); ++i)
{
NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
int j = nbttagcompound1.getByte("Slot") & 255;
if (j >= 0 && j < this.chestContents.length)
{
this.chestContents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
}
}
}
public void writeToNBT(NBTTagCompound compound)
{
super.writeToNBT(compound);
NBTTagList nbttaglist = new NBTTagList();
for (int i = 0; i < this.chestContents.length; ++i)
{
if (this.chestContents[i] != null)
{
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setByte("Slot", (byte)i);
this.chestContents[i].writeToNBT(nbttagcompound1);
nbttaglist.appendTag(nbttagcompound1);
}
}
compound.setTag("Items", nbttaglist);
if (this.isCustomInventoryName())
{
compound.setString("CustomName", this.customName);
}
}
/**
* Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
* this more of a set than a get?*
*/
public int getInventoryStackLimit()
{
return 64;
}
/**
* Do not make give this method the name canInteractWith because it clashes with Container
*/
public boolean isUseableByPlayer(EntityPlayer player)
{
return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
}
public void updateContainingBlockInfo()
{
super.updateContainingBlockInfo();
this.adjacentChestChecked = false;
}
private void func_145978_a(TileEntityChest p_145978_1_, int p_145978_2_)
{
if (p_145978_1_.isInvalid())
{
this.adjacentChestChecked = false;
}
else if (this.adjacentChestChecked)
{
switch (p_145978_2_)
{
case 0:
if (this.adjacentChestZPos != p_145978_1_)
{
this.adjacentChestChecked = false;
}
break;
case 1:
if (this.adjacentChestXNeg != p_145978_1_)
{
this.adjacentChestChecked = false;
}
break;
case 2:
if (this.adjacentChestZNeg != p_145978_1_)
{
this.adjacentChestChecked = false;
}
break;
case 3:
if (this.adjacentChestXPos != p_145978_1_)
{
this.adjacentChestChecked = false;
}
}
}
}
/**
* Performs the check for adjacent chests to determine if this chest is double or not.
*/
public void checkForAdjacentChests()
{
if (!this.adjacentChestChecked)
{
this.adjacentChestChecked = true;
this.adjacentChestZNeg = null;
this.adjacentChestXPos = null;
this.adjacentChestXNeg = null;
this.adjacentChestZPos = null;
if (this.isSameTypeChestAt(this.xCoord - 1, this.yCoord, this.zCoord))
{
this.adjacentChestXNeg = (TileEntityChest)this.worldObj.getTileEntity(this.xCoord - 1, this.yCoord, this.zCoord);
}
if (this.isSameTypeChestAt(this.xCoord + 1, this.yCoord, this.zCoord))
{
this.adjacentChestXPos = (TileEntityChest)this.worldObj.getTileEntity(this.xCoord + 1, this.yCoord, this.zCoord);
}
if (this.isSameTypeChestAt(this.xCoord, this.yCoord, this.zCoord - 1))
{
this.adjacentChestZNeg = (TileEntityChest)this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord - 1);
}
if (this.isSameTypeChestAt(this.xCoord, this.yCoord, this.zCoord + 1))
{
this.adjacentChestZPos = (TileEntityChest)this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord + 1);
}
if (this.adjacentChestZNeg != null)
{
this.adjacentChestZNeg.func_145978_a(this, 0);
}
if (this.adjacentChestZPos != null)
{
this.adjacentChestZPos.func_145978_a(this, 2);
}
if (this.adjacentChestXPos != null)
{
this.adjacentChestXPos.func_145978_a(this, 1);
}
if (this.adjacentChestXNeg != null)
{
this.adjacentChestXNeg.func_145978_a(this, 3);
}
}
}
/**
* Test if the block is a chest, with the same type (normal, trapped, ...)
*/
private boolean isSameTypeChestAt(int p_145977_1_, int p_145977_2_, int p_145977_3_)
{
if (this.worldObj == null)
{
return false;
}
else
{
Block block = this.worldObj.getBlock(p_145977_1_, p_145977_2_, p_145977_3_);
return block instanceof BlockChest && ((BlockChest)block).chestType == this.getChestType();
}
}
public void updateEntity()
{
super.updateEntity();
this.checkForAdjacentChests();
++this.ticksSinceSync;
float f;
if (!this.worldObj.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + this.xCoord + this.yCoord + this.zCoord) % 200 == 0)
{
this.numPlayersUsing = 0;
f = 5.0F;
List list = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox((double)((float)this.xCoord - f), (double)((float)this.yCoord - f), (double)((float)this.zCoord - f), (double)((float)(this.xCoord + 1) + f), (double)((float)(this.yCoord + 1) + f), (double)((float)(this.zCoord + 1) + f)));
Iterator iterator = list.iterator();
while (iterator.hasNext())
{
EntityPlayer entityplayer = (EntityPlayer)iterator.next();
if (entityplayer.openContainer instanceof ContainerChest)
{
IInventory iinventory = ((ContainerChest)entityplayer.openContainer).getLowerChestInventory();
if (iinventory == this || iinventory instanceof InventoryLargeChest && ((InventoryLargeChest)iinventory).isPartOfLargeChest(this))
{
++this.numPlayersUsing;
}
}
}
}
this.prevLidAngle = this.lidAngle;
f = 0.1F;
double d2;
if (this.numPlayersUsing > 0 && this.lidAngle == 0.0F && this.adjacentChestZNeg == null && this.adjacentChestXNeg == null)
{
double d1 = (double)this.xCoord + 0.5D;
d2 = (double)this.zCoord + 0.5D;
if (this.adjacentChestZPos != null)
{
d2 += 0.5D;
}
if (this.adjacentChestXPos != null)
{
d1 += 0.5D;
}
this.worldObj.playSoundEffect(d1, (double)this.yCoord + 0.5D, d2, "random.chestopen", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
}
if (this.numPlayersUsing == 0 && this.lidAngle > 0.0F || this.numPlayersUsing > 0 && this.lidAngle < 1.0F)
{
float f1 = this.lidAngle;
if (this.numPlayersUsing > 0)
{
this.lidAngle += f;
}
else
{
this.lidAngle -= f;
}
if (this.lidAngle > 1.0F)
{
this.lidAngle = 1.0F;
}
float f2 = 0.5F;
if (this.lidAngle < f2 && f1 >= f2 && this.adjacentChestZNeg == null && this.adjacentChestXNeg == null)
{
d2 = (double)this.xCoord + 0.5D;
double d0 = (double)this.zCoord + 0.5D;
if (this.adjacentChestZPos != null)
{
d0 += 0.5D;
}
if (this.adjacentChestXPos != null)
{
d2 += 0.5D;
}
this.worldObj.playSoundEffect(d2, (double)this.yCoord + 0.5D, d0, "random.chestclosed", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
}
if (this.lidAngle < 0.0F)
{
this.lidAngle = 0.0F;
}
}
}
public boolean receiveClientEvent(int id, int type)
{
if (id == 1)
{
this.numPlayersUsing = type;
return true;
}
else
{
return super.receiveClientEvent(id, type);
}
}
public void openChest()
{
if (this.numPlayersUsing < 0)
{
this.numPlayersUsing = 0;
}
++this.numPlayersUsing;
this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, this.numPlayersUsing);
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType());
}
public void closeChest()
{
if (this.getBlockType() instanceof BlockChest)
{
--this.numPlayersUsing;
this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, this.numPlayersUsing);
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType());
}
}
/**
* Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
*/
public boolean isItemValidForSlot(int index, ItemStack stack)
{
return true;
}
/**
* invalidates a tile entity
*/
public void invalidate()
{
super.invalidate();
this.updateContainingBlockInfo();
this.checkForAdjacentChests();
}
public int getChestType()
{
if (this.cachedChestType == -1)
{
if (this.worldObj == null || !(this.getBlockType() instanceof BlockChest))
{
return 0;
}
this.cachedChestType = ((BlockChest)this.getBlockType()).chestType;
}
return this.cachedChestType;
}
}