EnderDragon .java

net.minecraft.world.entity.boss.enderdragon.EnderDragon (class, 867 lines): members: SITTING_ALLOWED_DAMAGE_PERCENTAGE, SITTING_DAMAGE_RECEIVED_KEY, tail3.

net.minecraft.world.entity.boss.enderdragonclass867 linescommonextends Mob30.0 KB

Implements

API Surface (37)

void setDragonFight(final EnderDragonFight fight)
void setFightOrigin(final BlockPos fightOrigin)
BlockPos getFightOrigin()
AttributeSupplier.Builder createAttributes()
boolean isFlapping()
void onFlap()
void defineSynchedData(final SynchedEntityData.Builder entityData)
void aiStep()
boolean hurt(final ServerLevel level, final EnderDragonPart part, final DamageSource source, float damage)
boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage)
void reallyHurt(final ServerLevel level, final DamageSource source, final float damage)
void handleKillingBlow()
void knockback(final double power, final double xd, final double zd, final DamageSource source, final float damage, final boolean comesFromEffect)
void kill(final ServerLevel level)
void tickDeath()
int findClosestNode()
int findClosestNode(final double tX, final double tY, final double tZ)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
void checkDespawn()
EnderDragonPart[] getSubEntities()
boolean isPickable()
SoundSource getSoundSource()
SoundEvent getAmbientSound()
SoundEvent getHurtSound(final DamageSource source)
float getSoundVolume()
Vec3 getHeadLookVector(final float a)
void onCrystalDestroyed(final ServerLevel level, final EndCrystal crystal, final BlockPos pos, final DamageSource source)
void onSyncedDataUpdated(final EntityDataAccessor<?> accessor)
EnderDragonPhaseManager getPhaseManager()
boolean addEffect(final MobEffectInstance newEffect, final @Nullable Entity source)
boolean canRide(final Entity vehicle)
boolean canUsePortal(final boolean ignorePassenger)
void recreateFromPacket(final ClientboundAddEntityPacket packet)
boolean canAttack(final LivingEntity target)
float sanitizeScale(final float scale)
MoveSimulationType getMoveSimulationType()

Fields & Constants (34)

LOGGER
DATA_PHASE
CRYSTAL_DESTROY_TARGETING
GROWL_INTERVAL_MIN
GROWL_INTERVAL_MAX
SITTING_ALLOWED_DAMAGE_PERCENTAGE
DRAGON_DEATH_TIME_KEY
DRAGON_PHASE_KEY
SITTING_DAMAGE_RECEIVED_KEY
DEFAULT_DEATH_TIME
flightHistory
subEntities
head
neck
body
tail1
tail2
tail3
wing1
wing2
oFlapTime
flapTime
inWall
dragonDeathTime
yRotA
nearestCrystal
dragonFight
fightOrigin
phaseManager
growlTime
sittingDamageReceived
nodes
nodeAdjacency
openSet

String literals (8)

"DragonDeathTime"
"DragonPhase"
"sitting_damage_received"
"head"
"neck"
"body"
"tail"
"wing"

Code Graph: Dependencies (50)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

