| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.SectionPos;
|
| 5 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 6 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 7 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 8 | import net.minecraft.world.entity.ai.util.LandRandomPos;
|
| 9 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 10 | import net.minecraft.world.entity.npc.villager.Villager;
|
| 11 | import net.minecraft.world.phys.Vec3;
|
| 12 |
|
| 13 | public class GoToClosestVillage {
|
| 14 | public static BehaviorControl<Villager> create(final float speedModifier, final int closeEnoughDistance) {
|
| 15 | return BehaviorBuilder.create(i -> i.group(i.absent(MemoryModuleType.WALK_TARGET)).apply(i, walkTarget -> (level, body, timestamp) -> {
|
| 16 | if (level.isVillage(body.blockPosition())) {
|
| 17 | return false;
|
| 18 | }
|
| 19 |
|
| 20 | PoiManager poiManager = level.getPoiManager();
|
| 21 | int sectionsToVillage = poiManager.sectionsToVillage(SectionPos.of(body.blockPosition()));
|
| 22 | Vec3 targetPos = null;
|
| 23 |
|
| 24 | for (int j = 0; j < 5; j++) {
|
| 25 | Vec3 landPos = LandRandomPos.getPos(body, 15, 7, p -> -poiManager.sectionsToVillage(SectionPos.of(p)));
|
| 26 | if (landPos != null) {
|
| 27 | int landPosSectionsToVillage = poiManager.sectionsToVillage(SectionPos.of(BlockPos.containing(landPos)));
|
| 28 | if (landPosSectionsToVillage < sectionsToVillage) {
|
| 29 | targetPos = landPos;
|
| 30 | break;
|
| 31 | }
|
| 32 |
|
| 33 | if (landPosSectionsToVillage == sectionsToVillage) {
|
| 34 | targetPos = landPos;
|
| 35 | }
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | if (targetPos != null) {
|
| 40 | walkTarget.set(new WalkTarget(targetPos, speedModifier, closeEnoughDistance));
|
| 41 | }
|
| 42 |
|
| 43 | return true;
|
| 44 | }));
|
| 45 | }
|
| 46 | }
|
| 47 |
|