Phantom .java

net.minecraft.world.entity.monster.Phantom (class, 497 lines): members: PhantomAttackPlayerTargetGoal, PhantomCircleAroundAnchorGoal, PhantomLookControl.

net.minecraft.world.entity.monsterclass497 linescommonextends Mob16.6 KB

Implements

API Surface (37)

boolean isFlapping()
BodyRotationControl createBodyControl()
void registerGoals()
void defineSynchedData(final SynchedEntityData.Builder entityData)
void setPhantomSize(final int size)
int getPhantomSize()
void onSyncedDataUpdated(final EntityDataAccessor<?> accessor)
int getUniqueFlapTickOffset()
void tick()
void checkFallDamage(final double ya, final boolean onGround, final BlockState onState, final BlockPos pos)
boolean onClimbable()
void travel(final Vec3 input)
void readAdditionalSaveData(final ValueInput input)
void addAdditionalSaveData(final ValueOutput output)
boolean shouldRenderAtSqrDistance(final double distance)
SoundSource getSoundSource()
SoundEvent getAmbientSound()
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getDeathSound()
EntityDimensions getDefaultDimensions(final Pose pose)
boolean canUse()
boolean canContinueToUse()
boolean canUse()
void start()
void stop()
void tick()
void clientTick()
boolean canUse()
void start()
void tick()
void tick()
void tick()
boolean touchingTarget()
boolean canUse()
boolean canContinueToUse()
void stop()
void tick()

Fields & Constants (7)

FLAP_DEGREES_PER_TICK
TICKS_PER_FLAP
ID_SIZE
moveTargetPoint
anchorPoint
attackPhase
spawnReason

String literals (2)

"anchor_pos"
"size"

Full Source

Phantom.java497 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.monster;
2
3
import java.util.Comparator;
4
import java.util.EnumSet;
5
import java.util.List;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.particles.ParticleTypes;
8
import net.minecraft.network.syncher.EntityDataAccessor;
9
import net.minecraft.network.syncher.EntityDataSerializers;
10
import net.minecraft.network.syncher.SynchedEntityData;
11
import net.minecraft.server.level.ServerLevel;
12
import net.minecraft.sounds.SoundEvent;
13
import net.minecraft.sounds.SoundEvents;
14
import net.minecraft.sounds.SoundSource;
15
import net.minecraft.util.Mth;
16
import net.minecraft.world.DifficultyInstance;
17
import net.minecraft.world.damagesource.DamageSource;
18
import net.minecraft.world.entity.Entity;
19
import net.minecraft.world.entity.EntityDimensions;
20
import net.minecraft.world.entity.EntitySelector;
21
import net.minecraft.world.entity.EntitySpawnReason;
22
import net.minecraft.world.entity.EntityType;
23
import net.minecraft.world.entity.LivingEntity;
24
import net.minecraft.world.entity.Mob;
25
import net.minecraft.world.entity.Pose;
26
import net.minecraft.world.entity.SpawnGroupData;
27
import net.minecraft.world.entity.ai.attributes.Attributes;
28
import net.minecraft.world.entity.ai.control.BodyRotationControl;
29
import net.minecraft.world.entity.ai.control.LookControl;
30
import net.minecraft.world.entity.ai.control.MoveControl;
31
import net.minecraft.world.entity.ai.goal.Goal;
32
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
33
import net.minecraft.world.entity.animal.feline.Cat;
34
import net.minecraft.world.entity.player.Player;
35
import net.minecraft.world.level.Level;
36
import net.minecraft.world.level.ServerLevelAccessor;
37
import net.minecraft.world.level.block.state.BlockState;
38
import net.minecraft.world.level.levelgen.Heightmap;
39
import net.minecraft.world.level.storage.ValueInput;
40
import net.minecraft.world.level.storage.ValueOutput;
41
import net.minecraft.world.phys.Vec3;
42
import org.jspecify.annotations.Nullable;
43
44
public class Phantom extends Mob implements Enemy {
45
	public static final float FLAP_DEGREES_PER_TICK = 7.448451F;
46
	public static final int TICKS_PER_FLAP = Mth.ceil(24.166098F);
47
	private static final EntityDataAccessor<Integer> ID_SIZE = SynchedEntityData.defineId(Phantom.class, EntityDataSerializers.INT);
48
	private Vec3 moveTargetPoint = Vec3.ZERO;
49
	private @Nullable BlockPos anchorPoint;
50
	private Phantom.AttackPhase attackPhase = Phantom.AttackPhase.CIRCLE;
51
52
	public Phantom(final EntityType<? extends Phantom> type, final Level level) {
53
		super(type, level);
54
		this.xpReward = 5;
55
		this.moveControl = new Phantom.PhantomMoveControl<>(this);
56
		this.lookControl = new Phantom.PhantomLookControl(this);
57
	}
58
59
	@Override
60
	public boolean isFlapping() {
61
		return (this.getUniqueFlapTickOffset() + this.tickCount) % TICKS_PER_FLAP == 0;
62
	}
63
64
	@Override
65
	protected BodyRotationControl createBodyControl() {
66
		return new Phantom.PhantomBodyRotationControl(this);
67
	}
68
69
	@Override
70
	protected void registerGoals() {
71
		this.goalSelector.addGoal(1, new Phantom.PhantomAttackStrategyGoal());
72
		this.goalSelector.addGoal(2, new Phantom.PhantomSweepAttackGoal());
73
		this.goalSelector.addGoal(3, new Phantom.PhantomCircleAroundAnchorGoal());
74
		this.targetSelector.addGoal(1, new Phantom.PhantomAttackPlayerTargetGoal());
75
	}
76
77
	@Override
78
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
79
		super.defineSynchedData(entityData);
80
		entityData.define(ID_SIZE, 0);
81
	}
