| 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.entity.ai.targeting.TargetingConditions;
|
| 7 | import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
|
| 8 | import net.minecraft.world.entity.player.Player;
|
| 9 | import net.minecraft.world.level.dimension.end.EnderDragonFight;
|
| 10 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 11 | import net.minecraft.world.level.pathfinder.Node;
|
| 12 | import net.minecraft.world.level.pathfinder.Path;
|
| 13 | import net.minecraft.world.phys.Vec3;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public class DragonLandingApproachPhase extends AbstractDragonPhaseInstance {
|
| 17 | private static final TargetingConditions NEAR_EGG_TARGETING = TargetingConditions.forCombat().ignoreLineOfSight();
|
| 18 | private @Nullable Path currentPath;
|
| 19 | private @Nullable Vec3 targetLocation;
|
| 20 |
|
| 21 | public DragonLandingApproachPhase(final EnderDragon dragon) {
|
| 22 | super(dragon);
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public EnderDragonPhase<DragonLandingApproachPhase> getPhase() {
|
| 27 | return EnderDragonPhase.LANDING_APPROACH;
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public void begin() {
|
| 32 | this.currentPath = null;
|
| 33 | this.targetLocation = null;
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public void doServerTick(final ServerLevel level) {
|
| 38 | double distToTarget = this.targetLocation == null ? 0.0 : this.targetLocation.distanceToSqr(this.dragon.getX(), this.dragon.getY(), this.dragon.getZ());
|
| 39 | if (distToTarget < 100.0 || distToTarget > 22500.0 || this.dragon.horizontalCollision || this.dragon.verticalCollision) {
|
| 40 | this.findNewTarget(level);
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public @Nullable Vec3 getFlyTargetLocation() {
|
| 46 | return this.targetLocation;
|
| 47 | }
|
| 48 |
|
| 49 | private void findNewTarget(final ServerLevel level) {
|
| 50 | if (this.currentPath == null || this.currentPath.isDone()) {
|
| 51 | int currentNodeIndex = this.dragon.findClosestNode();
|
| 52 | BlockPos egg = level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, EnderDragonFight.getPodiumLocation(this.dragon.getFightOrigin()));
|
| 53 | Player playerNearestToEgg = level.getNearestPlayer(NEAR_EGG_TARGETING, this.dragon, egg.getX(), egg.getY(), egg.getZ());
|
| 54 | int targetNodeIndex;
|
| 55 | if (playerNearestToEgg != null) {
|
| 56 | Vec3 aim = new Vec3(playerNearestToEgg.getX(), 0.0, playerNearestToEgg.getZ()).normalize();
|
| 57 | targetNodeIndex = this.dragon.findClosestNode(-aim.x * 40.0, 105.0, -aim.z * 40.0);
|
| 58 | } else {
|
| 59 | targetNodeIndex = this.dragon.findClosestNode(40.0, egg.getY(), 0.0);
|
| 60 | }
|
| 61 |
|
| 62 | Node finalNode = new Node(egg.getX(), egg.getY(), egg.getZ());
|
| 63 | this.currentPath = this.dragon.findPath(currentNodeIndex, targetNodeIndex, finalNode);
|
| 64 | if (this.currentPath != null) {
|
| 65 | this.currentPath.advance();
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | this.navigateToNextPathNode();
|
| 70 | if (this.currentPath != null && this.currentPath.isDone()) {
|
| 71 | this.dragon.getPhaseManager().setPhase(EnderDragonPhase.LANDING);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | private void navigateToNextPathNode() {
|
| 76 | if (this.currentPath != null && !this.currentPath.isDone()) {
|
| 77 | Vec3i current = this.currentPath.getNextNodePos();
|
| 78 | this.currentPath.advance();
|
| 79 | double xTarget = current.getX();
|
| 80 | double zTarget = current.getZ();
|
| 81 |
|
| 82 | double yTarget;
|
| 83 | do {
|
| 84 | yTarget = current.getY() + this.dragon.getRandom().nextFloat() * 20.0F;
|
| 85 | } while (yTarget < current.getY());
|
| 86 |
|
| 87 | this.targetLocation = new Vec3(xTarget, yTarget, zTarget);
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 |
|