EntityGhast .java

net.minecraft.entity.monster.EntityGhast (class): members: getFireballStrength, explosionStrength, GhastMoveHelper, AILookAround, setAttacking, entityInit.

net.minecraft.entity.monsterclass413 linescommonextends EntityFlying13.1 KB

Implements

API Surface (28)

boolean isAttacking()
void setAttacking(boolean attacking)
int getFireballStrength()
void onUpdate()
boolean attackEntityFrom(DamageSource source, float amount)
void entityInit()
void applyEntityAttributes()
String getLivingSound()
String getHurtSound()
String getDeathSound()
Item getDropItem()
void dropFewItems(boolean wasRecentlyHit, int lootingModifier)
float getSoundVolume()
boolean getCanSpawnHere()
int getMaxSpawnedInChunk()
void writeEntityToNBT(NBTTagCompound tagCompound)
void readEntityFromNBT(NBTTagCompound tagCompund)
float getEyeHeight()
boolean shouldExecute()
void startExecuting()
void resetTask()
void updateTask()
boolean shouldExecute()
void updateTask()
boolean shouldExecute()
boolean continueExecuting()
void startExecuting()
void onUpdateMoveHelper()

Fields & Constants (1)

explosionStrength

String literals (5)

"fireball"
"mob.ghast.moan"
"mob.ghast.scream"
"mob.ghast.death"
"ExplosionPower"

Full Source

