2026-07-09 00:03:09 +08:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrHua269 <mrhua269@gmail.com>
Date: Wed, 8 Jul 2026 21:12:31 +0800
Subject: [PATCH] Fix region threading with entity ai data access
diff --git a/net/minecraft/world/entity/Mob.java b/net/minecraft/world/entity/Mob.java
index 3b1c3e84ab24b6e3a0e4d129b3617b393b6d6651..c6ce88fb63e0d40ed011958a1afd36c645d965c8 100644
--- a/net/minecraft/world/entity/Mob.java
+++ b/net/minecraft/world/entity/Mob.java
@@ -299,6 +299,11 @@ public abstract class Mob extends LivingEntity implements Targeting, EquipmentUs
if (Objects.equals(currentTarget, target)) {
return false;
}
2026-07-14 11:32:51 +08:00
+ // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
+ if (!ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(target)) {
+ return false;
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
LivingEntity originalTarget = target;
target = asValidTarget(target);
if (reason != null) {
diff --git a/net/minecraft/world/entity/ai/Brain.java b/net/minecraft/world/entity/ai/Brain.java
index e097bc1268b4050e3948413c52dea69950858458..977a31f995799cf44fa59cc1db73571edbcf36ff 100644
--- a/net/minecraft/world/entity/ai/Brain.java
+++ b/net/minecraft/world/entity/ai/Brain.java
@@ -383,7 +383,7 @@ public class Brain<E extends LivingEntity> {
}
public void tick(final ServerLevel level, final E body) {
- this.forgetOutdatedMemories();
2026-07-14 11:32:51 +08:00
+ this.forgetOutdatedMemories(body); // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
this.tickSensors(level, body);
this.startEachNonRunningBehavior(level, body);
this.tickEachRunningBehavior(level, body);
@@ -395,8 +395,8 @@ public class Brain<E extends LivingEntity> {
}
}
- private void forgetOutdatedMemories() {
- this.memories.values().forEach(MemorySlot::tick);
2026-07-14 11:32:51 +08:00
+ private void forgetOutdatedMemories(E body) { // Shiroha - Fix region threading with entity ai data access
+ this.memories.values().forEach(slot -> slot.tick(body)); // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
}
public void stopAll(final ServerLevel level, final E body) {
diff --git a/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java b/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java
index f744ecd20dc70786311a7162b52aa4ceca0717db..c8373151e280764e3175a263d201c078cca6fac8 100644
--- a/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java
+++ b/net/minecraft/world/entity/ai/behavior/BehaviorUtils.java
@@ -81,6 +81,11 @@ public class BehaviorUtils {
public static void setWalkAndLookTargetMemories(
final LivingEntity walker, final PositionTracker target, final float speedModifier, final int closeEnoughDistance
) {
2026-07-14 11:32:51 +08:00
+ // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
+ if (!target.checkThread(walker.level())) {
+ return;
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
WalkTarget walkTarget = new WalkTarget(target, speedModifier, closeEnoughDistance);
walker.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, target);
walker.getBrain().setMemory(MemoryModuleType.WALK_TARGET, walkTarget);
diff --git a/net/minecraft/world/entity/ai/behavior/BlockPosTracker.java b/net/minecraft/world/entity/ai/behavior/BlockPosTracker.java
index 68251875edfa47ac34c64f99510998ad4bbb14b4..cb386eac7855d53c8c1df36305b6b0156cccd62d 100644
--- a/net/minecraft/world/entity/ai/behavior/BlockPosTracker.java
+++ b/net/minecraft/world/entity/ai/behavior/BlockPosTracker.java
@@ -37,4 +37,11 @@ public class BlockPosTracker implements PositionTracker {
public String toString() {
return "BlockPosTracker{blockPos=" + this.blockPos + ", centerPosition=" + this.centerPosition + "}";
}
+
2026-07-14 11:32:51 +08:00
+ // Shiroha start - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
+ @Override
+ public boolean checkThread(net.minecraft.world.level.Level currOwnedByLevel) {
+ return ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(currOwnedByLevel, this.blockPos);
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
}
diff --git a/net/minecraft/world/entity/ai/behavior/EntityTracker.java b/net/minecraft/world/entity/ai/behavior/EntityTracker.java
index 3ac52b025ac3e1a3f9135b8e593a385a847afe0f..61cdd99e21cf894cf73c1b90c1d4c45a94d222ff 100644
--- a/net/minecraft/world/entity/ai/behavior/EntityTracker.java
+++ b/net/minecraft/world/entity/ai/behavior/EntityTracker.java
@@ -55,4 +55,11 @@ public class EntityTracker implements PositionTracker {
public String toString() {
return "EntityTracker for " + this.entity;
}
+
2026-07-14 11:32:51 +08:00
+ // Shiroha start - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
+ @Override
+ public boolean checkThread(net.minecraft.world.level.Level currOwnedByLevel) {
+ return ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(this.entity);
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
}
diff --git a/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java b/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java
index b40da004b5281a041ee896ae176bfb9c4660f353..1e2a8c2beaec24f04f70baee9eb6a678ce325efa 100644
--- a/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java
+++ b/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java
@@ -117,7 +117,9 @@ public class MoveToTargetSink extends Behavior<Mob> {
private boolean tryComputePath(final Mob body, final WalkTarget walkTarget, final long timestamp) {
BlockPos targetPos = walkTarget.getTarget().currentBlockPosition();
2026-07-14 11:32:51 +08:00
+ if (ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(body.level(), targetPos)) // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
this.path = body.getNavigation().createPath(targetPos, 0);
2026-07-14 11:32:51 +08:00
+ else this.path = null; // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
this.speedModifier = walkTarget.getSpeedModifier();
Brain<?> brain = body.getBrain();
if (this.reachedTarget(body, walkTarget)) {
diff --git a/net/minecraft/world/entity/ai/behavior/PositionTracker.java b/net/minecraft/world/entity/ai/behavior/PositionTracker.java
index ce6cf5ecfb190428e3ef9b7dd39c98e3d27a7b9d..2194a77e1746c061e294840ba9a3b8d8604ff0b9 100644
--- a/net/minecraft/world/entity/ai/behavior/PositionTracker.java
+++ b/net/minecraft/world/entity/ai/behavior/PositionTracker.java
@@ -10,4 +10,6 @@ public interface PositionTracker {
BlockPos currentBlockPosition();
boolean isVisibleBy(final LivingEntity body);
+
2026-07-14 11:32:51 +08:00
+ boolean checkThread(net.minecraft.world.level.Level currOwnedByLevel); // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
}
diff --git a/net/minecraft/world/entity/ai/behavior/SleepInBed.java b/net/minecraft/world/entity/ai/behavior/SleepInBed.java
index 16017d819c28b077f732e2ef571eac179d24e323..7d0f7a42c26fe1e236b0b9696b2b962281dd6c70 100644
--- a/net/minecraft/world/entity/ai/behavior/SleepInBed.java
+++ b/net/minecraft/world/entity/ai/behavior/SleepInBed.java
@@ -49,6 +49,11 @@ public class SleepInBed extends Behavior<LivingEntity> {
return false;
}
2026-07-14 11:32:51 +08:00
+ // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
+ if (!ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(level, target.pos())) {
+ return false;
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
Optional<Long> lastWokenMemory = brain.getMemory(MemoryModuleType.LAST_WOKEN);
if (lastWokenMemory.isPresent()) {
long timeSinceLastWoken = level.getGameTime() - lastWokenMemory.get();
@@ -56,7 +61,6 @@ public class SleepInBed extends Behavior<LivingEntity> {
return false;
}
}
-
BlockState blockState = level.getBlockStateIfLoaded(target.pos()); // Paper - Prevent sync chunk loads when villagers try to find beds
if (blockState == null) return false; // Paper - Prevent sync chunk loads when villagers try to find beds
return target.pos().closerToCenterThan(body.position(), 2.0) && blockState.is(BlockTags.BEDS) && !blockState.getValue(BedBlock.OCCUPIED);
diff --git a/net/minecraft/world/entity/ai/memory/MemorySlot.java b/net/minecraft/world/entity/ai/memory/MemorySlot.java
index 88a89b4c72cc99dc89d3f3cc928b2dfda4125759..af210abbe1d4b75e1c6828cb25181c4caa0eca35 100644
--- a/net/minecraft/world/entity/ai/memory/MemorySlot.java
+++ b/net/minecraft/world/entity/ai/memory/MemorySlot.java
@@ -13,7 +13,7 @@ public class MemorySlot<T> {
this.timeToLive = timeToLive;
}
- public void tick() {
2026-07-14 11:32:51 +08:00
+ public void tick(net.minecraft.world.entity.Entity owner) { // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
if (this.hasValue() && this.canExpire()) {
if (this.hasExpired()) {
this.clear();
@@ -21,6 +21,41 @@ public class MemorySlot<T> {
this.timeToLive--;
}
}
2026-07-14 11:32:51 +08:00
+ // Shiroha start - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
+ final net.minecraft.world.level.Level ownerLevel = owner.level();
+
+ // type: entity
+ if (moe.monnairealms.camellia.config.modules.fixes.ForceCleanupEntityBrainMemoryConfig.enabledForEntity && this.value instanceof net.minecraft.world.entity.Entity entity) {
+ if (!ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(entity)) {
+ this.clear();
+ }
+ }
+
+ // type: block_pos
+ if (moe.monnairealms.camellia.config.modules.fixes.ForceCleanupEntityBrainMemoryConfig.enabledForBlockPos && this.value instanceof net.minecraft.core.BlockPos blockPos) {
+ if (!ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(ownerLevel, blockPos)) {
+ this.clear();
+ }
+ }
+
+
+ //type: position_tracker and walk_target
+ if (moe.monnairealms.camellia.config.modules.fixes.ForceCleanupEntityBrainMemoryConfig.enabledForPositionTracker) {
+ net.minecraft.world.entity.ai.behavior.PositionTracker tracker = null;
+
+ if (value instanceof net.minecraft.world.entity.ai.behavior.PositionTracker positionTracker) {
+ tracker = positionTracker;
+ }
+
+ if (value instanceof net.minecraft.world.entity.ai.memory.WalkTarget walkTarget) {
+ tracker = walkTarget.getTarget();
+ }
+
+ if (tracker != null && !tracker.checkThread(owner.level())) {
+ this.clear();
+ }
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
}
public static <T> MemorySlot<T> create() {
diff --git a/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java b/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java
index e44814cfb6afb594456b8215bd13a92e93de2c85..e0ca242da89d06005da692172a65a37377de68aa 100644
--- a/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java
+++ b/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java
@@ -60,6 +60,17 @@ public class FlyingPathNavigation extends PathNavigation {
if (!this.isDone()) {
Vec3 target = this.path.getNextEntityPos(this.mob);
2026-07-14 11:32:51 +08:00
+ // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
+ if (moe.monnairealms.camellia.config.modules.fixes.PathfindingFixesConfig.breakDownPathfindingWhenOutOfRegion) {
+ // we assume that:
+ // 1. The code above doesn't touch the 'main thread context' with the position from 'this.path'
+ // 2. The pathfinder could correctly recompute or discard the incorrect target position and this situation is happening rarely
+ if (!ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(this.mob.level(), target)) {
+ this.hasDelayedRecomputation = true;
+ return;
+ }
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
this.mob.getMoveControl().setWantedPosition(target.x, target.y, target.z, this.speedModifier);
}
}
diff --git a/net/minecraft/world/entity/ai/navigation/PathNavigation.java b/net/minecraft/world/entity/ai/navigation/PathNavigation.java
index 2ea1ec39a37899ae1510d0036aa670e758077537..18069d0b38ee08d798edc434875f6b5ca8bb15b5 100644
--- a/net/minecraft/world/entity/ai/navigation/PathNavigation.java
+++ b/net/minecraft/world/entity/ai/navigation/PathNavigation.java
@@ -188,6 +188,18 @@ public abstract class PathNavigation {
}
}
// Paper end - EntityPathfindEvent
2026-07-14 11:32:51 +08:00
+ // Shiroha start - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
+ if (moe.monnairealms.camellia.config.modules.fixes.PathfindingFixesConfig.doNotPathfindToNotOwnedTargets) {
+ // filter the targets not owned by current region
+ targets = new java.util.HashSet<>(targets); // well no idea about how to determine if this should be copied to a modifiable one
+ targets.removeIf(pos -> !ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(this.mob.level(), pos));
+
+ // return if no available (observe the logic in the first if block)
+ if (targets.isEmpty()) {
+ return null;
+ }
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha end
2026-07-09 00:03:09 +08:00
ProfilerFiller profiler = Profiler.get();
profiler.push("pathfind");
BlockPos fromPos = above ? this.mob.blockPosition().above() : this.mob.blockPosition();
diff --git a/net/minecraft/world/entity/animal/allay/AllayAi.java b/net/minecraft/world/entity/animal/allay/AllayAi.java
index c3667e7997551e0f5ce63bc6ee890082d1431400..95c93df76eeec7eb3f19380efb1e2876b0625b2e 100644
--- a/net/minecraft/world/entity/animal/allay/AllayAi.java
+++ b/net/minecraft/world/entity/animal/allay/AllayAi.java
@@ -112,6 +112,16 @@ public class AllayAi {
Optional<GlobalPos> likedNoteblockPos = brain.getMemory(MemoryModuleType.LIKED_NOTEBLOCK_POSITION);
if (likedNoteblockPos.isPresent()) {
GlobalPos position = likedNoteblockPos.get();
2026-07-14 11:32:51 +08:00
+ // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
+ final Level targetLevel = allay.level().getServer().getLevel(position.dimension());
+ final BlockPos targetPos = position.pos();
+
+ // thread checks
+ if (!ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(targetLevel, targetPos)) {
+ brain.eraseMemory(MemoryModuleType.LIKED_NOTEBLOCK_POSITION); // The memory value is not being belong to current tick region anymore
+ return Optional.empty();
+ }
2026-07-14 11:32:51 +08:00
+ // Shiroha
2026-07-09 00:03:09 +08:00
if (shouldDepositItemsAtLikedNoteblock(allay, brain, position)) {
return Optional.of(new BlockPosTracker(position.pos().above()));
}
diff --git a/net/minecraft/world/entity/animal/sniffer/Sniffer.java b/net/minecraft/world/entity/animal/sniffer/Sniffer.java
index d5394ae7ce56555ad21aeafb2d78c291c62e2988..c49b0be376f29d21e81680e48659483f0d84b6f1 100644
--- a/net/minecraft/world/entity/animal/sniffer/Sniffer.java
+++ b/net/minecraft/world/entity/animal/sniffer/Sniffer.java
@@ -279,8 +279,18 @@ public class Sniffer extends Animal {
private boolean canDig(final BlockPos position) {
return this.level().getBlockState(position).is(BlockTags.SNIFFER_DIGGABLE_BLOCK)
- && this.getExploredPositions().noneMatch(explored -> GlobalPos.of(this.level().dimension(), position).equals(explored))
- && Optional.ofNullable(this.getNavigation().createPath(position, 1)).map(Path::canReach).orElse(false);
2026-07-14 11:32:51 +08:00
+ && this.getExploredPositions().noneMatch(explored -> { // Shiroha - Fix region threading with entity ai data access
2026-07-09 00:03:09 +08:00
+ // thread checks
+ final Level targetLevel = net.minecraft.server.MinecraftServer.getServer().getLevel(explored.dimension());
+ final BlockPos targetPos = explored.pos();
+
+ if (!ca.spottedleaf.moonrise.common.util.TickThread.isTickThreadFor(targetLevel, targetPos)) {
+ return false;
+ }
+
+ return GlobalPos.of(this.level().dimension(), position).equals(explored); // Original logic
2026-07-14 11:32:51 +08:00
+ }) // Shiroha end
2026-07-09 00:03:09 +08:00
+ && Optional.ofNullable(this.getNavigation().createPath(position, 1)).map(Path::canReach).orElse(false);
}
private void dropSeed() {