001package net.minecraft.entity;
002
003import net.minecraft.entity.player.EntityPlayer;
004import net.minecraft.item.Item;
005import net.minecraft.item.ItemStack;
006import net.minecraft.nbt.NBTTagCompound;
007import net.minecraft.world.World;
008
009public abstract class EntityAgeable extends EntityCreature
010{
011    private float field_98056_d = -1.0F;
012    private float field_98057_e;
013
014    public EntityAgeable(World par1World)
015    {
016        super(par1World);
017    }
018
019    public abstract EntityAgeable createChild(EntityAgeable entityageable);
020
021    /**
022     * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
023     */
024    public boolean interact(EntityPlayer par1EntityPlayer)
025    {
026        ItemStack itemstack = par1EntityPlayer.inventory.getCurrentItem();
027
028        if (itemstack != null && itemstack.itemID == Item.monsterPlacer.itemID && !this.worldObj.isRemote)
029        {
030            Class oclass = EntityList.getClassFromID(itemstack.getItemDamage());
031
032            if (oclass != null && oclass.isAssignableFrom(this.getClass()))
033            {
034                EntityAgeable entityageable = this.createChild(this);
035
036                if (entityageable != null)
037                {
038                    entityageable.setGrowingAge(-24000);
039                    entityageable.setLocationAndAngles(this.posX, this.posY, this.posZ, 0.0F, 0.0F);
040                    this.worldObj.spawnEntityInWorld(entityageable);
041
042                    if (itemstack.hasDisplayName())
043                    {
044                        entityageable.func_94058_c(itemstack.getDisplayName());
045                    }
046
047                    if (!par1EntityPlayer.capabilities.isCreativeMode)
048                    {
049                        --itemstack.stackSize;
050
051                        if (itemstack.stackSize <= 0)
052                        {
053                            par1EntityPlayer.inventory.setInventorySlotContents(par1EntityPlayer.inventory.currentItem, (ItemStack)null);
054                        }
055                    }
056                }
057            }
058        }
059
060        return super.interact(par1EntityPlayer);
061    }
062
063    protected void entityInit()
064    {
065        super.entityInit();
066        this.dataWatcher.addObject(12, new Integer(0));
067    }
068
069    /**
070     * The age value may be negative or positive or zero. If it's negative, it get's incremented on each tick, if it's
071     * positive, it get's decremented each tick. Don't confuse this with EntityLiving.getAge. With a negative value the
072     * Entity is considered a child.
073     */
074    public int getGrowingAge()
075    {
076        return this.dataWatcher.getWatchableObjectInt(12);
077    }
078
079    /**
080     * The age value may be negative or positive or zero. If it's negative, it get's incremented on each tick, if it's
081     * positive, it get's decremented each tick. With a negative value the Entity is considered a child.
082     */
083    public void setGrowingAge(int par1)
084    {
085        this.dataWatcher.updateObject(12, Integer.valueOf(par1));
086        this.func_98054_a(this.isChild());
087    }
088
089    /**
090     * (abstract) Protected helper method to write subclass entity data to NBT.
091     */
092    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
093    {
094        super.writeEntityToNBT(par1NBTTagCompound);
095        par1NBTTagCompound.setInteger("Age", this.getGrowingAge());
096    }
097
098    /**
099     * (abstract) Protected helper method to read subclass entity data from NBT.
100     */
101    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
102    {
103        super.readEntityFromNBT(par1NBTTagCompound);
104        this.setGrowingAge(par1NBTTagCompound.getInteger("Age"));
105    }
106
107    /**
108     * Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
109     * use this to react to sunlight and start to burn.
110     */
111    public void onLivingUpdate()
112    {
113        super.onLivingUpdate();
114
115        if (this.worldObj.isRemote)
116        {
117            this.func_98054_a(this.isChild());
118        }
119        else
120        {
121            int i = this.getGrowingAge();
122
123            if (i < 0)
124            {
125                ++i;
126                this.setGrowingAge(i);
127            }
128            else if (i > 0)
129            {
130                --i;
131                this.setGrowingAge(i);
132            }
133        }
134    }
135
136    /**
137     * If Animal, checks if the age timer is negative
138     */
139    public boolean isChild()
140    {
141        return this.getGrowingAge() < 0;
142    }
143
144    public void func_98054_a(boolean par1)
145    {
146        this.func_98055_j(par1 ? 0.5F : 1.0F);
147    }
148
149    /**
150     * Sets the width and height of the entity. Args: width, height
151     */
152    protected final void setSize(float par1, float par2)
153    {
154        boolean flag = this.field_98056_d > 0.0F;
155        this.field_98056_d = par1;
156        this.field_98057_e = par2;
157
158        if (!flag)
159        {
160            this.func_98055_j(1.0F);
161        }
162    }
163
164    private void func_98055_j(float par1)
165    {
166        super.setSize(this.field_98056_d * par1, this.field_98057_e * par1);
167    }
168}