| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.SectionPos;
|
| 6 | import net.minecraft.world.entity.PathfinderMob;
|
| 7 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 8 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 9 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 10 | import net.minecraft.world.entity.ai.util.DefaultRandomPos;
|
| 11 | import net.minecraft.world.entity.ai.util.LandRandomPos;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 |
|
| 14 | public class VillageBoundRandomStroll {
|
| 15 | private static final int MAX_XZ_DIST = 10;
|
| 16 | private static final int MAX_Y_DIST = 7;
|
| 17 |
|
| 18 | public static OneShot<PathfinderMob> create(final float speedModifier) {
|
| 19 | return create(speedModifier, 10, 7);
|
| 20 | }
|
| 21 |
|
| 22 | public static OneShot<PathfinderMob> create(final float speedModifier, final int maxXyDist, final int maxYDist) {
|
| 23 | return BehaviorBuilder.create(i -> i.group(i.absent(MemoryModuleType.WALK_TARGET)).apply(i, walkTarget -> (level, body, timestamp) -> {
|
| 24 | BlockPos bodyPos = body.blockPosition();
|
| 25 | Vec3 landPos;
|
| 26 | if (level.isVillage(bodyPos)) {
|
| 27 | landPos = LandRandomPos.getPos(body, maxXyDist, maxYDist);
|
| 28 | } else {
|
| 29 | SectionPos sectionPos = SectionPos.of(bodyPos);
|
| 30 | SectionPos optimalSectionPos = BehaviorUtils.findSectionClosestToVillage(level, sectionPos, 2);
|
| 31 | if (optimalSectionPos != sectionPos) {
|
| 32 | landPos = DefaultRandomPos.getPosTowards(body, maxXyDist, maxYDist, Vec3.atBottomCenterOf(optimalSectionPos.center()), (float) (Math.PI / 2));
|
| 33 | } else {
|
| 34 | landPos = LandRandomPos.getPos(body, maxXyDist, maxYDist);
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | walkTarget.setOrErase(Optional.ofNullable(landPos).map(pos -> new WalkTarget(pos, speedModifier, 0)));
|
| 39 | return true;
|
| 40 | }));
|
| 41 | }
|
| 42 | }
|
| 43 |
|