EntityGhast.java413 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.monster;
2
3
import java.util.Random;
4
import net.minecraft.entity.EntityFlying;
5
import net.minecraft.entity.EntityLivingBase;
6
import net.minecraft.entity.SharedMonsterAttributes;
7
import net.minecraft.entity.ai.EntityAIBase;
8
import net.minecraft.entity.ai.EntityAIFindEntityNearestPlayer;
9
import net.minecraft.entity.ai.EntityMoveHelper;
10
import net.minecraft.entity.player.EntityPlayer;
11
import net.minecraft.entity.projectile.EntityLargeFireball;
12
import net.minecraft.init.Items;
13
import net.minecraft.item.Item;
14
import net.minecraft.nbt.NBTTagCompound;
15
import net.minecraft.stats.AchievementList;
16
import net.minecraft.util.AxisAlignedBB;
17
import net.minecraft.util.BlockPos;
18
import net.minecraft.util.DamageSource;
19
import net.minecraft.util.MathHelper;
20
import net.minecraft.util.Vec3;
21
import net.minecraft.world.EnumDifficulty;
22
import net.minecraft.world.World;
23
24
public class EntityGhast extends EntityFlying implements IMob
25
{
26
    /** The explosion radius of spawned fireballs. */
27
    private int explosionStrength = 1;
28
29
    public EntityGhast(World worldIn)
30
    {
31
        super(worldIn);
32
        this.setSize(4.0F, 4.0F);
33
        this.isImmuneToFire = true;
34
        this.experienceValue = 5;
35
        this.moveHelper = new EntityGhast.GhastMoveHelper(this);
36
        this.tasks.addTask(5, new EntityGhast.AIRandomFly(this));
37
        this.tasks.addTask(7, new EntityGhast.AILookAround(this));
38
        this.tasks.addTask(7, new EntityGhast.AIFireballAttack(this));
39
        this.targetTasks.addTask(1, new EntityAIFindEntityNearestPlayer(this));
40
    }
41
42
    public boolean isAttacking()
43
    {
44
        return this.dataWatcher.getWatchableObjectByte(16) != 0;
45
    }
46
47
    public void setAttacking(boolean attacking)
48
    {
49
        this.dataWatcher.updateObject(16, Byte.valueOf((byte)(attacking ? 1 : 0)));
50
    }
51
52
    public int getFireballStrength()
53
    {
54
        return this.explosionStrength;
55
    }
56
57
    /**
58
     * Called to update the entity's position/logic.
59
     */
60
    public void onUpdate()
61
    {
62
        super.onUpdate();
63
64
        if (!this.worldObj.isRemote && this.worldObj.getDifficulty() == EnumDifficulty.PEACEFUL)
65
        {
66
            this.setDead();
67
        }
68
    }
69
70
    /**
71
     * Called when the entity is attacked.
72
     */
73
    public boolean attackEntityFrom(DamageSource source, float amount)
74
    {
75
        if (this.isEntityInvulnerable(source))
76
        {
77
            return false;
78
        }
79
        else if ("fireball".equals(source.getDamageType()) && source.getEntity() instanceof EntityPlayer)
80
        {
81
            super.attackEntityFrom(source, 1000.0F);
82
            ((EntityPlayer)source.getEntity()).triggerAchievement(AchievementList.ghast);
83
            return true;
84
        }
85
        else
86
        {
87
            return super.attackEntityFrom(source, amount);
88
        }
89
    }
90
91
    protected void entityInit()
92
    {
93
        super.entityInit();
94
        this.dataWatcher.addObject(16, Byte.valueOf((byte)0));
95
    }
96
97
    protected void applyEntityAttributes()
98
    {
99
        super.applyEntityAttributes();
100
        this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(10.0D);
101
        this.getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(100.0D);
102
    }
103
104
    /**
105
     * Returns the sound this mob makes while it's alive.
106
     */
107
    protected String getLivingSound()
108
    {
109
        return "mob.ghast.moan";
110
    }
111
112
    /**
113
     * Returns the sound this mob makes when it is hurt.
114
     */
115
    protected String getHurtSound()
116
    {
117
        return "mob.ghast.scream";
118
    }
119
120
    /**
121
     * Returns the sound this mob makes on death.
122
     */
123
    protected String getDeathSound()
124
    {
125
        return "mob.ghast.death";
126
    }
127
128
    protected Item getDropItem()
129
    {
130
        return Items.gunpowder;
131
    }
132
133
    /**
134
     * Drop 0-2 items of this living's type
135
     *  
136
     * @param wasRecentlyHit true if this this entity was recently hit by appropriate entity (generally only if player
137
     * or tameable)
138
     * @param lootingModifier level of enchanment to be applied to this drop
139
     */
140
    protected void dropFewItems(boolean wasRecentlyHit, int lootingModifier)
141
    {
142
        int i = this.rand.nextInt(2) + this.rand.nextInt(1 + lootingModifier);
143
144
        for (int j = 0; j < i; ++j)
145
        {
146
            this.dropItem(Items.ghast_tear, 1);
147
        }
148
149
        i = this.rand.nextInt(3) + this.rand.nextInt(1 + lootingModifier);
150
151
        for (int k = 0; k < i; ++k)
152
        {
153
            this.dropItem(Items.gunpowder, 1);
154
        }
155
    }
156
157
    /**
158
     * Returns the volume for the sounds this mob makes.
159
     */
160
    protected float getSoundVolume()
161
    {
162
        return 10.0F;
163
    }
164
165
    /**
166
     * Checks if the entity's current position is a valid location to spawn this entity.
167
     */
168
    public boolean getCanSpawnHere()
169
    {
170
        return this.rand.nextInt(20) == 0 && super.getCanSpawnHere() && this.worldObj.getDifficulty() != EnumDifficulty.PEACEFUL;
171
    }
172
173
    /**
174
     * Will return how many at most can spawn in a chunk at once.
175
     */
176
    public int getMaxSpawnedInChunk()
177
    {
178
        return 1;
179
    }
180
181
    /**
182
     * (abstract) Protected helper method to write subclass entity data to NBT.
183
     */
184
    public void writeEntityToNBT(NBTTagCompound tagCompound)
185
    {
186
        super.writeEntityToNBT(tagCompound);
187
        tagCompound.setInteger("ExplosionPower", this.explosionStrength);
188
    }
189
190
    /**
191
     * (abstract) Protected helper method to read subclass entity data from NBT.
192
     */
193
    public void readEntityFromNBT(NBTTagCompound tagCompund)
194
    {
195
        super.readEntityFromNBT(tagCompund);
196
197
        if (tagCompund.hasKey("ExplosionPower", 99))
198
        {
199
            this.explosionStrength = tagCompund.getInteger("ExplosionPower");
200
        }
201
    }
202
203
    public float getEyeHeight()
204
    {
205
        return 2.6F;
206
    }
207
208
    static class AIFireballAttack extends EntityAIBase
209
    {
210
        private EntityGhast parentEntity;
211
        public int attackTimer;
212
213
        public AIFireballAttack(EntityGhast ghast)
214
        {
215
            this.parentEntity = ghast;
216
        }
217
218
        public boolean shouldExecute()
219
        {
220
            return this.parentEntity.getAttackTarget() != null;
221
        }
222
223
        public void startExecuting()
224
        {
225
            this.attackTimer = 0;
226
        }
227
228
        public void resetTask()
229
        {
230
            this.parentEntity.setAttacking(false);
231
        }
232
233
        public void updateTask()
234
        {
235
            EntityLivingBase entitylivingbase = this.parentEntity.getAttackTarget();
236
            double d0 = 64.0D;
237
238
            if (entitylivingbase.getDistanceSqToEntity(this.parentEntity) < d0 * d0 && this.parentEntity.canEntityBeSeen(entitylivingbase))
239
            {
240
                World world = this.parentEntity.worldObj;
241
                ++this.attackTimer;
242
243
                if (this.attackTimer == 10)
244
                {
245
                    world.playAuxSFXAtEntity((EntityPlayer)null, 1007, new BlockPos(this.parentEntity), 0);
246
                }
247
248
                if (this.attackTimer == 20)
249
                {
250
                    double d1 = 4.0D;
251
                    Vec3 vec3 = this.parentEntity.getLook(1.0F);
252
                    double d2 = entitylivingbase.posX - (this.parentEntity.posX + vec3.xCoord * d1);
253
                    double d3 = entitylivingbase.getEntityBoundingBox().minY + (double)(entitylivingbase.height / 2.0F) - (0.5D + this.parentEntity.posY + (double)(this.parentEntity.height / 2.0F));
254
                    double d4 = entitylivingbase.posZ - (this.parentEntity.posZ + vec3.zCoord * d1);
255
                    world.playAuxSFXAtEntity((EntityPlayer)null, 1008, new BlockPos(this.parentEntity), 0);
256
                    EntityLargeFireball entitylargefireball = new EntityLargeFireball(world, this.parentEntity, d2, d3, d4);
257
                    entitylargefireball.explosionPower = this.parentEntity.getFireballStrength();
258
                    entitylargefireball.posX = this.parentEntity.posX + vec3.xCoord * d1;
259
                    entitylargefireball.posY = this.parentEntity.posY + (double)(this.parentEntity.height / 2.0F) + 0.5D;
260
                    entitylargefireball.posZ = this.parentEntity.posZ + vec3.zCoord * d1;
261
                    world.spawnEntityInWorld(entitylargefireball);
262
                    this.attackTimer = -40;
263
                }
264
            }
265
            else if (this.attackTimer > 0)
266
            {
267
                --this.attackTimer;
268
            }
269
270
            this.parentEntity.setAttacking(this.attackTimer > 10);
271
        }
272
    }
273
274
    static class AILookAround extends EntityAIBase
275
    {
276
        private EntityGhast parentEntity;
277
278
        public AILookAround(EntityGhast ghast)
279
        {
280
            this.parentEntity = ghast;
281
            this.setMutexBits(2);
282
        }
283
284
        public boolean shouldExecute()
285
        {
286
            return true;
287
        }
288
289
        public void updateTask()
290
        {
291
            if (this.parentEntity.getAttackTarget() == null)
292
            {
293
                this.parentEntity.renderYawOffset = this.parentEntity.rotationYaw = -((float)MathHelper.atan2(this.parentEntity.motionX, this.parentEntity.motionZ)) * 180.0F / (float)Math.PI;
294
            }
295
            else
296
            {
297
                EntityLivingBase entitylivingbase = this.parentEntity.getAttackTarget();
298
                double d0 = 64.0D;
299
300
                if (entitylivingbase.getDistanceSqToEntity(this.parentEntity) < d0 * d0)
301
                {
302
                    double d1 = entitylivingbase.posX - this.parentEntity.posX;
303
                    double d2 = entitylivingbase.posZ - this.parentEntity.posZ;
304
                    this.parentEntity.renderYawOffset = this.parentEntity.rotationYaw = -((float)MathHelper.atan2(d1, d2)) * 180.0F / (float)Math.PI;
305
                }
306
            }
307
        }
308
    }
309
310
    static class AIRandomFly extends EntityAIBase
311
    {
312
        private EntityGhast parentEntity;
313
314
        public AIRandomFly(EntityGhast ghast)
315
        {
316
            this.parentEntity = ghast;
317
            this.setMutexBits(1);
318
        }
319
320
        public boolean shouldExecute()
321
        {
322
            EntityMoveHelper entitymovehelper = this.parentEntity.getMoveHelper();
323
324
            if (!entitymovehelper.isUpdating())
325
            {
326
                return true;
327
            }
328
            else
329
            {
330
                double d0 = entitymovehelper.getX() - this.parentEntity.posX;
331
                double d1 = entitymovehelper.getY() - this.parentEntity.posY;
332
                double d2 = entitymovehelper.getZ() - this.parentEntity.posZ;
333
                double d3 = d0 * d0 + d1 * d1 + d2 * d2;
334
                return d3 < 1.0D || d3 > 3600.0D;
335
            }
336
        }
337
338
        public boolean continueExecuting()
339
        {
340
            return false;
341
        }
342
343
        public void startExecuting()
344
        {
345
            Random random = this.parentEntity.getRNG();
346
            double d0 = this.parentEntity.posX + (double)((random.nextFloat() * 2.0F - 1.0F) * 16.0F);
347
            double d1 = this.parentEntity.posY + (double)((random.nextFloat() * 2.0F - 1.0F) * 16.0F);
348
            double d2 = this.parentEntity.posZ + (double)((random.nextFloat() * 2.0F - 1.0F) * 16.0F);
349
            this.parentEntity.getMoveHelper().setMoveTo(d0, d1, d2, 1.0D);
350
        }
351
    }
352
353
    static class GhastMoveHelper extends EntityMoveHelper
354
    {
355
        private EntityGhast parentEntity;
356
        private int courseChangeCooldown;
357
358
        public GhastMoveHelper(EntityGhast ghast)
359
        {
360
            super(ghast);
361
            this.parentEntity = ghast;
362
        }
363
364
        public void onUpdateMoveHelper()
365
        {
366
            if (this.update)
367
            {
368
                double d0 = this.posX - this.parentEntity.posX;
369
                double d1 = this.posY - this.parentEntity.posY;
370
                double d2 = this.posZ - this.parentEntity.posZ;
371
                double d3 = d0 * d0 + d1 * d1 + d2 * d2;
372
373
                if (this.courseChangeCooldown-- <= 0)
374
                {
375
                    this.courseChangeCooldown += this.parentEntity.getRNG().nextInt(5) + 2;
376
                    d3 = (double)MathHelper.sqrt_double(d3);
377
378
                    if (this.isNotColliding(this.posX, this.posY, this.posZ, d3))
379
                    {
380
                        this.parentEntity.motionX += d0 / d3 * 0.1D;
381
                        this.parentEntity.motionY += d1 / d3 * 0.1D;
382
                        this.parentEntity.motionZ += d2 / d3 * 0.1D;
383
                    }
384
                    else
385
                    {
386
                        this.update = false;
387
                    }
388
                }
389
            }
390
        }
391
392
        private boolean isNotColliding(double x, double y, double z, double p_179926_7_)
393
        {
394
            double d0 = (x - this.parentEntity.posX) / p_179926_7_;
395
            double d1 = (y - this.parentEntity.posY) / p_179926_7_;
396
            double d2 = (z - this.parentEntity.posZ) / p_179926_7_;
397
            AxisAlignedBB axisalignedbb = this.parentEntity.getEntityBoundingBox();
398
399
            for (int i = 1; (double)i < p_179926_7_; ++i)
400
            {
401
                axisalignedbb = axisalignedbb.offset(d0, d1, d2);
402
403
                if (!this.parentEntity.worldObj.getCollidingBoundingBoxes(this.parentEntity, axisalignedbb).isEmpty())
404
                {
405
                    return false;
406
                }
407
            }
408
409
            return true;
410
        }
411
    }
412
}
413

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