| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import com.mojang.datafixers.kinds.OptionalBox.Mu;
|
| 5 | import java.util.Iterator;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Objects;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.Set;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.GlobalPos;
|
| 12 | import net.minecraft.server.level.ServerLevel;
|
| 13 | import net.minecraft.tags.BlockTags;
|
| 14 | import net.minecraft.world.entity.LivingEntity;
|
| 15 | import net.minecraft.world.entity.ai.Brain;
|
| 16 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 17 | import net.minecraft.world.entity.ai.behavior.declarative.MemoryAccessor;
|
| 18 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 19 | import net.minecraft.world.level.block.DoorBlock;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.level.pathfinder.Node;
|
| 22 | import net.minecraft.world.level.pathfinder.Path;
|
| 23 | import org.apache.commons.lang3.mutable.MutableInt;
|
| 24 | import org.apache.commons.lang3.mutable.MutableObject;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class InteractWithDoor {
|
| 28 | private static final int COOLDOWN_BEFORE_RERUNNING_IN_SAME_NODE = 20;
|
| 29 | private static final double SKIP_CLOSING_DOOR_IF_FURTHER_AWAY_THAN = 3.0;
|
| 30 | private static final double MAX_DISTANCE_TO_HOLD_DOOR_OPEN_FOR_OTHER_MOBS = 2.0;
|
| 31 |
|
| 32 | public static BehaviorControl<LivingEntity> create() {
|
| 33 | MutableObject<Node> lastCheckedNode = new MutableObject<>();
|
| 34 | MutableInt remainingCooldown = new MutableInt(0);
|
| 35 | return BehaviorBuilder.create(
|
| 36 | i -> i.group(i.present(MemoryModuleType.PATH), i.registered(MemoryModuleType.DOORS_TO_CLOSE), i.registered(MemoryModuleType.NEAREST_LIVING_ENTITIES))
|
| 37 | .apply(i, (pathMemory, doorsMemory, nearestEntities) -> (level, body, timestamp) -> {
|
| 38 | Path path = i.get(pathMemory);
|
| 39 | Optional<Set<GlobalPos>> doors = i.tryGet(doorsMemory);
|
| 40 | if (!path.notStarted() && !path.isDone()) {
|
| 41 | if (Objects.equals(lastCheckedNode.get(), path.getNextNode())) {
|
| 42 | remainingCooldown.setValue(20);
|
| 43 | } else if (remainingCooldown.decrementAndGet() > 0) {
|
| 44 | return false;
|
| 45 | }
|
| 46 |
|
| 47 | lastCheckedNode.setValue(path.getNextNode());
|
| 48 | Node fromNode = path.getPreviousNode();
|
| 49 | Node toNode = path.getNextNode();
|
| 50 | BlockPos fromPos = fromNode.asBlockPos();
|
| 51 | BlockState fromState = level.getBlockState(fromPos);
|
| 52 | if (fromState.is(BlockTags.MOB_INTERACTABLE_DOORS, s -> s.getBlock() instanceof DoorBlock)) {
|
| 53 | DoorBlock fromBlock = (DoorBlock)fromState.getBlock();
|
| 54 | if (!fromBlock.isOpen(fromState)) {
|
| 55 | fromBlock.setOpen(body, level, fromState, fromPos, true);
|
| 56 | }
|
| 57 |
|
| 58 | doors = rememberDoorToClose(doorsMemory, doors, level, fromPos);
|
| 59 | }
|
| 60 |
|
| 61 | BlockPos toPos = toNode.asBlockPos();
|
| 62 | BlockState toState = level.getBlockState(toPos);
|
| 63 | if (toState.is(BlockTags.MOB_INTERACTABLE_DOORS, s -> s.getBlock() instanceof DoorBlock)) {
|
| 64 | DoorBlock door = (DoorBlock)toState.getBlock();
|
| 65 | if (!door.isOpen(toState)) {
|
| 66 | door.setOpen(body, level, toState, toPos, true);
|
| 67 | doors = rememberDoorToClose(doorsMemory, doors, level, toPos);
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | doors.ifPresent(doorSet -> closeDoorsThatIHaveOpenedOrPassedThrough(level, body, fromNode, toNode, (Set<GlobalPos>)doorSet, i.tryGet(nearestEntities)));
|
| 72 | return true;
|
| 73 | } else {
|
| 74 | return false;
|
| 75 | }
|
| 76 | })
|
| 77 | );
|
| 78 | }
|
| 79 |
|
| 80 | public static void closeDoorsThatIHaveOpenedOrPassedThrough(
|
| 81 | final ServerLevel level,
|
| 82 | final LivingEntity body,
|
| 83 | final @Nullable Node movingFromNode,
|
| 84 | final @Nullable Node movingToNode,
|
| 85 | final Set<GlobalPos> doors,
|
| 86 | final Optional<List<LivingEntity>> nearestEntities
|
| 87 | ) {
|
| 88 | Iterator<GlobalPos> iterator = doors.iterator();
|
| 89 |
|
| 90 | while (iterator.hasNext()) {
|
| 91 | GlobalPos doorGlobalPos = iterator.next();
|
| 92 | BlockPos doorPos = doorGlobalPos.pos();
|
| 93 | if ((movingFromNode == null || !movingFromNode.asBlockPos().equals(doorPos)) && (movingToNode == null || !movingToNode.asBlockPos().equals(doorPos))) {
|
| 94 | if (isDoorTooFarAway(level, body, doorGlobalPos)) {
|
| 95 | iterator.remove();
|
| 96 | } else {
|
| 97 | BlockState state = level.getBlockState(doorPos);
|
| 98 | if (!state.is(BlockTags.MOB_INTERACTABLE_DOORS, s -> s.getBlock() instanceof DoorBlock)) {
|
| 99 | iterator.remove();
|
| 100 | } else {
|
| 101 | DoorBlock block = (DoorBlock)state.getBlock();
|
| 102 | if (!block.isOpen(state)) {
|
| 103 | iterator.remove();
|
| 104 | } else if (areOtherMobsComingThroughDoor(body, doorPos, nearestEntities)) {
|
| 105 | iterator.remove();
|
| 106 | } else {
|
| 107 | block.setOpen(body, level, state, doorPos, false);
|
| 108 | iterator.remove();
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | private static boolean areOtherMobsComingThroughDoor(final LivingEntity body, final BlockPos doorPos, final Optional<List<LivingEntity>> nearestEntities) {
|
| 117 | return nearestEntities.isEmpty()
|
| 118 | ? false
|
| 119 | : nearestEntities.get()
|
| 120 | .stream()
|
| 121 | .filter(otherMob -> otherMob.getType() == body.getType())
|
| 122 | .filter(otherMob -> doorPos.closerToCenterThan(otherMob.position(), 2.0))
|
| 123 | .anyMatch(otherMob -> isMobComingThroughDoor(otherMob.getBrain(), doorPos));
|
| 124 | }
|
| 125 |
|
| 126 | private static boolean isMobComingThroughDoor(final Brain<?> otherBrain, final BlockPos doorPos) {
|
| 127 | if (!otherBrain.hasMemoryValue(MemoryModuleType.PATH)) {
|
| 128 | return false;
|
| 129 | }
|
| 130 |
|
| 131 | Path path = otherBrain.getMemory(MemoryModuleType.PATH).get();
|
| 132 | if (path.isDone()) {
|
| 133 | return false;
|
| 134 | }
|
| 135 |
|
| 136 | Node movingFromNode = path.getPreviousNode();
|
| 137 | if (movingFromNode == null) {
|
| 138 | return false;
|
| 139 | }
|
| 140 |
|
| 141 | Node movingToNode = path.getNextNode();
|
| 142 | return doorPos.equals(movingFromNode.asBlockPos()) || doorPos.equals(movingToNode.asBlockPos());
|
| 143 | }
|
| 144 |
|
| 145 | private static boolean isDoorTooFarAway(final ServerLevel level, final LivingEntity body, final GlobalPos doorGlobalPos) {
|
| 146 | return doorGlobalPos.dimension() != level.dimension() || !doorGlobalPos.pos().closerToCenterThan(body.position(), 3.0);
|
| 147 | }
|
| 148 |
|
| 149 | private static Optional<Set<GlobalPos>> rememberDoorToClose(
|
| 150 | final MemoryAccessor<Mu, Set<GlobalPos>> doorsMemory, final Optional<Set<GlobalPos>> doors, final ServerLevel level, final BlockPos doorPos
|
| 151 | ) {
|
| 152 | GlobalPos globalDoorPos = GlobalPos.of(level.dimension(), doorPos);
|
| 153 | return Optional.of(doors.<Set<GlobalPos>>map(set -> {
|
| 154 | set.add(globalDoorPos);
|
| 155 | return set;
|
| 156 | }).orElseGet(() -> {
|
| 157 | Set<GlobalPos> set = Sets.newHashSet(globalDoorPos);
|
| 158 | doorsMemory.set(set);
|
| 159 | return set;
|
| 160 | }));
|
| 161 | }
|
| 162 | }
|
| 163 |
|