| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.world.entity.LivingEntity;
|
| 5 | import net.minecraft.world.entity.TamableAnimal;
|
| 6 | import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
|
| 7 | import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
| 8 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 9 | import net.minecraft.world.level.pathfinder.PathType;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public class FollowOwnerGoal extends Goal {
|
| 13 | private final TamableAnimal tamable;
|
| 14 | private @Nullable LivingEntity owner;
|
| 15 | private final double speedModifier;
|
| 16 | private final PathNavigation navigation;
|
| 17 | private int timeToRecalcPath;
|
| 18 | private final float stopDistance;
|
| 19 | private final float startDistance;
|
| 20 | private float oldWaterCost;
|
| 21 |
|
| 22 | public FollowOwnerGoal(final TamableAnimal tamable, final double speedModifier, final float startDistance, final float stopDistance) {
|
| 23 | this.tamable = tamable;
|
| 24 | this.speedModifier = speedModifier;
|
| 25 | this.navigation = tamable.getNavigation();
|
| 26 | this.startDistance = startDistance;
|
| 27 | this.stopDistance = stopDistance;
|
| 28 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 29 | if (!(tamable.getNavigation() instanceof GroundPathNavigation) && !(tamable.getNavigation() instanceof FlyingPathNavigation)) {
|
| 30 | throw new IllegalArgumentException("Unsupported mob type for FollowOwnerGoal");
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public boolean canUse() {
|
| 36 | LivingEntity owner = this.tamable.getOwner();
|
| 37 | if (owner == null) {
|
| 38 | return false;
|
| 39 | }
|
| 40 |
|
| 41 | if (this.tamable.unableToMoveToOwner()) {
|
| 42 | return false;
|
| 43 | }
|
| 44 |
|
| 45 | if (this.tamable.distanceToSqr(owner) < this.startDistance * this.startDistance) {
|
| 46 | return false;
|
| 47 | }
|
| 48 |
|
| 49 | this.owner = owner;
|
| 50 | return true;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public boolean canContinueToUse() {
|
| 55 | if (this.navigation.isDone()) {
|
| 56 | return false;
|
| 57 | } else {
|
| 58 | return this.tamable.unableToMoveToOwner() ? false : !(this.tamable.distanceToSqr(this.owner) <= this.stopDistance * this.stopDistance);
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public void start() {
|
| 64 | this.timeToRecalcPath = 0;
|
| 65 | this.oldWaterCost = this.tamable.getPathfindingMalus(PathType.WATER);
|
| 66 | this.tamable.setPathfindingMalus(PathType.WATER, 0.0F);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void stop() {
|
| 71 | this.owner = null;
|
| 72 | this.navigation.stop();
|
| 73 | this.tamable.setPathfindingMalus(PathType.WATER, this.oldWaterCost);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public void tick() {
|
| 78 | boolean isOwnerFarAway = this.tamable.shouldTryTeleportToOwner();
|
| 79 | if (!isOwnerFarAway) {
|
| 80 | this.tamable.getLookControl().setLookAt(this.owner, 10.0F, this.tamable.getMaxHeadXRot());
|
| 81 | }
|
| 82 |
|
| 83 | if (--this.timeToRecalcPath <= 0) {
|
| 84 | this.timeToRecalcPath = this.adjustedTickDelay(10);
|
| 85 | if (isOwnerFarAway) {
|
| 86 | this.tamable.tryToTeleportToOwner();
|
| 87 | } else {
|
| 88 | this.navigation.moveTo(this.owner, this.speedModifier);
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|