82
83
	public void setPhantomSize(final int size) {
84
		this.entityData.set(ID_SIZE, Mth.clamp(size, 0, 64));
85
	}
86
87
	private void updatePhantomSizeInfo() {
88
		this.refreshDimensions();
89
		this.getAttribute(Attributes.ATTACK_DAMAGE).setBaseValue(6 + this.getPhantomSize());
90
	}
91
92
	public int getPhantomSize() {
93
		return this.entityData.get(ID_SIZE);
94
	}
95
96
	@Override
97
	public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
98
		if (ID_SIZE.equals(accessor)) {
99
			this.updatePhantomSizeInfo();
100
		}
101
102
		super.onSyncedDataUpdated(accessor);
103
	}
104
105
	public int getUniqueFlapTickOffset() {
106
		return this.getId() * 3;
107
	}
108
109
	@Override
110
	public void tick() {
111
		super.tick();
112
		if (this.level().isClientSide()) {
113
			float anim = Mth.cos((this.getUniqueFlapTickOffset() + this.tickCount) * 7.448451F * (float) (Math.PI / 180.0) + (float) Math.PI);
114
			float nextAnim = Mth.cos((this.getUniqueFlapTickOffset() + this.tickCount + 1) * 7.448451F * (float) (Math.PI / 180.0) + (float) Math.PI);
115
			if (anim > 0.0F && nextAnim <= 0.0F) {
116
				this.level()
117
					.playLocalSound(this, SoundEvents.PHANTOM_FLAP, this.getSoundSource(), 0.95F + this.random.nextFloat() * 0.05F, 0.95F + this.random.nextFloat() * 0.05F);
118
			}
119
120
			float width = this.getBbWidth() * 1.48F;
121
			float c = Mth.cos(this.getYRot() * (float) (Math.PI / 180.0)) * width;
122
			float s = Mth.sin(this.getYRot() * (float) (Math.PI / 180.0)) * width;
123
			float h = (0.3F + anim * 0.45F) * this.getBbHeight() * 2.5F;
124
			this.level().addParticle(ParticleTypes.MYCELIUM, this.getX() + c, this.getY() + h, this.getZ() + s, 0.0, 0.0, 0.0);
125
			this.level().addParticle(ParticleTypes.MYCELIUM, this.getX() - c, this.getY() + h, this.getZ() - s, 0.0, 0.0, 0.0);
126
		}
127
	}
