| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import it.unimi.dsi.fastutil.longs.Long2LongMap;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.Set;
|
| 8 | import java.util.function.Predicate;
|
| 9 | import java.util.stream.Collectors;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.world.entity.PathfinderMob;
|
| 13 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 14 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 15 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 16 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 17 | import net.minecraft.world.entity.ai.village.poi.PoiType;
|
| 18 | import net.minecraft.world.entity.ai.village.poi.PoiTypes;
|
| 19 | import net.minecraft.world.level.pathfinder.Path;
|
| 20 | import org.apache.commons.lang3.mutable.MutableInt;
|
| 21 | import org.apache.commons.lang3.mutable.MutableLong;
|
| 22 |
|
| 23 | public class SetClosestHomeAsWalkTarget {
|
| 24 | private static final int CACHE_TIMEOUT = 40;
|
| 25 | private static final int BATCH_SIZE = 5;
|
| 26 | private static final int RATE = 20;
|
| 27 | private static final int OK_DISTANCE_SQR = 4;
|
| 28 |
|
| 29 | public static BehaviorControl<PathfinderMob> create(final float speedModifier) {
|
| 30 | Long2LongMap batchCache = new Long2LongOpenHashMap();
|
| 31 | MutableLong lastUpdate = new MutableLong(0L);
|
| 32 | return BehaviorBuilder.create(
|
| 33 | i -> i.group(i.absent(MemoryModuleType.WALK_TARGET), i.absent(MemoryModuleType.HOME))
|
| 34 | .apply(
|
| 35 | i,
|
| 36 | (walkTarget, home) -> (level, body, timestamp) -> {
|
| 37 | if (level.getGameTime() - lastUpdate.longValue() < 20L) {
|
| 38 | return false;
|
| 39 | }
|
| 40 |
|
| 41 | PoiManager poiManager = level.getPoiManager();
|
| 42 | Optional<BlockPos> closest = poiManager.findClosest(p -> p.is(PoiTypes.HOME), body.blockPosition(), 48, PoiManager.Occupancy.ANY);
|
| 43 | if (!closest.isEmpty() && !(closest.get().distSqr(body.blockPosition()) <= 4.0)) {
|
| 44 | MutableInt triedCount = new MutableInt(0);
|
| 45 | lastUpdate.setValue(level.getGameTime() + level.getRandom().nextInt(20));
|
| 46 | Predicate<BlockPos> cacheTest = pos -> {
|
| 47 | long key = pos.asLong();
|
| 48 | if (batchCache.containsKey(key)) {
|
| 49 | return false;
|
| 50 | }
|
| 51 |
|
| 52 | if (triedCount.incrementAndGet() >= 5) {
|
| 53 | return false;
|
| 54 | }
|
| 55 |
|
| 56 | batchCache.put(key, lastUpdate.longValue() + 40L);
|
| 57 | return true;
|
| 58 | };
|
| 59 | Set<Pair<Holder<PoiType>, BlockPos>> pois = poiManager.findAllWithType(
|
| 60 | p -> p.is(PoiTypes.HOME), cacheTest, body.blockPosition(), 48, PoiManager.Occupancy.ANY
|
| 61 | )
|
| 62 | .collect(Collectors.toSet());
|
| 63 | Path path = AcquirePoi.findPathToPois(body, pois);
|
| 64 | if (path != null && path.canReach()) {
|
| 65 | BlockPos targetPos = path.getTarget();
|
| 66 | Optional<Holder<PoiType>> type = poiManager.getType(targetPos);
|
| 67 | if (type.isPresent()) {
|
| 68 | walkTarget.set(new WalkTarget(targetPos, speedModifier, 1));
|
| 69 | level.debugSynchronizers().updatePoi(targetPos);
|
| 70 | }
|
| 71 | } else if (triedCount.intValue() < 5) {
|
| 72 | batchCache.long2LongEntrySet().removeIf(entry -> entry.getLongValue() < lastUpdate.longValue());
|
| 73 | }
|
| 74 |
|
| 75 | return true;
|
| 76 | } else {
|
| 77 | return false;
|
| 78 | }
|
| 79 | }
|
| 80 | )
|
| 81 | );
|
| 82 | }
|
| 83 | }
|
| 84 |
|