EntitySlime .java

net.minecraft.entity.monster.EntitySlime (class, 587 lines): members: AISlimeFaceRandom, makesSoundOnJump, prevSquishFactor, SlimeMoveHelper, AISlimeFloat.

net.minecraft.entity.monsterclass587 linescommonextends EntityLiving18.2 KB

Implements

API Surface (42)

void entityInit()
void setSlimeSize(int size)
int getSlimeSize()
void writeEntityToNBT(NBTTagCompound tagCompound)
void readEntityFromNBT(NBTTagCompound tagCompund)
EnumParticleTypes getParticleType()
String getJumpSound()
void onUpdate()
void alterSquishAmount()
int getJumpDelay()
EntitySlime createInstance()
void onDataWatcherUpdate(int dataID)
void setDead()
void applyEntityCollision(Entity entityIn)
void onCollideWithPlayer(EntityPlayer entityIn)
void func_175451_e(EntityLivingBase p_175451_1_)
float getEyeHeight()
boolean canDamagePlayer()
int getAttackStrength()
String getHurtSound()
String getDeathSound()
Item getDropItem()
boolean getCanSpawnHere()
float getSoundVolume()
int getVerticalFaceSpeed()
boolean makesSoundOnJump()
boolean makesSoundOnLand()
void jump()
IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, IEntityLivingData livingdata)
boolean shouldExecute()
void startExecuting()
boolean continueExecuting()
void updateTask()
boolean shouldExecute()
void updateTask()
boolean shouldExecute()
void updateTask()
boolean shouldExecute()
void updateTask()
void func_179920_a(float p_179920_1_, boolean p_179920_2_)
void setSpeed(double speedIn)
void onUpdateMoveHelper()

Fields & Constants (4)

squishAmount
squishFactor
prevSquishFactor
wasOnGround

String literals (5)

"Size"
"wasOnGround"
"mob.slime."
"small"
"mob.attack"

Full Source

