001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    
006    public class EntityItemFrame extends EntityHanging
007    {
008        /** Chance for this item frame's item to drop from the frame. */
009        private float itemDropChance = 1.0F;
010    
011        public EntityItemFrame(World par1World)
012        {
013            super(par1World);
014        }
015    
016        public EntityItemFrame(World par1World, int par2, int par3, int par4, int par5)
017        {
018            super(par1World, par2, par3, par4, par5);
019            this.setDirection(par5);
020        }
021    
022        protected void entityInit()
023        {
024            this.getDataWatcher().addObjectByDataType(2, 5);
025            this.getDataWatcher().addObject(3, Byte.valueOf((byte)0));
026        }
027    
028        public int func_82329_d()
029        {
030            return 9;
031        }
032    
033        public int func_82330_g()
034        {
035            return 9;
036        }
037    
038        @SideOnly(Side.CLIENT)
039    
040        /**
041         * Checks if the entity is in range to render by using the past in distance and comparing it to its average edge
042         * length * 64 * renderDistanceWeight Args: distance
043         */
044        public boolean isInRangeToRenderDist(double par1)
045        {
046            double var3 = 16.0D;
047            var3 *= 64.0D * this.renderDistanceWeight;
048            return par1 < var3 * var3;
049        }
050    
051        /**
052         * Drop the item currently on this item frame.
053         */
054        public void dropItemStack()
055        {
056            this.entityDropItem(new ItemStack(Item.itemFrame), 0.0F);
057            ItemStack var1 = this.getDisplayedItem();
058    
059            if (var1 != null && this.rand.nextFloat() < this.itemDropChance)
060            {
061                var1 = var1.copy();
062                var1.setItemFrame((EntityItemFrame)null);
063                this.entityDropItem(var1, 0.0F);
064            }
065        }
066    
067        public ItemStack getDisplayedItem()
068        {
069            return this.getDataWatcher().getWatchableObjectItemStack(2);
070        }
071    
072        public void setDisplayedItem(ItemStack par1ItemStack)
073        {
074            par1ItemStack = par1ItemStack.copy();
075            par1ItemStack.stackSize = 1;
076            par1ItemStack.setItemFrame(this);
077            this.getDataWatcher().updateObject(2, par1ItemStack);
078            this.getDataWatcher().func_82708_h(2);
079        }
080    
081        /**
082         * Return the rotation of the item currently on this frame.
083         */
084        public int getRotation()
085        {
086            return this.getDataWatcher().getWatchableObjectByte(3);
087        }
088    
089        public void setItemRotation(int par1)
090        {
091            this.getDataWatcher().updateObject(3, Byte.valueOf((byte)(par1 % 4)));
092        }
093    
094        /**
095         * (abstract) Protected helper method to write subclass entity data to NBT.
096         */
097        public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
098        {
099            if (this.getDisplayedItem() != null)
100            {
101                par1NBTTagCompound.setCompoundTag("Item", this.getDisplayedItem().writeToNBT(new NBTTagCompound()));
102                par1NBTTagCompound.setByte("ItemRotation", (byte)this.getRotation());
103                par1NBTTagCompound.setFloat("ItemDropChance", this.itemDropChance);
104            }
105    
106            super.writeEntityToNBT(par1NBTTagCompound);
107        }
108    
109        /**
110         * (abstract) Protected helper method to read subclass entity data from NBT.
111         */
112        public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
113        {
114            NBTTagCompound var2 = par1NBTTagCompound.getCompoundTag("Item");
115    
116            if (var2 != null && !var2.hasNoTags())
117            {
118                this.setDisplayedItem(ItemStack.loadItemStackFromNBT(var2));
119                this.setItemRotation(par1NBTTagCompound.getByte("ItemRotation"));
120    
121                if (par1NBTTagCompound.hasKey("ItemDropChance"))
122                {
123                    this.itemDropChance = par1NBTTagCompound.getFloat("ItemDropChance");
124                }
125            }
126    
127            super.readEntityFromNBT(par1NBTTagCompound);
128        }
129    
130        /**
131         * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
132         */
133        public boolean interact(EntityPlayer par1EntityPlayer)
134        {
135            if (this.getDisplayedItem() == null)
136            {
137                ItemStack var2 = par1EntityPlayer.getHeldItem();
138    
139                if (var2 != null && !this.worldObj.isRemote)
140                {
141                    this.setDisplayedItem(var2);
142    
143                    if (!par1EntityPlayer.capabilities.isCreativeMode && --var2.stackSize <= 0)
144                    {
145                        par1EntityPlayer.inventory.setInventorySlotContents(par1EntityPlayer.inventory.currentItem, (ItemStack)null);
146                    }
147                }
148            }
149            else if (!this.worldObj.isRemote)
150            {
151                this.setItemRotation(this.getRotation() + 1);
152            }
153    
154            return true;
155        }
156    }