Fix villager brain initialization before destination world is applied during async portal teleport #446

Open
nekoneko2872 wants to merge 1 commit from nekoneko2872/ver/1.21.11-fixed into ver/1.21.11
nekoneko2872 commented 2026-03-15 11:15:11 +01:00 (Migrated from github.com)

Summary

This PR fixes a Folia villager Nether portal bug where villager AI could be initialized before the destination world was fully applied during async portal teleportation.

As a result, Villager.makeBrain() and Brain.updateActivityFromSchedule() could run while the villager was still in a temporary world-mismatch state.

What changed

  • Deferred villager brain activity initialization when the entity is being restored through async teleport.
  • Prevented the initial schedule-based brain update from running before the destination world is actually available.
  • Added a retry path so that if the villager still has a world mismatch, brain initialization is attempted again on the next tick.
  • Rechecked the deferred brain initialization after dimension change so AI can resume as soon as the world switch is complete.

How it works

  • Entity.transformForAsyncTeleport(...) now exposes the destination world context during async teleport restore.
  • Villager.makeBrain() detects that async teleport destination context and marks brain initialization as deferred.
  • The initial Brain.updateActivityFromSchedule(...) call is skipped while the villager is still waiting for the destination world to be fully applied.
  • During the villager AI tick, deferred initialization is retried until the villager is in the expected world and fully added.
  • Once the world switch is complete, the normal brain schedule update is applied and AI resumes normally.

Root cause

The async portal flow could reach the villager brain setup path in this order:

netherPortalLogicAsync -> portalToAsync -> Entity.transformForAsyncTeleport -> Villager.makeBrain -> Brain.updateActivityFromSchedule

At that point, the destination world was not always fully reflected on the entity yet, which made villager AI initialization observe the wrong world state.

Result

With this change, villager brain initialization no longer runs before the destination world is applied, and temporary world mismatches are handled safely by deferring initialization until a later tick.

Testing

  • Verified server compilation successfully.
  • Confirmed the fix is limited to the async teleport restore path and villager brain initialization timing.
## Summary This PR fixes a Folia villager Nether portal bug where villager AI could be initialized before the destination world was fully applied during async portal teleportation. As a result, `Villager.makeBrain()` and `Brain.updateActivityFromSchedule()` could run while the villager was still in a temporary world-mismatch state. ## What changed - Deferred villager brain activity initialization when the entity is being restored through async teleport. - Prevented the initial schedule-based brain update from running before the destination world is actually available. - Added a retry path so that if the villager still has a world mismatch, brain initialization is attempted again on the next tick. - Rechecked the deferred brain initialization after dimension change so AI can resume as soon as the world switch is complete. ## How it works - `Entity.transformForAsyncTeleport(...)` now exposes the destination world context during async teleport restore. - `Villager.makeBrain()` detects that async teleport destination context and marks brain initialization as deferred. - The initial `Brain.updateActivityFromSchedule(...)` call is skipped while the villager is still waiting for the destination world to be fully applied. - During the villager AI tick, deferred initialization is retried until the villager is in the expected world and fully added. - Once the world switch is complete, the normal brain schedule update is applied and AI resumes normally. ## Root cause The async portal flow could reach the villager brain setup path in this order: `netherPortalLogicAsync -> portalToAsync -> Entity.transformForAsyncTeleport -> Villager.makeBrain -> Brain.updateActivityFromSchedule` At that point, the destination world was not always fully reflected on the entity yet, which made villager AI initialization observe the wrong world state. ## Result With this change, villager brain initialization no longer runs before the destination world is applied, and temporary world mismatches are handled safely by deferring initialization until a later tick. ## Testing - Verified server compilation successfully. - Confirmed the fix is limited to the async teleport restore path and villager brain initialization timing.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin nekoneko2872/ver/1.21.11-fixed:nekoneko2872/ver/1.21.11-fixed
git switch nekoneko2872/ver/1.21.11-fixed

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch ver/1.21.11
git merge --no-ff nekoneko2872/ver/1.21.11-fixed
git switch nekoneko2872/ver/1.21.11-fixed
git rebase ver/1.21.11
git switch ver/1.21.11
git merge --ff-only nekoneko2872/ver/1.21.11-fixed
git switch nekoneko2872/ver/1.21.11-fixed
git rebase ver/1.21.11
git switch ver/1.21.11
git merge --no-ff nekoneko2872/ver/1.21.11-fixed
git switch ver/1.21.11
git merge --squash nekoneko2872/ver/1.21.11-fixed
git switch ver/1.21.11
git merge --ff-only nekoneko2872/ver/1.21.11-fixed
git switch ver/1.21.11
git merge nekoneko2872/ver/1.21.11-fixed
git push origin ver/1.21.11
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Minecraft/Folia!446
No description provided.