EntitySlime.java587 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.monster;
2
3
import net.minecraft.entity.Entity;
4
import net.minecraft.entity.EntityLiving;
5
import net.minecraft.entity.EntityLivingBase;
6
import net.minecraft.entity.IEntityLivingData;
7
import net.minecraft.entity.SharedMonsterAttributes;
8
import net.minecraft.entity.ai.EntityAIBase;
9
import net.minecraft.entity.ai.EntityAIFindEntityNearest;
10
import net.minecraft.entity.ai.EntityAIFindEntityNearestPlayer;
11
import net.minecraft.entity.ai.EntityMoveHelper;
12
import net.minecraft.entity.player.EntityPlayer;
13
import net.minecraft.init.Items;
14
import net.minecraft.item.Item;
15
import net.minecraft.nbt.NBTTagCompound;
16
import net.minecraft.pathfinding.PathNavigateGround;
17
import net.minecraft.util.BlockPos;
18
import net.minecraft.util.DamageSource;
19
import net.minecraft.util.EnumParticleTypes;
20
import net.minecraft.util.MathHelper;
21
import net.minecraft.world.DifficultyInstance;
22
import net.minecraft.world.EnumDifficulty;
23
import net.minecraft.world.World;
24
import net.minecraft.world.WorldType;
25
import net.minecraft.world.biome.BiomeGenBase;
26
import net.minecraft.world.chunk.Chunk;
27
28
public class EntitySlime extends EntityLiving implements IMob
29
{
30
    public float squishAmount;
31
    public float squishFactor;
32
    public float prevSquishFactor;
33
    private boolean wasOnGround;
34
35
    public EntitySlime(World worldIn)
36
    {
37
        super(worldIn);
38
        this.moveHelper = new EntitySlime.SlimeMoveHelper(this);
39
        this.tasks.addTask(1, new EntitySlime.AISlimeFloat(this));
40
        this.tasks.addTask(2, new EntitySlime.AISlimeAttack(this));
41
        this.tasks.addTask(3, new EntitySlime.AISlimeFaceRandom(this));
42
        this.tasks.addTask(5, new EntitySlime.AISlimeHop(this));
43
        this.targetTasks.addTask(1, new EntityAIFindEntityNearestPlayer(this));
44
        this.targetTasks.addTask(3, new EntityAIFindEntityNearest(this, EntityIronGolem.class));
45
    }
46
47
    protected void entityInit()
48
    {
49
        super.entityInit();
50
        this.dataWatcher.addObject(16, Byte.valueOf((byte)1));
51
    }
52
53
    protected void setSlimeSize(int size)
54
    {
55
        this.dataWatcher.updateObject(16, Byte.valueOf((byte)size));
56
        this.setSize(0.51000005F * (float)size, 0.51000005F * (float)size);
57
        this.setPosition(this.posX, this.posY, this.posZ);
58
        this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue((double)(size * size));
59
        this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue((double)(0.2F + 0.1F * (float)size));
60
        this.setHealth(this.getMaxHealth());
61
        this.experienceValue = size;
62
    }
63
64
    /**
65
     * Returns the size of the slime.
66
     */
67
    public int getSlimeSize()
68
    {
69
        return this.dataWatcher.getWatchableObjectByte(16);
70
    }
71
72
    /**
73
     * (abstract) Protected helper method to write subclass entity data to NBT.
74
     */
75
    public void writeEntityToNBT(NBTTagCompound tagCompound)
76
    {
77
        super.writeEntityToNBT(tagCompound);
78
        tagCompound.setInteger("Size", this.getSlimeSize() - 1);
79
        tagCompound.setBoolean("wasOnGround", this.wasOnGround);
80
    }
81
82
    /**
83
     * (abstract) Protected helper method to read subclass entity data from NBT.
84
     */
85
    public void readEntityFromNBT(NBTTagCompound tagCompund)
86
    {
87
        super.readEntityFromNBT(tagCompund);
88
        int i = tagCompund.getInteger("Size");
89
90
        if (i < 0)
91
        {
92
            i = 0;
93
        }
94
95
        this.setSlimeSize(i + 1);
96
        this.wasOnGround = tagCompund.getBoolean("wasOnGround");
97
    }
98
99
    protected EnumParticleTypes getParticleType()
100
    {
101
        return EnumParticleTypes.SLIME;
102
    }
103
104
    /**
105
     * Returns the name of the sound played when the slime jumps.
106
     */
107
    protected String getJumpSound()
108
    {
109
        return "mob.slime." + (this.getSlimeSize() > 1 ? "big" : "small");
110
    }
111
112
    /**
113
     * Called to update the entity's position/logic.
114
     */
115
    public void onUpdate()
116
    {
117
        if (!this.worldObj.isRemote && this.worldObj.getDifficulty() == EnumDifficulty.PEACEFUL && this.getSlimeSize() > 0)
118
        {
119
            this.isDead = true;
120
        }
121
122
        this.squishFactor += (this.squishAmount - this.squishFactor) * 0.5F;
123
        this.prevSquishFactor = this.squishFactor;
124
        super.onUpdate();
125
126
        if (this.onGround && !this.wasOnGround)
127
        {
128
            int i = this.getSlimeSize();
129
130
            for (int j = 0; j < i * 8; ++j)
131
            {
132
                float f = this.rand.nextFloat() * (float)Math.PI * 2.0F;
133
                float f1 = this.rand.nextFloat() * 0.5F + 0.5F;
134
                float f2 = MathHelper.sin(f) * (float)i * 0.5F * f1;
135
                float f3 = MathHelper.cos(f) * (float)i * 0.5F * f1;
136
                World world = this.worldObj;
137
                EnumParticleTypes enumparticletypes = this.getParticleType();
138
                double d0 = this.posX + (double)f2;
139
                double d1 = this.posZ + (double)f3;
140
                world.spawnParticle(enumparticletypes, d0, this.getEntityBoundingBox().minY, d1, 0.0D, 0.0D, 0.0D, new int[0]);
141
            }
142
143
            if (this.makesSoundOnLand())
144
            {
145
                this.playSound(this.getJumpSound(), this.getSoundVolume(), ((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F + 1.0F) / 0.8F);
146
            }
147
148
            this.squishAmount = -0.5F;
149
        }
150
        else if (!this.onGround && this.wasOnGround)
151
        {
152
            this.squishAmount = 1.0F;
153
        }
154
155
        this.wasOnGround = this.onGround;
156
        this.alterSquishAmount();
157
    }
158
159
    protected void alterSquishAmount()
160
    {
161
        this.squishAmount *= 0.6F;
162
    }
163
164
    /**
165
     * Gets the amount of time the slime needs to wait between jumps.
166
     */
167
    protected int getJumpDelay()
168
    {
169
        return this.rand.nextInt(20) + 10;
170
    }
171
172
    protected EntitySlime createInstance()
173
    {
174
        return new EntitySlime(this.worldObj);
175
    }
176
177
    public void onDataWatcherUpdate(int dataID)
178
    {
179
        if (dataID == 16)
180
        {
181
            int i = this.getSlimeSize();
182
            this.setSize(0.51000005F * (float)i, 0.51000005F * (float)i);
183
            this.rotationYaw = this.rotationYawHead;
184
            this.renderYawOffset = this.rotationYawHead;
185
186
            if (this.isInWater() && this.rand.nextInt(20) == 0)
187
            {
188
                this.resetHeight();
189
            }
190
        }
191
192
        super.onDataWatcherUpdate(dataID);
193
    }
194
195
    /**
196
     * Will get destroyed next tick.
197
     */
198
    public void setDead()
199
    {
200
        int i = this.getSlimeSize();
201
202
        if (!this.worldObj.isRemote && i > 1 && this.getHealth() <= 0.0F)
203
        {
204
            int j = 2 + this.rand.nextInt(3);
205
206
            for (int k = 0; k < j; ++k)
207
            {
208
                float f = ((float)(k % 2) - 0.5F) * (float)i / 4.0F;
209
                float f1 = ((float)(k / 2) - 0.5F) * (float)i / 4.0F;
210
                EntitySlime entityslime = this.createInstance();
211
212
                if (this.hasCustomName())
213
                {
214
                    entityslime.setCustomNameTag(this.getCustomNameTag());
215
                }
216
217
                if (this.isNoDespawnRequired())
218
                {
219
                    entityslime.enablePersistence();
220
                }
221
222
                entityslime.setSlimeSize(i / 2);
223
                entityslime.setLocationAndAngles(this.posX + (double)f, this.posY + 0.5D, this.posZ + (double)f1, this.rand.nextFloat() * 360.0F, 0.0F);
224
                this.worldObj.spawnEntityInWorld(entityslime);
225
            }
226
        }
227
228
        super.setDead();
229
    }
230
231
    /**
232
     * Applies a velocity to each of the entities pushing them away from each other. Args: entity
233
     */
234
    public void applyEntityCollision(Entity entityIn)
235
    {
236
        super.applyEntityCollision(entityIn);
237
238
        if (entityIn instanceof EntityIronGolem && this.canDamagePlayer())
239
        {
240
            this.func_175451_e((EntityLivingBase)entityIn);
241
        }
242
    }
243
244
    /**
245
     * Called by a player entity when they collide with an entity
246
     */
247
    public void onCollideWithPlayer(EntityPlayer entityIn)
248
    {
249
        if (this.canDamagePlayer())
250
        {
251
            this.func_175451_e(entityIn);
252
        }
253
    }
254
255
    protected void func_175451_e(EntityLivingBase p_175451_1_)
256
    {
257
        int i = this.getSlimeSize();
258
259
        if (this.canEntityBeSeen(p_175451_1_) && this.getDistanceSqToEntity(p_175451_1_) < 0.6D * (double)i * 0.6D * (double)i && p_175451_1_.attackEntityFrom(DamageSource.causeMobDamage(this), (float)this.getAttackStrength()))
260
        {
261
            this.playSound("mob.attack", 1.0F, (this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F + 1.0F);
262
            this.applyEnchantments(this, p_175451_1_);
263
        }
264
    }
265
266
    public float getEyeHeight()
267
    {
268
        return 0.625F * this.height;
269
    }
270
271
    /**
272
     * Indicates weather the slime is able to damage the player (based upon the slime's size)
273
     */
274
    protected boolean canDamagePlayer()
275
    {
276
        return this.getSlimeSize() > 1;
277
    }
278
279
    /**
280
     * Gets the amount of damage dealt to the player when "attacked" by the slime.
281
     */
282
    protected int getAttackStrength()
283
    {
284
        return this.getSlimeSize();
285
    }
286
287
    /**
288
     * Returns the sound this mob makes when it is hurt.
289
     */
290
    protected String getHurtSound()
291
    {
292
        return "mob.slime." + (this.getSlimeSize() > 1 ? "big" : "small");
293
    }
294
295
    /**
296
     * Returns the sound this mob makes on death.
297
     */
298
    protected String getDeathSound()
299
    {
300
        return "mob.slime." + (this.getSlimeSize() > 1 ? "big" : "small");
301
    }
302
303
    protected Item getDropItem()
304
    {
305
        return this.getSlimeSize() == 1 ? Items.slime_ball : null;
306
    }
307
308
    /**
309
     * Checks if the entity's current position is a valid location to spawn this entity.
310
     */
311
    public boolean getCanSpawnHere()
312
    {
313
        BlockPos blockpos = new BlockPos(MathHelper.floor_double(this.posX), 0, MathHelper.floor_double(this.posZ));
314
        Chunk chunk = this.worldObj.getChunkFromBlockCoords(blockpos);
315
316
        if (this.worldObj.getWorldInfo().getTerrainType() == WorldType.FLAT && this.rand.nextInt(4) != 1)
317
        {
318
            return false;
319
        }
320
        else
321
        {
322
            if (this.worldObj.getDifficulty() != EnumDifficulty.PEACEFUL)
323
            {
324
                BiomeGenBase biomegenbase = this.worldObj.getBiomeGenForCoords(blockpos);
325
326
                if (biomegenbase == BiomeGenBase.swampland && this.posY > 50.0D && this.posY < 70.0D && this.rand.nextFloat() < 0.5F && this.rand.nextFloat() < this.worldObj.getCurrentMoonPhaseFactor() && this.worldObj.getLightFromNeighbors(new BlockPos(this)) <= this.rand.nextInt(8))
327
                {
328
                    return super.getCanSpawnHere();
329
                }
330
331
                if (this.rand.nextInt(10) == 0 && chunk.getRandomWithSeed(987234911L).nextInt(10) == 0 && this.posY < 40.0D)
332
                {
333
                    return super.getCanSpawnHere();
334
                }
335
            }
336
337
            return false;
338
        }
339
    }
340
341
    /**
342
     * Returns the volume for the sounds this mob makes.
343
     */
344
    protected float getSoundVolume()
345
    {
346
        return 0.4F * (float)this.getSlimeSize();
347
    }
348
349
    /**
350
     * The speed it takes to move the entityliving's rotationPitch through the faceEntity method. This is only currently
351
     * use in wolves.
352
     */
353
    public int getVerticalFaceSpeed()
354
    {
355
        return 0;
356
    }
357
358
    /**
359
     * Returns true if the slime makes a sound when it jumps (based upon the slime's size)
360
     */
361
    protected boolean makesSoundOnJump()
362
    {
363
        return this.getSlimeSize() > 0;
364
    }
365
366
    /**
367
     * Returns true if the slime makes a sound when it lands after a jump (based upon the slime's size)
368
     */
369
    protected boolean makesSoundOnLand()
370
    {
371
        return this.getSlimeSize() > 2;
372
    }
373
374
    /**
375
     * Causes this entity to do an upwards motion (jumping).
376
     */
377
    protected void jump()
378
    {
379
        this.motionY = 0.41999998688697815D;
380
        this.isAirBorne = true;
381
    }
382
383
    /**
384
     * Called only once on an entity when first time spawned, via egg, mob spawner, natural spawning etc, but not called
385
     * when entity is reloaded from nbt. Mainly used for initializing attributes and inventory
386
     */
387
    public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, IEntityLivingData livingdata)
388
    {
389
        int i = this.rand.nextInt(3);
390
391
        if (i < 2 && this.rand.nextFloat() < 0.5F * difficulty.getClampedAdditionalDifficulty())
392
        {
393
            ++i;
394
        }
395
396
        int j = 1 << i;
397
        this.setSlimeSize(j);
398
        return super.onInitialSpawn(difficulty, livingdata);
399
    }
400
401
    static class AISlimeAttack extends EntityAIBase
402
    {
403
        private EntitySlime slime;
404
        private int field_179465_b;
405
406
        public AISlimeAttack(EntitySlime slimeIn)
407
        {
408
            this.slime = slimeIn;
409
            this.setMutexBits(2);
410
        }
411
412
        public boolean shouldExecute()
413
        {
414
            EntityLivingBase entitylivingbase = this.slime.getAttackTarget();
415
            return entitylivingbase == null ? false : (!entitylivingbase.isEntityAlive() ? false : !(entitylivingbase instanceof EntityPlayer) || !((EntityPlayer)entitylivingbase).capabilities.disableDamage);
416
        }
417
418
        public void startExecuting()
419
        {
420
            this.field_179465_b = 300;
421
            super.startExecuting();
422
        }
423
424
        public boolean continueExecuting()
425
        {
426
            EntityLivingBase entitylivingbase = this.slime.getAttackTarget();
427
            return entitylivingbase == null ? false : (!entitylivingbase.isEntityAlive() ? false : (entitylivingbase instanceof EntityPlayer && ((EntityPlayer)entitylivingbase).capabilities.disableDamage ? false : --this.field_179465_b > 0));
428
        }
429
430
        public void updateTask()
431
        {
432
            this.slime.faceEntity(this.slime.getAttackTarget(), 10.0F, 10.0F);
433
            ((EntitySlime.SlimeMoveHelper)this.slime.getMoveHelper()).func_179920_a(this.slime.rotationYaw, this.slime.canDamagePlayer());
434
        }
435
    }
436
437
    static class AISlimeFaceRandom extends EntityAIBase
438
    {
439
        private EntitySlime slime;
440
        private float field_179459_b;
441
        private int field_179460_c;
442
443
        public AISlimeFaceRandom(EntitySlime slimeIn)
444
        {
445
            this.slime = slimeIn;
446
            this.setMutexBits(2);
447
        }
448
449
        public boolean shouldExecute()
450
        {
451
            return this.slime.getAttackTarget() == null && (this.slime.onGround || this.slime.isInWater() || this.slime.isInLava());
452
        }
453
454
        public void updateTask()
455
        {
456
            if (--this.field_179460_c <= 0)
457
            {
458
                this.field_179460_c = 40 + this.slime.getRNG().nextInt(60);
459
                this.field_179459_b = (float)this.slime.getRNG().nextInt(360);
460
            }
461
462
            ((EntitySlime.SlimeMoveHelper)this.slime.getMoveHelper()).func_179920_a(this.field_179459_b, false);
463
        }
464
    }
465
466
    static class AISlimeFloat extends EntityAIBase
467
    {
468
        private EntitySlime slime;
469
470
        public AISlimeFloat(EntitySlime slimeIn)
471
        {
472
            this.slime = slimeIn;
473
            this.setMutexBits(5);
474
            ((PathNavigateGround)slimeIn.getNavigator()).setCanSwim(true);
475
        }
476
477
        public boolean shouldExecute()
478
        {
479
            return this.slime.isInWater() || this.slime.isInLava();
480
        }
481
482
        public void updateTask()
483
        {
484
            if (this.slime.getRNG().nextFloat() < 0.8F)
485
            {
486
                this.slime.getJumpHelper().setJumping();
487
            }
488
489
            ((EntitySlime.SlimeMoveHelper)this.slime.getMoveHelper()).setSpeed(1.2D);
490
        }
491
    }
492
493
    static class AISlimeHop extends EntityAIBase
494
    {
495
        private EntitySlime slime;
496
497
        public AISlimeHop(EntitySlime slimeIn)
498
        {
499
            this.slime = slimeIn;
500
            this.setMutexBits(5);
501
        }
502
503
        public boolean shouldExecute()
504
        {
505
            return true;
506
        }
507
508
        public void updateTask()
509
        {
510
            ((EntitySlime.SlimeMoveHelper)this.slime.getMoveHelper()).setSpeed(1.0D);
511
        }
512
    }
513
514
    static class SlimeMoveHelper extends EntityMoveHelper
515
    {
516
        private float field_179922_g;
517
        private int field_179924_h;
518
        private EntitySlime slime;
519
        private boolean field_179923_j;
520
521
        public SlimeMoveHelper(EntitySlime slimeIn)
522
        {
523
            super(slimeIn);
524
            this.slime = slimeIn;
525
        }
526
527
        public void func_179920_a(float p_179920_1_, boolean p_179920_2_)
528
        {
529
            this.field_179922_g = p_179920_1_;
530
            this.field_179923_j = p_179920_2_;
531
        }
532
533
        public void setSpeed(double speedIn)
534
        {
535
            this.speed = speedIn;
536
            this.update = true;
537
        }
538
539
        public void onUpdateMoveHelper()
540
        {
541
            this.entity.rotationYaw = this.limitAngle(this.entity.rotationYaw, this.field_179922_g, 30.0F);
542
            this.entity.rotationYawHead = this.entity.rotationYaw;
543
            this.entity.renderYawOffset = this.entity.rotationYaw;
544
545
            if (!this.update)
546
            {
547
                this.entity.setMoveForward(0.0F);
548
            }
549
            else
550
            {
551
                this.update = false;
552
553
                if (this.entity.onGround)
554
                {
555
                    this.entity.setAIMoveSpeed((float)(this.speed * this.entity.getEntityAttribute(SharedMonsterAttributes.movementSpeed).getAttributeValue()));
556
557
                    if (this.field_179924_h-- <= 0)
558
                    {
559
                        this.field_179924_h = this.slime.getJumpDelay();
560
561
                        if (this.field_179923_j)
562
                        {
563
                            this.field_179924_h /= 3;
564
                        }
565
566
                        this.slime.getJumpHelper().setJumping();
567
568
                        if (this.slime.makesSoundOnJump())
569
                        {
570
                            this.slime.playSound(this.slime.getJumpSound(), this.slime.getSoundVolume(), ((this.slime.getRNG().nextFloat() - this.slime.getRNG().nextFloat()) * 0.2F + 1.0F) * 0.8F);
571
                        }
572
                    }
573
                    else
574
                    {
575
                        this.slime.moveStrafing = this.slime.moveForward = 0.0F;
576
                        this.entity.setAIMoveSpeed(0.0F);
577
                    }
578
                }
579
                else
580
                {
581
                    this.entity.setAIMoveSpeed((float)(this.speed * this.entity.getEntityAttribute(SharedMonsterAttributes.movementSpeed).getAttributeValue()));
582
                }
583
            }
584
        }
585
    }
586
}
587

Deobfuscated MCP 1.8.9 source of EntitySlime from Marcelektro/MavenMCP-1.8.9 (net/minecraft/entity/monster/EntitySlime.java), stored verbatim.