[PR #6163] [MERGED] Native mods: a server's levels loading and unloading (LevelEvent) #6689

Closed
opened 2026-11-03 06:24:57 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/6163
Author: @levzzz5154
Created: 11/1/2026
Status: ✅ Merged
Merged: 11/1/2026
Merged by: @ludovic111

Base: main ← Head: mods/native-level-events


📝 Commits (1)

  • c60b4c8 Native mods: a server's levels loading and unloading (LevelEvent)

📊 Changes

5 files changed (+85 additions, -0 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+2 -0)
📝 crates/horizon-client/src/game/levels.rs (+36 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+22 -0)
📝 crates/horizon-client/src/game/native_lifecycle.rs (+5 -0)
➕ tools/native-hook-fragments/level_events.json (+20 -0)

📄 Description

🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Opus 5.5), not by levzzz5154 personally.

What and why

Fixes #6154, part of #4953 ("Level load and unload for each dimension (LevelEvent)"). A native mod that keeps state per dimension (a pipe network, a machine registry) had no point to set it up when a level comes and drop it when the level goes.

Two server hook points, API from a fragment (tools/native-hook-fragments/level_events.json):

  • native_level_load(dimension) (LevelEvent.Load): the level the world opens in, at the end of finish_open_world, before any of its chunks and before WorldOpenReady; every other level when enter_level opens it, with it in place.
  • native_level_unload(dimension) (LevelEvent.Unload): a level closed by close_idle_levels, after its chunks' unloads and its last save; at the server's stop every level still open, after the final save and before WorldCloseFinal, in Overworld, Nether, End order. A failed open or a failed server loop tells it too, before WorldOpenFailed / WorldCloseFailed.

Levels keeps the dimensions whose load the mods heard, so each load has exactly one unload whichever way the server ends. The deprecated in-frame path and clients of a server never fire them.

Kind

  • feature: something new for players (mod authors)

Vanilla reference

NeoForge's MinecraftServer patch posts LevelEvent.Load in createLevels (the Overworld's after its saved data is read, before setInitialSpawn; the others' right after levels.put) and LevelEvent.Unload after each level.close() in stopServer, after saveAllChunks. Vanilla opens every level at start; Horizon's server opens the Nether and End when someone first goes there and closes them after 300 idle ticks (docs/server-levels.md), so the hooks follow Horizon's levels. The client side of LevelEvent (a ClientLevel made or dropped) is left for another item.

Other projects' code

None.

Proof in the real game

Engine from this branch with throwaway eprintln!s in the hook bodies (not in this PR); the Nether visited, left for 22 s, then the End visited and the server stopped.

Server thread (horizon-ctl launch --world L --creative --offscreen):

TESTHOOK level_load minecraft:overworld tick=0
TESTHOOK first chunk_load minecraft:overworld [-2, 0]
TESTHOOK lifecycle WorldOpenReady
TESTHOOK level_load minecraft:the_nether tick=68
TESTHOOK first chunk_load minecraft:the_nether [0, 0]
TESTHOOK level_unload minecraft:the_nether tick=530
TESTHOOK level_load minecraft:the_end tick=671
TESTHOOK first chunk_load minecraft:the_end [0, 0]
TESTHOOK server_stopping
TESTHOOK level_unload minecraft:overworld tick=792
TESTHOOK level_unload minecraft:the_end tick=792
TESTHOOK lifecycle WorldCloseFinal

Dedicated server, a client connected, moved from the console with execute in … run tp Alex:

TESTHOOK level_load minecraft:overworld tick=0
TESTHOOK first chunk_load minecraft:overworld [0, 0]
[Server thread/INFO]: Done (0.898s)! For help, type "help"
TESTHOOK lifecycle WorldOpenReady
[Server] MARK nether
TESTHOOK level_load minecraft:the_nether tick=178
TESTHOOK first chunk_load minecraft:the_nether [0, 0]
[Server] MARK back
TESTHOOK level_unload minecraft:the_nether tick=638
[Server] MARK end
TESTHOOK level_load minecraft:the_end tick=779
TESTHOOK first chunk_load minecraft:the_end [0, 0]
[Server] MARK stop
TESTHOOK server_stopping
[Server thread/INFO]: Stopping server
TESTHOOK level_unload minecraft:overworld tick=900
TESTHOOK level_unload minecraft:the_end tick=900
TESTHOOK lifecycle WorldCloseFinal

