| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.SectionPos;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.entity.PathfinderMob;
|
| 9 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 10 | import net.minecraft.world.entity.ai.util.LandRandomPos;
|
| 11 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class StrollThroughVillageGoal extends Goal {
|
| 16 | private static final int DISTANCE_THRESHOLD = 10;
|
| 17 | private final PathfinderMob mob;
|
| 18 | private final int interval;
|
| 19 | private @Nullable BlockPos wantedPos;
|
| 20 |
|
| 21 | public StrollThroughVillageGoal(final PathfinderMob mob, final int interval) {
|
| 22 | this.mob = mob;
|
| 23 | this.interval = reducedTickDelay(interval);
|
| 24 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public boolean canUse() {
|
| 29 | if (this.mob.hasControllingPassenger()) {
|
| 30 | return false;
|
| 31 | }
|
| 32 |
|
| 33 | if (this.mob.level().isBrightOutside()) {
|
| 34 | return false;
|
| 35 | }
|
| 36 |
|
| 37 | if (this.mob.getRandom().nextInt(this.interval) != 0) {
|
| 38 | return false;
|
| 39 | }
|
| 40 |
|
| 41 | ServerLevel level = (ServerLevel)this.mob.level();
|
| 42 | BlockPos pos = this.mob.blockPosition();
|
| 43 | if (!level.isCloseToVillage(pos, 6)) {
|
| 44 | return false;
|
| 45 | }
|
| 46 |
|
| 47 | Vec3 landPos = LandRandomPos.getPos(this.mob, 15, 7, p -> -level.sectionsToVillage(SectionPos.of(p)));
|
| 48 | this.wantedPos = landPos == null ? null : BlockPos.containing(landPos);
|
| 49 | return this.wantedPos != null;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public boolean canContinueToUse() {
|
| 54 | return this.wantedPos != null && !this.mob.getNavigation().isDone() && this.mob.getNavigation().getTargetPos().equals(this.wantedPos);
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public void tick() {
|
| 59 | if (this.wantedPos != null) {
|
| 60 | PathNavigation navigation = this.mob.getNavigation();
|
| 61 | if (navigation.isDone() && !this.wantedPos.closerToCenterThan(this.mob.position(), 10.0)) {
|
| 62 | Vec3 longDistanceTarget = Vec3.atBottomCenterOf(this.wantedPos);
|
| 63 | Vec3 selfVector = this.mob.position();
|
| 64 | Vec3 distance = selfVector.subtract(longDistanceTarget);
|
| 65 | longDistanceTarget = distance.scale(0.4).add(longDistanceTarget);
|
| 66 | Vec3 moveTarget = longDistanceTarget.subtract(selfVector).normalize().scale(10.0).add(selfVector);
|
| 67 | BlockPos pathTarget = BlockPos.containing(moveTarget);
|
| 68 | pathTarget = this.mob.level().getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, pathTarget);
|
| 69 | if (!navigation.moveTo(pathTarget.getX(), pathTarget.getY(), pathTarget.getZ(), 1.0)) {
|
| 70 | this.moveRandomly();
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | private void moveRandomly() {
|
| 77 | RandomSource random = this.mob.getRandom();
|
| 78 | BlockPos pathTarget = this.mob
|
| 79 | .level()
|
| 80 | .getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, this.mob.blockPosition().offset(-8 + random.nextInt(16), 0, -8 + random.nextInt(16)));
|
| 81 | this.mob.getNavigation().moveTo(pathTarget.getX(), pathTarget.getY(), pathTarget.getZ(), 1.0);
|
| 82 | }
|
| 83 | }
|
| 84 |
|