| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.world.entity.animal.Animal;
|
| 5 | import org.jspecify.annotations.Nullable;
|
| 6 |
|
| 7 | public class FollowParentGoal extends Goal {
|
| 8 | public static final int HORIZONTAL_SCAN_RANGE = 8;
|
| 9 | public static final int VERTICAL_SCAN_RANGE = 4;
|
| 10 | public static final int DONT_FOLLOW_IF_CLOSER_THAN = 3;
|
| 11 | private final Animal animal;
|
| 12 | private @Nullable Animal parent;
|
| 13 | private final double speedModifier;
|
| 14 | private int timeToRecalcPath;
|
| 15 |
|
| 16 | public FollowParentGoal(final Animal animal, final double speedModifier) {
|
| 17 | this.animal = animal;
|
| 18 | this.speedModifier = speedModifier;
|
| 19 | }
|
| 20 |
|
| 21 | @Override
|
| 22 | public boolean canUse() {
|
| 23 | if (this.animal.getAge() >= 0) {
|
| 24 | return false;
|
| 25 | }
|
| 26 |
|
| 27 | List<? extends Animal> parents = this.animal
|
| 28 | .level()
|
| 29 | .getEntitiesOfClass((Class<? extends Animal>)this.animal.getClass(), this.animal.getBoundingBox().inflate(8.0, 4.0, 8.0));
|
| 30 | Animal closest = null;
|
| 31 | double closestDistSqr = Double.MAX_VALUE;
|
| 32 |
|
| 33 | for (Animal parent : parents) {
|
| 34 | if (parent.getAge() >= 0) {
|
| 35 | double distSqr = this.animal.distanceToSqr(parent);
|
| 36 | if (!(distSqr > closestDistSqr)) {
|
| 37 | closestDistSqr = distSqr;
|
| 38 | closest = parent;
|
| 39 | }
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | if (closest == null) {
|
| 44 | return false;
|
| 45 | }
|
| 46 |
|
| 47 | if (closestDistSqr < 9.0) {
|
| 48 | return false;
|
| 49 | }
|
| 50 |
|
| 51 | this.parent = closest;
|
| 52 | return true;
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public boolean canContinueToUse() {
|
| 57 | if (this.animal.getAge() >= 0) {
|
| 58 | return false;
|
| 59 | }
|
| 60 |
|
| 61 | if (!this.parent.isAlive()) {
|
| 62 | return false;
|
| 63 | }
|
| 64 |
|
| 65 | double distSqr = this.animal.distanceToSqr(this.parent);
|
| 66 | return !(distSqr < 9.0) && !(distSqr > 256.0);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void start() {
|
| 71 | this.timeToRecalcPath = 0;
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public void stop() {
|
| 76 | this.parent = null;
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public void tick() {
|
| 81 | if (--this.timeToRecalcPath <= 0) {
|
| 82 | this.timeToRecalcPath = this.adjustedTickDelay(10);
|
| 83 | this.animal.getNavigation().moveTo(this.parent, this.speedModifier);
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|