001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    import java.util.Iterator;
006    import java.util.List;
007    
008    public class EntityPigZombie extends EntityZombie
009    {
010        /** Above zero if this PigZombie is Angry. */
011        private int angerLevel = 0;
012    
013        /** A random delay until this PigZombie next makes a sound. */
014        private int randomSoundDelay = 0;
015    
016        /** The ItemStack that any PigZombie holds (a gold sword, in fact). */
017        private static final ItemStack defaultHeldItem = new ItemStack(Item.swordGold, 1);
018    
019        public EntityPigZombie(World par1World)
020        {
021            super(par1World);
022            this.texture = "/mob/pigzombie.png";
023            this.moveSpeed = 0.5F;
024            this.attackStrength = 5;
025            this.isImmuneToFire = true;
026        }
027    
028        /**
029         * Returns true if the newer Entity AI code should be run
030         */
031        protected boolean isAIEnabled()
032        {
033            return false;
034        }
035    
036        /**
037         * Called to update the entity's position/logic.
038         */
039        public void onUpdate()
040        {
041            this.moveSpeed = this.entityToAttack != null ? 0.95F : 0.5F;
042    
043            if (this.randomSoundDelay > 0 && --this.randomSoundDelay == 0)
044            {
045                this.worldObj.playSoundAtEntity(this, "mob.zombiepig.zpigangry", this.getSoundVolume() * 2.0F, ((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F + 1.0F) * 1.8F);
046            }
047    
048            super.onUpdate();
049        }
050    
051        /**
052         * Checks if the entity's current position is a valid location to spawn this entity.
053         */
054        public boolean getCanSpawnHere()
055        {
056            return this.worldObj.difficultySetting > 0 && this.worldObj.checkIfAABBIsClear(this.boundingBox) && this.worldObj.getCollidingBoundingBoxes(this, this.boundingBox).isEmpty() && !this.worldObj.isAnyLiquid(this.boundingBox);
057        }
058    
059        /**
060         * (abstract) Protected helper method to write subclass entity data to NBT.
061         */
062        public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
063        {
064            super.writeEntityToNBT(par1NBTTagCompound);
065            par1NBTTagCompound.setShort("Anger", (short)this.angerLevel);
066        }
067    
068        /**
069         * (abstract) Protected helper method to read subclass entity data from NBT.
070         */
071        public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
072        {
073            super.readEntityFromNBT(par1NBTTagCompound);
074            this.angerLevel = par1NBTTagCompound.getShort("Anger");
075        }
076    
077        /**
078         * Finds the closest player within 16 blocks to attack, or null if this Entity isn't interested in attacking
079         * (Animals, Spiders at day, peaceful PigZombies).
080         */
081        protected Entity findPlayerToAttack()
082        {
083            return this.angerLevel == 0 ? null : super.findPlayerToAttack();
084        }
085    
086        /**
087         * Called when the entity is attacked.
088         */
089        public boolean attackEntityFrom(DamageSource par1DamageSource, int par2)
090        {
091            Entity var3 = par1DamageSource.getEntity();
092    
093            if (var3 instanceof EntityPlayer)
094            {
095                List var4 = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.expand(32.0D, 32.0D, 32.0D));
096                Iterator var5 = var4.iterator();
097    
098                while (var5.hasNext())
099                {
100                    Entity var6 = (Entity)var5.next();
101    
102                    if (var6 instanceof EntityPigZombie)
103                    {
104                        EntityPigZombie var7 = (EntityPigZombie)var6;
105                        var7.becomeAngryAt(var3);
106                    }
107                }
108    
109                this.becomeAngryAt(var3);
110            }
111    
112            return super.attackEntityFrom(par1DamageSource, par2);
113        }
114    
115        /**
116         * Causes this PigZombie to become angry at the supplied Entity (which will be a player).
117         */
118        private void becomeAngryAt(Entity par1Entity)
119        {
120            this.entityToAttack = par1Entity;
121            this.angerLevel = 400 + this.rand.nextInt(400);
122            this.randomSoundDelay = this.rand.nextInt(40);
123        }
124    
125        /**
126         * Returns the sound this mob makes while it's alive.
127         */
128        protected String getLivingSound()
129        {
130            return "mob.zombiepig.zpig";
131        }
132    
133        /**
134         * Returns the sound this mob makes when it is hurt.
135         */
136        protected String getHurtSound()
137        {
138            return "mob.zombiepig.zpighurt";
139        }
140    
141        /**
142         * Returns the sound this mob makes on death.
143         */
144        protected String getDeathSound()
145        {
146            return "mob.zombiepig.zpigdeath";
147        }
148    
149        /**
150         * Drop 0-2 items of this living's type
151         */
152        protected void dropFewItems(boolean par1, int par2)
153        {
154            int var3 = this.rand.nextInt(2 + par2);
155            int var4;
156    
157            for (var4 = 0; var4 < var3; ++var4)
158            {
159                this.dropItem(Item.rottenFlesh.shiftedIndex, 1);
160            }
161    
162            var3 = this.rand.nextInt(2 + par2);
163    
164            for (var4 = 0; var4 < var3; ++var4)
165            {
166                this.dropItem(Item.goldNugget.shiftedIndex, 1);
167            }
168        }
169    
170        protected void dropRareDrop(int par1)
171        {
172            if (par1 > 0)
173            {
174                ItemStack var2 = new ItemStack(Item.swordGold);
175                EnchantmentHelper.addRandomEnchantment(this.rand, var2, 5);
176                this.entityDropItem(var2, 0.0F);
177            }
178            else
179            {
180                int var3 = this.rand.nextInt(3);
181    
182                if (var3 == 0)
183                {
184                    this.dropItem(Item.ingotGold.shiftedIndex, 1);
185                }
186                else if (var3 == 1)
187                {
188                    this.dropItem(Item.swordGold.shiftedIndex, 1);
189                }
190                else if (var3 == 2)
191                {
192                    this.dropItem(Item.helmetGold.shiftedIndex, 1);
193                }
194            }
195        }
196    
197        /**
198         * Returns the item ID for the item the mob drops on death.
199         */
200        protected int getDropItemId()
201        {
202            return Item.rottenFlesh.shiftedIndex;
203        }
204    
205        @SideOnly(Side.CLIENT)
206    
207        /**
208         * Returns the item that this EntityLiving is holding, if any.
209         */
210        public ItemStack getHeldItem()
211        {
212            return defaultHeldItem;
213        }
214    }