| 1 | package net.minecraft.entity.monster;
|
| 2 |
|
| 3 | import net.minecraft.entity.Entity;
|
| 4 | import net.minecraft.entity.SharedMonsterAttributes;
|
| 5 | import net.minecraft.entity.ai.EntityAIAttackOnCollide;
|
| 6 | import net.minecraft.entity.ai.EntityAIAvoidEntity;
|
| 7 | import net.minecraft.entity.ai.EntityAICreeperSwell;
|
| 8 | import net.minecraft.entity.ai.EntityAIHurtByTarget;
|
| 9 | import net.minecraft.entity.ai.EntityAILookIdle;
|
| 10 | import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
|
| 11 | import net.minecraft.entity.ai.EntityAISwimming;
|
| 12 | import net.minecraft.entity.ai.EntityAIWander;
|
| 13 | import net.minecraft.entity.ai.EntityAIWatchClosest;
|
| 14 | import net.minecraft.entity.effect.EntityLightningBolt;
|
| 15 | import net.minecraft.entity.passive.EntityOcelot;
|
| 16 | import net.minecraft.entity.player.EntityPlayer;
|
| 17 | import net.minecraft.init.Items;
|
| 18 | import net.minecraft.item.Item;
|
| 19 | import net.minecraft.item.ItemStack;
|
| 20 | import net.minecraft.nbt.NBTTagCompound;
|
| 21 | import net.minecraft.util.DamageSource;
|
| 22 | import net.minecraft.world.World;
|
| 23 |
|
| 24 | public class EntityCreeper extends EntityMob
|
| 25 | {
|
| 26 | /**
|
| 27 | * Time when this creeper was last in an active state (Messed up code here, probably causes creeper animation to go
|
| 28 | * weird)
|
| 29 | */
|
| 30 | private int lastActiveTime;
|
| 31 |
|
| 32 | /**
|
| 33 | * The amount of time since the creeper was close enough to the player to ignite
|
| 34 | */
|
| 35 | private int timeSinceIgnited;
|
| 36 | private int fuseTime = 30;
|
| 37 |
|
| 38 | /** Explosion radius for this creeper. */
|
| 39 | private int explosionRadius = 3;
|
| 40 | private int field_175494_bm = 0;
|
| 41 |
|
| 42 | public EntityCreeper(World worldIn)
|
| 43 | {
|
| 44 | super(worldIn);
|
| 45 | this.tasks.addTask(1, new EntityAISwimming(this));
|
| 46 | this.tasks.addTask(2, new EntityAICreeperSwell(this));
|
| 47 | this.tasks.addTask(3, new EntityAIAvoidEntity(this, EntityOcelot.class, 6.0F, 1.0D, 1.2D));
|
| 48 | this.tasks.addTask(4, new EntityAIAttackOnCollide(this, 1.0D, false));
|
| 49 | this.tasks.addTask(5, new EntityAIWander(this, 0.8D));
|
| 50 | this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
|
| 51 | this.tasks.addTask(6, new EntityAILookIdle(this));
|
| 52 | this.targetTasks.addTask(1, new EntityAINearestAttackableTarget(this, EntityPlayer.class, true));
|
| 53 | this.targetTasks.addTask(2, new EntityAIHurtByTarget(this, false, new Class[0]));
|
| 54 | }
|
| 55 |
|
| 56 | protected void applyEntityAttributes()
|
| 57 | {
|
| 58 | super.applyEntityAttributes();
|
| 59 | this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.25D);
|
| 60 | }
|
| 61 |
|
| 62 | /**
|
| 63 | * The maximum height from where the entity is alowed to jump (used in pathfinder)
|
| 64 | */
|
| 65 | public int getMaxFallHeight()
|
| 66 | {
|
| 67 | return this.getAttackTarget() == null ? 3 : 3 + (int)(this.getHealth() - 1.0F);
|
| 68 | }
|
| 69 |
|
| 70 | public void fall(float distance, float damageMultiplier)
|
| 71 | {
|
| 72 | super.fall(distance, damageMultiplier);
|
| 73 | this.timeSinceIgnited = (int)((float)this.timeSinceIgnited + distance * 1.5F);
|
| 74 |
|
| 75 | if (this.timeSinceIgnited > this.fuseTime - 5)
|
| 76 | {
|
| 77 | this.timeSinceIgnited = this.fuseTime - 5;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | protected void entityInit()
|
| 82 | {
|
| 83 | super.entityInit();
|
| 84 | this.dataWatcher.addObject(16, Byte.valueOf((byte) - 1));
|
| 85 | this.dataWatcher.addObject(17, Byte.valueOf((byte)0));
|
| 86 | this.dataWatcher.addObject(18, Byte.valueOf((byte)0));
|
| 87 | }
|
| 88 |
|
| 89 | /**
|
| 90 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 91 | */
|
| 92 | public void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 93 | {
|
| 94 | super.writeEntityToNBT(tagCompound);
|
| 95 |
|
| 96 | if (this.dataWatcher.getWatchableObjectByte(17) == 1)
|
| 97 | {
|
| 98 | tagCompound.setBoolean("powered", true);
|
| 99 | }
|
| 100 |
|
| 101 | tagCompound.setShort("Fuse", (short)this.fuseTime);
|
| 102 | tagCompound.setByte("ExplosionRadius", (byte)this.explosionRadius);
|
| 103 | tagCompound.setBoolean("ignited", this.hasIgnited());
|
| 104 | }
|
| 105 |
|
| 106 | /**
|
| 107 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 108 | */
|
| 109 | public void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 110 | {
|
| 111 | super.readEntityFromNBT(tagCompund);
|
| 112 | this.dataWatcher.updateObject(17, Byte.valueOf((byte)(tagCompund.getBoolean("powered") ? 1 : 0)));
|
| 113 |
|
| 114 | if (tagCompund.hasKey("Fuse", 99))
|
| 115 | {
|
| 116 | this.fuseTime = tagCompund.getShort("Fuse");
|
| 117 | }
|
| 118 |
|
| 119 | if (tagCompund.hasKey("ExplosionRadius", 99))
|
| 120 | {
|
| 121 | this.explosionRadius = tagCompund.getByte("ExplosionRadius");
|
| 122 | }
|
| 123 |
|
| 124 | if (tagCompund.getBoolean("ignited"))
|
| 125 | {
|
| 126 | this.ignite();
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | /**
|
| 131 | * Called to update the entity's position/logic.
|
| 132 | */
|
| 133 | public void onUpdate()
|
| 134 | {
|
| 135 | if (this.isEntityAlive())
|
| 136 | {
|
| 137 | this.lastActiveTime = this.timeSinceIgnited;
|
| 138 |
|
| 139 | if (this.hasIgnited())
|
| 140 | {
|
| 141 | this.setCreeperState(1);
|
| 142 | }
|
| 143 |
|
| 144 | int i = this.getCreeperState();
|
| 145 |
|
| 146 | if (i > 0 && this.timeSinceIgnited == 0)
|
| 147 | {
|
| 148 | this.playSound("creeper.primed", 1.0F, 0.5F);
|
| 149 | }
|
| 150 |
|
| 151 | this.timeSinceIgnited += i;
|
| 152 |
|
| 153 | if (this.timeSinceIgnited < 0)
|
| 154 | {
|
| 155 | this.timeSinceIgnited = 0;
|
| 156 | }
|
| 157 |
|
| 158 | if (this.timeSinceIgnited >= this.fuseTime)
|
| 159 | {
|
| 160 | this.timeSinceIgnited = this.fuseTime;
|
| 161 | this.explode();
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | super.onUpdate();
|
| 166 | }
|
| 167 |
|
| 168 | /**
|
| 169 | * Returns the sound this mob makes when it is hurt.
|
| 170 | */
|
| 171 | protected String getHurtSound()
|
| 172 | {
|
| 173 | return "mob.creeper.say";
|
| 174 | }
|
| 175 |
|
| 176 | /**
|
| 177 | * Returns the sound this mob makes on death.
|
| 178 | */
|
| 179 | protected String getDeathSound()
|
| 180 | {
|
| 181 | return "mob.creeper.death";
|
| 182 | }
|
| 183 |
|
| 184 | /**
|
| 185 | * Called when the mob's health reaches 0.
|
| 186 | */
|
| 187 | public void onDeath(DamageSource cause)
|
| 188 | {
|
| 189 | super.onDeath(cause);
|
| 190 |
|
| 191 | if (cause.getEntity() instanceof EntitySkeleton)
|
| 192 | {
|
| 193 | int i = Item.getIdFromItem(Items.record_13);
|
| 194 | int j = Item.getIdFromItem(Items.record_wait);
|
| 195 | int k = i + this.rand.nextInt(j - i + 1);
|
| 196 | this.dropItem(Item.getItemById(k), 1);
|
| 197 | }
|
| 198 | else if (cause.getEntity() instanceof EntityCreeper && cause.getEntity() != this && ((EntityCreeper)cause.getEntity()).getPowered() && ((EntityCreeper)cause.getEntity()).isAIEnabled())
|
| 199 | {
|
| 200 | ((EntityCreeper)cause.getEntity()).func_175493_co();
|
| 201 | this.entityDropItem(new ItemStack(Items.skull, 1, 4), 0.0F);
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | public boolean attackEntityAsMob(Entity entityIn)
|
| 206 | {
|
| 207 | return true;
|
| 208 | }
|
| 209 |
|
| 210 | /**
|
| 211 | * Returns true if the creeper is powered by a lightning bolt.
|
| 212 | */
|
| 213 | public boolean getPowered()
|
| 214 | {
|
| 215 | return this.dataWatcher.getWatchableObjectByte(17) == 1;
|
| 216 | }
|
| 217 |
|
| 218 | /**
|
| 219 | * Params: (Float)Render tick. Returns the intensity of the creeper's flash when it is ignited.
|
| 220 | */
|
| 221 | public float getCreeperFlashIntensity(float p_70831_1_)
|
| 222 | {
|
| 223 | return ((float)this.lastActiveTime + (float)(this.timeSinceIgnited - this.lastActiveTime) * p_70831_1_) / (float)(this.fuseTime - 2);
|
| 224 | }
|
| 225 |
|
| 226 | protected Item getDropItem()
|
| 227 | {
|
| 228 | return Items.gunpowder;
|
| 229 | }
|
| 230 |
|
| 231 | /**
|
| 232 | * Returns the current state of creeper, -1 is idle, 1 is 'in fuse'
|
| 233 | */
|
| 234 | public int getCreeperState()
|
| 235 | {
|
| 236 | return this.dataWatcher.getWatchableObjectByte(16);
|
| 237 | }
|
| 238 |
|
| 239 | /**
|
| 240 | * Sets the state of creeper, -1 to idle and 1 to be 'in fuse'
|
| 241 | */
|
| 242 | public void setCreeperState(int state)
|
| 243 | {
|
| 244 | this.dataWatcher.updateObject(16, Byte.valueOf((byte)state));
|
| 245 | }
|
| 246 |
|
| 247 | /**
|
| 248 | * Called when a lightning bolt hits the entity.
|
| 249 | */
|
| 250 | public void onStruckByLightning(EntityLightningBolt lightningBolt)
|
| 251 | {
|
| 252 | super.onStruckByLightning(lightningBolt);
|
| 253 | this.dataWatcher.updateObject(17, Byte.valueOf((byte)1));
|
| 254 | }
|
| 255 |
|
| 256 | /**
|
| 257 | * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
|
| 258 | */
|
| 259 | protected boolean interact(EntityPlayer player)
|
| 260 | {
|
| 261 | ItemStack itemstack = player.inventory.getCurrentItem();
|
| 262 |
|
| 263 | if (itemstack != null && itemstack.getItem() == Items.flint_and_steel)
|
| 264 | {
|
| 265 | this.worldObj.playSoundEffect(this.posX + 0.5D, this.posY + 0.5D, this.posZ + 0.5D, "fire.ignite", 1.0F, this.rand.nextFloat() * 0.4F + 0.8F);
|
| 266 | player.swingItem();
|
| 267 |
|
| 268 | if (!this.worldObj.isRemote)
|
| 269 | {
|
| 270 | this.ignite();
|
| 271 | itemstack.damageItem(1, player);
|
| 272 | return true;
|
| 273 | }
|
| 274 | }
|
| 275 |
|
| 276 | return super.interact(player);
|
| 277 | }
|
| 278 |
|
| 279 | /**
|
| 280 | * Creates an explosion as determined by this creeper's power and explosion radius.
|
| 281 | */
|
| 282 | private void explode()
|
| 283 | {
|
| 284 | if (!this.worldObj.isRemote)
|
| 285 | {
|
| 286 | boolean flag = this.worldObj.getGameRules().getBoolean("mobGriefing");
|
| 287 | float f = this.getPowered() ? 2.0F : 1.0F;
|
| 288 | this.worldObj.createExplosion(this, this.posX, this.posY, this.posZ, (float)this.explosionRadius * f, flag);
|
| 289 | this.setDead();
|
| 290 | }
|
| 291 | }
|
| 292 |
|
| 293 | public boolean hasIgnited()
|
| 294 | {
|
| 295 | return this.dataWatcher.getWatchableObjectByte(18) != 0;
|
| 296 | }
|
| 297 |
|
| 298 | public void ignite()
|
| 299 | {
|
| 300 | this.dataWatcher.updateObject(18, Byte.valueOf((byte)1));
|
| 301 | }
|
| 302 |
|
| 303 | /**
|
| 304 | * Returns true if the newer Entity AI code should be run
|
| 305 | */
|
| 306 | public boolean isAIEnabled()
|
| 307 | {
|
| 308 | return this.field_175494_bm < 1 && this.worldObj.getGameRules().getBoolean("doMobLoot");
|
| 309 | }
|
| 310 |
|
| 311 | public void func_175493_co()
|
| 312 | {
|
| 313 | ++this.field_175494_bm;
|
| 314 | }
|
| 315 | }
|
| 316 |
|