001    package net.minecraft.src;
002    
003    public class TileEntityEnderChest extends TileEntity
004    {
005        /** The current angle of the chest lid (between 0 and 1) */
006        public float lidAngle;
007    
008        /** The angle of the chest lid last tick */
009        public float prevLidAngle;
010    
011        /** The number of players currently using this ender chest. */
012        public int numUsingPlayers;
013    
014        /** Server sync counter (once per 20 ticks) */
015        private int ticksSinceSync;
016    
017        /**
018         * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
019         * ticks and creates a new spawn inside its implementation.
020         */
021        public void updateEntity()
022        {
023            super.updateEntity();
024    
025            if (++this.ticksSinceSync % 20 * 4 == 0)
026            {
027                this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, Block.enderChest.blockID, 1, this.numUsingPlayers);
028            }
029    
030            this.prevLidAngle = this.lidAngle;
031            float var1 = 0.1F;
032            double var4;
033    
034            if (this.numUsingPlayers > 0 && this.lidAngle == 0.0F)
035            {
036                double var2 = (double)this.xCoord + 0.5D;
037                var4 = (double)this.zCoord + 0.5D;
038                this.worldObj.playSoundEffect(var2, (double)this.yCoord + 0.5D, var4, "random.chestopen", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
039            }
040    
041            if (this.numUsingPlayers == 0 && this.lidAngle > 0.0F || this.numUsingPlayers > 0 && this.lidAngle < 1.0F)
042            {
043                float var8 = this.lidAngle;
044    
045                if (this.numUsingPlayers > 0)
046                {
047                    this.lidAngle += var1;
048                }
049                else
050                {
051                    this.lidAngle -= var1;
052                }
053    
054                if (this.lidAngle > 1.0F)
055                {
056                    this.lidAngle = 1.0F;
057                }
058    
059                float var3 = 0.5F;
060    
061                if (this.lidAngle < var3 && var8 >= var3)
062                {
063                    var4 = (double)this.xCoord + 0.5D;
064                    double var6 = (double)this.zCoord + 0.5D;
065                    this.worldObj.playSoundEffect(var4, (double)this.yCoord + 0.5D, var6, "random.chestclosed", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
066                }
067    
068                if (this.lidAngle < 0.0F)
069                {
070                    this.lidAngle = 0.0F;
071                }
072            }
073        }
074    
075        /**
076         * Called when a client event is received with the event number and argument, see World.sendClientEvent
077         */
078        public void receiveClientEvent(int par1, int par2)
079        {
080            if (par1 == 1)
081            {
082                this.numUsingPlayers = par2;
083            }
084        }
085    
086        /**
087         * invalidates a tile entity
088         */
089        public void invalidate()
090        {
091            this.updateContainingBlockInfo();
092            super.invalidate();
093        }
094    
095        public void openChest()
096        {
097            ++this.numUsingPlayers;
098            this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, Block.enderChest.blockID, 1, this.numUsingPlayers);
099        }
100    
101        public void closeChest()
102        {
103            --this.numUsingPlayers;
104            this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, Block.enderChest.blockID, 1, this.numUsingPlayers);
105        }
106    
107        public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
108        {
109            return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
110        }
111    }