EnderDragon.java867 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.boss.enderdragon;
2
3
import com.google.common.collect.Lists;
4
import com.mojang.logging.LogUtils;
5
import java.util.List;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.particles.ParticleTypes;
8
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
9
import net.minecraft.network.syncher.EntityDataAccessor;
10
import net.minecraft.network.syncher.EntityDataSerializers;
11
import net.minecraft.network.syncher.SynchedEntityData;
12
import net.minecraft.server.level.ServerLevel;
13
import net.minecraft.sounds.SoundEvent;
14
import net.minecraft.sounds.SoundEvents;
15
import net.minecraft.sounds.SoundSource;
16
import net.minecraft.tags.BlockTags;
17
import net.minecraft.tags.DamageTypeTags;
18
import net.minecraft.util.Mth;
19
import net.minecraft.world.damagesource.DamageSource;
20
import net.minecraft.world.effect.MobEffectInstance;
21
import net.minecraft.world.entity.Entity;
22
import net.minecraft.world.entity.EntitySelector;
23
import net.minecraft.world.entity.EntityType;
24
import net.minecraft.world.entity.EntityTypes;
25
import net.minecraft.world.entity.ExperienceOrb;
26
import net.minecraft.world.entity.LivingEntity;
27
import net.minecraft.world.entity.Mob;
28
import net.minecraft.world.entity.MoveSimulationType;
29
import net.minecraft.world.entity.MoverType;
30
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
31
import net.minecraft.world.entity.ai.attributes.Attributes;
32
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
33
import net.minecraft.world.entity.boss.enderdragon.phases.DragonPhaseInstance;
34
import net.minecraft.world.entity.boss.enderdragon.phases.EnderDragonPhase;
35
import net.minecraft.world.entity.boss.enderdragon.phases.EnderDragonPhaseManager;
36
import net.minecraft.world.entity.monster.Enemy;
37
import net.minecraft.world.entity.player.Player;
38
import net.minecraft.world.item.enchantment.EnchantmentHelper;
39
import net.minecraft.world.level.Level;
40
import net.minecraft.world.level.block.state.BlockState;
41
import net.minecraft.world.level.dimension.end.EnderDragonFight;
42
import net.minecraft.world.level.gameevent.GameEvent;
43
import net.minecraft.world.level.gamerules.GameRules;
44
import net.minecraft.world.level.levelgen.Heightmap;
45
import net.minecraft.world.level.pathfinder.BinaryHeap;
46
import net.minecraft.world.level.pathfinder.Node;
47
import net.minecraft.world.level.pathfinder.Path;
48
import net.minecraft.world.level.storage.ValueInput;
49
import net.minecraft.world.level.storage.ValueOutput;
50
import net.minecraft.world.phys.AABB;
51
import net.minecraft.world.phys.Vec3;
52
import org.jspecify.annotations.Nullable;
53
import org.slf4j.Logger;
54
55
public class EnderDragon extends Mob implements Enemy {
56
	private static final Logger LOGGER = LogUtils.getLogger();
57
	public static final EntityDataAccessor<Integer> DATA_PHASE = SynchedEntityData.defineId(EnderDragon.class, EntityDataSerializers.INT);
58
	private static final TargetingConditions CRYSTAL_DESTROY_TARGETING = TargetingConditions.forCombat().range(64.0);
59
	private static final int GROWL_INTERVAL_MIN = 200;
60
	private static final int GROWL_INTERVAL_MAX = 400;
61
	private static final float SITTING_ALLOWED_DAMAGE_PERCENTAGE = 0.25F;
62
	private static final String DRAGON_DEATH_TIME_KEY = "DragonDeathTime";
63
	private static final String DRAGON_PHASE_KEY = "DragonPhase";
64
	private static final String SITTING_DAMAGE_RECEIVED_KEY = "sitting_damage_received";
65
	private static final int DEFAULT_DEATH_TIME = 0;
66
	public final DragonFlightHistory flightHistory = new DragonFlightHistory();
67
	private final EnderDragonPart[] subEntities;
68
	public final EnderDragonPart head;
69
	private final EnderDragonPart neck;
70
	private final EnderDragonPart body;
71
	private final EnderDragonPart tail1;
72
	private final EnderDragonPart tail2;
73
	private final EnderDragonPart tail3;
74
	private final EnderDragonPart wing1;
75
	private final EnderDragonPart wing2;
76
	public float oFlapTime;
77
	public float flapTime;
78
	public boolean inWall;
79
	public int dragonDeathTime = 0;
80
	public float yRotA;
81
	public @Nullable EndCrystal nearestCrystal;
82
	private @Nullable EnderDragonFight dragonFight;
83
	private BlockPos fightOrigin = BlockPos.ZERO;
84
	private final EnderDragonPhaseManager phaseManager;
85
	private int growlTime = 100;
86
	private float sittingDamageReceived;
87
	private final Node[] nodes = new Node[24];
88
	private final int[] nodeAdjacency = new int[24];
89
	private final BinaryHeap openSet = new BinaryHeap();
90
91
	public EnderDragon(final EntityType<? extends EnderDragon> type, final Level level) {
92
		super(EntityTypes.ENDER_DRAGON, level);
93
		this.head = new EnderDragonPart(this, "head", 1.0F, 1.0F);
94
		this.neck = new EnderDragonPart(this, "neck", 3.0F, 3.0F);
95
		this.body = new EnderDragonPart(this, "body", 5.0F, 3.0F);
96
		this.tail1 = new EnderDragonPart(this, "tail", 2.0F, 2.0F);
97
		this.tail2 = new EnderDragonPart(this, "tail", 2.0F, 2.0F);
98
		this.tail3 = new EnderDragonPart(this, "tail", 2.0F, 2.0F);
99
		this.wing1 = new EnderDragonPart(this, "wing", 4.0F, 2.0F);
100
		this.wing2 = new EnderDragonPart(this, "wing", 4.0F, 2.0F);
101
		this.subEntities = new EnderDragonPart[]{this.head, this.neck, this.body, this.tail1, this.tail2, this.tail3, this.wing1, this.wing2};
102
		this.setHealth(this.getMaxHealth());
103
		this.noPhysics = true;
104
		this.phaseManager = new EnderDragonPhaseManager(this);
105
	}
106
107
	public void setDragonFight(final EnderDragonFight fight) {
108
		this.dragonFight = fight;
109
	}
110
111
	public void setFightOrigin(final BlockPos fightOrigin) {
112
		this.fightOrigin = fightOrigin;
113
	}
114
115
	public BlockPos getFightOrigin() {
116
		return this.fightOrigin;
117
	}
118
119
	public static AttributeSupplier.Builder createAttributes() {
120
		return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 200.0).add(Attributes.CAMERA_DISTANCE, 16.0);
121
	}
122
123
	@Override
124
	public boolean isFlapping() {
125
		float flap = Mth.cos(this.flapTime * (float) (Math.PI * 2));
126
		float oldFlap = Mth.cos(this.oFlapTime * (float) (Math.PI * 2));
127
		return oldFlap <= -0.3F && flap >= -0.3F;
128
	}
129
130
	@Override
131
	public void onFlap() {
132
		if (this.level().isClientSide() && !this.isSilent()) {
133
			this.level()
134
				.playLocalSound(
135
					this.getX(), this.getY(), this.getZ(), SoundEvents.ENDER_DRAGON_FLAP, this.getSoundSource(), 5.0F, 0.8F + this.random.nextFloat() * 0.3F, false
136
				);
137
		}
138
	}
139
140
	@Override
141
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
142
		super.defineSynchedData(entityData);
143
		entityData.define(DATA_PHASE, EnderDragonPhase.HOVERING.getId());
144
	}
145
146
	@Override
