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