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