147
	public void aiStep() {
148
		this.processFlappingMovement();
149
		if (this.level().isClientSide()) {
150
			this.setHealth(this.getHealth());
151
			if (!this.isSilent() && !this.phaseManager.getCurrentPhase().isSitting() && --this.growlTime < 0) {
152
				this.level()
153
					.playLocalSound(
154
						this.getX(), this.getY(), this.getZ(), SoundEvents.ENDER_DRAGON_GROWL, this.getSoundSource(), 2.5F, 0.8F + this.random.nextFloat() * 0.3F, false
155
					);
156
				this.growlTime = 200 + this.random.nextInt(200);
157
			}
158
		}
159
160
		if (this.dragonFight == null && this.level() instanceof ServerLevel serverLevel) {
161
			EnderDragonFight maybeOurFight = serverLevel.getDragonFight();
162
			if (maybeOurFight != null && this.getUUID().equals(maybeOurFight.dragonUUID())) {
163
				this.dragonFight = maybeOurFight;
164
			}
165
		}
166
167
		this.oFlapTime = this.flapTime;
168
		if (this.isDeadOrDying()) {
169
			float xo = (this.random.nextFloat() - 0.5F) * 8.0F;
170
			float yo = (this.random.nextFloat() - 0.5F) * 4.0F;
171
			float zo = (this.random.nextFloat() - 0.5F) * 8.0F;
172
			this.level().addParticle(ParticleTypes.EXPLOSION, this.getX() + xo, this.getY() + 2.0 + yo, this.getZ() + zo, 0.0, 0.0, 0.0);
173
		} else {
174
			this.checkCrystals();
175
			Vec3 movement = this.getDeltaMovement();
176
			float flapSpeed = 0.2F / ((float)movement.horizontalDistance() * 10.0F + 1.0F);
177
			flapSpeed *= (float)Math.pow(2.0, movement.y);
178
			if (this.phaseManager.getCurrentPhase().isSitting()) {
179
				this.flapTime += 0.1F;
180
			} else if (this.inWall) {
181
				this.flapTime += flapSpeed * 0.5F;
182
			} else {
183
				this.flapTime += flapSpeed;
184
			}
185
186
			this.setYRot(Mth.wrapDegrees(this.getYRot()));
187
			if (this.isNoAi()) {
188
				this.flapTime = 0.5F;
189
			} else {
190
				this.flightHistory.record(this.getY(), this.getYRot());
191
				if (this.level() instanceof ServerLevel level) {
192
					DragonPhaseInstance currentPhase = this.phaseManager.getCurrentPhase();
193
					currentPhase.doServerTick(level);
194
					if (this.phaseManager.getCurrentPhase() != currentPhase) {
195
						currentPhase = this.phaseManager.getCurrentPhase();
196
						currentPhase.doServerTick(level);
197
					}
198
199
					Vec3 targetLocation = currentPhase.getFlyTargetLocation();
200
					if (targetLocation != null) {
201
						double xdd = targetLocation.x - this.getX();
202
						double ydd = targetLocation.y - this.getY();
203
						double zdd = targetLocation.z - this.getZ();
204
						double distToTarget = xdd * xdd + ydd * ydd + zdd * zdd;
205
						float max = currentPhase.getFlySpeed();
206
						double horizontalDist = Math.sqrt(xdd * xdd + zdd * zdd);
207
						if (horizontalDist > 0.0) {
208
							ydd = Mth.clamp(ydd / horizontalDist, -max, max);
209
						}
210
211
						this.setDeltaMovement(this.getDeltaMovement().add(0.0, ydd * 0.01, 0.0));
212
						this.setYRot(Mth.wrapDegrees(this.getYRot()));
213
						Vec3 aim = targetLocation.subtract(this.getX(), this.getY(), this.getZ()).normalize();
214
						Vec3 dir = new Vec3(Mth.sin(this.getYRot() * (float) (Math.PI / 180.0)), this.getDeltaMovement().y, -Mth.cos(this.getYRot() * (float) (Math.PI / 180.0)))
215
							.normalize();
216
						float dot = Math.max(((float)dir.dot(aim) + 0.5F) / 1.5F, 0.0F);
217
						if (Math.abs(xdd) > 1.0E-5F || Math.abs(zdd) > 1.0E-5F) {
218
							float yRotD = Mth.clamp(Mth.wrapDegrees(180.0F - (float)Mth.atan2(xdd, zdd) * (180.0F / (float)Math.PI) - this.getYRot()), -50.0F, 50.0F);
219
							this.yRotA *= 0.8F;
220
							this.yRotA = this.yRotA + yRotD * currentPhase.getTurnSpeed();
221
							this.setYRot(this.getYRot() + this.yRotA * 0.1F);
222
						}
223
224
						float span = (float)(2.0 / (distToTarget + 1.0));
225
						float speed = 0.06F;
226
						this.moveRelative(0.06F * (dot * span + (1.0F - span)), new Vec3(0.0, 0.0, -1.0));
227
						if (this.inWall) {
228
							this.move(MoverType.SELF, this.getDeltaMovement().scale(0.8F));
229
						} else {
230
							this.move(MoverType.SELF, this.getDeltaMovement());
231
						}
232
233
						Vec3 actual = this.getDeltaMovement().normalize();
234
						double slide = 0.8 + 0.15 * (actual.dot(dir) + 1.0) / 2.0;
235
						this.setDeltaMovement(this.getDeltaMovement().multiply(slide, 0.91F, slide));
236
					}
237
				} else {
238
					this.phaseManager.getCurrentPhase().doClientTick();
239
				}
240
241
				if (!this.level().isClientSide()) {
242
					this.applyEffectsFromBlocks();
243
				}
244
245
				this.yBodyRot = this.getYRot();
246
				Vec3[] oldPos = new Vec3[this.subEntities.length];
247
248
				for (int i = 0; i < this.subEntities.length; i++) {
249
					oldPos[i] = new Vec3(this.subEntities[i].getX(), this.subEntities[i].getY(), this.subEntities[i].getZ());
250
				}
251
252
				float tilt = (float)(this.flightHistory.get(5).y() - this.flightHistory.get(10).y()) * 10.0F * (float) (Math.PI / 180.0);
253
				float ccTilt = Mth.cos(tilt);
254
				float ssTilt = Mth.sin(tilt);
255
				float rot1 = this.getYRot() * (float) (Math.PI / 180.0);
256
				float ss1 = Mth.sin(rot1);
257
				float cc1 = Mth.cos(rot1);
258
				this.tickPart(this.body, ss1 * 0.5F, 0.0, -cc1 * 0.5F);
259
				this.tickPart(this.wing1, cc1 * 4.5F, 2.0, ss1 * 4.5F);
260
				this.tickPart(this.wing2, cc1 * -4.5F, 2.0, ss1 * -4.5F);
261
				if (this.level() instanceof ServerLevel serverLevel && !this.wasHurtRecently()) {
262
					this.knockBack(
263
						serverLevel,
264
						serverLevel.getEntities(this, this.wing1.getBoundingBox().inflate(4.0, 2.0, 4.0).move(0.0, -2.0, 0.0), EntitySelector.NO_CREATIVE_OR_SPECTATOR)
265
					);
266
					this.knockBack(
267
						serverLevel,
268
						serverLevel.getEntities(this, this.wing2.getBoundingBox().inflate(4.0, 2.0, 4.0).move(0.0, -2.0, 0.0), EntitySelector.NO_CREATIVE_OR_SPECTATOR)
269
					);
270
					this.hurt(serverLevel, serverLevel.getEntities(this, this.head.getBoundingBox().inflate(1.0), EntitySelector.NO_CREATIVE_OR_SPECTATOR));
271
					this.hurt(serverLevel, serverLevel.getEntities(this, this.neck.getBoundingBox().inflate(1.0), EntitySelector.NO_CREATIVE_OR_SPECTATOR));
272
				}
273
274
				float ss2 = Mth.sin(this.getYRot() * (float) (Math.PI / 180.0) - this.yRotA * 0.01F);
275
				float cc2 = Mth.cos(this.getYRot() * (float) (Math.PI / 180.0) - this.yRotA * 0.01F);
276
				float yOffset = this.getHeadYOffset();
277
				this.tickPart(this.head, ss2 * 6.5F * ccTilt, yOffset + ssTilt * 6.5F, -cc2 * 6.5F * ccTilt);
278
				this.tickPart(this.neck, ss2 * 5.5F * ccTilt, yOffset + ssTilt * 5.5F, -cc2 * 5.5F * ccTilt);
279
				DragonFlightHistory.Sample p1 = this.flightHistory.get(5);
280
281
				for (int i = 0; i < 3; i++) {
282
					EnderDragonPart part = null;
283
					if (i == 0) {
284
						part = this.tail1;
285
					}
286
287
					if (i == 1) {
288
						part = this.tail2;
289
					}
290
291
					if (i == 2) {
292
						part = this.tail3;
293
					}
294
295
					DragonFlightHistory.Sample p0 = this.flightHistory.get(12 + i * 2);
296
					float rot = this.getYRot() * (float) (Math.PI / 180.0) + this.rotWrap(p0.yRot() - p1.yRot()) * (float) (Math.PI / 180.0);
297
					float ss = Mth.sin(rot);
298
					float cc = Mth.cos(rot);
299
					float dd1 = 1.5F;
300
					float dd = (i + 1) * 2.0F;
301
					this.tickPart(part, -(ss1 * 1.5F + ss * dd) * ccTilt, p0.y() - p1.y() - (dd + 1.5F) * ssTilt + 1.5, (cc1 * 1.5F + cc * dd) * ccTilt);
302
				}
303
304
				if (this.level() instanceof ServerLevel level) {
305
					this.inWall = this.checkWalls(level, this.head.getBoundingBox())
306
						| this.checkWalls(level, this.neck.getBoundingBox())
307
						| this.checkWalls(level, this.body.getBoundingBox());
308
					if (this.dragonFight != null) {
309
						this.dragonFight.updateDragon(this);
310
					}
311
				}
312
313
				for (int i = 0; i < this.subEntities.length; i++) {
314
					this.subEntities[i].xo = oldPos[i].x;
315
					this.subEntities[i].yo = oldPos[i].y;
316
					this.subEntities[i].zo = oldPos[i].z;
317
					this.subEntities[i].xOld = oldPos[i].x;
318
					this.subEntities[i].yOld = oldPos[i].y;
319
					this.subEntities[i].zOld = oldPos[i].z;
320
				}
321
			}
322
		}
323
	}
