| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.world.entity.Entity;
|
| 6 | import net.minecraft.world.entity.MoverType;
|
| 7 | import net.minecraft.world.entity.PathfinderMob;
|
| 8 | import net.minecraft.world.entity.player.Player;
|
| 9 | import net.minecraft.world.phys.Vec3;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public class FollowPlayerRiddenEntityGoal extends Goal {
|
| 13 | private int timeToRecalcPath;
|
| 14 | private final PathfinderMob mob;
|
| 15 | private final Class<? extends Entity> entityTypeToFollow;
|
| 16 | private @Nullable Player following;
|
| 17 | private FollowPlayerRiddenEntityGoal.FollowEntityGoal currentGoal;
|
| 18 |
|
| 19 | public FollowPlayerRiddenEntityGoal(final PathfinderMob mob, final Class<? extends Entity> entityTypeToFollow) {
|
| 20 | this.mob = mob;
|
| 21 | this.entityTypeToFollow = entityTypeToFollow;
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public boolean canUse() {
|
| 26 | if (this.following != null && this.following.hasMovedHorizontallyRecently()) {
|
| 27 | return true;
|
| 28 | }
|
| 29 |
|
| 30 | for (Entity entity : this.mob.level().getEntitiesOfClass(this.entityTypeToFollow, this.mob.getBoundingBox().inflate(5.0))) {
|
| 31 | if (entity.getControllingPassenger() instanceof Player controllingPlayer && controllingPlayer.hasMovedHorizontallyRecently()) {
|
| 32 | return true;
|
| 33 | }
|
| 34 | }
|
| 35 |
|
| 36 | return false;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public boolean isInterruptable() {
|
| 41 | return true;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public boolean canContinueToUse() {
|
| 46 | return this.following != null && this.following.isPassenger() && this.following.hasMovedHorizontallyRecently();
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public void start() {
|
| 51 | for (Entity entity : this.mob.level().getEntitiesOfClass(this.entityTypeToFollow, this.mob.getBoundingBox().inflate(5.0))) {
|
| 52 | if (entity.getControllingPassenger() instanceof Player player) {
|
| 53 | this.following = player;
|
| 54 | break;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | this.timeToRecalcPath = 0;
|
| 59 | this.currentGoal = FollowPlayerRiddenEntityGoal.FollowEntityGoal.GO_TO_ENTITY;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public void stop() {
|
| 64 | this.following = null;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public void tick() {
|
| 69 | float speed = this.currentGoal == FollowPlayerRiddenEntityGoal.FollowEntityGoal.GO_IN_ENTITY_DIRECTION ? 0.01F : 0.015F;
|
| 70 | this.mob.moveRelative(speed, new Vec3(this.mob.xxa, this.mob.yya, this.mob.zza));
|
| 71 | this.mob.move(MoverType.SELF, this.mob.getDeltaMovement());
|
| 72 | if (--this.timeToRecalcPath <= 0) {
|
| 73 | this.timeToRecalcPath = this.adjustedTickDelay(10);
|
| 74 | if (this.currentGoal == FollowPlayerRiddenEntityGoal.FollowEntityGoal.GO_TO_ENTITY) {
|
| 75 | BlockPos behindEntityPos = this.following.blockPosition().relative(this.following.getDirection().getOpposite());
|
| 76 | behindEntityPos = behindEntityPos.offset(0, -1, 0);
|
| 77 | this.mob.getNavigation().moveTo(behindEntityPos.getX(), behindEntityPos.getY(), behindEntityPos.getZ(), 1.0);
|
| 78 | if (this.mob.distanceTo(this.following) < 4.0F) {
|
| 79 | this.timeToRecalcPath = 0;
|
| 80 | this.currentGoal = FollowPlayerRiddenEntityGoal.FollowEntityGoal.GO_IN_ENTITY_DIRECTION;
|
| 81 | }
|
| 82 | } else if (this.currentGoal == FollowPlayerRiddenEntityGoal.FollowEntityGoal.GO_IN_ENTITY_DIRECTION) {
|
| 83 | Direction direction = this.following.getMotionDirection();
|
| 84 | BlockPos goTo = this.following.blockPosition().relative(direction, 10);
|
| 85 | this.mob.getNavigation().moveTo(goTo.getX(), goTo.getY() - 1, goTo.getZ(), 1.0);
|
| 86 | if (this.mob.distanceTo(this.following) > 12.0F) {
|
| 87 | this.timeToRecalcPath = 0;
|
| 88 | this.currentGoal = FollowPlayerRiddenEntityGoal.FollowEntityGoal.GO_TO_ENTITY;
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | private enum FollowEntityGoal {
|
| 95 | GO_TO_ENTITY,
|
| 96 | GO_IN_ENTITY_DIRECTION;
|
| 97 | }
|
| 98 | }
|
| 99 |
|