    001    package net.minecraft.src;
    002
    003    public class EntityPig extends EntityAnimal
    004    {
    005        /** AI task for player control. */
    006        private final EntityAIControlledByPlayer aiControlledByPlayer;
    007
    008        public EntityPig(World par1World)
    009        {
    010            super(par1World);
    011            this.texture = "/mob/pig.png";
    012            this.setSize(0.9F, 0.9F);
    013            this.getNavigator().setAvoidsWater(true);
    014            float var2 = 0.25F;
    015            this.tasks.addTask(0, new EntityAISwimming(this));
    016            this.tasks.addTask(1, new EntityAIPanic(this, 0.38F));
    017            this.tasks.addTask(2, this.aiControlledByPlayer = new EntityAIControlledByPlayer(this, 0.34F));
    018            this.tasks.addTask(3, new EntityAIMate(this, var2));
    019            this.tasks.addTask(4, new EntityAITempt(this, 0.3F, Item.carrotOnAStick.shiftedIndex, false));
    020            this.tasks.addTask(4, new EntityAITempt(this, 0.3F, Item.carrot.shiftedIndex, false));
    021            this.tasks.addTask(5, new EntityAIFollowParent(this, 0.28F));
    022            this.tasks.addTask(6, new EntityAIWander(this, var2));
    023            this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
    024            this.tasks.addTask(8, new EntityAILookIdle(this));
    025        }
    026
    027        /**
    028         * Returns true if the newer Entity AI code should be run
    029         */
    030        public boolean isAIEnabled()
    031        {
    032            return true;
    033        }
    034
    035        public int getMaxHealth()
    036        {
    037            return 10;
    038        }
    039
    040        protected void updateAITasks()
    041        {
    042            super.updateAITasks();
    043        }
    044
    045        /**
    046         * returns true if all the conditions for steering the entity are met. For pigs, this is true if it is being ridden
    047         * by a player and the player is holding a carrot-on-a-stick
    048         */
    049        public boolean canBeSteered()
    050        {
    051            ItemStack var1 = ((EntityPlayer)this.riddenByEntity).getHeldItem();
    052            return var1 != null && var1.itemID == Item.carrotOnAStick.shiftedIndex;
    053        }
    054
    055        protected void entityInit()
    056        {
    057            super.entityInit();
    058            this.dataWatcher.addObject(16, Byte.valueOf((byte)0));
    059        }
    060
    061        /**
    062         * (abstract) Protected helper method to write subclass entity data to NBT.
    063         */
    064        public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
    065        {
    066            super.writeEntityToNBT(par1NBTTagCompound);
    067            par1NBTTagCompound.setBoolean("Saddle", this.getSaddled());
    068        }
    069
    070        /**
    071         * (abstract) Protected helper method to read subclass entity data from NBT.
    072         */
    073        public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
    074        {
    075            super.readEntityFromNBT(par1NBTTagCompound);
    076            this.setSaddled(par1NBTTagCompound.getBoolean("Saddle"));
    077        }
    078
    079        /**
    080         * Returns the sound this mob makes while it's alive.
    081         */
    082        protected String getLivingSound()
    083        {
    084            return "mob.pig.say";
    085        }
    086
    087        /**
    088         * Returns the sound this mob makes when it is hurt.
    089         */
    090        protected String getHurtSound()
    091        {
    092            return "mob.pig.say";
    093        }
    094
    095        /**
    096         * Returns the sound this mob makes on death.
    097         */
    098        protected String getDeathSound()
    099        {
    100            return "mob.pig.death";
    101        }
    102
    103        /**
    104         * Plays step sound at given x, y, z for the entity
    105         */
    106        protected void playStepSound(int par1, int par2, int par3, int par4)
    107        {
    108            this.func_85030_a("mob.pig.step", 0.15F, 1.0F);
    109        }
    110
    111        /**
    112         * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
    113         */
    114        public boolean interact(EntityPlayer par1EntityPlayer)
    115        {
    116            if (super.interact(par1EntityPlayer))
    117            {
    118                return true;
    119            }
    120            else if (this.getSaddled() && !this.worldObj.isRemote && (this.riddenByEntity == null || this.riddenByEntity == par1EntityPlayer))
    121            {
    122                par1EntityPlayer.mountEntity(this);
    123                return true;
    124            }
    125            else
    126            {
    127                return false;
    128            }
    129        }
    130
    131        /**
    132         * Returns the item ID for the item the mob drops on death.
    133         */
    134        protected int getDropItemId()
    135        {
    136            return this.isBurning() ? Item.porkCooked.shiftedIndex : Item.porkRaw.shiftedIndex;
    137        }
    138
    139        /**
    140         * Drop 0-2 items of this living's type
    141         */
    142        protected void dropFewItems(boolean par1, int par2)
    143        {
    144            int var3 = this.rand.nextInt(3) + 1 + this.rand.nextInt(1 + par2);
    145
    146            for (int var4 = 0; var4 < var3; ++var4)
    147            {
    148                if (this.isBurning())
    149                {
    150                    this.dropItem(Item.porkCooked.shiftedIndex, 1);
    151                }
    152                else
    153                {
    154                    this.dropItem(Item.porkRaw.shiftedIndex, 1);
    155                }
    156            }
    157
    158            if (this.getSaddled())
    159            {
    160                this.dropItem(Item.saddle.shiftedIndex, 1);
    161            }
    162        }
    163
    164        /**
    165         * Returns true if the pig is saddled.
    166         */
    167        public boolean getSaddled()
    168        {
    169            return (this.dataWatcher.getWatchableObjectByte(16) & 1) != 0;
    170        }
    171
    172        /**
    173         * Set or remove the saddle of the pig.
    174         */
    175        public void setSaddled(boolean par1)
    176        {
    177            if (par1)
    178            {
    179                this.dataWatcher.updateObject(16, Byte.valueOf((byte)1));
    180            }
    181            else
    182            {
    183                this.dataWatcher.updateObject(16, Byte.valueOf((byte)0));
    184            }
    185        }
    186
    187        /**
    188         * Called when a lightning bolt hits the entity.
    189         */
    190        public void onStruckByLightning(EntityLightningBolt par1EntityLightningBolt)
    191        {
    192            if (!this.worldObj.isRemote)
    193            {
    194                EntityPigZombie var2 = new EntityPigZombie(this.worldObj);
    195                var2.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotationYaw, this.rotationPitch);
    196                this.worldObj.spawnEntityInWorld(var2);
    197                this.setDead();
    198            }
    199        }
    200
    201        /**
    202         * Called when the mob is falling. Calculates and applies fall damage.
    203         */
    204        protected void fall(float par1)
    205        {
    206            super.fall(par1);
    207
    208            if (par1 > 5.0F && this.riddenByEntity instanceof EntityPlayer)
    209            {
    210                ((EntityPlayer)this.riddenByEntity).triggerAchievement(AchievementList.flyPig);
    211            }
    212        }
    213
    214        /**
    215         * This function is used when two same-species animals in 'love mode' breed to generate the new baby animal.
    216         */
    217        public EntityAnimal spawnBabyAnimal(EntityAnimal par1EntityAnimal)
    218        {
    219            return new EntityPig(this.worldObj);
    220        }
    221
    222        /**
    223         * Checks if the parameter is an item which this animal can be fed to breed it (wheat, carrots or seeds depending on
    224         * the animal type)
    225         */
    226        public boolean isBreedingItem(ItemStack par1ItemStack)
    227        {
    228            return par1ItemStack != null && par1ItemStack.itemID == Item.carrot.shiftedIndex;
    229        }
    230
    231        /**
    232         * Return the AI task for player control.
    233         */
    234        public EntityAIControlledByPlayer getAIControlledByPlayer()
    235        {
    236            return this.aiControlledByPlayer;
    237        }
    238    }