324
325
	private void tickPart(final EnderDragonPart part, final double x, final double y, final double z) {
326
		part.setPos(this.getX() + x, this.getY() + y, this.getZ() + z);
327
	}
328
329
	private float getHeadYOffset() {
330
		if (this.phaseManager.getCurrentPhase().isSitting()) {
331
			return -1.0F;
332
		}
333
334
		DragonFlightHistory.Sample p0 = this.flightHistory.get(5);
335
		DragonFlightHistory.Sample p1 = this.flightHistory.get(0);
336
		return (float)(p0.y() - p1.y());
337
	}
338
339
	private void checkCrystals() {
340
		if (this.nearestCrystal != null) {
341
			if (this.nearestCrystal.isRemoved()) {
342
				this.nearestCrystal = null;
343
			} else if (this.tickCount % 10 == 0 && this.getHealth() < this.getMaxHealth()) {
344
				this.setHealth(this.getHealth() + 1.0F);
345
			}
346
		}
347
348
		if (this.random.nextInt(10) == 0) {
349
			List<EndCrystal> crystals = this.level().getEntitiesOfClass(EndCrystal.class, this.getBoundingBox().inflate(32.0));
350
			EndCrystal nearest = null;
351
			double distance = Double.MAX_VALUE;
352
353
			for (EndCrystal crystal : crystals) {
354
				double dist = crystal.distanceToSqr(this);
355
				if (dist < distance) {
356
					distance = dist;
357
					nearest = crystal;
358
				}
359
			}
360
361
			this.nearestCrystal = nearest;
362
		}
363
	}