128
129
	@Override
130
	protected void checkFallDamage(final double ya, final boolean onGround, final BlockState onState, final BlockPos pos) {
131
	}
132
133
	@Override
134
	public boolean onClimbable() {
135
		return false;
136
	}
137
138
	@Override
139
	public void travel(final Vec3 input) {
140
		this.travelFlying(input, 0.2F);
141
	}
142
143
	@Override
144
	public @Nullable SpawnGroupData finalizeSpawn(
145
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
146
	) {
147
		this.anchorPoint = this.blockPosition().above(5);
148
		this.setPhantomSize(0);
149
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
150
	}
151
152
	@Override
153
	protected void readAdditionalSaveData(final ValueInput input) {
154
		super.readAdditionalSaveData(input);
155
		this.anchorPoint = input.read("anchor_pos", BlockPos.CODEC).orElse(null);
156
		this.setPhantomSize(input.getIntOr("size", 0));
157
	}
158
159
	@Override
160
	protected void addAdditionalSaveData(final ValueOutput output) {
161
		super.addAdditionalSaveData(output);
162
		output.storeNullable("anchor_pos", BlockPos.CODEC, this.anchorPoint);
163
		output.putInt("size", this.getPhantomSize());
164
	}
165
166
	@Override
167
	public boolean shouldRenderAtSqrDistance(final double distance) {
168
		return true;
169
	}
170
171
	@Override
172
	public SoundSource getSoundSource() {
173
		return SoundSource.HOSTILE;
174
	}
175
176
	@Override
177
	protected SoundEvent getAmbientSound() {
178
		return SoundEvents.PHANTOM_AMBIENT;
179
	}
180
181
	@Override
182
	protected SoundEvent getHurtSound(final DamageSource source) {
183
		return SoundEvents.PHANTOM_HURT;
184
	}
185
186
	@Override
187
	protected SoundEvent getDeathSound() {
188
		return SoundEvents.PHANTOM_DEATH;
189
	}
190
191
	@Override
192
	public EntityDimensions getDefaultDimensions(final Pose pose) {
193
		int size = this.getPhantomSize();
194
		EntityDimensions originalDimensions = super.getDefaultDimensions(pose);
195
		return originalDimensions.scale(1.0F + 0.15F * size);
196
	}
197
198
	private boolean canAttack(final ServerLevel level, final LivingEntity target, final TargetingConditions targetConditions) {
199
		return targetConditions.test(level, this, target);
200
	}
201
202
	private enum AttackPhase {
203
		CIRCLE,
204
		SWOOP;
205
	}
206
207
	private class PhantomAttackPlayerTargetGoal extends Goal {
208
		private final TargetingConditions attackTargeting = TargetingConditions.forCombat().range(64.0);
209
		private int nextScanTick = reducedTickDelay(20);
210
211
		@Override
212
		public boolean canUse() {
213
			if (this.nextScanTick > 0) {
214
				this.nextScanTick--;
215
				return false;
216
			}
217
218
			this.nextScanTick = reducedTickDelay(60);
219
			ServerLevel level = getServerLevel(Phantom.this.level());
220
			List<Player> players = level.getNearbyPlayers(this.attackTargeting, Phantom.this, Phantom.this.getBoundingBox().inflate(16.0, 64.0, 16.0));
221
			if (!players.isEmpty()) {
222
				players.sort(Comparator.<Player, Double>comparing(Entity::getY).reversed());
223
224
				for (Player player : players) {
225
					if (Phantom.this.canAttack(level, player, TargetingConditions.DEFAULT)) {
226
						Phantom.this.setTarget(player);
227
						return true;
228
					}
229
				}
230
			}
231
232
			return false;
233
		}
234
235
		@Override
236
		public boolean canContinueToUse() {
237
			LivingEntity target = Phantom.this.getTarget();
238
			return target != null ? Phantom.this.canAttack(getServerLevel(Phantom.this.level()), target, TargetingConditions.DEFAULT) : false;
239
		}
240
	}
