001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    
006    public class EntityIronGolem extends EntityGolem
007    {
008        /** deincrements, and a distance-to-home check is done at 0 */
009        private int homeCheckTimer = 0;
010        Village villageObj = null;
011        private int attackTimer;
012        private int holdRoseTick;
013    
014        public EntityIronGolem(World par1World)
015        {
016            super(par1World);
017            this.texture = "/mob/villager_golem.png";
018            this.setSize(1.4F, 2.9F);
019            this.getNavigator().setAvoidsWater(true);
020            this.tasks.addTask(1, new EntityAIAttackOnCollide(this, 0.25F, true));
021            this.tasks.addTask(2, new EntityAIMoveTowardsTarget(this, 0.22F, 32.0F));
022            this.tasks.addTask(3, new EntityAIMoveThroughVillage(this, 0.16F, true));
023            this.tasks.addTask(4, new EntityAIMoveTwardsRestriction(this, 0.16F));
024            this.tasks.addTask(5, new EntityAILookAtVillager(this));
025            this.tasks.addTask(6, new EntityAIWander(this, 0.16F));
026            this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
027            this.tasks.addTask(8, new EntityAILookIdle(this));
028            this.targetTasks.addTask(1, new EntityAIDefendVillage(this));
029            this.targetTasks.addTask(2, new EntityAIHurtByTarget(this, false));
030            this.targetTasks.addTask(3, new EntityAINearestAttackableTarget(this, EntityLiving.class, 16.0F, 0, false, true, IMob.mobSelector));
031        }
032    
033        protected void entityInit()
034        {
035            super.entityInit();
036            this.dataWatcher.addObject(16, Byte.valueOf((byte)0));
037        }
038    
039        /**
040         * Returns true if the newer Entity AI code should be run
041         */
042        public boolean isAIEnabled()
043        {
044            return true;
045        }
046    
047        /**
048         * main AI tick function, replaces updateEntityActionState
049         */
050        protected void updateAITick()
051        {
052            if (--this.homeCheckTimer <= 0)
053            {
054                this.homeCheckTimer = 70 + this.rand.nextInt(50);
055                this.villageObj = this.worldObj.villageCollectionObj.findNearestVillage(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.posY), MathHelper.floor_double(this.posZ), 32);
056    
057                if (this.villageObj == null)
058                {
059                    this.detachHome();
060                }
061                else
062                {
063                    ChunkCoordinates var1 = this.villageObj.getCenter();
064                    this.setHomeArea(var1.posX, var1.posY, var1.posZ, (int)((float)this.villageObj.getVillageRadius() * 0.6F));
065                }
066            }
067    
068            super.updateAITick();
069        }
070    
071        public int getMaxHealth()
072        {
073            return 100;
074        }
075    
076        /**
077         * Decrements the entity's air supply when underwater
078         */
079        protected int decreaseAirSupply(int par1)
080        {
081            return par1;
082        }
083    
084        protected void collideWithEntity(Entity par1Entity)
085        {
086            if (par1Entity instanceof IMob && this.getRNG().nextInt(20) == 0)
087            {
088                this.setAttackTarget((EntityLiving)par1Entity);
089            }
090    
091            super.collideWithEntity(par1Entity);
092        }
093    
094        /**
095         * Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
096         * use this to react to sunlight and start to burn.
097         */
098        public void onLivingUpdate()
099        {
100            super.onLivingUpdate();
101    
102            if (this.attackTimer > 0)
103            {
104                --this.attackTimer;
105            }
106    
107            if (this.holdRoseTick > 0)
108            {
109                --this.holdRoseTick;
110            }
111    
112            if (this.motionX * this.motionX + this.motionZ * this.motionZ > 2.500000277905201E-7D && this.rand.nextInt(5) == 0)
113            {
114                int var1 = MathHelper.floor_double(this.posX);
115                int var2 = MathHelper.floor_double(this.posY - 0.20000000298023224D - (double)this.yOffset);
116                int var3 = MathHelper.floor_double(this.posZ);
117                int var4 = this.worldObj.getBlockId(var1, var2, var3);
118    
119                if (var4 > 0)
120                {
121                    this.worldObj.spawnParticle("tilecrack_" + var4 + "_" + this.worldObj.getBlockMetadata(var1, var2, var3), this.posX + ((double)this.rand.nextFloat() - 0.5D) * (double)this.width, this.boundingBox.minY + 0.1D, this.posZ + ((double)this.rand.nextFloat() - 0.5D) * (double)this.width, 4.0D * ((double)this.rand.nextFloat() - 0.5D), 0.5D, ((double)this.rand.nextFloat() - 0.5D) * 4.0D);
122                }
123            }
124        }
125    
126        /**
127         * Returns true if this entity can attack entities of the specified class.
128         */
129        public boolean canAttackClass(Class par1Class)
130        {
131            return this.isPlayerCreated() && EntityPlayer.class.isAssignableFrom(par1Class) ? false : super.canAttackClass(par1Class);
132        }
133    
134        /**
135         * (abstract) Protected helper method to write subclass entity data to NBT.
136         */
137        public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
138        {
139            super.writeEntityToNBT(par1NBTTagCompound);
140            par1NBTTagCompound.setBoolean("PlayerCreated", this.isPlayerCreated());
141        }
142    
143        /**
144         * (abstract) Protected helper method to read subclass entity data from NBT.
145         */
146        public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
147        {
148            super.readEntityFromNBT(par1NBTTagCompound);
149            this.setPlayerCreated(par1NBTTagCompound.getBoolean("PlayerCreated"));
150        }
151    
152        public boolean attackEntityAsMob(Entity par1Entity)
153        {
154            this.attackTimer = 10;
155            this.worldObj.setEntityState(this, (byte)4);
156            boolean var2 = par1Entity.attackEntityFrom(DamageSource.causeMobDamage(this), 7 + this.rand.nextInt(15));
157    
158            if (var2)
159            {
160                par1Entity.motionY += 0.4000000059604645D;
161            }
162    
163            this.func_85030_a("mob.irongolem.throw", 1.0F, 1.0F);
164            return var2;
165        }
166    
167        public Village getVillage()
168        {
169            return this.villageObj;
170        }
171    
172        @SideOnly(Side.CLIENT)
173        public void handleHealthUpdate(byte par1)
174        {
175            if (par1 == 4)
176            {
177                this.attackTimer = 10;
178                this.func_85030_a("mob.irongolem.throw", 1.0F, 1.0F);
179            }
180            else if (par1 == 11)
181            {
182                this.holdRoseTick = 400;
183            }
184            else
185            {
186                super.handleHealthUpdate(par1);
187            }
188        }
189    
190        @SideOnly(Side.CLIENT)
191        public int getAttackTimer()
192        {
193            return this.attackTimer;
194        }
195    
196        public void setHoldingRose(boolean par1)
197        {
198            this.holdRoseTick = par1 ? 400 : 0;
199            this.worldObj.setEntityState(this, (byte)11);
200        }
201    
202        /**
203         * Returns the sound this mob makes while it's alive.
204         */
205        protected String getLivingSound()
206        {
207            return "none";
208        }
209    
210        /**
211         * Returns the sound this mob makes when it is hurt.
212         */
213        protected String getHurtSound()
214        {
215            return "mob.irongolem.hit";
216        }
217    
218        /**
219         * Returns the sound this mob makes on death.
220         */
221        protected String getDeathSound()
222        {
223            return "mob.irongolem.death";
224        }
225    
226        /**
227         * Plays step sound at given x, y, z for the entity
228         */
229        protected void playStepSound(int par1, int par2, int par3, int par4)
230        {
231            this.func_85030_a("mob.irongolem.walk", 1.0F, 1.0F);
232        }
233    
234        /**
235         * Drop 0-2 items of this living's type
236         */
237        protected void dropFewItems(boolean par1, int par2)
238        {
239            int var3 = this.rand.nextInt(3);
240            int var4;
241    
242            for (var4 = 0; var4 < var3; ++var4)
243            {
244                this.dropItem(Block.plantRed.blockID, 1);
245            }
246    
247            var4 = 3 + this.rand.nextInt(3);
248    
249            for (int var5 = 0; var5 < var4; ++var5)
250            {
251                this.dropItem(Item.ingotIron.shiftedIndex, 1);
252            }
253        }
254    
255        public int getHoldRoseTick()
256        {
257            return this.holdRoseTick;
258        }
259    
260        public boolean isPlayerCreated()
261        {
262            return (this.dataWatcher.getWatchableObjectByte(16) & 1) != 0;
263        }
264    
265        public void setPlayerCreated(boolean par1)
266        {
267            byte var2 = this.dataWatcher.getWatchableObjectByte(16);
268    
269            if (par1)
270            {
271                this.dataWatcher.updateObject(16, Byte.valueOf((byte)(var2 | 1)));
272            }
273            else
274            {
275                this.dataWatcher.updateObject(16, Byte.valueOf((byte)(var2 & -2)));
276            }
277        }
278    
279        /**
280         * Called when the mob's health reaches 0.
281         */
282        public void onDeath(DamageSource par1DamageSource)
283        {
284            if (!this.isPlayerCreated() && this.attackingPlayer != null && this.villageObj != null)
285            {
286                this.villageObj.setReputationForPlayer(this.attackingPlayer.getCommandSenderName(), -5);
287            }
288    
289            super.onDeath(par1DamageSource);
290        }
291    }