| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.stream.Collectors;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.SectionPos;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.entity.EntityTypes;
|
| 10 | import net.minecraft.world.entity.PathfinderMob;
|
| 11 | import net.minecraft.world.entity.ai.util.LandRandomPos;
|
| 12 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 13 | import net.minecraft.world.entity.ai.village.poi.PoiRecord;
|
| 14 | import net.minecraft.world.entity.npc.villager.Villager;
|
| 15 | import net.minecraft.world.phys.Vec3;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public class GolemRandomStrollInVillageGoal extends RandomStrollGoal {
|
| 19 | private static final int POI_SECTION_SCAN_RADIUS = 2;
|
| 20 | private static final int VILLAGER_SCAN_RADIUS = 32;
|
| 21 | private static final int RANDOM_POS_XY_DISTANCE = 10;
|
| 22 | private static final int RANDOM_POS_Y_DISTANCE = 7;
|
| 23 |
|
| 24 | public GolemRandomStrollInVillageGoal(final PathfinderMob mob, final double speedModifier) {
|
| 25 | super(mob, speedModifier, 240, false);
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | protected @Nullable Vec3 getPosition() {
|
| 30 | RandomSource random = this.mob.level().getRandom();
|
| 31 | if (random.nextFloat() < 0.3F) {
|
| 32 | return this.getPositionTowardsAnywhere();
|
| 33 | }
|
| 34 |
|
| 35 | Vec3 target;
|
| 36 | if (random.nextFloat() < 0.7F) {
|
| 37 | target = this.getPositionTowardsVillagerWhoWantsGolem();
|
| 38 | if (target == null) {
|
| 39 | target = this.getPositionTowardsPoi();
|
| 40 | }
|
| 41 | } else {
|
| 42 | target = this.getPositionTowardsPoi();
|
| 43 | if (target == null) {
|
| 44 | target = this.getPositionTowardsVillagerWhoWantsGolem();
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | return target == null ? this.getPositionTowardsAnywhere() : target;
|
| 49 | }
|
| 50 |
|
| 51 | private @Nullable Vec3 getPositionTowardsAnywhere() {
|
| 52 | return LandRandomPos.getPos(this.mob, 10, 7);
|
| 53 | }
|
| 54 |
|
| 55 | private @Nullable Vec3 getPositionTowardsVillagerWhoWantsGolem() {
|
| 56 | ServerLevel level = (ServerLevel)this.mob.level();
|
| 57 | List<Villager> villagers = level.getEntities(EntityTypes.VILLAGER, this.mob.getBoundingBox().inflate(32.0), this::doesVillagerWantGolem);
|
| 58 | if (villagers.isEmpty()) {
|
| 59 | return null;
|
| 60 | }
|
| 61 |
|
| 62 | Villager villager = villagers.get(this.mob.level().getRandom().nextInt(villagers.size()));
|
| 63 | Vec3 targetPos = villager.position();
|
| 64 | return LandRandomPos.getPosTowards(this.mob, 10, 7, targetPos);
|
| 65 | }
|
| 66 |
|
| 67 | private @Nullable Vec3 getPositionTowardsPoi() {
|
| 68 | SectionPos targetSection = this.getRandomVillageSection();
|
| 69 | if (targetSection == null) {
|
| 70 | return null;
|
| 71 | }
|
| 72 |
|
| 73 | BlockPos targetPos = this.getRandomPoiWithinSection(targetSection);
|
| 74 | return targetPos == null ? null : LandRandomPos.getPosTowards(this.mob, 10, 7, Vec3.atBottomCenterOf(targetPos));
|
| 75 | }
|
| 76 |
|
| 77 | private @Nullable SectionPos getRandomVillageSection() {
|
| 78 | ServerLevel level = (ServerLevel)this.mob.level();
|
| 79 | List<SectionPos> villageSections = SectionPos.cube(SectionPos.of(this.mob), 2)
|
| 80 | .filter(sectionPos -> level.sectionsToVillage(sectionPos) == 0)
|
| 81 | .collect(Collectors.toList());
|
| 82 | return villageSections.isEmpty() ? null : villageSections.get(level.getRandom().nextInt(villageSections.size()));
|
| 83 | }
|
| 84 |
|
| 85 | private @Nullable BlockPos getRandomPoiWithinSection(final SectionPos sectionPos) {
|
| 86 | ServerLevel level = (ServerLevel)this.mob.level();
|
| 87 | PoiManager poiManager = level.getPoiManager();
|
| 88 | List<BlockPos> pois = poiManager.getInRange(poiType -> true, sectionPos.center(), 8, PoiManager.Occupancy.IS_OCCUPIED)
|
| 89 | .map(PoiRecord::getPos)
|
| 90 | .collect(Collectors.toList());
|
| 91 | return pois.isEmpty() ? null : pois.get(level.getRandom().nextInt(pois.size()));
|
| 92 | }
|
| 93 |
|
| 94 | private boolean doesVillagerWantGolem(final Villager villager) {
|
| 95 | return villager.wantsToSpawnGolem(this.mob.level().getGameTime());
|
| 96 | }
|
| 97 | }
|
| 98 |
|