| 1 | package net.minecraft.world.entity.ai.sensing;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2LongMap;
|
| 6 | import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.function.Predicate;
|
| 10 | import java.util.stream.Collectors;
|
| 11 | import net.minecraft.core.BlockPos;
|
| 12 | import net.minecraft.core.Holder;
|
| 13 | import net.minecraft.server.level.ServerLevel;
|
| 14 | import net.minecraft.world.entity.Mob;
|
| 15 | import net.minecraft.world.entity.ai.behavior.AcquirePoi;
|
| 16 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 17 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 18 | import net.minecraft.world.entity.ai.village.poi.PoiType;
|
| 19 | import net.minecraft.world.entity.ai.village.poi.PoiTypes;
|
| 20 | import net.minecraft.world.level.pathfinder.Path;
|
| 21 |
|
| 22 | public class NearestBedSensor extends Sensor<Mob> {
|
| 23 | private static final int CACHE_TIMEOUT = 40;
|
| 24 | private static final int BATCH_SIZE = 5;
|
| 25 | private static final int RATE = 20;
|
| 26 | private final Long2LongMap batchCache = new Long2LongOpenHashMap();
|
| 27 | private int triedCount;
|
| 28 | private long lastUpdate;
|
| 29 |
|
| 30 | public NearestBedSensor() {
|
| 31 | super(20);
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public Set<MemoryModuleType<?>> requires() {
|
| 36 | return ImmutableSet.of(MemoryModuleType.NEAREST_BED);
|
| 37 | }
|
| 38 |
|
| 39 | protected void doTick(final ServerLevel level, final Mob body) {
|
| 40 | if (body.isBaby()) {
|
| 41 | this.triedCount = 0;
|
| 42 | this.lastUpdate = level.getGameTime() + level.getRandom().nextInt(20);
|
| 43 | PoiManager poiManager = level.getPoiManager();
|
| 44 | Predicate<BlockPos> cacheTest = pos -> {
|
| 45 | long key = pos.asLong();
|
| 46 | if (this.batchCache.containsKey(key)) {
|
| 47 | return false;
|
| 48 | }
|
| 49 |
|
| 50 | if (++this.triedCount >= 5) {
|
| 51 | return false;
|
| 52 | }
|
| 53 |
|
| 54 | this.batchCache.put(key, this.lastUpdate + 40L);
|
| 55 | return true;
|
| 56 | };
|
| 57 | Set<Pair<Holder<PoiType>, BlockPos>> pois = poiManager.findAllWithType(
|
| 58 | e -> e.is(PoiTypes.HOME), cacheTest, body.blockPosition(), 48, PoiManager.Occupancy.ANY
|
| 59 | )
|
| 60 | .collect(Collectors.toSet());
|
| 61 | Path path = AcquirePoi.findPathToPois(body, pois);
|
| 62 | if (path != null && path.canReach()) {
|
| 63 | BlockPos targetPos = path.getTarget();
|
| 64 | Optional<Holder<PoiType>> type = poiManager.getType(targetPos);
|
| 65 | if (type.isPresent()) {
|
| 66 | body.getBrain().setMemory(MemoryModuleType.NEAREST_BED, targetPos);
|
| 67 | }
|
| 68 | } else if (this.triedCount < 5) {
|
| 69 | this.batchCache.long2LongEntrySet().removeIf(entry -> entry.getLongValue() < this.lastUpdate);
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|