364
365
	private void knockBack(final ServerLevel serverLevel, final List<Entity> entities) {
366
		double xm = (this.body.getBoundingBox().minX + this.body.getBoundingBox().maxX) / 2.0;
367
		double zm = (this.body.getBoundingBox().minZ + this.body.getBoundingBox().maxZ) / 2.0;
368
369
		for (Entity entity : entities) {
370
			if (entity instanceof LivingEntity livingTarget) {
371
				double xd = entity.getX() - xm;
372
				double zd = entity.getZ() - zm;
373
				double dd = Math.max(xd * xd + zd * zd, 0.1);
374
				entity.push(xd / dd * 4.0, 0.2F, zd / dd * 4.0);
375
				if (!this.phaseManager.getCurrentPhase().isSitting()
376
					&& livingTarget.getLastHurtByMobTimestamp() < entity.tickCount - 2
377
					&& this.doTeamsAllowDamage(livingTarget)) {
378
					DamageSource damageSource = this.damageSources().mobAttack(this);
379
					entity.hurtServer(serverLevel, damageSource, 5.0F);
380
					EnchantmentHelper.doPostAttackEffects(serverLevel, entity, damageSource);
381
				}
382
			}
383
		}
384
	}
385
386
	private void hurt(final ServerLevel level, final List<Entity> entities) {
387
		for (Entity target : entities) {
388
			if (target instanceof LivingEntity && this.doTeamsAllowDamage(target)) {
389
				DamageSource damageSource = this.damageSources().mobAttack(this);
390
				target.hurtServer(level, damageSource, 10.0F);
391
				EnchantmentHelper.doPostAttackEffects(level, target, damageSource);
392
			}
393
		}
394
	}
395
396
	private float rotWrap(final double d) {
397
		return (float)Mth.wrapDegrees(d);
398
	}
399
400
	private boolean checkWalls(final ServerLevel level, final AABB bb) {
401
		int x0 = Mth.floor(bb.minX);
402
		int y0 = Mth.floor(bb.minY);
403
		int z0 = Mth.floor(bb.minZ);
404
		int x1 = Mth.floor(bb.maxX);
405
		int y1 = Mth.floor(bb.maxY);
406
		int z1 = Mth.floor(bb.maxZ);
407
		boolean hitWall = false;
408
		boolean destroyedBlock = false;
409
410
		for (int x = x0; x <= x1; x++) {
411
			for (int y = y0; y <= y1; y++) {
412
				for (int z = z0; z <= z1; z++) {
413
					BlockPos blockPos = new BlockPos(x, y, z);
414
					BlockState state = level.getBlockState(blockPos);
415
					if (!state.isAir() && !state.is(BlockTags.DRAGON_TRANSPARENT)) {
416
						if (level.getGameRules().get(GameRules.MOB_GRIEFING) && !state.is(BlockTags.DRAGON_IMMUNE)) {
417
							destroyedBlock = level.removeBlock(blockPos, false) || destroyedBlock;
418
						} else {
419
							hitWall = true;
420
						}
421
					}
422
				}
423
			}
424
		}
425
426
		if (destroyedBlock) {
427
			BlockPos randomPos = new BlockPos(x0 + this.random.nextInt(x1 - x0 + 1), y0 + this.random.nextInt(y1 - y0 + 1), z0 + this.random.nextInt(z1 - z0 + 1));
428
			level.levelEvent(2008, randomPos, 0);
429
		}
430
431
		return hitWall;
432
	}
433
434
	public boolean hurt(final ServerLevel level, final EnderDragonPart part, final DamageSource source, float damage) {
435
		if (this.phaseManager.getCurrentPhase().getPhase() == EnderDragonPhase.DYING) {
436
			return false;
437
		}
438
439
		damage = this.phaseManager.getCurrentPhase().onHurt(source, damage);
440
		if (part != this.head && part != this.neck) {
441
			damage = damage / 4.0F + Math.min(damage, 1.0F);
442
		}
443
444
		if (damage < 0.01F) {
445
			return false;
446
		}
447
448
		if (source.getEntity() instanceof Player || source.is(DamageTypeTags.ALWAYS_HURTS_ENDER_DRAGONS)) {
449
			float healthBefore = this.getHealth();
450
			this.reallyHurt(level, source, damage);
451
			if (this.phaseManager.getCurrentPhase().isSitting()) {
452
				this.sittingDamageReceived = this.sittingDamageReceived + healthBefore - this.getHealth();
453
				if (this.sittingDamageReceived > 0.25F * this.getMaxHealth()) {
454
					this.sittingDamageReceived = 0.0F;
455
					this.phaseManager.setPhase(EnderDragonPhase.TAKEOFF);
456
				}
457
			}
458
		}
459
460
		return true;
461
	}
