| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.EnumSet;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Objects;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.function.BooleanSupplier;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.tags.PoiTypeTags;
|
| 12 | import net.minecraft.world.entity.PathfinderMob;
|
| 13 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 14 | import net.minecraft.world.entity.ai.util.DefaultRandomPos;
|
| 15 | import net.minecraft.world.entity.ai.util.GoalUtils;
|
| 16 | import net.minecraft.world.entity.ai.util.LandRandomPos;
|
| 17 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 18 | import net.minecraft.world.level.block.DoorBlock;
|
| 19 | import net.minecraft.world.level.pathfinder.Node;
|
| 20 | import net.minecraft.world.level.pathfinder.Path;
|
| 21 | import net.minecraft.world.phys.Vec3;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | public class MoveThroughVillageGoal extends Goal {
|
| 25 | protected final PathfinderMob mob;
|
| 26 | private final double speedModifier;
|
| 27 | private @Nullable Path path;
|
| 28 | private BlockPos poiPos;
|
| 29 | private final boolean onlyAtNight;
|
| 30 | private final List<BlockPos> visited = Lists.newArrayList();
|
| 31 | private final int distanceToPoi;
|
| 32 | private final BooleanSupplier canDealWithDoors;
|
| 33 |
|
| 34 | public MoveThroughVillageGoal(
|
| 35 | final PathfinderMob mob, final double speedModifier, final boolean onlyAtNight, final int distanceToPoi, final BooleanSupplier canDealWithDoors
|
| 36 | ) {
|
| 37 | this.mob = mob;
|
| 38 | this.speedModifier = speedModifier;
|
| 39 | this.onlyAtNight = onlyAtNight;
|
| 40 | this.distanceToPoi = distanceToPoi;
|
| 41 | this.canDealWithDoors = canDealWithDoors;
|
| 42 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 43 | if (!GoalUtils.hasGroundPathNavigation(mob)) {
|
| 44 | throw new IllegalArgumentException("Unsupported mob for MoveThroughVillageGoal");
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public boolean canUse() {
|
| 50 | if (!GoalUtils.hasGroundPathNavigation(this.mob)) {
|
| 51 | return false;
|
| 52 | }
|
| 53 |
|
| 54 | this.updateVisited();
|
| 55 | if (this.onlyAtNight && this.mob.level().isBrightOutside()) {
|
| 56 | return false;
|
| 57 | }
|
| 58 |
|
| 59 | ServerLevel level = (ServerLevel)this.mob.level();
|
| 60 | BlockPos pos = this.mob.blockPosition();
|
| 61 | if (!level.isCloseToVillage(pos, 6)) {
|
| 62 | return false;
|
| 63 | }
|
| 64 |
|
| 65 | Vec3 landPos = LandRandomPos.getPos(this.mob, 15, 7, p -> {
|
| 66 | if (!level.isVillage(p)) {
|
| 67 | return Double.NEGATIVE_INFINITY;
|
| 68 | }
|
| 69 |
|
| 70 | Optional<BlockPos> newPoiPos = level.getPoiManager().find(e -> e.is(PoiTypeTags.VILLAGE), this::hasNotVisited, p, 10, PoiManager.Occupancy.IS_OCCUPIED);
|
| 71 | return newPoiPos.<Double>map(poiPos -> -poiPos.distSqr(pos)).orElse(Double.NEGATIVE_INFINITY);
|
| 72 | });
|
| 73 | if (landPos == null) {
|
| 74 | return false;
|
| 75 | }
|
| 76 |
|
| 77 | Optional<BlockPos> target = level.getPoiManager()
|
| 78 | .find(e -> e.is(PoiTypeTags.VILLAGE), this::hasNotVisited, BlockPos.containing(landPos), 10, PoiManager.Occupancy.IS_OCCUPIED);
|
| 79 | if (target.isEmpty()) {
|
| 80 | return false;
|
| 81 | }
|
| 82 |
|
| 83 | this.poiPos = target.get().immutable();
|
| 84 | PathNavigation navigation = this.mob.getNavigation();
|
| 85 | navigation.setCanOpenDoors(this.canDealWithDoors.getAsBoolean());
|
| 86 | this.path = navigation.createPath(this.poiPos, 0);
|
| 87 | navigation.setCanOpenDoors(true);
|
| 88 | if (this.path == null) {
|
| 89 | Vec3 partialStep = DefaultRandomPos.getPosTowards(this.mob, 10, 7, Vec3.atBottomCenterOf(this.poiPos), (float) (Math.PI / 2));
|
| 90 | if (partialStep == null) {
|
| 91 | return false;
|
| 92 | }
|
| 93 |
|
| 94 | navigation.setCanOpenDoors(this.canDealWithDoors.getAsBoolean());
|
| 95 | this.path = this.mob.getNavigation().createPath(partialStep.x, partialStep.y, partialStep.z, 0);
|
| 96 | navigation.setCanOpenDoors(true);
|
| 97 | if (this.path == null) {
|
| 98 | return false;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | for (int i = 0; i < this.path.getNodeCount(); i++) {
|
| 103 | Node node = this.path.getNode(i);
|
| 104 | BlockPos doorPos = new BlockPos(node.x, node.y + 1, node.z);
|
| 105 | if (DoorBlock.isWoodenDoor(this.mob.level(), doorPos)) {
|
| 106 | this.path = this.mob.getNavigation().createPath(node.x, node.y, node.z, 0);
|
| 107 | break;
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | return this.path != null;
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public boolean canContinueToUse() {
|
| 116 | return this.mob.getNavigation().isDone() ? false : !this.poiPos.closerToCenterThan(this.mob.position(), this.mob.getBbWidth() + this.distanceToPoi);
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | public void start() {
|
| 121 | this.mob.getNavigation().moveTo(this.path, this.speedModifier);
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public void stop() {
|
| 126 | if (this.mob.getNavigation().isDone() || this.poiPos.closerToCenterThan(this.mob.position(), this.distanceToPoi)) {
|
| 127 | this.visited.add(this.poiPos);
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | private boolean hasNotVisited(final BlockPos poi) {
|
| 132 | for (BlockPos visitedPoi : this.visited) {
|
| 133 | if (Objects.equals(poi, visitedPoi)) {
|
| 134 | return false;
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | return true;
|
| 139 | }
|
| 140 |
|
| 141 | private void updateVisited() {
|
| 142 | if (this.visited.size() > 15) {
|
| 143 | this.visited.remove(0);
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|