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    public EntityAgeable(World par1World)
012    {
013        super(par1World);
014    }
015
016    public abstract EntityAgeable createChild(EntityAgeable var1);
017
018    /**
019     * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
020     */
021    public boolean interact(EntityPlayer par1EntityPlayer)
022    {
023        ItemStack var2 = par1EntityPlayer.inventory.getCurrentItem();
024
025        if (var2 != null && var2.itemID == Item.monsterPlacer.itemID && !this.worldObj.isRemote)
026        {
027            Class var3 = EntityList.getClassFromID(var2.getItemDamage());
028
029            if (var3 != null && var3.isAssignableFrom(this.getClass()))
030            {
031                EntityAgeable var4 = this.createChild(this);
032
033                if (var4 != null)
034                {
035                    var4.setGrowingAge(-24000);
036                    var4.setLocationAndAngles(this.posX, this.posY, this.posZ, 0.0F, 0.0F);
037                    this.worldObj.spawnEntityInWorld(var4);
038
039                    if (!par1EntityPlayer.capabilities.isCreativeMode)
040                    {
041                        --var2.stackSize;
042
043                        if (var2.stackSize <= 0)
044                        {
045                            par1EntityPlayer.inventory.setInventorySlotContents(par1EntityPlayer.inventory.currentItem, (ItemStack)null);
046                        }
047                    }
048                }
049            }
050        }
051
052        return super.interact(par1EntityPlayer);
053    }
054
055    protected void entityInit()
056    {
057        super.entityInit();
058        this.dataWatcher.addObject(12, new Integer(0));
059    }
060
061    /**
062     * The age value may be negative or positive or zero. If it's negative, it get's incremented on each tick, if it's
063     * positive, it get's decremented each tick. Don't confuse this with EntityLiving.getAge. With a negative value the
064     * Entity is considered a child.
065     */
066    public int getGrowingAge()
067    {
068        return this.dataWatcher.getWatchableObjectInt(12);
069    }
070
071    /**
072     * The age value may be negative or positive or zero. If it's negative, it get's incremented on each tick, if it's
073     * positive, it get's decremented each tick. With a negative value the Entity is considered a child.
074     */
075    public void setGrowingAge(int par1)
076    {
077        this.dataWatcher.updateObject(12, Integer.valueOf(par1));
078    }
079
080    /**
081     * (abstract) Protected helper method to write subclass entity data to NBT.
082     */
083    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
084    {
085        super.writeEntityToNBT(par1NBTTagCompound);
086        par1NBTTagCompound.setInteger("Age", this.getGrowingAge());
087    }
088
089    /**
090     * (abstract) Protected helper method to read subclass entity data from NBT.
091     */
092    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
093    {
094        super.readEntityFromNBT(par1NBTTagCompound);
095        this.setGrowingAge(par1NBTTagCompound.getInteger("Age"));
096    }
097
098    /**
099     * Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
100     * use this to react to sunlight and start to burn.
101     */
102    public void onLivingUpdate()
103    {
104        super.onLivingUpdate();
105        int var1 = this.getGrowingAge();
106
107        if (var1 < 0)
108        {
109            ++var1;
110            this.setGrowingAge(var1);
111        }
112        else if (var1 > 0)
113        {
114            --var1;
115            this.setGrowingAge(var1);
116        }
117    }
118
119    /**
120     * If Animal, checks if the age timer is negative
121     */
122    public boolean isChild()
123    {
124        return this.getGrowingAge() < 0;
125    }
126}