| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.Predicate;
|
| 6 | import net.minecraft.world.entity.Mob;
|
| 7 | import net.minecraft.world.entity.ai.control.LookControl;
|
| 8 | import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
|
| 9 | import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
| 10 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 11 | import net.minecraft.world.level.pathfinder.PathType;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public class FollowMobGoal extends Goal {
|
| 15 | private final Mob mob;
|
| 16 | private final Predicate<Mob> followPredicate;
|
| 17 | private @Nullable Mob followingMob;
|
| 18 | private final double speedModifier;
|
| 19 | private final PathNavigation navigation;
|
| 20 | private int timeToRecalcPath;
|
| 21 | private final float stopDistance;
|
| 22 | private float oldWaterCost;
|
| 23 | private final float areaSize;
|
| 24 |
|
| 25 | public FollowMobGoal(final Mob mob, final double speedModifier, final float stopDistance, final float areaSize) {
|
| 26 | this.mob = mob;
|
| 27 | this.followPredicate = input -> mob.getClass() != input.getClass();
|
| 28 | this.speedModifier = speedModifier;
|
| 29 | this.navigation = mob.getNavigation();
|
| 30 | this.stopDistance = stopDistance;
|
| 31 | this.areaSize = areaSize;
|
| 32 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 33 | if (!(mob.getNavigation() instanceof GroundPathNavigation) && !(mob.getNavigation() instanceof FlyingPathNavigation)) {
|
| 34 | throw new IllegalArgumentException("Unsupported mob type for FollowMobGoal");
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public boolean canUse() {
|
| 40 | List<Mob> mobs = this.mob.level().getEntitiesOfClass(Mob.class, this.mob.getBoundingBox().inflate(this.areaSize), this.followPredicate);
|
| 41 | if (!mobs.isEmpty()) {
|
| 42 | for (Mob mobInList : mobs) {
|
| 43 | if (!mobInList.isInvisible()) {
|
| 44 | this.followingMob = mobInList;
|
| 45 | return true;
|
| 46 | }
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | return false;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public boolean canContinueToUse() {
|
| 55 | return this.followingMob != null && !this.navigation.isDone() && this.mob.distanceToSqr(this.followingMob) > this.stopDistance * this.stopDistance;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public void start() {
|
| 60 | this.timeToRecalcPath = 0;
|
| 61 | this.oldWaterCost = this.mob.getPathfindingMalus(PathType.WATER);
|
| 62 | this.mob.setPathfindingMalus(PathType.WATER, 0.0F);
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public void stop() {
|
| 67 | this.followingMob = null;
|
| 68 | this.navigation.stop();
|
| 69 | this.mob.setPathfindingMalus(PathType.WATER, this.oldWaterCost);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public void tick() {
|
| 74 | if (this.followingMob != null && !this.mob.isLeashed()) {
|
| 75 | this.mob.getLookControl().setLookAt(this.followingMob, 10.0F, this.mob.getMaxHeadXRot());
|
| 76 | if (--this.timeToRecalcPath <= 0) {
|
| 77 | this.timeToRecalcPath = this.adjustedTickDelay(10);
|
| 78 | double xxd = this.mob.getX() - this.followingMob.getX();
|
| 79 | double yyd = this.mob.getY() - this.followingMob.getY();
|
| 80 | double zzd = this.mob.getZ() - this.followingMob.getZ();
|
| 81 | double distSqr = xxd * xxd + yyd * yyd + zzd * zzd;
|
| 82 | if (!(distSqr <= this.stopDistance * this.stopDistance)) {
|
| 83 | this.navigation.moveTo(this.followingMob, this.speedModifier);
|
| 84 | } else {
|
| 85 | this.navigation.stop();
|
| 86 | LookControl lookControl = this.followingMob.getLookControl();
|
| 87 | if (distSqr <= this.stopDistance
|
| 88 | || lookControl.getWantedX() == this.mob.getX() && lookControl.getWantedY() == this.mob.getY() && lookControl.getWantedZ() == this.mob.getZ()) {
|
| 89 | double deltaX = this.followingMob.getX() - this.mob.getX();
|
| 90 | double deltaZ = this.followingMob.getZ() - this.mob.getZ();
|
| 91 | this.navigation.moveTo(this.mob.getX() - deltaX, this.mob.getY(), this.mob.getZ() - deltaZ, this.speedModifier);
|
| 92 | }
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|