| 1 | package net.minecraft.entity.monster;
|
| 2 |
|
| 3 | import com.google.common.base.Predicate;
|
| 4 | import net.minecraft.block.Block;
|
| 5 | import net.minecraft.block.BlockFlower;
|
| 6 | import net.minecraft.block.material.Material;
|
| 7 | import net.minecraft.block.state.IBlockState;
|
| 8 | import net.minecraft.entity.Entity;
|
| 9 | import net.minecraft.entity.EntityCreature;
|
| 10 | import net.minecraft.entity.EntityLiving;
|
| 11 | import net.minecraft.entity.EntityLivingBase;
|
| 12 | import net.minecraft.entity.SharedMonsterAttributes;
|
| 13 | import net.minecraft.entity.ai.EntityAIAttackOnCollide;
|
| 14 | import net.minecraft.entity.ai.EntityAIDefendVillage;
|
| 15 | import net.minecraft.entity.ai.EntityAIHurtByTarget;
|
| 16 | import net.minecraft.entity.ai.EntityAILookAtVillager;
|
| 17 | import net.minecraft.entity.ai.EntityAILookIdle;
|
| 18 | import net.minecraft.entity.ai.EntityAIMoveThroughVillage;
|
| 19 | import net.minecraft.entity.ai.EntityAIMoveTowardsRestriction;
|
| 20 | import net.minecraft.entity.ai.EntityAIMoveTowardsTarget;
|
| 21 | import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
|
| 22 | import net.minecraft.entity.ai.EntityAIWander;
|
| 23 | import net.minecraft.entity.ai.EntityAIWatchClosest;
|
| 24 | import net.minecraft.entity.player.EntityPlayer;
|
| 25 | import net.minecraft.init.Blocks;
|
| 26 | import net.minecraft.init.Items;
|
| 27 | import net.minecraft.item.Item;
|
| 28 | import net.minecraft.nbt.NBTTagCompound;
|
| 29 | import net.minecraft.pathfinding.PathNavigateGround;
|
| 30 | import net.minecraft.util.BlockPos;
|
| 31 | import net.minecraft.util.DamageSource;
|
| 32 | import net.minecraft.util.EnumParticleTypes;
|
| 33 | import net.minecraft.util.MathHelper;
|
| 34 | import net.minecraft.village.Village;
|
| 35 | import net.minecraft.world.World;
|
| 36 |
|
| 37 | public class EntityIronGolem extends EntityGolem
|
| 38 | {
|
| 39 | /** deincrements, and a distance-to-home check is done at 0 */
|
| 40 | private int homeCheckTimer;
|
| 41 | Village villageObj;
|
| 42 | private int attackTimer;
|
| 43 | private int holdRoseTick;
|
| 44 |
|
| 45 | public EntityIronGolem(World worldIn)
|
| 46 | {
|
| 47 | super(worldIn);
|
| 48 | this.setSize(1.4F, 2.9F);
|
| 49 | ((PathNavigateGround)this.getNavigator()).setAvoidsWater(true);
|
| 50 | this.tasks.addTask(1, new EntityAIAttackOnCollide(this, 1.0D, true));
|
| 51 | this.tasks.addTask(2, new EntityAIMoveTowardsTarget(this, 0.9D, 32.0F));
|
| 52 | this.tasks.addTask(3, new EntityAIMoveThroughVillage(this, 0.6D, true));
|
| 53 | this.tasks.addTask(4, new EntityAIMoveTowardsRestriction(this, 1.0D));
|
| 54 | this.tasks.addTask(5, new EntityAILookAtVillager(this));
|
| 55 | this.tasks.addTask(6, new EntityAIWander(this, 0.6D));
|
| 56 | this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
|
| 57 | this.tasks.addTask(8, new EntityAILookIdle(this));
|
| 58 | this.targetTasks.addTask(1, new EntityAIDefendVillage(this));
|
| 59 | this.targetTasks.addTask(2, new EntityAIHurtByTarget(this, false, new Class[0]));
|
| 60 | this.targetTasks.addTask(3, new EntityIronGolem.AINearestAttackableTargetNonCreeper(this, EntityLiving.class, 10, false, true, IMob.VISIBLE_MOB_SELECTOR));
|
| 61 | }
|
| 62 |
|
| 63 | protected void entityInit()
|
| 64 | {
|
| 65 | super.entityInit();
|
| 66 | this.dataWatcher.addObject(16, Byte.valueOf((byte)0));
|
| 67 | }
|
| 68 |
|
| 69 | protected void updateAITasks()
|
| 70 | {
|
| 71 | if (--this.homeCheckTimer <= 0)
|
| 72 | {
|
| 73 | this.homeCheckTimer = 70 + this.rand.nextInt(50);
|
| 74 | this.villageObj = this.worldObj.getVillageCollection().getNearestVillage(new BlockPos(this), 32);
|
| 75 |
|
| 76 | if (this.villageObj == null)
|
| 77 | {
|
| 78 | this.detachHome();
|
| 79 | }
|
| 80 | else
|
| 81 | {
|
| 82 | BlockPos blockpos = this.villageObj.getCenter();
|
| 83 | this.setHomePosAndDistance(blockpos, (int)((float)this.villageObj.getVillageRadius() * 0.6F));
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | super.updateAITasks();
|
| 88 | }
|
| 89 |
|
| 90 | protected void applyEntityAttributes()
|
| 91 | {
|
| 92 | super.applyEntityAttributes();
|
| 93 | this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(100.0D);
|
| 94 | this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.25D);
|
| 95 | }
|
| 96 |
|
| 97 | /**
|
| 98 | * Decrements the entity's air supply when underwater
|
| 99 | */
|
| 100 | protected int decreaseAirSupply(int p_70682_1_)
|
| 101 | {
|
| 102 | return p_70682_1_;
|
| 103 | }
|
| 104 |
|
| 105 | protected void collideWithEntity(Entity entityIn)
|
| 106 | {
|
| 107 | if (entityIn instanceof IMob && !(entityIn instanceof EntityCreeper) && this.getRNG().nextInt(20) == 0)
|
| 108 | {
|
| 109 | this.setAttackTarget((EntityLivingBase)entityIn);
|
| 110 | }
|
| 111 |
|
| 112 | super.collideWithEntity(entityIn);
|
| 113 | }
|
| 114 |
|
| 115 | /**
|
| 116 | * Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
|
| 117 | * use this to react to sunlight and start to burn.
|
| 118 | */
|
| 119 | public void onLivingUpdate()
|
| 120 | {
|
| 121 | super.onLivingUpdate();
|
| 122 |
|
| 123 | if (this.attackTimer > 0)
|
| 124 | {
|
| 125 | --this.attackTimer;
|
| 126 | }
|
| 127 |
|
| 128 | if (this.holdRoseTick > 0)
|
| 129 | {
|
| 130 | --this.holdRoseTick;
|
| 131 | }
|
| 132 |
|
| 133 | if (this.motionX * this.motionX + this.motionZ * this.motionZ > 2.500000277905201E-7D && this.rand.nextInt(5) == 0)
|
| 134 | {
|
| 135 | int i = MathHelper.floor_double(this.posX);
|
| 136 | int j = MathHelper.floor_double(this.posY - 0.20000000298023224D);
|
| 137 | int k = MathHelper.floor_double(this.posZ);
|
| 138 | IBlockState iblockstate = this.worldObj.getBlockState(new BlockPos(i, j, k));
|
| 139 | Block block = iblockstate.getBlock();
|
| 140 |
|
| 141 | if (block.getMaterial() != Material.air)
|
| 142 | {
|
| 143 | this.worldObj.spawnParticle(EnumParticleTypes.BLOCK_CRACK, this.posX + ((double)this.rand.nextFloat() - 0.5D) * (double)this.width, this.getEntityBoundingBox().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, new int[] {Block.getStateId(iblockstate)});
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | /**
|
| 149 | * Returns true if this entity can attack entities of the specified class.
|
| 150 | */
|
| 151 | public boolean canAttackClass(Class <? extends EntityLivingBase > cls)
|
| 152 | {
|
| 153 | return this.isPlayerCreated() && EntityPlayer.class.isAssignableFrom(cls) ? false : (cls == EntityCreeper.class ? false : super.canAttackClass(cls));
|
| 154 | }
|
| 155 |
|
| 156 | /**
|
| 157 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 158 | */
|
| 159 | public void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 160 | {
|
| 161 | super.writeEntityToNBT(tagCompound);
|
| 162 | tagCompound.setBoolean("PlayerCreated", this.isPlayerCreated());
|
| 163 | }
|
| 164 |
|
| 165 | /**
|
| 166 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 167 | */
|
| 168 | public void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 169 | {
|
| 170 | super.readEntityFromNBT(tagCompund);
|
| 171 | this.setPlayerCreated(tagCompund.getBoolean("PlayerCreated"));
|
| 172 | }
|
| 173 |
|
| 174 | public boolean attackEntityAsMob(Entity entityIn)
|
| 175 | {
|
| 176 | this.attackTimer = 10;
|
| 177 | this.worldObj.setEntityState(this, (byte)4);
|
| 178 | boolean flag = entityIn.attackEntityFrom(DamageSource.causeMobDamage(this), (float)(7 + this.rand.nextInt(15)));
|
| 179 |
|
| 180 | if (flag)
|
| 181 | {
|
| 182 | entityIn.motionY += 0.4000000059604645D;
|
| 183 | this.applyEnchantments(this, entityIn);
|
| 184 | }
|
| 185 |
|
| 186 | this.playSound("mob.irongolem.throw", 1.0F, 1.0F);
|
| 187 | return flag;
|
| 188 | }
|
| 189 |
|
| 190 | public void handleStatusUpdate(byte id)
|
| 191 | {
|
| 192 | if (id == 4)
|
| 193 | {
|
| 194 | this.attackTimer = 10;
|
| 195 | this.playSound("mob.irongolem.throw", 1.0F, 1.0F);
|
| 196 | }
|
| 197 | else if (id == 11)
|
| 198 | {
|
| 199 | this.holdRoseTick = 400;
|
| 200 | }
|
| 201 | else
|
| 202 | {
|
| 203 | super.handleStatusUpdate(id);
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | public Village getVillage()
|
| 208 | {
|
| 209 | return this.villageObj;
|
| 210 | }
|
| 211 |
|
| 212 | public int getAttackTimer()
|
| 213 | {
|
| 214 | return this.attackTimer;
|
| 215 | }
|
| 216 |
|
| 217 | public void setHoldingRose(boolean p_70851_1_)
|
| 218 | {
|
| 219 | this.holdRoseTick = p_70851_1_ ? 400 : 0;
|
| 220 | this.worldObj.setEntityState(this, (byte)11);
|
| 221 | }
|
| 222 |
|
| 223 | /**
|
| 224 | * Returns the sound this mob makes when it is hurt.
|
| 225 | */
|
| 226 | protected String getHurtSound()
|
| 227 | {
|
| 228 | return "mob.irongolem.hit";
|
| 229 | }
|
| 230 |
|
| 231 | /**
|
| 232 | * Returns the sound this mob makes on death.
|
| 233 | */
|
| 234 | protected String getDeathSound()
|
| 235 | {
|
| 236 | return "mob.irongolem.death";
|
| 237 | }
|
| 238 |
|
| 239 | protected void playStepSound(BlockPos pos, Block blockIn)
|
| 240 | {
|
| 241 | this.playSound("mob.irongolem.walk", 1.0F, 1.0F);
|
| 242 | }
|
| 243 |
|
| 244 | /**
|
| 245 | * Drop 0-2 items of this living's type
|
| 246 | *
|
| 247 | * @param wasRecentlyHit true if this this entity was recently hit by appropriate entity (generally only if player
|
| 248 | * or tameable)
|
| 249 | * @param lootingModifier level of enchanment to be applied to this drop
|
| 250 | */
|
| 251 | protected void dropFewItems(boolean wasRecentlyHit, int lootingModifier)
|
| 252 | {
|
| 253 | int i = this.rand.nextInt(3);
|
| 254 |
|
| 255 | for (int j = 0; j < i; ++j)
|
| 256 | {
|
| 257 | this.dropItemWithOffset(Item.getItemFromBlock(Blocks.red_flower), 1, (float)BlockFlower.EnumFlowerType.POPPY.getMeta());
|
| 258 | }
|
| 259 |
|
| 260 | int l = 3 + this.rand.nextInt(3);
|
| 261 |
|
| 262 | for (int k = 0; k < l; ++k)
|
| 263 | {
|
| 264 | this.dropItem(Items.iron_ingot, 1);
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | public int getHoldRoseTick()
|
| 269 | {
|
| 270 | return this.holdRoseTick;
|
| 271 | }
|
| 272 |
|
| 273 | public boolean isPlayerCreated()
|
| 274 | {
|
| 275 | return (this.dataWatcher.getWatchableObjectByte(16) & 1) != 0;
|
| 276 | }
|
| 277 |
|
| 278 | public void setPlayerCreated(boolean p_70849_1_)
|
| 279 | {
|
| 280 | byte b0 = this.dataWatcher.getWatchableObjectByte(16);
|
| 281 |
|
| 282 | if (p_70849_1_)
|
| 283 | {
|
| 284 | this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 | 1)));
|
| 285 | }
|
| 286 | else
|
| 287 | {
|
| 288 | this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 & -2)));
|
| 289 | }
|
| 290 | }
|
| 291 |
|
| 292 | /**
|
| 293 | * Called when the mob's health reaches 0.
|
| 294 | */
|
| 295 | public void onDeath(DamageSource cause)
|
| 296 | {
|
| 297 | if (!this.isPlayerCreated() && this.attackingPlayer != null && this.villageObj != null)
|
| 298 | {
|
| 299 | this.villageObj.setReputationForPlayer(this.attackingPlayer.getName(), -5);
|
| 300 | }
|
| 301 |
|
| 302 | super.onDeath(cause);
|
| 303 | }
|
| 304 |
|
| 305 | static class AINearestAttackableTargetNonCreeper<T extends EntityLivingBase> extends EntityAINearestAttackableTarget<T>
|
| 306 | {
|
| 307 | public AINearestAttackableTargetNonCreeper(final EntityCreature creature, Class<T> classTarget, int chance, boolean p_i45858_4_, boolean p_i45858_5_, final Predicate <? super T > p_i45858_6_)
|
| 308 | {
|
| 309 | super(creature, classTarget, chance, p_i45858_4_, p_i45858_5_, p_i45858_6_);
|
| 310 | this.targetEntitySelector = new Predicate<T>()
|
| 311 | {
|
| 312 | public boolean apply(T p_apply_1_)
|
| 313 | {
|
| 314 | if (p_i45858_6_ != null && !p_i45858_6_.apply(p_apply_1_))
|
| 315 | {
|
| 316 | return false;
|
| 317 | }
|
| 318 | else if (p_apply_1_ instanceof EntityCreeper)
|
| 319 | {
|
| 320 | return false;
|
| 321 | }
|
| 322 | else
|
| 323 | {
|
| 324 | if (p_apply_1_ instanceof EntityPlayer)
|
| 325 | {
|
| 326 | double d0 = AINearestAttackableTargetNonCreeper.this.getTargetDistance();
|
| 327 |
|
| 328 | if (p_apply_1_.isSneaking())
|
| 329 | {
|
| 330 | d0 *= 0.800000011920929D;
|
| 331 | }
|
| 332 |
|
| 333 | if (p_apply_1_.isInvisible())
|
| 334 | {
|
| 335 | float f = ((EntityPlayer)p_apply_1_).getArmorVisibility();
|
| 336 |
|
| 337 | if (f < 0.1F)
|
| 338 | {
|
| 339 | f = 0.1F;
|
| 340 | }
|
| 341 |
|
| 342 | d0 *= (double)(0.7F * f);
|
| 343 | }
|
| 344 |
|
| 345 | if ((double)p_apply_1_.getDistanceToEntity(creature) > d0)
|
| 346 | {
|
| 347 | return false;
|
| 348 | }
|
| 349 | }
|
| 350 |
|
| 351 | return AINearestAttackableTargetNonCreeper.this.isSuitableTarget(p_apply_1_, false);
|
| 352 | }
|
| 353 | }
|
| 354 | };
|
| 355 | }
|
| 356 | }
|
| 357 | }
|
| 358 |
|