462
463
	@Override
464
	public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
465
		return this.hurt(level, this.body, source, damage);
466
	}
467
468
	protected void reallyHurt(final ServerLevel level, final DamageSource source, final float damage) {
469
		super.hurtServer(level, source, damage);
470
	}
471
472
	@Override
473
	protected void handleKillingBlow() {
474
		if (!this.phaseManager.getCurrentPhase().isSitting()) {
475
			this.setHealth(1.0F);
476
			this.phaseManager.setPhase(EnderDragonPhase.DYING);
477
		}
478
	}
479
480
	@Override
481
	public void knockback(final double power, final double xd, final double zd, final DamageSource source, final float damage, final boolean comesFromEffect) {
482
		if (!this.phaseManager.getCurrentPhase().isSitting()) {
483
			super.knockback(power, xd, zd, source, damage, comesFromEffect);
484
		}
485
	}
486
487
	@Override
488
	public void kill(final ServerLevel level) {
489
		this.remove(Entity.RemovalReason.KILLED);
490
		this.gameEvent(GameEvent.ENTITY_DIE);
491
		if (this.dragonFight != null) {
492
			this.dragonFight.updateDragon(this);
493
			this.dragonFight.setDragonKilled(this);
494
		}
495
	}
496
497
	@Override
498
	protected void tickDeath() {
499
		if (this.dragonFight != null) {
500
			this.dragonFight.updateDragon(this);
501
		}
502
503
		this.dragonDeathTime++;
504
		if (this.dragonDeathTime >= 180 && this.dragonDeathTime <= 200) {
505
			float xo = (this.random.nextFloat() - 0.5F) * 8.0F;
506
			float yo = (this.random.nextFloat() - 0.5F) * 4.0F;
507
			float zo = (this.random.nextFloat() - 0.5F) * 8.0F;
508
			this.level().addParticle(ParticleTypes.EXPLOSION_EMITTER, this.getX() + xo, this.getY() + 2.0 + yo, this.getZ() + zo, 0.0, 0.0, 0.0);
509
		}
510
511
		int xpCount = 500;
512
		if (this.dragonFight != null && !this.dragonFight.hasPreviouslyKilledDragon()) {
513
			xpCount = 12000;
514
		}
515
516
		if (this.level() instanceof ServerLevel level) {
517
			if (this.dragonDeathTime > 150 && this.dragonDeathTime % 5 == 0 && level.getGameRules().get(GameRules.MOB_DROPS)) {
518
				ExperienceOrb.award(level, this.position(), Mth.floor(xpCount * 0.08F));
519
			}
520
521
			if (this.dragonDeathTime == 1 && !this.isSilent()) {
522
				level.globalLevelEvent(1028, this.blockPosition(), 0);
523
			}
524
		}
525
526
		Vec3 deathMove = new Vec3(0.0, 0.1F, 0.0);
527
		this.move(MoverType.SELF, deathMove);
528
529
		for (EnderDragonPart dragonPart : this.subEntities) {
530
			dragonPart.setOldPosAndRot();
531
			dragonPart.setPos(dragonPart.position().add(deathMove));
532
		}
533
534
		if (this.dragonDeathTime >= 200 && this.level() instanceof ServerLevel level) {
535
			if (level.getGameRules().get(GameRules.MOB_DROPS)) {
536
				ExperienceOrb.award(level, this.position(), Mth.floor(xpCount * 0.2F));
537
			}
538
539
			if (this.dragonFight != null) {
540
				this.dragonFight.setDragonKilled(this);
541
			}
542
543
			this.remove(Entity.RemovalReason.KILLED);
544
			this.gameEvent(GameEvent.ENTITY_DIE);
545
		}
546
	}
547
548
	public int findClosestNode() {
549
		if (this.nodes[0] == null) {
550
			for (int i = 0; i < 24; i++) {
551
				int yAdjustment = 5;
552
				int multiplier = i;
553
				int nodeX;
554
				int nodeZ;
555
				if (i < 12) {
556
					nodeX = Mth.floor(60.0F * Mth.cos(2.0F * ((float) -Math.PI + (float) (Math.PI / 12) * multiplier)));
557
					nodeZ = Mth.floor(60.0F * Mth.sin(2.0F * ((float) -Math.PI + (float) (Math.PI / 12) * multiplier)));
558
				} else if (i < 20) {
559
					multiplier -= 12;
560
					nodeX = Mth.floor(40.0F * Mth.cos(2.0F * ((float) -Math.PI + (float) (Math.PI / 8) * multiplier)));
561
					nodeZ = Mth.floor(40.0F * Mth.sin(2.0F * ((float) -Math.PI + (float) (Math.PI / 8) * multiplier)));
562
					yAdjustment += 10;
563
				} else {
564
					multiplier -= 20;
565
					nodeX = Mth.floor(20.0F * Mth.cos(2.0F * ((float) -Math.PI + (float) (Math.PI / 4) * multiplier)));
566
					nodeZ = Mth.floor(20.0F * Mth.sin(2.0F * ((float) -Math.PI + (float) (Math.PI / 4) * multiplier)));
567
				}
568
569
				int nodeY = Math.max(73, this.level().getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, new BlockPos(nodeX, 0, nodeZ)).getY() + yAdjustment);
570
				this.nodes[i] = new Node(nodeX, nodeY, nodeZ);
571
			}
572
573
			this.nodeAdjacency[0] = 6146;
574
			this.nodeAdjacency[1] = 8197;
575
			this.nodeAdjacency[2] = 8202;
576
			this.nodeAdjacency[3] = 16404;
577
			this.nodeAdjacency[4] = 32808;
578
			this.nodeAdjacency[5] = 32848;
579
			this.nodeAdjacency[6] = 65696;
580
			this.nodeAdjacency[7] = 131392;
581
			this.nodeAdjacency[8] = 131712;
582
			this.nodeAdjacency[9] = 263424;
583
			this.nodeAdjacency[10] = 526848;
584
			this.nodeAdjacency[11] = 525313;
585
			this.nodeAdjacency[12] = 1581057;
586
			this.nodeAdjacency[13] = 3166214;
587
			this.nodeAdjacency[14] = 2138120;
588
			this.nodeAdjacency[15] = 6373424;
589
			this.nodeAdjacency[16] = 4358208;
590
			this.nodeAdjacency[17] = 12910976;
591
			this.nodeAdjacency[18] = 9044480;
592
			this.nodeAdjacency[19] = 9706496;
593
			this.nodeAdjacency[20] = 15216640;
594
			this.nodeAdjacency[21] = 13688832;
595
			this.nodeAdjacency[22] = 11763712;
596
			this.nodeAdjacency[23] = 8257536;
597
		}