241
242
	private class PhantomAttackStrategyGoal extends Goal {
243
		private int nextSweepTick;
244
245
		@Override
246
		public boolean canUse() {
247
			LivingEntity target = Phantom.this.getTarget();
248
			return target != null ? Phantom.this.canAttack(getServerLevel(Phantom.this.level()), target, TargetingConditions.DEFAULT) : false;
249
		}
250
251
		@Override
252
		public void start() {
253
			this.nextSweepTick = this.adjustedTickDelay(10);
254
			Phantom.this.attackPhase = Phantom.AttackPhase.CIRCLE;
255
			this.setAnchorAboveTarget();
256
		}
257
258
		@Override
259
		public void stop() {
260
			if (Phantom.this.anchorPoint != null) {
261
				Phantom.this.anchorPoint = Phantom.this.level()
262
					.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING, Phantom.this.anchorPoint)
263
					.above(10 + Phantom.this.random.nextInt(20));
264
			}
265
		}
266
267
		@Override
268
		public void tick() {
269
			if (Phantom.this.attackPhase == Phantom.AttackPhase.CIRCLE) {
270
				this.nextSweepTick--;
271
				if (this.nextSweepTick <= 0) {
272
					Phantom.this.attackPhase = Phantom.AttackPhase.SWOOP;
273
					this.setAnchorAboveTarget();
274
					this.nextSweepTick = this.adjustedTickDelay((8 + Phantom.this.random.nextInt(4)) * 20);
275
					Phantom.this.playSound(SoundEvents.PHANTOM_SWOOP, 10.0F, 0.95F + Phantom.this.random.nextFloat() * 0.1F);
276
				}
277
			}
278
		}
279
280
		private void setAnchorAboveTarget() {
281
			if (Phantom.this.anchorPoint != null) {
282
				Phantom.this.anchorPoint = Phantom.this.getTarget().blockPosition().above(20 + Phantom.this.random.nextInt(20));
283
				if (Phantom.this.anchorPoint.getY() < Phantom.this.level().getSeaLevel()) {
284
					Phantom.this.anchorPoint = new BlockPos(Phantom.this.anchorPoint.getX(), Phantom.this.level().getSeaLevel() + 1, Phantom.this.anchorPoint.getZ());
285
				}
286
			}
287
		}
288
	}
289
290
	private class PhantomBodyRotationControl extends BodyRotationControl {
291
		public PhantomBodyRotationControl(final Mob mob) {
292
			super(mob);
293
		}
294
295
		@Override
296
		public void clientTick() {
297
			Phantom.this.yHeadRot = Phantom.this.yBodyRot;
298
			Phantom.this.yBodyRot = Phantom.this.getYRot();
299
		}
300
	}
