| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.function.Predicate;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.GlobalPos;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.tags.BlockTags;
|
| 10 | import net.minecraft.world.entity.LivingEntity;
|
| 11 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 12 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 13 | import net.minecraft.world.entity.ai.village.poi.PoiType;
|
| 14 | import net.minecraft.world.entity.npc.villager.Villager;
|
| 15 | import net.minecraft.world.level.block.BedBlock;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.phys.AABB;
|
| 18 |
|
| 19 | public class ValidateNearbyPoi {
|
| 20 | private static final int MAX_DISTANCE = 16;
|
| 21 |
|
| 22 | public static BehaviorControl<LivingEntity> create(final Predicate<Holder<PoiType>> poiType, final MemoryModuleType<GlobalPos> memoryType) {
|
| 23 | return BehaviorBuilder.create(i -> i.group(i.present(memoryType)).apply(i, memory -> (level, body, timestamp) -> {
|
| 24 | GlobalPos globalPos = i.get(memory);
|
| 25 | BlockPos pos = globalPos.pos();
|
| 26 | if (level.dimension() == globalPos.dimension() && pos.closerToCenterThan(body.position(), 16.0)) {
|
| 27 | ServerLevel poiLevel = level.getServer().getLevel(globalPos.dimension());
|
| 28 | if (poiLevel == null || !poiLevel.getPoiManager().exists(pos, poiType)) {
|
| 29 | memory.erase();
|
| 30 | } else if (bedIsOccupied(poiLevel, pos, body)) {
|
| 31 | memory.erase();
|
| 32 | if (!bedIsOccupiedByVillager(poiLevel, pos)) {
|
| 33 | level.getPoiManager().release(pos);
|
| 34 | level.debugSynchronizers().updatePoi(pos);
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | return true;
|
| 39 | } else {
|
| 40 | return false;
|
| 41 | }
|
| 42 | }));
|
| 43 | }
|
| 44 |
|
| 45 | private static boolean bedIsOccupied(final ServerLevel poiLevel, final BlockPos poiPos, final LivingEntity body) {
|
| 46 | BlockState blockState = poiLevel.getBlockState(poiPos);
|
| 47 | return blockState.is(BlockTags.VILLAGERS_CAN_SLEEP_ON_BED) && blockState.getValue(BedBlock.OCCUPIED) && !body.isSleeping();
|
| 48 | }
|
| 49 |
|
| 50 | private static boolean bedIsOccupiedByVillager(final ServerLevel poiLevel, final BlockPos poiPos) {
|
| 51 | List<Villager> villagers = poiLevel.getEntitiesOfClass(Villager.class, new AABB(poiPos), LivingEntity::isSleeping);
|
| 52 | return !villagers.isEmpty();
|
| 53 | }
|
| 54 | }
|
| 55 |
|