| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.world.entity.PathfinderMob;
|
| 5 | import net.minecraft.world.entity.ai.util.DefaultRandomPos;
|
| 6 | import net.minecraft.world.phys.Vec3;
|
| 7 | import org.jspecify.annotations.Nullable;
|
| 8 |
|
| 9 | public class RandomStrollGoal extends Goal {
|
| 10 | public static final int DEFAULT_INTERVAL = 120;
|
| 11 | protected final PathfinderMob mob;
|
| 12 | protected double wantedX;
|
| 13 | protected double wantedY;
|
| 14 | protected double wantedZ;
|
| 15 | protected final double speedModifier;
|
| 16 | protected int interval;
|
| 17 | protected boolean forceTrigger;
|
| 18 | private final boolean checkNoActionTime;
|
| 19 |
|
| 20 | public RandomStrollGoal(final PathfinderMob mob, final double speedModifier) {
|
| 21 | this(mob, speedModifier, 120);
|
| 22 | }
|
| 23 |
|
| 24 | public RandomStrollGoal(final PathfinderMob mob, final double speedModifier, final int interval) {
|
| 25 | this(mob, speedModifier, interval, true);
|
| 26 | }
|
| 27 |
|
| 28 | public RandomStrollGoal(final PathfinderMob mob, final double speedModifier, final int interval, final boolean checkNoActionTime) {
|
| 29 | this.mob = mob;
|
| 30 | this.speedModifier = speedModifier;
|
| 31 | this.interval = interval;
|
| 32 | this.checkNoActionTime = checkNoActionTime;
|
| 33 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public boolean canUse() {
|
| 38 | if (this.mob.hasControllingPassenger()) {
|
| 39 | return false;
|
| 40 | }
|
| 41 |
|
| 42 | if (!this.forceTrigger) {
|
| 43 | if (this.checkNoActionTime && this.mob.getNoActionTime() >= 100) {
|
| 44 | return false;
|
| 45 | }
|
| 46 |
|
| 47 | if (this.mob.getRandom().nextInt(reducedTickDelay(this.interval)) != 0) {
|
| 48 | return false;
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | Vec3 pos = this.getPosition();
|
| 53 | if (pos == null) {
|
| 54 | return false;
|
| 55 | }
|
| 56 |
|
| 57 | this.wantedX = pos.x;
|
| 58 | this.wantedY = pos.y;
|
| 59 | this.wantedZ = pos.z;
|
| 60 | this.forceTrigger = false;
|
| 61 | return true;
|
| 62 | }
|
| 63 |
|
| 64 | protected @Nullable Vec3 getPosition() {
|
| 65 | return DefaultRandomPos.getPos(this.mob, 10, 7);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public boolean canContinueToUse() {
|
| 70 | return !this.mob.getNavigation().isDone() && !this.mob.hasControllingPassenger();
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public void start() {
|
| 75 | this.mob.getNavigation().moveTo(this.wantedX, this.wantedY, this.wantedZ, this.speedModifier);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void stop() {
|
| 80 | this.mob.getNavigation().stop();
|
| 81 | super.stop();
|
| 82 | }
|
| 83 |
|
| 84 | public void trigger() {
|
| 85 | this.forceTrigger = true;
|
| 86 | }
|
| 87 |
|
| 88 | public void setInterval(final int interval) {
|
| 89 | this.interval = interval;
|
| 90 | }
|
| 91 | }
|
| 92 |
|