301
302
	private class PhantomCircleAroundAnchorGoal extends Phantom.PhantomMoveTargetGoal {
303
		private float angle;
304
		private float distance;
305
		private float height;
306
		private float clockwise;
307
308
		@Override
309
		public boolean canUse() {
310
			return Phantom.this.getTarget() == null || Phantom.this.attackPhase == Phantom.AttackPhase.CIRCLE;
311
		}
312
313
		@Override
314
		public void start() {
315
			this.distance = 5.0F + Phantom.this.random.nextFloat() * 10.0F;
316
			this.height = -4.0F + Phantom.this.random.nextFloat() * 9.0F;
317
			this.clockwise = Phantom.this.random.nextBoolean() ? 1.0F : -1.0F;
318
			this.selectNext();
319
		}
320
321
		@Override
322
		public void tick() {
323
			if (Phantom.this.random.nextInt(this.adjustedTickDelay(350)) == 0) {
324
				this.height = -4.0F + Phantom.this.random.nextFloat() * 9.0F;
325
			}
326
327
			if (Phantom.this.random.nextInt(this.adjustedTickDelay(250)) == 0) {
328
				this.distance++;
329
				if (this.distance > 15.0F) {
330
					this.distance = 5.0F;
331
					this.clockwise = -this.clockwise;
332
				}
333
			}
334
335
			if (Phantom.this.random.nextInt(this.adjustedTickDelay(450)) == 0) {
336
				this.angle = Phantom.this.random.nextFloat() * 2.0F * (float) Math.PI;
337
				this.selectNext();
338
			}
339
340
			if (this.touchingTarget()) {
341
				this.selectNext();
342
			}
343
344
			if (Phantom.this.moveTargetPoint.y < Phantom.this.getY() && !Phantom.this.level().isEmptyBlock(Phantom.this.blockPosition().below(1))) {
345
				this.height = Math.max(1.0F, this.height);
346
				this.selectNext();
347
			}
348
349
			if (Phantom.this.moveTargetPoint.y > Phantom.this.getY() && !Phantom.this.level().isEmptyBlock(Phantom.this.blockPosition().above(1))) {
350
				this.height = Math.min(-1.0F, this.height);
351
				this.selectNext();
352
			}
353
		}
354
355
		private void selectNext() {
356
			if (Phantom.this.anchorPoint == null) {
357
				Phantom.this.anchorPoint = Phantom.this.blockPosition();
358
			}
359
360
			this.angle = this.angle + this.clockwise * 15.0F * (float) (Math.PI / 180.0);
361
			Phantom.this.moveTargetPoint = Vec3.atLowerCornerOf(Phantom.this.anchorPoint)
362
				.add(this.distance * Mth.cos(this.angle), -4.0F + this.height, this.distance * Mth.sin(this.angle));
363
		}
364
	}
365
366
	private static class PhantomLookControl extends LookControl {
367
		public PhantomLookControl(final Mob mob) {
368
			super(mob);
369
		}
370
371
		@Override
372
		public void tick() {
373
		}
374
	}
375
376
	private class PhantomMoveControl<T extends Mob> extends MoveControl<T> {
377
		private float speed = 0.1F;
378
379
		public PhantomMoveControl(final T mob) {
380
			super(mob);
381
		}
382
383
		@Override
384
		public void tick() {
385
			if (Phantom.this.horizontalCollision) {
386
				Phantom.this.setYRot(Phantom.this.getYRot() + 180.0F);
387
				this.speed = 0.1F;
388
			}
389
390
			double tdx = Phantom.this.moveTargetPoint.x - Phantom.this.getX();
391
			double tdy = Phantom.this.moveTargetPoint.y - Phantom.this.getY();
392
			double tdz = Phantom.this.moveTargetPoint.z - Phantom.this.getZ();
393
			double sd = Math.sqrt(tdx * tdx + tdz * tdz);
394
			if (Math.abs(sd) > 1.0E-5F) {
395
				double yRelativeScale = 1.0 - Math.abs(tdy * 0.7F) / sd;
396
				tdx *= yRelativeScale;
397
				tdz *= yRelativeScale;
398
				sd = Math.sqrt(tdx * tdx + tdz * tdz);
399
				double sd2 = Math.sqrt(tdx * tdx + tdz * tdz + tdy * tdy);
400
				float prev = Phantom.this.getYRot();
401
				float angle = (float)Mth.atan2(tdz, tdx);
402
				float a = Mth.wrapDegrees(Phantom.this.getYRot() + 90.0F);
403
				float b = Mth.wrapDegrees(angle * (180.0F / (float)Math.PI));
404
				Phantom.this.setYRot(Mth.approachDegrees(a, b, 4.0F) - 90.0F);
405
				Phantom.this.yBodyRot = Phantom.this.getYRot();
406
				if (Mth.degreesDifferenceAbs(prev, Phantom.this.getYRot()) < 3.0F) {
407
					this.speed = Mth.approach(this.speed, 1.8F, 0.005F * (1.8F / this.speed));
408
				} else {
409
					this.speed = Mth.approach(this.speed, 0.2F, 0.025F);
410
				}
411
412
				float xRotD = (float)(-(Mth.atan2(-tdy, sd) * 180.0F / (float)Math.PI));
413
				Phantom.this.setXRot(xRotD);
414
				float moveAngle = Phantom.this.getYRot() + 90.0F;
415
				double txd = this.speed * Mth.cos(moveAngle * (float) (Math.PI / 180.0)) * Math.abs(tdx / sd2);
416
				double tzd = this.speed * Mth.sin(moveAngle * (float) (Math.PI / 180.0)) * Math.abs(tdz / sd2);
417
				double tyd = this.speed * Mth.sin(xRotD * (float) (Math.PI / 180.0)) * Math.abs(tdy / sd2);
418
				Vec3 movement = Phantom.this.getDeltaMovement();
419
				Phantom.this.setDeltaMovement(movement.add(new Vec3(txd, tyd, tzd).subtract(movement).scale(0.2)));
420
			}
421
		}
422
	}
