| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import java.util.HashSet;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.function.BiConsumer;
|
| 10 | import java.util.function.Consumer;
|
| 11 | import java.util.function.Predicate;
|
| 12 | import java.util.stream.Stream;
|
| 13 | import net.minecraft.core.BlockPos;
|
| 14 | import net.minecraft.core.Direction;
|
| 15 | import net.minecraft.core.GlobalPos;
|
| 16 | import net.minecraft.server.level.ServerLevel;
|
| 17 | import net.minecraft.world.Container;
|
| 18 | import net.minecraft.world.entity.EquipmentSlot;
|
| 19 | import net.minecraft.world.entity.PathfinderMob;
|
| 20 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 21 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 22 | import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
|
| 23 | import net.minecraft.world.item.ItemStack;
|
| 24 | import net.minecraft.world.level.ChunkPos;
|
| 25 | import net.minecraft.world.level.ClipContext;
|
| 26 | import net.minecraft.world.level.Level;
|
| 27 | import net.minecraft.world.level.block.ChestBlock;
|
| 28 | import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
|
| 29 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 30 | import net.minecraft.world.level.block.entity.ChestBlockEntity;
|
| 31 | import net.minecraft.world.level.block.state.BlockState;
|
| 32 | import net.minecraft.world.level.block.state.properties.ChestType;
|
| 33 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 34 | import net.minecraft.world.level.pathfinder.Path;
|
| 35 | import net.minecraft.world.phys.AABB;
|
| 36 | import net.minecraft.world.phys.HitResult;
|
| 37 | import net.minecraft.world.phys.Vec3;
|
| 38 | import org.apache.commons.lang3.function.TriConsumer;
|
| 39 | import org.jspecify.annotations.Nullable;
|
| 40 |
|
| 41 | public class TransportItemsBetweenContainers extends Behavior<PathfinderMob> {
|
| 42 | public static final int TARGET_INTERACTION_TIME = 60;
|
| 43 | private static final int VISITED_POSITIONS_MEMORY_TIME = 6000;
|
| 44 | private static final int TRANSPORTED_ITEM_MAX_STACK_SIZE = 16;
|
| 45 | private static final int MAX_VISITED_POSITIONS = 10;
|
| 46 | private static final int MAX_UNREACHABLE_POSITIONS = 50;
|
| 47 | private static final int PASSENGER_MOB_TARGET_SEARCH_DISTANCE = 1;
|
| 48 | private static final int IDLE_COOLDOWN = 140;
|
| 49 | private static final double CLOSE_ENOUGH_TO_START_QUEUING_DISTANCE = 3.0;
|
| 50 | private static final double CLOSE_ENOUGH_TO_START_INTERACTING_WITH_TARGET_DISTANCE = 0.5;
|
| 51 | private static final double CLOSE_ENOUGH_TO_START_INTERACTING_WITH_TARGET_PATH_END_DISTANCE = 1.0;
|
| 52 | private static final double CLOSE_ENOUGH_TO_CONTINUE_INTERACTING_WITH_TARGET = 2.0;
|
| 53 | private final float speedModifier;
|
| 54 | private final int horizontalSearchDistance;
|
| 55 | private final int verticalSearchDistance;
|
| 56 | private final Predicate<BlockState> sourceBlockType;
|
| 57 | private final Predicate<BlockState> destinationBlockType;
|
| 58 | private final Predicate<TransportItemsBetweenContainers.TransportItemTarget> shouldQueueForTarget;
|
| 59 | private final Consumer<PathfinderMob> onStartTravelling;
|
| 60 | private final Map<TransportItemsBetweenContainers.ContainerInteractionState, TransportItemsBetweenContainers.OnTargetReachedInteraction> onTargetInteractionActions;
|
| 61 | private TransportItemsBetweenContainers.@Nullable TransportItemTarget target = null;
|
| 62 | private TransportItemsBetweenContainers.TransportItemState state;
|
| 63 | private TransportItemsBetweenContainers.@Nullable ContainerInteractionState interactionState;
|
| 64 | private int ticksSinceReachingTarget;
|
| 65 |
|
| 66 | public TransportItemsBetweenContainers(
|
| 67 | final float speedModifier,
|
| 68 | final Predicate<BlockState> sourceBlockType,
|
| 69 | final Predicate<BlockState> destinationBlockType,
|
| 70 | final int horizontalSearchDistance,
|
| 71 | final int verticalSearchDistance,
|
| 72 | final Map<TransportItemsBetweenContainers.ContainerInteractionState, TransportItemsBetweenContainers.OnTargetReachedInteraction> onTargetInteractionActions,
|
| 73 | final Consumer<PathfinderMob> onStartTravelling,
|
| 74 | final Predicate<TransportItemsBetweenContainers.TransportItemTarget> shouldQueueForTarget
|
| 75 | ) {
|
| 76 | super(
|
| 77 | ImmutableMap.of(
|
| 78 | MemoryModuleType.VISITED_BLOCK_POSITIONS,
|
| 79 | MemoryStatus.REGISTERED,
|
| 80 | MemoryModuleType.UNREACHABLE_TRANSPORT_BLOCK_POSITIONS,
|
| 81 | MemoryStatus.REGISTERED,
|
| 82 | MemoryModuleType.TRANSPORT_ITEMS_COOLDOWN_TICKS,
|
| 83 | MemoryStatus.VALUE_ABSENT,
|
| 84 | MemoryModuleType.IS_PANICKING,
|
| 85 | MemoryStatus.VALUE_ABSENT
|
| 86 | )
|
| 87 | );
|
| 88 | this.speedModifier = speedModifier;
|
| 89 | this.sourceBlockType = sourceBlockType;
|
| 90 | this.destinationBlockType = destinationBlockType;
|
| 91 | this.horizontalSearchDistance = horizontalSearchDistance;
|
| 92 | this.verticalSearchDistance = verticalSearchDistance;
|
| 93 | this.onStartTravelling = onStartTravelling;
|
| 94 | this.shouldQueueForTarget = shouldQueueForTarget;
|
| 95 | this.onTargetInteractionActions = onTargetInteractionActions;
|
| 96 | this.state = TransportItemsBetweenContainers.TransportItemState.TRAVELLING;
|
| 97 | }
|
| 98 |
|
| 99 | protected void start(final ServerLevel level, final PathfinderMob body, final long timestamp) {
|
| 100 | if (body.getNavigation() instanceof GroundPathNavigation pathNavigation) {
|
| 101 | pathNavigation.setCanPathToTargetsBelowSurface(true);
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | protected boolean checkExtraStartConditions(final ServerLevel level, final PathfinderMob body) {
|
| 106 | return !body.isLeashed();
|
| 107 | }
|
| 108 |
|
| 109 | protected boolean canStillUse(final ServerLevel level, final PathfinderMob body, final long timestamp) {
|
| 110 | return body.getBrain().getMemory(MemoryModuleType.TRANSPORT_ITEMS_COOLDOWN_TICKS).isEmpty() && !body.isPanicking() && !body.isLeashed();
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | protected boolean timedOut(final long timestamp) {
|
| 115 | return false;
|
| 116 | }
|
| 117 |
|
| 118 | protected void tick(final ServerLevel level, final PathfinderMob body, final long timestamp) {
|
| 119 | boolean updatedInvalidTarget = this.updateInvalidTarget(level, body);
|
| 120 | if (this.target == null) {
|
| 121 | this.stop(level, body, timestamp);
|
| 122 | } else if (!updatedInvalidTarget) {
|
| 123 | if (this.state.equals(TransportItemsBetweenContainers.TransportItemState.QUEUING)) {
|
| 124 | this.onQueuingForTarget(this.target, level, body);
|
| 125 | }
|
| 126 |
|
| 127 | if (this.state.equals(TransportItemsBetweenContainers.TransportItemState.TRAVELLING)) {
|
| 128 | this.onTravelToTarget(this.target, level, body);
|
| 129 | }
|
| 130 |
|
| 131 | if (this.state.equals(TransportItemsBetweenContainers.TransportItemState.INTERACTING)) {
|
| 132 | this.onReachedTarget(this.target, level, body);
|
| 133 | }
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | private boolean updateInvalidTarget(final ServerLevel level, final PathfinderMob body) {
|
| 138 | if (!this.hasValidTarget(level, body)) {
|
| 139 | this.stopTargetingCurrentTarget(body);
|
| 140 | Optional<TransportItemsBetweenContainers.TransportItemTarget> targetBlockPosition = this.getTransportTarget(level, body);
|
| 141 | if (targetBlockPosition.isPresent()) {
|
| 142 | this.target = targetBlockPosition.get();
|
| 143 | this.onStartTravelling(body);
|
| 144 | this.setVisitedBlockPos(body, level, this.target.pos);
|
| 145 | return true;
|
| 146 | } else {
|
| 147 | this.enterCooldownAfterNoMatchingTargetFound(body);
|
| 148 | return true;
|
| 149 | }
|
| 150 | } else {
|
| 151 | return false;
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | private void onQueuingForTarget(final TransportItemsBetweenContainers.TransportItemTarget target, final Level level, final PathfinderMob body) {
|
| 156 | if (!this.isAnotherMobInteractingWithTarget(target, level)) {
|
| 157 | this.resumeTravelling(body);
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | protected void onTravelToTarget(final TransportItemsBetweenContainers.TransportItemTarget target, final Level level, final PathfinderMob body) {
|
| 162 | if (this.isWithinTargetDistance(3.0, target, level, body, this.getCenterPos(body)) && this.isAnotherMobInteractingWithTarget(target, level)) {
|
| 163 | this.startQueuing(body);
|
| 164 | } else if (this.isWithinTargetDistance(getInteractionRange(body), target, level, body, this.getCenterPos(body))) {
|
| 165 | this.startOnReachedTargetInteraction(target, body);
|
| 166 | } else {
|
| 167 | this.walkTowardsTarget(body);
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | private Vec3 getCenterPos(final PathfinderMob body) {
|
| 172 | return this.setMiddleYPosition(body, body.position());
|
| 173 | }
|
| 174 |
|
| 175 | protected void onReachedTarget(final TransportItemsBetweenContainers.TransportItemTarget target, final Level level, final PathfinderMob body) {
|
| 176 | if (!this.isWithinTargetDistance(2.0, target, level, body, this.getCenterPos(body))) {
|
| 177 | this.onStartTravelling(body);
|
| 178 | } else {
|
| 179 | this.ticksSinceReachingTarget++;
|
| 180 | this.onTargetInteraction(target, body);
|
| 181 | if (this.ticksSinceReachingTarget >= 60) {
|
| 182 | this.doReachedTargetInteraction(
|
| 183 | body,
|
| 184 | target.container,
|
| 185 | this::pickUpItems,
|
| 186 | (mob, container) -> this.stopTargetingCurrentTarget(body),
|
| 187 | this::putDownItem,
|
| 188 | (mob, container) -> this.stopTargetingCurrentTarget(body)
|
| 189 | );
|
| 190 | this.onStartTravelling(body);
|
| 191 | }
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | private void startQueuing(final PathfinderMob body) {
|
| 196 | this.stopInPlace(body);
|
| 197 | this.setTransportingState(TransportItemsBetweenContainers.TransportItemState.QUEUING);
|
| 198 | }
|
| 199 |
|
| 200 | private void resumeTravelling(final PathfinderMob body) {
|
| 201 | this.setTransportingState(TransportItemsBetweenContainers.TransportItemState.TRAVELLING);
|
| 202 | this.walkTowardsTarget(body);
|
| 203 | }
|
| 204 |
|
| 205 | private void walkTowardsTarget(final PathfinderMob body) {
|
| 206 | if (this.target != null) {
|
| 207 | BehaviorUtils.setWalkAndLookTargetMemories(body, this.target.pos, this.speedModifier, 0);
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | private void startOnReachedTargetInteraction(final TransportItemsBetweenContainers.TransportItemTarget target, final PathfinderMob body) {
|
| 212 | this.doReachedTargetInteraction(
|
| 213 | body,
|
| 214 | target.container,
|
| 215 | this.onReachedInteraction(TransportItemsBetweenContainers.ContainerInteractionState.PICKUP_ITEM),
|
| 216 | this.onReachedInteraction(TransportItemsBetweenContainers.ContainerInteractionState.PICKUP_NO_ITEM),
|
| 217 | this.onReachedInteraction(TransportItemsBetweenContainers.ContainerInteractionState.PLACE_ITEM),
|
| 218 | this.onReachedInteraction(TransportItemsBetweenContainers.ContainerInteractionState.PLACE_NO_ITEM)
|
| 219 | );
|
| 220 | this.setTransportingState(TransportItemsBetweenContainers.TransportItemState.INTERACTING);
|
| 221 | }
|
| 222 |
|
| 223 | private void onStartTravelling(final PathfinderMob body) {
|
| 224 | this.onStartTravelling.accept(body);
|
| 225 | this.setTransportingState(TransportItemsBetweenContainers.TransportItemState.TRAVELLING);
|
| 226 | this.interactionState = null;
|
| 227 | this.ticksSinceReachingTarget = 0;
|
| 228 | }
|
| 229 |
|
| 230 | private BiConsumer<PathfinderMob, Container> onReachedInteraction(final TransportItemsBetweenContainers.ContainerInteractionState state) {
|
| 231 | return (mob, container) -> this.setInteractionState(state);
|
| 232 | }
|
| 233 |
|
| 234 | private void setTransportingState(final TransportItemsBetweenContainers.TransportItemState state) {
|
| 235 | this.state = state;
|
| 236 | }
|
| 237 |
|
| 238 | private void setInteractionState(final TransportItemsBetweenContainers.ContainerInteractionState state) {
|
| 239 | this.interactionState = state;
|
| 240 | }
|
| 241 |
|
| 242 | private void onTargetInteraction(final TransportItemsBetweenContainers.TransportItemTarget target, final PathfinderMob body) {
|
| 243 | body.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new BlockPosTracker(target.pos));
|
| 244 | this.stopInPlace(body);
|
| 245 | if (this.interactionState != null) {
|
| 246 | Optional.ofNullable(this.onTargetInteractionActions.get(this.interactionState))
|
| 247 | .ifPresent(action -> action.accept(body, target, this.ticksSinceReachingTarget));
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | private void doReachedTargetInteraction(
|
| 252 | final PathfinderMob body,
|
| 253 | final Container container,
|
| 254 | final BiConsumer<PathfinderMob, Container> onPickupSuccess,
|
| 255 | final BiConsumer<PathfinderMob, Container> onPickupFailure,
|
| 256 | final BiConsumer<PathfinderMob, Container> onPlaceSuccess,
|
| 257 | final BiConsumer<PathfinderMob, Container> onPlaceFailure
|
| 258 | ) {
|
| 259 | if (isPickingUpItems(body)) {
|
| 260 | if (matchesGettingItemsRequirement(container)) {
|
| 261 | onPickupSuccess.accept(body, container);
|
| 262 | } else {
|
| 263 | onPickupFailure.accept(body, container);
|
| 264 | }
|
| 265 | } else if (matchesLeavingItemsRequirement(body, container)) {
|
| 266 | onPlaceSuccess.accept(body, container);
|
| 267 | } else {
|
| 268 | onPlaceFailure.accept(body, container);
|
| 269 | }
|
| 270 | }
|
| 271 |
|
| 272 | private Optional<TransportItemsBetweenContainers.TransportItemTarget> getTransportTarget(final ServerLevel level, final PathfinderMob body) {
|
| 273 | AABB targetBlockSearchArea = this.getTargetSearchArea(body);
|
| 274 | Set<GlobalPos> visitedPositions = getVisitedPositions(body);
|
| 275 | Set<GlobalPos> unreachablePositions = getUnreachablePositions(body);
|
| 276 | List<ChunkPos> list = ChunkPos.rangeClosed(ChunkPos.containing(body.blockPosition()), Math.floorDiv(this.getHorizontalSearchDistance(body), 16) + 1).toList();
|
| 277 | TransportItemsBetweenContainers.TransportItemTarget target = null;
|
| 278 | double closestDistance = Float.MAX_VALUE;
|
| 279 |
|
| 280 | for (ChunkPos chunkPos : list) {
|
| 281 | LevelChunk levelChunk = level.getChunkSource().getChunkNow(chunkPos.x(), chunkPos.z());
|
| 282 | if (levelChunk != null) {
|
| 283 | for (BlockEntity potentialTarget : levelChunk.getBlockEntities().values()) {
|
| 284 | if (potentialTarget instanceof ChestBlockEntity chestBlockEntity) {
|
| 285 | double distance = chestBlockEntity.getBlockPos().distToCenterSqr(body.position());
|
| 286 | if (distance < closestDistance) {
|
| 287 | TransportItemsBetweenContainers.TransportItemTarget targetValidToPick = this.isTargetValidToPick(
|
| 288 | body, level, chestBlockEntity, visitedPositions, unreachablePositions, targetBlockSearchArea
|
| 289 | );
|
| 290 | if (targetValidToPick != null) {
|
| 291 | target = targetValidToPick;
|
| 292 | closestDistance = distance;
|
| 293 | }
|
| 294 | }
|
| 295 | }
|
| 296 | }
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | return target == null ? Optional.empty() : Optional.of(target);
|
| 301 | }
|
| 302 |
|
| 303 | private TransportItemsBetweenContainers.@Nullable TransportItemTarget isTargetValidToPick(
|
| 304 | final PathfinderMob body,
|
| 305 | final Level level,
|
| 306 | final BlockEntity blockEntity,
|
| 307 | final Set<GlobalPos> visitedPositions,
|
| 308 | final Set<GlobalPos> unreachablePositions,
|
| 309 | final AABB targetBlockSearchArea
|
| 310 | ) {
|
| 311 | BlockPos blockPos = blockEntity.getBlockPos();
|
| 312 | boolean isWithinSearchArea = targetBlockSearchArea.contains(blockPos.getX(), blockPos.getY(), blockPos.getZ());
|
| 313 | if (!isWithinSearchArea) {
|
| 314 | return null;
|
| 315 | }
|
| 316 |
|
| 317 | TransportItemsBetweenContainers.TransportItemTarget transportItemTarget = TransportItemsBetweenContainers.TransportItemTarget.tryCreatePossibleTarget(
|
| 318 | blockEntity, level
|
| 319 | );
|
| 320 | if (transportItemTarget == null) {
|
| 321 | return null;
|
| 322 | }
|
| 323 |
|
| 324 | boolean isValidTarget = this.isWantedBlock(body, transportItemTarget.state)
|
| 325 | && !this.isPositionAlreadyVisited(visitedPositions, unreachablePositions, transportItemTarget, level)
|
| 326 | && !this.isContainerLocked(transportItemTarget);
|
| 327 | return isValidTarget ? transportItemTarget : null;
|
| 328 | }
|
| 329 |
|
| 330 | private boolean isContainerLocked(final TransportItemsBetweenContainers.TransportItemTarget transportItemTarget) {
|
| 331 | return transportItemTarget.blockEntity instanceof BaseContainerBlockEntity blockEntity && blockEntity.isLocked();
|
| 332 | }
|
| 333 |
|
| 334 | private boolean hasValidTarget(final Level level, final PathfinderMob body) {
|
| 335 | boolean targetIsOfValidType = this.target != null && this.isWantedBlock(body, this.target.state) && this.targetHasNotChanged(level, this.target);
|
| 336 | if (targetIsOfValidType && !this.isTargetBlocked(level, this.target)) {
|
| 337 | if (!this.state.equals(TransportItemsBetweenContainers.TransportItemState.TRAVELLING)) {
|
| 338 | return true;
|
| 339 | }
|
| 340 |
|
| 341 | if (this.hasValidTravellingPath(level, this.target, body)) {
|
| 342 | return true;
|
| 343 | }
|
| 344 |
|
| 345 | this.markVisitedBlockPosAsUnreachable(body, level, this.target.pos);
|
| 346 | }
|
| 347 |
|
| 348 | return false;
|
| 349 | }
|
| 350 |
|
| 351 | private boolean hasValidTravellingPath(final Level level, final TransportItemsBetweenContainers.TransportItemTarget target, final PathfinderMob body) {
|
| 352 | Path path = body.getNavigation().getPath() == null ? body.getNavigation().createPath(target.pos, 0) : body.getNavigation().getPath();
|
| 353 | Vec3 posFromWhichToReachTarget = this.getPositionToReachTargetFrom(path, body);
|
| 354 | boolean canReachTarget = this.isWithinTargetDistance(getInteractionRange(body), target, level, body, posFromWhichToReachTarget);
|
| 355 | boolean hasNotYetCreatedPathToTarget = path == null && !canReachTarget;
|
| 356 | return hasNotYetCreatedPathToTarget || this.targetIsReachableFromPosition(level, canReachTarget, posFromWhichToReachTarget, target, body);
|
| 357 | }
|
| 358 |
|
| 359 | private Vec3 getPositionToReachTargetFrom(final @Nullable Path path, final PathfinderMob body) {
|
| 360 | boolean haveNoValidPath = path == null || path.getEndNode() == null;
|
| 361 | Vec3 bottomCenter = haveNoValidPath ? body.position() : Vec3.atBottomCenterOf(path.getEndNode().asBlockPos());
|
| 362 | return this.setMiddleYPosition(body, bottomCenter);
|
| 363 | }
|
| 364 |
|
| 365 | private Vec3 setMiddleYPosition(final PathfinderMob body, final Vec3 pos) {
|
| 366 | return pos.add(0.0, body.getBoundingBox().getYsize() / 2.0, 0.0);
|
| 367 | }
|
| 368 |
|
| 369 | private boolean isTargetBlocked(final Level level, final TransportItemsBetweenContainers.TransportItemTarget target) {
|
| 370 | return ChestBlock.isChestBlockedAt(level, target.pos);
|
| 371 | }
|
| 372 |
|
| 373 | private boolean targetHasNotChanged(final Level level, final TransportItemsBetweenContainers.TransportItemTarget target) {
|
| 374 | return target.blockEntity.equals(level.getBlockEntity(target.pos));
|
| 375 | }
|
| 376 |
|
| 377 | private Stream<TransportItemsBetweenContainers.TransportItemTarget> getConnectedTargets(
|
| 378 | final TransportItemsBetweenContainers.TransportItemTarget target, final Level level
|
| 379 | ) {
|
| 380 | if (target.state.getValueOrElse(ChestBlock.TYPE, ChestType.SINGLE) != ChestType.SINGLE) {
|
| 381 | TransportItemsBetweenContainers.TransportItemTarget connectedTarget = TransportItemsBetweenContainers.TransportItemTarget.tryCreatePossibleTarget(
|
| 382 | ChestBlock.getConnectedBlockPos(target.pos, target.state), level
|
| 383 | );
|
| 384 | return connectedTarget != null ? Stream.of(target, connectedTarget) : Stream.of(target);
|
| 385 | } else {
|
| 386 | return Stream.of(target);
|
| 387 | }
|
| 388 | }
|
| 389 |
|
| 390 | private AABB getTargetSearchArea(final PathfinderMob mob) {
|
| 391 | int horizontalSearchDistance = this.getHorizontalSearchDistance(mob);
|
| 392 | return new AABB(mob.blockPosition()).inflate(horizontalSearchDistance, this.getVerticalSearchDistance(mob), horizontalSearchDistance);
|
| 393 | }
|
| 394 |
|
| 395 | private int getHorizontalSearchDistance(final PathfinderMob mob) {
|
| 396 | return mob.isPassenger() ? 1 : this.horizontalSearchDistance;
|
| 397 | }
|
| 398 |
|
| 399 | private int getVerticalSearchDistance(final PathfinderMob mob) {
|
| 400 | return mob.isPassenger() ? 1 : this.verticalSearchDistance;
|
| 401 | }
|
| 402 |
|
| 403 | private static Set<GlobalPos> getVisitedPositions(final PathfinderMob mob) {
|
| 404 | return mob.getBrain().getMemory(MemoryModuleType.VISITED_BLOCK_POSITIONS).orElse(Set.of());
|
| 405 | }
|
| 406 |
|
| 407 | private static Set<GlobalPos> getUnreachablePositions(final PathfinderMob mob) {
|
| 408 | return mob.getBrain().getMemory(MemoryModuleType.UNREACHABLE_TRANSPORT_BLOCK_POSITIONS).orElse(Set.of());
|
| 409 | }
|
| 410 |
|
| 411 | private boolean isPositionAlreadyVisited(
|
| 412 | final Set<GlobalPos> visitedPositions,
|
| 413 | final Set<GlobalPos> unreachablePositions,
|
| 414 | final TransportItemsBetweenContainers.TransportItemTarget target,
|
| 415 | final Level level
|
| 416 | ) {
|
| 417 | return this.getConnectedTargets(target, level)
|
| 418 | .map(transportItemTarget -> new GlobalPos(level.dimension(), transportItemTarget.pos))
|
| 419 | .anyMatch(pos -> visitedPositions.contains(pos) || unreachablePositions.contains(pos));
|
| 420 | }
|
| 421 |
|
| 422 | private static boolean hasFinishedPath(final PathfinderMob body) {
|
| 423 | return body.getNavigation().getPath() != null && body.getNavigation().getPath().isDone();
|
| 424 | }
|
| 425 |
|
| 426 | protected void setVisitedBlockPos(final PathfinderMob body, final Level level, final BlockPos target) {
|
| 427 | Set<GlobalPos> visitedPositions = new HashSet<>(getVisitedPositions(body));
|
| 428 | visitedPositions.add(new GlobalPos(level.dimension(), target));
|
| 429 | if (visitedPositions.size() > 10) {
|
| 430 | this.enterCooldownAfterNoMatchingTargetFound(body);
|
| 431 | } else {
|
| 432 | body.getBrain().setMemoryWithExpiry(MemoryModuleType.VISITED_BLOCK_POSITIONS, visitedPositions, 6000L);
|
| 433 | }
|
| 434 | }
|
| 435 |
|
| 436 | protected void markVisitedBlockPosAsUnreachable(final PathfinderMob body, final Level level, final BlockPos target) {
|
| 437 | Set<GlobalPos> visitedPositions = new HashSet<>(getVisitedPositions(body));
|
| 438 | visitedPositions.remove(new GlobalPos(level.dimension(), target));
|
| 439 | Set<GlobalPos> unreachablePositions = new HashSet<>(getUnreachablePositions(body));
|
| 440 | unreachablePositions.add(new GlobalPos(level.dimension(), target));
|
| 441 | if (unreachablePositions.size() > 50) {
|
| 442 | this.enterCooldownAfterNoMatchingTargetFound(body);
|
| 443 | } else {
|
| 444 | body.getBrain().setMemoryWithExpiry(MemoryModuleType.VISITED_BLOCK_POSITIONS, visitedPositions, 6000L);
|
| 445 | body.getBrain().setMemoryWithExpiry(MemoryModuleType.UNREACHABLE_TRANSPORT_BLOCK_POSITIONS, unreachablePositions, 6000L);
|
| 446 | }
|
| 447 | }
|
| 448 |
|
| 449 | private boolean isWantedBlock(final PathfinderMob mob, final BlockState block) {
|
| 450 | return isPickingUpItems(mob) ? this.sourceBlockType.test(block) : this.destinationBlockType.test(block);
|
| 451 | }
|
| 452 |
|
| 453 | private static double getInteractionRange(final PathfinderMob body) {
|
| 454 | return hasFinishedPath(body) ? 1.0 : 0.5;
|
| 455 | }
|
| 456 |
|
| 457 | private boolean isWithinTargetDistance(
|
| 458 | final double distance, final TransportItemsBetweenContainers.TransportItemTarget target, final Level level, final PathfinderMob body, final Vec3 fromPos
|
| 459 | ) {
|
| 460 | AABB boundingBox = body.getBoundingBox();
|
| 461 | AABB movedBoundBox = AABB.ofSize(fromPos, boundingBox.getXsize(), boundingBox.getYsize(), boundingBox.getZsize());
|
| 462 | return target.state.getCollisionShape(level, target.pos).bounds().inflate(distance, 0.5, distance).move(target.pos).intersects(movedBoundBox);
|
| 463 | }
|
| 464 |
|
| 465 | private boolean targetIsReachableFromPosition(
|
| 466 | final Level level, final boolean canReachTarget, final Vec3 pos, final TransportItemsBetweenContainers.TransportItemTarget target, final PathfinderMob body
|
| 467 | ) {
|
| 468 | return canReachTarget && this.canSeeAnyTargetSide(target, level, body, pos);
|
| 469 | }
|
| 470 |
|
| 471 | private boolean canSeeAnyTargetSide(
|
| 472 | final TransportItemsBetweenContainers.TransportItemTarget target, final Level level, final PathfinderMob body, final Vec3 eyePosition
|
| 473 | ) {
|
| 474 | Vec3 center = Vec3.atCenterOf(target.pos);
|
| 475 | return Direction.stream()
|
| 476 | .map(direction -> center.add(0.5 * direction.getStepX(), 0.5 * direction.getStepY(), 0.5 * direction.getStepZ()))
|
| 477 | .map(hitTarget -> level.clip(new ClipContext(eyePosition, hitTarget, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, body)))
|
| 478 | .anyMatch(hitResult -> hitResult.getType() == HitResult.Type.BLOCK && hitResult.getBlockPos().equals(target.pos));
|
| 479 | }
|
| 480 |
|
| 481 | private boolean isAnotherMobInteractingWithTarget(final TransportItemsBetweenContainers.TransportItemTarget target, final Level level) {
|
| 482 | return this.getConnectedTargets(target, level).anyMatch(this.shouldQueueForTarget);
|
| 483 | }
|
| 484 |
|
| 485 | private static boolean isPickingUpItems(final PathfinderMob body) {
|
| 486 | return body.getMainHandItem().isEmpty();
|
| 487 | }
|
| 488 |
|
| 489 | private static boolean matchesGettingItemsRequirement(final Container container) {
|
| 490 | return !container.isEmpty();
|
| 491 | }
|
| 492 |
|
| 493 | private static boolean matchesLeavingItemsRequirement(final PathfinderMob body, final Container container) {
|
| 494 | return container.isEmpty() || hasItemMatchingHandItem(body, container);
|
| 495 | }
|
| 496 |
|
| 497 | private static boolean hasItemMatchingHandItem(final PathfinderMob body, final Container container) {
|
| 498 | ItemStack mainHandItem = body.getMainHandItem();
|
| 499 |
|
| 500 | for (ItemStack itemStack : container) {
|
| 501 | if (ItemStack.isSameItem(itemStack, mainHandItem)) {
|
| 502 | return true;
|
| 503 | }
|
| 504 | }
|
| 505 |
|
| 506 | return false;
|
| 507 | }
|
| 508 |
|
| 509 | private void pickUpItems(final PathfinderMob body, final Container container) {
|
| 510 | body.setItemSlot(EquipmentSlot.MAINHAND, pickupItemFromContainer(container));
|
| 511 | body.setGuaranteedDrop(EquipmentSlot.MAINHAND);
|
| 512 | container.setChanged();
|
| 513 | this.clearMemoriesAfterMatchingTargetFound(body);
|
| 514 | }
|
| 515 |
|
| 516 | private void putDownItem(final PathfinderMob body, final Container container) {
|
| 517 | ItemStack itemsLeftAfterVisitingChest = addItemsToContainer(body, container);
|
| 518 | container.setChanged();
|
| 519 | body.setItemSlot(EquipmentSlot.MAINHAND, itemsLeftAfterVisitingChest);
|
| 520 | if (itemsLeftAfterVisitingChest.isEmpty()) {
|
| 521 | this.clearMemoriesAfterMatchingTargetFound(body);
|
| 522 | } else {
|
| 523 | this.stopTargetingCurrentTarget(body);
|
| 524 | }
|
| 525 | }
|
| 526 |
|
| 527 | private static ItemStack pickupItemFromContainer(final Container container) {
|
| 528 | int slot = 0;
|
| 529 |
|
| 530 | for (ItemStack itemStack : container) {
|
| 531 | if (!itemStack.isEmpty()) {
|
| 532 | int itemCount = Math.min(itemStack.getCount(), 16);
|
| 533 | return container.removeItem(slot, itemCount);
|
| 534 | }
|
| 535 |
|
| 536 | slot++;
|
| 537 | }
|
| 538 |
|
| 539 | return ItemStack.EMPTY;
|
| 540 | }
|
| 541 |
|
| 542 | private static ItemStack addItemsToContainer(final PathfinderMob body, final Container container) {
|
| 543 | int slot = 0;
|
| 544 | ItemStack itemStack = body.getMainHandItem();
|
| 545 |
|
| 546 | for (ItemStack containerItemStack : container) {
|
| 547 | if (containerItemStack.isEmpty()) {
|
| 548 | container.setItem(slot, itemStack);
|
| 549 | return ItemStack.EMPTY;
|
| 550 | }
|
| 551 |
|
| 552 | if (ItemStack.isSameItemSameComponents(containerItemStack, itemStack) && containerItemStack.getCount() < containerItemStack.getMaxStackSize()) {
|
| 553 | int countThatCanBeAdded = containerItemStack.getMaxStackSize() - containerItemStack.getCount();
|
| 554 | int countToAdd = Math.min(countThatCanBeAdded, itemStack.getCount());
|
| 555 | containerItemStack.setCount(containerItemStack.getCount() + countToAdd);
|
| 556 | itemStack.setCount(itemStack.getCount() - countThatCanBeAdded);
|
| 557 | container.setItem(slot, containerItemStack);
|
| 558 | if (itemStack.isEmpty()) {
|
| 559 | return ItemStack.EMPTY;
|
| 560 | }
|
| 561 | }
|
| 562 |
|
| 563 | slot++;
|
| 564 | }
|
| 565 |
|
| 566 | return itemStack;
|
| 567 | }
|
| 568 |
|
| 569 | protected void stopTargetingCurrentTarget(final PathfinderMob body) {
|
| 570 | this.ticksSinceReachingTarget = 0;
|
| 571 | this.target = null;
|
| 572 | body.getNavigation().stop();
|
| 573 | body.getBrain().eraseMemory(MemoryModuleType.WALK_TARGET);
|
| 574 | }
|
| 575 |
|
| 576 | protected void clearMemoriesAfterMatchingTargetFound(final PathfinderMob body) {
|
| 577 | this.stopTargetingCurrentTarget(body);
|
| 578 | body.getBrain().eraseMemory(MemoryModuleType.VISITED_BLOCK_POSITIONS);
|
| 579 | body.getBrain().eraseMemory(MemoryModuleType.UNREACHABLE_TRANSPORT_BLOCK_POSITIONS);
|
| 580 | }
|
| 581 |
|
| 582 | private void enterCooldownAfterNoMatchingTargetFound(final PathfinderMob body) {
|
| 583 | this.stopTargetingCurrentTarget(body);
|
| 584 | body.getBrain().setMemory(MemoryModuleType.TRANSPORT_ITEMS_COOLDOWN_TICKS, 140);
|
| 585 | body.getBrain().eraseMemory(MemoryModuleType.VISITED_BLOCK_POSITIONS);
|
| 586 | body.getBrain().eraseMemory(MemoryModuleType.UNREACHABLE_TRANSPORT_BLOCK_POSITIONS);
|
| 587 | }
|
| 588 |
|
| 589 | protected void stop(final ServerLevel level, final PathfinderMob body, final long timestamp) {
|
| 590 | this.onStartTravelling(body);
|
| 591 | if (body.getNavigation() instanceof GroundPathNavigation pathNavigation) {
|
| 592 | pathNavigation.setCanPathToTargetsBelowSurface(false);
|
| 593 | }
|
| 594 | }
|
| 595 |
|
| 596 | private void stopInPlace(final PathfinderMob mob) {
|
| 597 | mob.getNavigation().stop();
|
| 598 | mob.setXxa(0.0F);
|
| 599 | mob.setYya(0.0F);
|
| 600 | mob.setSpeed(0.0F);
|
| 601 | mob.setDeltaMovement(0.0, mob.getDeltaMovement().y, 0.0);
|
| 602 | }
|
| 603 |
|
| 604 | public enum ContainerInteractionState {
|
| 605 | PICKUP_ITEM,
|
| 606 | PICKUP_NO_ITEM,
|
| 607 | PLACE_ITEM,
|
| 608 | PLACE_NO_ITEM;
|
| 609 | }
|
| 610 |
|
| 611 | @FunctionalInterface
|
| 612 | public interface OnTargetReachedInteraction extends TriConsumer<PathfinderMob, TransportItemsBetweenContainers.TransportItemTarget, Integer> {
|
| 613 | }
|
| 614 |
|
| 615 | public enum TransportItemState {
|
| 616 | TRAVELLING,
|
| 617 | QUEUING,
|
| 618 | INTERACTING;
|
| 619 | }
|
| 620 |
|
| 621 | public record TransportItemTarget(BlockPos pos, Container container, BlockEntity blockEntity, BlockState state) {
|
| 622 | public static TransportItemsBetweenContainers.@Nullable TransportItemTarget tryCreatePossibleTarget(final BlockEntity blockEntity, final Level level) {
|
| 623 | BlockPos blockPos = blockEntity.getBlockPos();
|
| 624 | BlockState blockState = blockEntity.getBlockState();
|
| 625 | Container container = getBlockEntityContainer(blockEntity, blockState, level, blockPos);
|
| 626 | return container != null ? new TransportItemsBetweenContainers.TransportItemTarget(blockPos, container, blockEntity, blockState) : null;
|
| 627 | }
|
| 628 |
|
| 629 | public static TransportItemsBetweenContainers.@Nullable TransportItemTarget tryCreatePossibleTarget(final BlockPos blockPos, final Level level) {
|
| 630 | BlockEntity blockEntity = level.getBlockEntity(blockPos);
|
| 631 | return blockEntity == null ? null : tryCreatePossibleTarget(blockEntity, level);
|
| 632 | }
|
| 633 |
|
| 634 | private static @Nullable Container getBlockEntityContainer(
|
| 635 | final BlockEntity blockEntity, final BlockState blockState, final Level level, final BlockPos blockPos
|
| 636 | ) {
|
| 637 | if (blockState.getBlock() instanceof ChestBlock chestBlock) {
|
| 638 | return ChestBlock.getContainer(chestBlock, blockState, level, blockPos, false);
|
| 639 | } else {
|
| 640 | return blockEntity instanceof Container container ? container : null;
|
| 641 | }
|
| 642 | }
|
| 643 | }
|
| 644 | }
|
| 645 |
|