| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.SectionPos;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.world.entity.PathfinderMob;
|
| 7 | import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
|
| 8 | import net.minecraft.world.entity.ai.util.DefaultRandomPos;
|
| 9 | import net.minecraft.world.phys.Vec3;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public class MoveBackToVillageGoal extends RandomStrollGoal {
|
| 13 | private static final int MAX_XZ_DIST = 10;
|
| 14 | private static final int MAX_Y_DIST = 7;
|
| 15 |
|
| 16 | public MoveBackToVillageGoal(final PathfinderMob mob, final double speedModifier, final boolean checkNoActionTime) {
|
| 17 | super(mob, speedModifier, 10, checkNoActionTime);
|
| 18 | }
|
| 19 |
|
| 20 | @Override
|
| 21 | public boolean canUse() {
|
| 22 | ServerLevel level = (ServerLevel)this.mob.level();
|
| 23 | BlockPos pos = this.mob.blockPosition();
|
| 24 | return level.isVillage(pos) ? false : super.canUse();
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | protected @Nullable Vec3 getPosition() {
|
| 29 | ServerLevel level = (ServerLevel)this.mob.level();
|
| 30 | BlockPos pos = this.mob.blockPosition();
|
| 31 | SectionPos sectionPos = SectionPos.of(pos);
|
| 32 | SectionPos optimalSectionPos = BehaviorUtils.findSectionClosestToVillage(level, sectionPos, 2);
|
| 33 | return optimalSectionPos != sectionPos
|
| 34 | ? DefaultRandomPos.getPosTowards(this.mob, 10, 7, Vec3.atBottomCenterOf(optimalSectionPos.center()), (float) (Math.PI / 2))
|
| 35 | : null;
|
| 36 | }
|
| 37 | }
|
| 38 |
|