The connected client printed nothing. Not run: a failed world open or a failed server loop (they take the same native_levels_closing call from the lifecycle's failure functions).

Self-check

pr_ready (tools/pr_ready.py --quick --base hr/main on cachyos-x8664, against hr/main at 975c07d91, 2 min 37 s)

Check Result
Scope ✅ 5 files, +85 −0
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 586 passed (32 s)
rustfmt ✅ (5 s)
clippy ✅ (1 min 27 s)
tests: horizon-client ✅ 1799 passed (32 s)
doc tests ✅ 0 passed (2 s)
content root tests ➖ content root untouched
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ not run (--quick): CI's parity job picks the affected harnesses

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/6163 **Author:** [@levzzz5154](https://github.com/levzzz5154) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `mods/native-level-events` --- ### 📝 Commits (1) - [`c60b4c8`](https://github.com/horizon-rust/horizon/commit/c60b4c8d46de05a261824cab0c6fd3a745cfdd57) Native mods: a server's levels loading and unloading (LevelEvent) ### 📊 Changes **5 files changed** (+85 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+2 -0) 📝 `crates/horizon-client/src/game/levels.rs` (+36 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+22 -0) 📝 `crates/horizon-client/src/game/native_lifecycle.rs` (+5 -0) ➕ `tools/native-hook-fragments/level_events.json` (+20 -0) </details> ### 📄 Description > 🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Opus 5.5), not by levzzz5154 personally. ## What and why Fixes #6154, part of #4953 ("Level load and unload for each dimension (`LevelEvent`)"). A native mod that keeps state per dimension (a pipe network, a machine registry) had no point to set it up when a level comes and drop it when the level goes. Two server hook points, API from a fragment (`tools/native-hook-fragments/level_events.json`): - `native_level_load(dimension)` (`LevelEvent.Load`): the level the world opens in, at the end of `finish_open_world`, before any of its chunks and before `WorldOpenReady`; every other level when `enter_level` opens it, with it in place. - `native_level_unload(dimension)` (`LevelEvent.Unload`): a level closed by `close_idle_levels`, after its chunks' unloads and its last save; at the server's stop every level still open, after the final save and before `WorldCloseFinal`, in Overworld, Nether, End order. A failed open or a failed server loop tells it too, before `WorldOpenFailed` / `WorldCloseFailed`. `Levels` keeps the dimensions whose load the mods heard, so each load has exactly one unload whichever way the server ends. The deprecated in-frame path and clients of a server never fire them. ## Kind - [x] feature: something new for players (mod authors) ## Vanilla reference NeoForge's `MinecraftServer` patch posts `LevelEvent.Load` in `createLevels` (the Overworld's after its saved data is read, before `setInitialSpawn`; the others' right after `levels.put`) and `LevelEvent.Unload` after each `level.close()` in `stopServer`, after `saveAllChunks`. Vanilla opens every level at start; Horizon's server opens the Nether and End when someone first goes there and closes them after 300 idle ticks (docs/server-levels.md), so the hooks follow Horizon's levels. The client side of `LevelEvent` (a `ClientLevel` made or dropped) is left for another item. ## Other projects' code None. ## Proof in the real game Engine from this branch with throwaway `eprintln!`s in the hook bodies (not in this PR); the Nether visited, left for 22 s, then the End visited and the server stopped. Server thread (`horizon-ctl launch --world L --creative --offscreen`): ``` TESTHOOK level_load minecraft:overworld tick=0 TESTHOOK first chunk_load minecraft:overworld [-2, 0] TESTHOOK lifecycle WorldOpenReady TESTHOOK level_load minecraft:the_nether tick=68 TESTHOOK first chunk_load minecraft:the_nether [0, 0] TESTHOOK level_unload minecraft:the_nether tick=530 TESTHOOK level_load minecraft:the_end tick=671 TESTHOOK first chunk_load minecraft:the_end [0, 0] TESTHOOK server_stopping TESTHOOK level_unload minecraft:overworld tick=792 TESTHOOK level_unload minecraft:the_end tick=792 TESTHOOK lifecycle WorldCloseFinal ``` Dedicated server, a client connected, moved from the console with `execute in … run tp Alex`: ``` TESTHOOK level_load minecraft:overworld tick=0 TESTHOOK first chunk_load minecraft:overworld [0, 0] [Server thread/INFO]: Done (0.898s)! For help, type "help" TESTHOOK lifecycle WorldOpenReady [Server] MARK nether TESTHOOK level_load minecraft:the_nether tick=178 TESTHOOK first chunk_load minecraft:the_nether [0, 0] [Server] MARK back TESTHOOK level_unload minecraft:the_nether tick=638 [Server] MARK end TESTHOOK level_load minecraft:the_end tick=779 TESTHOOK first chunk_load minecraft:the_end [0, 0] [Server] MARK stop TESTHOOK server_stopping [Server thread/INFO]: Stopping server TESTHOOK level_unload minecraft:overworld tick=900 TESTHOOK level_unload minecraft:the_end tick=900 TESTHOOK lifecycle WorldCloseFinal ``` The connected client printed nothing. Not run: a failed world open or a failed server loop (they take the same `native_levels_closing` call from the lifecycle's failure functions). ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base hr/main` on cachyos-x8664, against hr/main at 975c07d91, 2 min 37 s) | Check | Result | | --- | --- | | Scope | ✅ 5 files, +85 −0 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 586 passed (32 s) | | rustfmt | ✅ (5 s) | | clippy | ✅ (1 min 27 s) | | tests: horizon-client | ✅ 1799 passed (32 s) | | doc tests | ✅ 0 passed (2 s) | | content root tests | ➖ content root untouched | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses | 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:24:57 +07:00
Sign in to join this conversation.
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
horizon-rust/horizon#6689
No description provided.