423
424
	private abstract class PhantomMoveTargetGoal extends Goal {
425
		public PhantomMoveTargetGoal() {
426
			this.setFlags(EnumSet.of(Goal.Flag.MOVE));
427
		}
428
429
		protected boolean touchingTarget() {
430
			return Phantom.this.moveTargetPoint.distanceToSqr(Phantom.this.getX(), Phantom.this.getY(), Phantom.this.getZ()) < 4.0;
431
		}
432
	}
433
434
	private class PhantomSweepAttackGoal extends Phantom.PhantomMoveTargetGoal {
435
		private static final int CAT_SEARCH_TICK_DELAY = 20;
436
		private boolean isScaredOfCat;
437
		private int catSearchTick;
438
439
		@Override
440
		public boolean canUse() {
441
			return Phantom.this.getTarget() != null && Phantom.this.attackPhase == Phantom.AttackPhase.SWOOP;
442
		}
443
444
		@Override
445
		public boolean canContinueToUse() {
446
			LivingEntity target = Phantom.this.getTarget();
447
			if (target == null) {
448
				return false;
449
			} else if (!target.isAlive()) {
450
				return false;
451
			} else if (target instanceof Player player && (target.isSpectator() || player.isCreative())) {
452
				return false;
453
			} else {
454
				if (!this.canUse()) {
455
					return false;
456
				}
457
458
				if (Phantom.this.tickCount > this.catSearchTick) {
459
					this.catSearchTick = Phantom.this.tickCount + 20;
460
					List<Cat> cats = Phantom.this.level().getEntitiesOfClass(Cat.class, Phantom.this.getBoundingBox().inflate(16.0), EntitySelector.ENTITY_STILL_ALIVE);
461
462
					for (Cat cat : cats) {
463
						cat.hiss();
464
					}
465
466
					this.isScaredOfCat = !cats.isEmpty();
467
				}
468
469
				return !this.isScaredOfCat;
470
			}
471
		}
472
473
		@Override
474
		public void stop() {
475
			Phantom.this.setTarget(null);
476
			Phantom.this.attackPhase = Phantom.AttackPhase.CIRCLE;
477
		}
478
479
		@Override
480
		public void tick() {
481
			LivingEntity target = Phantom.this.getTarget();
482
			if (target != null) {
483
				Phantom.this.moveTargetPoint = new Vec3(target.getX(), target.getY(0.5), target.getZ());
484
				if (Phantom.this.getBoundingBox().inflate(0.2F).intersects(target.getBoundingBox())) {
485
					Phantom.this.doHurtTarget(getServerLevel(Phantom.this.level()), target);
486
					Phantom.this.attackPhase = Phantom.AttackPhase.CIRCLE;
487
					if (!Phantom.this.isSilent()) {
488
						Phantom.this.level().levelEvent(1039, Phantom.this.blockPosition(), 0);
489
					}
490
				} else if (Phantom.this.horizontalCollision || Phantom.this.wasHurtRecently()) {
491
					Phantom.this.attackPhase = Phantom.AttackPhase.CIRCLE;
492
				}
493
			}
494
		}
495
	}
496
}
497

Decompiled Minecraft 26.3 source of Phantom (net/minecraft/world/entity/monster/Phantom.java) with Mojang official mappings, stored verbatim on the site.