| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.Set;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.GlobalPos;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.tags.BlockTags;
|
| 11 | import net.minecraft.world.entity.LivingEntity;
|
| 12 | import net.minecraft.world.entity.ai.Brain;
|
| 13 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 14 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 15 | import net.minecraft.world.entity.schedule.Activity;
|
| 16 | import net.minecraft.world.level.block.AbstractBedBlock;
|
| 17 | import net.minecraft.world.level.block.state.BlockState;
|
| 18 |
|
| 19 | public class SleepInBed extends Behavior<LivingEntity> {
|
| 20 | public static final int COOLDOWN_AFTER_BEING_WOKEN = 100;
|
| 21 | private long nextOkStartTime;
|
| 22 |
|
| 23 | public SleepInBed() {
|
| 24 | super(
|
| 25 | ImmutableMap.of(
|
| 26 | MemoryModuleType.HOME,
|
| 27 | MemoryStatus.VALUE_PRESENT,
|
| 28 | MemoryModuleType.LAST_WOKEN,
|
| 29 | MemoryStatus.REGISTERED,
|
| 30 | MemoryModuleType.LAST_SLEPT,
|
| 31 | MemoryStatus.REGISTERED,
|
| 32 | MemoryModuleType.WALK_TARGET,
|
| 33 | MemoryStatus.REGISTERED,
|
| 34 | MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE,
|
| 35 | MemoryStatus.REGISTERED
|
| 36 | )
|
| 37 | );
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected boolean checkExtraStartConditions(final ServerLevel level, final LivingEntity body) {
|
| 42 | if (body.isPassenger()) {
|
| 43 | return false;
|
| 44 | }
|
| 45 |
|
| 46 | Brain<?> brain = body.getBrain();
|
| 47 | GlobalPos target = brain.getMemory(MemoryModuleType.HOME).get();
|
| 48 | if (level.dimension() != target.dimension()) {
|
| 49 | return false;
|
| 50 | }
|
| 51 |
|
| 52 | Optional<Long> lastWokenMemory = brain.getMemory(MemoryModuleType.LAST_WOKEN);
|
| 53 | if (lastWokenMemory.isPresent()) {
|
| 54 | long timeSinceLastWoken = level.getGameTime() - lastWokenMemory.get();
|
| 55 | if (timeSinceLastWoken > 0L && timeSinceLastWoken < 100L) {
|
| 56 | return false;
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | BlockState blockState = level.getBlockState(target.pos());
|
| 61 | return target.pos().closerToCenterThan(body.position(), 2.0)
|
| 62 | && blockState.is(BlockTags.VILLAGERS_CAN_SLEEP_ON_BED)
|
| 63 | && !blockState.getValue(AbstractBedBlock.OCCUPIED);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected boolean canStillUse(final ServerLevel level, final LivingEntity body, final long timestamp) {
|
| 68 | Optional<GlobalPos> memory = body.getBrain().getMemory(MemoryModuleType.HOME);
|
| 69 | if (memory.isEmpty()) {
|
| 70 | return false;
|
| 71 | }
|
| 72 |
|
| 73 | BlockPos bedPos = memory.get().pos();
|
| 74 | return body.getBrain().isActive(Activity.REST) && body.getY() > bedPos.getY() + 0.4 && bedPos.closerToCenterThan(body.position(), 1.14);
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | protected void start(final ServerLevel level, final LivingEntity body, final long timestamp) {
|
| 79 | if (timestamp > this.nextOkStartTime) {
|
| 80 | Brain<?> brain = body.getBrain();
|
| 81 | if (brain.hasMemoryValue(MemoryModuleType.DOORS_TO_CLOSE)) {
|
| 82 | Set<GlobalPos> doors = brain.getMemory(MemoryModuleType.DOORS_TO_CLOSE).get();
|
| 83 | Optional<List<LivingEntity>> nearestEntities;
|
| 84 | if (brain.hasMemoryValue(MemoryModuleType.NEAREST_LIVING_ENTITIES)) {
|
| 85 | nearestEntities = brain.getMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES);
|
| 86 | } else {
|
| 87 | nearestEntities = Optional.empty();
|
| 88 | }
|
| 89 |
|
| 90 | InteractWithDoor.closeDoorsThatIHaveOpenedOrPassedThrough(level, body, null, null, doors, nearestEntities);
|
| 91 | }
|
| 92 |
|
| 93 | if (body.startSleeping(body.getBrain().getMemory(MemoryModuleType.HOME).get().pos())) {
|
| 94 | brain.setMemory(MemoryModuleType.LAST_SLEPT, timestamp);
|
| 95 | }
|
| 96 |
|
| 97 | brain.eraseMemory(MemoryModuleType.WALK_TARGET);
|
| 98 | brain.eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | protected boolean timedOut(final long timestamp) {
|
| 104 | return false;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected void stop(final ServerLevel level, final LivingEntity body, final long timestamp) {
|
| 109 | if (body.isSleeping()) {
|
| 110 | body.stopSleeping();
|
| 111 | this.nextOkStartTime = timestamp + 40L;
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|