DragonHoldingPatternPhase .java

net.minecraft.world.entity.boss.enderdragon.phases.DragonHoldingPatternPhase (class): members: NEW_TARGET_TARGETING, clockwise, currentPath, doServerTick.

net.minecraft.world.entity.boss.enderdragon.phasesclass136 linescommonextends AbstractDragonPhaseInstance4.6 KB

API Surface (4)

EnderDragonPhase<DragonHoldingPatternPhase> getPhase()
void doServerTick(final ServerLevel level)
void begin()
void onCrystalDestroyed(final EndCrystal crystal, final BlockPos pos, final DamageSource source, final @Nullable Player player)

Fields & Constants (4)

NEW_TARGET_TARGETING
currentPath
targetLocation
clockwise

Full Source

DragonHoldingPatternPhase.java136 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.boss.enderdragon.phases;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Vec3i;
5
import net.minecraft.server.level.ServerLevel;
6
import net.minecraft.world.damagesource.DamageSource;
7
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
8
import net.minecraft.world.entity.boss.enderdragon.EndCrystal;
9
import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
10
import net.minecraft.world.entity.player.Player;
11
import net.minecraft.world.level.dimension.end.EnderDragonFight;
12
import net.minecraft.world.level.levelgen.Heightmap;
13
import net.minecraft.world.level.pathfinder.Path;
14
import net.minecraft.world.phys.Vec3;
15
import org.jspecify.annotations.Nullable;
16
17
public class DragonHoldingPatternPhase extends AbstractDragonPhaseInstance {
18
	private static final TargetingConditions NEW_TARGET_TARGETING = TargetingConditions.forCombat().ignoreLineOfSight();
19
	private @Nullable Path currentPath;
20
	private @Nullable Vec3 targetLocation;
21
	private boolean clockwise;
22
23
	public DragonHoldingPatternPhase(final EnderDragon dragon) {
24
		super(dragon);
25
	}
26
27
	@Override
28
	public EnderDragonPhase<DragonHoldingPatternPhase> getPhase() {
29
		return EnderDragonPhase.HOLDING_PATTERN;
30
	}
31
32
	@Override
33
	public void doServerTick(final ServerLevel level) {
34
		double distToTarget = this.targetLocation == null ? 0.0 : this.targetLocation.distanceToSqr(this.dragon.getX(), this.dragon.getY(), this.dragon.getZ());
35
		if (distToTarget < 100.0 || distToTarget > 22500.0 || this.dragon.horizontalCollision || this.dragon.verticalCollision) {
36
			this.findNewTarget(level);
37
		}
38
	}
39
40
	@Override
41
	public void begin() {
42
		this.currentPath = null;
43
		this.targetLocation = null;
44
	}
45
46
	@Override
47
	public @Nullable Vec3 getFlyTargetLocation() {
48
		return this.targetLocation;
49
	}
50
51
	private void findNewTarget(final ServerLevel level) {
52
		if (this.currentPath != null && this.currentPath.isDone()) {
53
			BlockPos egg = level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, EnderDragonFight.getPodiumLocation(this.dragon.getFightOrigin()));
54
			int crystals = this.dragon.getDragonFight() == null ? 0 : this.dragon.getDragonFight().aliveCrystals();
55
			if (this.dragon.getRandom().nextInt(crystals + 3) == 0) {
56
				this.dragon.getPhaseManager().setPhase(EnderDragonPhase.LANDING_APPROACH);
57
				return;
58
			}
59
60
			Player playerNearestToEgg = level.getNearestPlayer(NEW_TARGET_TARGETING, this.dragon, egg.getX(), egg.getY(), egg.getZ());
61
			double distSqr;
62
			if (playerNearestToEgg != null) {
63
				distSqr = egg.distToCenterSqr(playerNearestToEgg.position()) / 512.0;
64
			} else {
65
				distSqr = 64.0;
66
			}
67
68
			if (playerNearestToEgg != null && (this.dragon.getRandom().nextInt((int)(distSqr + 2.0)) == 0 || this.dragon.getRandom().nextInt(crystals + 2) == 0)) {
69
				this.strafePlayer(playerNearestToEgg);
70
				return;
71
			}
72
		}
73
74
		if (this.currentPath == null || this.currentPath.isDone()) {
75
			int currentNodeIndex = this.dragon.findClosestNode();
76
			int targetNodeIndex = currentNodeIndex;
77
			if (this.dragon.getRandom().nextInt(8) == 0) {
78
				this.clockwise = !this.clockwise;
79
				targetNodeIndex += 6;
80
			}
81
82
			if (this.clockwise) {
83
				targetNodeIndex++;
84
			} else {
85
				targetNodeIndex--;
86
			}
87
88
			if (this.dragon.getDragonFight() != null && this.dragon.getDragonFight().aliveCrystals() >= 0) {
89
				targetNodeIndex %= 12;
90
				if (targetNodeIndex < 0) {
91
					targetNodeIndex += 12;
92
				}
93
			} else {
94
				targetNodeIndex -= 12;
95
				targetNodeIndex &= 7;
96
				targetNodeIndex += 12;
97
			}
98
99
			this.currentPath = this.dragon.findPath(currentNodeIndex, targetNodeIndex, null);
100
			if (this.currentPath != null) {
101
				this.currentPath.advance();
102
			}
103
		}
104
105
		this.navigateToNextPathNode();
106
	}
107
108
	private void strafePlayer(final Player playerNearestToEgg) {
109
		this.dragon.getPhaseManager().setPhase(EnderDragonPhase.STRAFE_PLAYER);
110
		this.dragon.getPhaseManager().getPhase(EnderDragonPhase.STRAFE_PLAYER).setTarget(playerNearestToEgg);
111
	}
112
113
	private void navigateToNextPathNode() {
114
		if (this.currentPath != null && !this.currentPath.isDone()) {
115
			Vec3i current = this.currentPath.getNextNodePos();
116
			this.currentPath.advance();
117
			double xTarget = current.getX();
118
			double zTarget = current.getZ();
119
120
			double yTarget;
121
			do {
122
				yTarget = current.getY() + this.dragon.getRandom().nextFloat() * 20.0F;
123
			} while (yTarget < current.getY());
124
125
			this.targetLocation = new Vec3(xTarget, yTarget, zTarget);
126
		}
127
	}
128
129
	@Override
130
	public void onCrystalDestroyed(final EndCrystal crystal, final BlockPos pos, final DamageSource source, final @Nullable Player player) {
131
		if (player != null && this.dragon.canAttack(player)) {
132
			this.strafePlayer(player);
133
		}
134
	}
135
}
136

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