598
599
		return this.findClosestNode(this.getX(), this.getY(), this.getZ());
600
	}
601
602
	public int findClosestNode(final double tX, final double tY, final double tZ) {
603
		float closestDist = 10000.0F;
604
		int closestIndex = 0;
605
		Node currentPos = new Node(Mth.floor(tX), Mth.floor(tY), Mth.floor(tZ));
606
		int startIndex = 0;
607
		if (this.dragonFight == null || this.dragonFight.aliveCrystals() == 0) {
608
			startIndex = 12;
609
		}
610
611
		for (int i = startIndex; i < 24; i++) {
612
			if (this.nodes[i] != null) {
613
				float dist = this.nodes[i].distanceToSqr(currentPos);
614
				if (dist < closestDist) {
615
					closestDist = dist;
616
					closestIndex = i;
617
				}
618
			}
619
		}
620
621
		return closestIndex;
622
	}
623
624
	public @Nullable Path findPath(final int startIndex, final int endIndex, final @Nullable Node finalNode) {
625
		for (int i = 0; i < 24; i++) {
626
			Node node = this.nodes[i];
627
			node.closed = false;
628
			node.f = 0.0F;
629
			node.g = 0.0F;
630
			node.h = 0.0F;
631
			node.cameFrom = null;
632
			node.heapIdx = -1;
633
		}
634
635
		Node from = this.nodes[startIndex];
636
		Node to = this.nodes[endIndex];
637
		from.g = 0.0F;
638
		from.h = from.distanceTo(to);
639
		from.f = from.h;
640
		this.openSet.clear();
641
		this.openSet.insert(from);
642
		Node closest = from;
643
		int minimumNodeIndex = 0;
644
		if (this.dragonFight == null || this.dragonFight.aliveCrystals() == 0) {
645
			minimumNodeIndex = 12;
646
		}
647
648
		while (!this.openSet.isEmpty()) {
649
			Node openNode = this.openSet.pop();
650
			if (openNode.equals(to)) {
651
				if (finalNode != null) {
652
					finalNode.cameFrom = to;
653
					to = finalNode;
654
				}
655
656
				return this.reconstructPath(from, to);
657
			}
658
659
			if (openNode.distanceTo(to) < closest.distanceTo(to)) {
660
				closest = openNode;
661
			}
662
663
			openNode.closed = true;
664
			int xIndex = 0;
665
666
			for (int i = 0; i < 24; i++) {
667
				if (this.nodes[i] == openNode) {
668
					xIndex = i;
669
					break;
670
				}
671
			}
672
673
			for (int i = minimumNodeIndex; i < 24; i++) {
674
				if ((this.nodeAdjacency[xIndex] & 1 << i) > 0) {
675
					Node adjacentNode = this.nodes[i];
676
					if (!adjacentNode.closed) {
677
						float tentativeGScore = openNode.g + openNode.distanceTo(adjacentNode);
678
						if (!adjacentNode.inOpenSet() || tentativeGScore < adjacentNode.g) {
679
							adjacentNode.cameFrom = openNode;
680
							adjacentNode.g = tentativeGScore;
681
							adjacentNode.h = adjacentNode.distanceTo(to);
682
							if (adjacentNode.inOpenSet()) {
683
								this.openSet.changeCost(adjacentNode, adjacentNode.g + adjacentNode.h);
684
							} else {
685
								adjacentNode.f = adjacentNode.g + adjacentNode.h;
686
								this.openSet.insert(adjacentNode);
687
							}
688
						}
689
					}
690
				}
691
			}
692
		}
693
694
		if (closest == from) {
695
			return null;
696
		}
697
698
		LOGGER.debug("Failed to find path from {} to {}", startIndex, endIndex);
699
		if (finalNode != null) {
700
			finalNode.cameFrom = closest;
701
			closest = finalNode;
702
		}
703
704
		return this.reconstructPath(from, closest);
705
	}
706
707
	private Path reconstructPath(final Node from, final Node to) {
708
		List<Node> nodes = Lists.newArrayList();
709
		Node node = to;
710
		nodes.add(0, node);
711
712
		while (node.cameFrom != null) {
713
			node = node.cameFrom;
714
			nodes.add(0, node);
715
		}
716
717
		return new Path(nodes, new BlockPos(to.x, to.y, to.z), true);
718
	}
719
720
	@Override
721
	protected void addAdditionalSaveData(final ValueOutput output) {
722
		super.addAdditionalSaveData(output);
723
		output.putInt("DragonPhase", this.phaseManager.getCurrentPhase().getPhase().getId());
724
		output.putInt("DragonDeathTime", this.dragonDeathTime);
725
		output.putFloat("sitting_damage_received", this.sittingDamageReceived);
726
	}
727
728
	@Override
729
	protected void readAdditionalSaveData(final ValueInput input) {
730
		super.readAdditionalSaveData(input);
731
		input.getInt("DragonPhase").ifPresent(phaseId -> this.phaseManager.setPhase(EnderDragonPhase.getById(phaseId)));
732
		this.dragonDeathTime = input.getIntOr("DragonDeathTime", 0);
733
		this.sittingDamageReceived = input.getFloatOr("sitting_damage_received", 0.0F);
734
	}
735
736
	@Override
737
	public void checkDespawn() {
738
	}
739
740
	public EnderDragonPart[] getSubEntities() {
741
		return this.subEntities;
742
	}
743
744
	@Override
745
	public boolean isPickable() {
746
		return false;
747
	}
748
749
	@Override
750
	public SoundSource getSoundSource() {
751
		return SoundSource.HOSTILE;
752
	}
753
754
	@Override
755
	protected SoundEvent getAmbientSound() {
756
		return SoundEvents.ENDER_DRAGON_AMBIENT;
757
	}
758
759
	@Override
760
	protected SoundEvent getHurtSound(final DamageSource source) {
761
		return SoundEvents.ENDER_DRAGON_HURT;
762
	}
763
764
	@Override
765
	protected float getSoundVolume() {
766
		return 5.0F;
767
	}
768
769
	public Vec3 getHeadLookVector(final float a) {
770
		DragonPhaseInstance phaseInstance = this.phaseManager.getCurrentPhase();
771
		EnderDragonPhase<? extends DragonPhaseInstance> phase = phaseInstance.getPhase();
772
		Vec3 result;
773
		if (phase == EnderDragonPhase.LANDING || phase == EnderDragonPhase.TAKEOFF) {
774
			BlockPos egg = this.level().getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, EnderDragonFight.getPodiumLocation(this.fightOrigin));
775
			float dist = Math.max((float)Math.sqrt(egg.distToCenterSqr(this.position())) / 4.0F, 1.0F);
776
			float yOffset = 6.0F / dist;
777
			float xRotOld = this.getXRot();
778
			float rotScale = 1.5F;
779
			this.setXRot(-yOffset * 1.5F * 5.0F);
780
			result = this.getViewVector(a);
781
			this.setXRot(xRotOld);
782
		} else if (phaseInstance.isSitting()) {
783
			float xRotOld = this.getXRot();
784
			float rotScale = 1.5F;
785
			this.setXRot(-45.0F);
786
			result = this.getViewVector(a);
787
			this.setXRot(xRotOld);
788
		} else {
789
			result = this.getViewVector(a);
790
		}
791
792
		return result;
793
	}
794
795
	public void onCrystalDestroyed(final ServerLevel level, final EndCrystal crystal, final BlockPos pos, final DamageSource source) {
796
		Player player;
797
		if (source.getEntity() instanceof Player playerSource) {
798
			player = playerSource;
799
		} else {
800
			player = level.getNearestPlayer(CRYSTAL_DESTROY_TARGETING, pos.getX(), pos.getY(), pos.getZ());
801
		}
802
803
		if (crystal == this.nearestCrystal) {
804
			this.hurt(level, this.head, this.damageSources().explosion(crystal, player), 10.0F);
805
		}
806
807
		this.phaseManager.getCurrentPhase().onCrystalDestroyed(crystal, pos, source, player);
808
	}
809
810
	@Override
811
	public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
812
		if (DATA_PHASE.equals(accessor) && this.level().isClientSide()) {
813
			this.phaseManager.setPhase(EnderDragonPhase.getById(this.getEntityData().get(DATA_PHASE)));
814
		}
815
816
		super.onSyncedDataUpdated(accessor);
817
	}
818
819
	public EnderDragonPhaseManager getPhaseManager() {
820
		return this.phaseManager;
821
	}
822
823
	public @Nullable EnderDragonFight getDragonFight() {
824
		return this.dragonFight;
825
	}
826
827
	@Override
828
	public boolean addEffect(final MobEffectInstance newEffect, final @Nullable Entity source) {
829
		return false;
830
	}
831
832
	@Override
833
	protected boolean canRide(final Entity vehicle) {
834
		return false;
835
	}
836
837
	@Override
838
	public boolean canUsePortal(final boolean ignorePassenger) {
839
		return false;
840
	}
841
842
	@Override
843
	public void recreateFromPacket(final ClientboundAddEntityPacket packet) {
844
		super.recreateFromPacket(packet);
845
		EnderDragonPart[] subEntities = this.getSubEntities();
846
847
		for (int i = 0; i < subEntities.length; i++) {
848
			subEntities[i].setId(i + packet.getId() + 1);
849
		}
850
	}
851
852
	@Override
853
	public boolean canAttack(final LivingEntity target) {
854
		return target.canBeSeenAsEnemy();
855
	}
856
857
	@Override
858
	protected float sanitizeScale(final float scale) {
859
		return 1.0F;
860
	}
861
862
	@Override
863
	public MoveSimulationType getMoveSimulationType() {
864
		return this.isDeadOrDying() ? MoveSimulationType.SERVER_AND_CLIENT : super.getMoveSimulationType();
865
	}
866
}
867

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