[PR #6216] [MERGED] Typed native mod API: generated Hooks and Host, injections, attachments, dependencies, renderer #6724

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

📋 Pull Request Information

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

Base: main ← Head: v2/ses_01M6BECNQ499TYMMBQBDF00VJ5-mod-api


📝 Commits (10+)

  • 02122e2 Mods: one .hmod for content and native code (WIP, #5865)
  • df6e0f0 Mods: keep publishing .hnmod, write the mod file format down
  • 2aa6aa2 Merge remote-tracking branch 'upstream/main' into staging/unified-hmod
  • cb11c1d Block states: drop a needless borrow clippy 1.99 refuses
  • 18a02fe Merge native/api-mods (#5446) into staging/unified-hmod
  • 5482b97 Merge hr/main into the mod API rework (#5446)
  • 3f3bdfa Merge branch 'pr-5909' into mod-api-rework
  • aef97f1 Merge upstream/main into staging/unified-hmod
  • ddb317c Merge branch 'pr-5909-new' into mod-api-rework
  • 0f8b7db Merge upstream main (308ef65272) into mod-api-rework

📊 Changes

76 files changed (+11236 additions, -667 deletions)

View changed files

📝 .gitattributes (+5 -0)
📝 Cargo.lock (+3 -0)
📝 crates/horizon-client-render/src/block_entities.rs (+95 -1)
📝 crates/horizon-client-render/src/chunk.rs (+4 -0)
📝 crates/horizon-client-render/src/gfx/native_render.rs (+55 -3)
➕ crates/horizon-client-render/src/gfx/native_render/generated.rs (+36 -0)
📝 crates/horizon-client-render/src/streaming.rs (+7 -0)
📝 crates/horizon-client-render/src/world.rs (+3 -0)
📝 crates/horizon-client/src/entry.rs (+35 -1)
📝 crates/horizon-client/src/game.rs (+23 -4)
📝 crates/horizon-client/src/game/creatures.rs (+2 -1)
📝 crates/horizon-client/src/game/dispenser.rs (+2 -4)
📝 crates/horizon-client/src/game/end.rs (+2 -2)
📝 crates/horizon-client/src/game/lan.rs (+11 -3)
📝 crates/horizon-client/src/game/lan_actions.rs (+15 -0)
📝 crates/horizon-client/src/game/leads.rs (+2 -1)
📝 crates/horizon-client/src/game/levels.rs (+5 -0)
➕ crates/horizon-client/src/game/mod_api_host.rs (+227 -0)
➕ crates/horizon-client/src/game/mod_api_host_generated.rs (+344 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+344 -309)

...and 56 more files

📄 Description

🤖 Opened by Aria's agent swarm, Dyna, not by Aria.

What this is

A typed native mod API, following the design in #5638. A mod implements the generated
horizon_client::mod_api::Hooks trait and calls the game back through a generated Host. The launcher links trait
mods into the thin executable through entry::main_with. The game is patched only for bundles that still carry
mixins, so editing a trait mod rebuilds in seconds.

Closes #5638.

Overview: a mod implements the generated Hooks trait; the launcher checks dependencies, applies mixins only where present and links trait mods into the game; 108 hooks in four contracts and 78 Host helpers

Built on mod-api-rework. The branch starts from mod-api-rework (ddb317c771): #5446's library split plus
#5909's one-.hmod layout (#5865), with their commits and authorship kept.

  • Merging this lands them too. If #5446 merges first, this diff shrinks to the six API commits.
  • How it fits:
    • #5446: mod_api and main_with stay in horizon-client, so mods reach horizon-client-lib only through it.
    • #5909: a trait mod ships in the same .hmod, as horizon-native.json plus mixins/<id>/ with [api].
  • Unchanged: this PR adds no hook points and changes no catalog entry or API number. Existing mixin mods are
    unaffected.

Reading order: start at commit 2

Commits before 0f8b7dbdd0 are mod-api-rework (#5446, #5909) as levzzz pushed it. This PR's own work is the six commits from 93b36334e2 to 21a8f529c0: compare them on their own.

# Commit Hand-written Generated Catalog JSON
1 0f8b7dbdd0 Merge upstream main 308ef65272 into mod-api-rework: the main this API was built on (world_drawn_hooked and others). Only lib.rs conflicted, resolved the split's way — — —
2 93b36334e2 Hooks trait, a contract for every catalog hook, launcher linking +2,229/−512 +3,081 +427/−105
3 eb6b666426 Generated Host for the documented Game helpers +469/−112 +745 +75
4 1b1610facd [[injection]] into a mod's own Hooks, a patched-source cache; only mixin bundles reach horizon-mixins +670/−15 — —
5 fb17636912 Attachments: server-owned mod data on players, entities, block entities, chunks and levels +1,037/−15 +5 +1
6 2b91e83a48 Dependencies between trait mods +546/−14 — —
7 21a8f529c0 Renderer hooks (RenderHooks), block-entity drawing through Host +169/−16 +168 +10/−3
8 0813600988 Merge current main 975c07d915: block_entities.rs keeps both sides; ram_limit and block_edit go into client_lib.rs and entry.rs main's own changes, plus the ram_limit start and import in entry.rs and two lines in client_lib.rs +6/−3 —
9 4290465dbe Mark the mod API's generated files as generated (.gitattributes only) +5 — —
10 246f47f20d Hooks take one event struct (see Hooks take one event struct below); d7b307aed5 before it merges main 3310fcafb9, including #6462 +441/−90 +1,442/−889 —
The six features: Hooks, Host, injections, attachments, dependencies, renderer

Commit 2, file by file:

  1. xtask/src/mod_api.rs: the generator;
  2. crates/horizon-client/src/mod_api.rs: the hand-written types and re-exports;
  3. native-hooks.json: one mod_api contract per hook;
  4. the dispatch lines in game/native_hooks.rs;
  5. entry.rs;
  6. launcher/src/native_mods/api.rs.

Every commit from 2 on builds on its own, and passes xtask mod-api --check and cargo check --all-targets for
the client and the launcher.

Generated, so you can skip them (marked linguist-generated in .gitattributes, so GitHub collapses them in the diff; they are about 4k of the added lines):

  • crates/horizon-client/src/mod_api/generated.rs;
  • crates/horizon-client/src/mod_api/generated_host.rs;
  • crates/horizon-client/src/game/mod_api_host_generated.rs;
  • crates/horizon-client-render/src/gfx/native_render/generated.rs.

cargo run -p xtask -- mod-api --check fails if any of them differs from the generator's output. pr_ready.py
runs that check whenever native-hooks.json, native_hooks.rs or xtask/ changes.

Contracts: each hook has one of four (108 in all: 54 listen, 20 cancel, 18 collect, 16 replace).

  • Cancel stops at the first mod that cancels.
  • Replace takes the first Some.
  • Collect passes the value to every mod in order.

Every hook keeps its receiver's access: &Game or &mut Game, and level hosts by mutability.

Hooks take one event struct. Each trait hook method takes its host, where it has one, plus one generated #[non_exhaustive] event, named after the catalog hook in CamelCase plus Event, whose public fields are the hook point's arguments. A later version can add a field without breaking any mod, as NeoForge's event objects do.

fn use_item_on(&self, host: &mut dyn Host, event: &mut UseItemOnEvent<'_>) -> Flow
  • Fields: values are plain fields and borrowed arguments are references (event.stack). A number or bool a mod may change is a plain field (event.amount *= 0.5), written back once every mod has run, even after a Cancel. Anything else a mod may change, such as a game struct or a list, is a &mut field. One event goes through every mod, so each mod sees earlier changes.
  • Unchanged: cancel and replace answer with the return value; a collect hook's list is a field (event.lines). The native_* hook points, catalog contracts, API numbers and dispatch_* signatures don't change.
  • Trait injections: their bindings become the fields of Inject<ModId><Method>Event.
  • Not covered yet: a mod can't build an event in its own tests; see the test Host item in #6452.
  • Types that can grow: the mod API's enums and the structs a mod is handed are #[non_exhaustive] (610db0d288), so a later variant or field never breaks a mod; Flow and the structs mods build themselves stay open.

Not covered:

  • Pending hook fragments: a small follow-up PR adds their contracts. A fragment carries its mod_api key, and
    the stamp tool keeps it.
  • Helpers using gameplay-private types or generics: docs/native-mods.md lists each one with the reason.

NeoForge counterparts

Every trait method maps an existing catalog hook. The PR that added each hook named its counterpart; the catalog's
neoforge key records two (server_about_to_start, server_stopping). The new concepts follow NeoForge 26.3's
public javadoc or docs, never its source:

  • Attachments follow AttachmentType.Builder: the default supplier, optional serialization and
    copy-on-death. They don't include holder-aware suppliers, custom copy handlers, conditional serialization or sync.
  • Dependencies follow neoforge.mods.toml [[dependencies]]: required or optional, with version ranges.
  • RenderHooks::world_drawn matches the after-level stage of RenderLevelStageEvent.
  • Host and injections have no counterpart; they're Horizon-specific.

In-game proof

The PR's commits up to and including commit 7 equal fork mod-api 4c3ccc0371, except for a fork-only CONTRIBUTING
note, rustfmt line wrapping, and native_attachments moved from the hook points into the helpers block (the catalog test caught it). Commit 8 merges current main. The proofs ran on that fork at the heads listed.

The launcher built and installed the game; horizon-ctl launched the installed build (the launcher's own Play needs a Microsoft sign-in).

Check Fork head Result Time
Event structs: listen hook and scalar write-back, server thread 246f47f20d PASS: level_tick_pre logged the 200-tick clock line; a throwaway living_incoming_damage halved event.amount for cows and cancelled pigs: after /damage <cow> 4 the cow read Health 8.0, and the pig stayed at 10.0 —
Event structs: cancel hook, server thread (ported machine sample) 246f47f20d PASS: sneaking with a stick on the machine put [server] read #1 on the action bar and MACHINE_SAMPLE server read #1 in the log, through use_item_on —
Event structs: trait mod on a dedicated server through native-build --server-output (#6462) 246f47f20d PASS: the launcher built the engine with --no-default-features; horizon-server ran it headless on 127.0.0.1 (Starting Minecraft server on 127.0.0.1:25565); the trait hook logged MACHINE_SAMPLE server clock: 200 ticks in minecraft:overworld; clean shutdown —
Trait mod on the server thread: tick, cancel, screen hook (API-only bundle) 41ddf1689d PASS: SYNC2 level_tick_pre minecraft:overworld, SYNC2 use_item_on canceled [-25, 67, -1]; a separate read of the target block returned minecraft:air; SYNC2 screen_render_post creative launcher build 309 s
Host on the server thread and on a dedicated server 41ddf1689d PASS on both: HOST_PROOF before=minecraft:stone changed=true after=minecraft:air diamonds=1 message=true; separate reads: block air, one diamond, action bar Host proof: block removed, diamond given first native build 7 min 23 s
Attachments, server thread 41ddf1689d PASS: after a save and reopen, level=Ok(1) chunk=Ok(1) block=Ok(1) memory=Ok(0); after death, player_keep=Some(Ok(2)) player_drop=Some(Ok(0)); a cow's value goes 0 → 1, then 2 after a reopen incremental 14 s
Attachments, dedicated server 41ddf1689d PASS: after a clean stop (players, worlds and chunks saved), the reopen read the same values; the cow's value survived; a round trip through a build without the attachment mod read 2 everywhere, and the playerdata horizon:attachments bytes matched exactly —
HEAD injections, ordinary and trait 41ddf1689d PASS: INJECT_PROOF ordinary HEAD fired, then INJECT_PROOF trait HEAD fired —
Cancellable trait injection beside an API-only bundle 4c3ccc0371 PASS: INJECT_PROOF cancel HEAD fired; catalog server tick fired never appeared through tick 300 3 min 39 s
Edit loop: one line in a trait mod 41ddf1689d PASS: Reusing patched game source, and the new line printed 6.6 s
Edit loop: one line in a mixin 41ddf1689d PASS: Applying the mixins, and the new line printed 1 min 46 s
Dependencies: order and optional feature 41ddf1689d PASS: user before base in folder order, but base dispatched first, counter=1 extra=off; with the extra mod, extra=on 6.9 s and 7.6 s rebuilds
Renderer hooks beside the machine sample 4c3ccc0371 PASS: magenta square and stone cube above the machine; render-proof: render_needs, render-proof: world_drawn, render-proof: block_entity_submit; output chest {"count":4,"item":"minecraft:stone"} 122 s
Machine sample, mixin bundle 4c3ccc0371 PASS: output chest Items: [{count: 4, Slot: 0b, id: "minecraft:stone"}] 14 s
Machine sample, trait bundle alone 4c3ccc0371 PARTIAL: only the clock was checked, MACHINE_SAMPLE server clock: 200 ticks in minecraft:overworld; the stick read, zoom and tooltip weren't rerun at this head 124 s
Machine sample, trait and mixin bundles together 4c3ccc0371 PASS for the machine and the clock: output chest Items: [{count: 4, Slot: 0b, id: "minecraft:stone"}], and MACHINE_SAMPLE server clock: 200 ticks in minecraft:overworld 226 s
One folder with native code and content, laid out as #5909 (unpacked) 41ddf1689d PASS: the launcher built it (13 s); its hooks ran; Horizon: content from 1 mod(s); /give of its item worked. Packed .hmod reading isn't part of this PR 13 s
Crate graph 41ddf1689d PASS: the generated game and the mod depend on horizon-client; neither depends on horizon-client-lib directly —
In-game proofs: render hooks beside the machine sample, and the Host proof Rebuild time after an edit: trait mod 6.6 s, mod order 6.9 s, optional dependency 7.6 s, mixin edit 1 min 46 s, first build 7 min 21 s

Test route: https://github.com/arismoko/horizon-machine-sample (used)

Self-check

python3 tools/pr_ready.py --quick --base upstream/main at 0813600988:

pr_ready: 172 files changed since upstream/main; checks on local
  Public tree: pass (0 s)
  Python tests: pass (31 s)
  rustfmt: pass (7 s)
  clippy: pass (14 s)
  tests: workspace: fail (37 s)
    test result: FAILED. 550 passed; 2 failed; 4 ignored
    game::tnt::tests::a_soul_sand_bubble_column_lifts_primed_tnt
    game::tnt::tests::primed_tnt_follows_water_currents_after_moving
  doc tests: pass (16 s)
  content root tests: pass (3 min 7 s)
  mod API up to date: pass (6 s)
  tokens.css up to date: pass (0 s)
  launcher rustfmt: pass (1 s)
  launcher clippy: pass (1 min 37 s)
  launcher tests: stopped before it finished
  • The two TNT failures also fail on main 975c07d915 when the vanilla data is present (#6046's tests), so this PR doesn't cause them.
  • The launcher tests were stopped here. Every commit passes cargo check --tests for the launcher.
  • Trial merge into main 1bd24e4042 is clean; xtask mod-api --check passes on it. Upstream CI covers the rest.

Keeping this current: if main moves, merge it with a merge commit, never a rebase. Then run
cargo run -p xtask -- mod-api, and the checks that cover what changed. The generated files regenerate, so they
never need a hand merge.

Please merge with a merge commit rather than squash, or merge this branch into #5446, so commit authorship is kept.

🤖 Generated with Dyna.


🔄 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/6216 **Author:** [@arismoko](https://github.com/arismoko) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `v2/ses_01M6BECNQ499TYMMBQBDF00VJ5-mod-api` --- ### 📝 Commits (10+) - [`02122e2`](https://github.com/horizon-rust/horizon/commit/02122e253272fe730356ef4d9ad32729b293a62e) Mods: one .hmod for content and native code (WIP, #5865) - [`df6e0f0`](https://github.com/horizon-rust/horizon/commit/df6e0f0547b9c45afde1d5350b7ad426110098ec) Mods: keep publishing .hnmod, write the mod file format down - [`2aa6aa2`](https://github.com/horizon-rust/horizon/commit/2aa6aa28cd5bfcec9cd639c51137d3853c062c47) Merge remote-tracking branch 'upstream/main' into staging/unified-hmod - [`cb11c1d`](https://github.com/horizon-rust/horizon/commit/cb11c1d221d972f79037aba789e48a03b44b47a6) Block states: drop a needless borrow clippy 1.99 refuses - [`18a02fe`](https://github.com/horizon-rust/horizon/commit/18a02fe574448a4d43b30975d66fa75ee6394aa9) Merge native/api-mods (#5446) into staging/unified-hmod - [`5482b97`](https://github.com/horizon-rust/horizon/commit/5482b975fbe86220b6cc2ed6788d167268d8b310) Merge hr/main into the mod API rework (#5446) - [`3f3bdfa`](https://github.com/horizon-rust/horizon/commit/3f3bdfa663a0e4e7762868327320f96d34a6b0b6) Merge branch 'pr-5909' into mod-api-rework - [`aef97f1`](https://github.com/horizon-rust/horizon/commit/aef97f1e437b1cfffa7e918fb95e3bb6c606934b) Merge upstream/main into staging/unified-hmod - [`ddb317c`](https://github.com/horizon-rust/horizon/commit/ddb317c7712739d48dc6407a18d83fe35dcd2265) Merge branch 'pr-5909-new' into mod-api-rework - [`0f8b7db`](https://github.com/horizon-rust/horizon/commit/0f8b7dbdd05748b275a53380cab22c8c3ce7635c) Merge upstream main (308ef65272) into mod-api-rework ### 📊 Changes **76 files changed** (+11236 additions, -667 deletions) <details> <summary>View changed files</summary> 📝 `.gitattributes` (+5 -0) 📝 `Cargo.lock` (+3 -0) 📝 `crates/horizon-client-render/src/block_entities.rs` (+95 -1) 📝 `crates/horizon-client-render/src/chunk.rs` (+4 -0) 📝 `crates/horizon-client-render/src/gfx/native_render.rs` (+55 -3) ➕ `crates/horizon-client-render/src/gfx/native_render/generated.rs` (+36 -0) 📝 `crates/horizon-client-render/src/streaming.rs` (+7 -0) 📝 `crates/horizon-client-render/src/world.rs` (+3 -0) 📝 `crates/horizon-client/src/entry.rs` (+35 -1) 📝 `crates/horizon-client/src/game.rs` (+23 -4) 📝 `crates/horizon-client/src/game/creatures.rs` (+2 -1) 📝 `crates/horizon-client/src/game/dispenser.rs` (+2 -4) 📝 `crates/horizon-client/src/game/end.rs` (+2 -2) 📝 `crates/horizon-client/src/game/lan.rs` (+11 -3) 📝 `crates/horizon-client/src/game/lan_actions.rs` (+15 -0) 📝 `crates/horizon-client/src/game/leads.rs` (+2 -1) 📝 `crates/horizon-client/src/game/levels.rs` (+5 -0) ➕ `crates/horizon-client/src/game/mod_api_host.rs` (+227 -0) ➕ `crates/horizon-client/src/game/mod_api_host_generated.rs` (+344 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+344 -309) _...and 56 more files_ </details> ### 📄 Description > 🤖 Opened by Aria's agent swarm, Dyna, not by Aria. ## What this is A typed native mod API, following the design in #5638. A mod implements the generated `horizon_client::mod_api::Hooks` trait and calls the game back through a generated `Host`. The launcher links trait mods into the thin executable through `entry::main_with`. The game is patched only for bundles that still carry mixins, so editing a trait mod rebuilds in seconds. Closes #5638. <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/arismoko/horizon/0e4a4c62545b79220d1364887f25c25c00c77230/mod-api-overview-v2-dark.png"> <img src="https://raw.githubusercontent.com/arismoko/horizon/0e4a4c62545b79220d1364887f25c25c00c77230/mod-api-overview-v2-light.png" alt="Overview: a mod implements the generated Hooks trait; the launcher checks dependencies, applies mixins only where present and links trait mods into the game; 108 hooks in four contracts and 78 Host helpers"> </picture> **Built on `mod-api-rework`.** The branch starts from `mod-api-rework` (ddb317c771): #5446's library split plus #5909's one-`.hmod` layout (#5865), with their commits and authorship kept. - **Merging this lands them too.** If #5446 merges first, this diff shrinks to the six API commits. - **How it fits:** - **#5446:** `mod_api` and `main_with` stay in `horizon-client`, so mods reach `horizon-client-lib` only through it. - **#5909:** a trait mod ships in the same `.hmod`, as `horizon-native.json` plus `mixins/<id>/` with `[api]`. - **Unchanged:** this PR adds no hook points and changes no catalog entry or API number. Existing mixin mods are unaffected. ## Reading order: start at commit 2 Commits before [0f8b7dbdd0](https://github.com/arismoko/horizon/commit/0f8b7dbdd0) are `mod-api-rework` (#5446, #5909) as levzzz pushed it. This PR's own work is the six commits from [93b36334e2](https://github.com/arismoko/horizon/commit/93b36334e2) to [21a8f529c0](https://github.com/arismoko/horizon/commit/21a8f529c0): [compare them on their own](https://github.com/arismoko/horizon/compare/0f8b7dbdd0...21a8f529c0). | # | Commit | Hand-written | Generated | Catalog JSON | |---|---|---|---|---| | 1 | 0f8b7dbdd0 Merge upstream main 308ef65272 into `mod-api-rework`: the main this API was built on (`world_drawn_hooked` and others). Only `lib.rs` conflicted, resolved the split's way | — | — | — | | 2 | 93b36334e2 `Hooks` trait, a contract for every catalog hook, launcher linking | +2,229/−512 | +3,081 | +427/−105 | | 3 | eb6b666426 Generated `Host` for the documented `Game` helpers | +469/−112 | +745 | +75 | | 4 | 1b1610facd `[[injection]]` into a mod's own `Hooks`, a patched-source cache; only mixin bundles reach horizon-mixins | +670/−15 | — | — | | 5 | fb17636912 Attachments: server-owned mod data on players, entities, block entities, chunks and levels | +1,037/−15 | +5 | +1 | | 6 | 2b91e83a48 Dependencies between trait mods | +546/−14 | — | — | | 7 | 21a8f529c0 Renderer hooks (`RenderHooks`), block-entity drawing through `Host` | +169/−16 | +168 | +10/−3 | | 8 | 0813600988 Merge current main 975c07d915: `block_entities.rs` keeps both sides; `ram_limit` and `block_edit` go into `client_lib.rs` and `entry.rs` | main's own changes, plus the `ram_limit` start and import in `entry.rs` and two lines in `client_lib.rs` | +6/−3 | — | | 9 | 4290465dbe Mark the mod API's generated files as generated (`.gitattributes` only) | +5 | — | — | | 10 | 246f47f20d Hooks take one event struct (see **Hooks take one event struct** below); d7b307aed5 before it merges main 3310fcafb9, including #6462 | +441/−90 | +1,442/−889 | — | <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/arismoko/horizon/0e4a4c62545b79220d1364887f25c25c00c77230/mod-api-features-v2-dark.png"> <img src="https://raw.githubusercontent.com/arismoko/horizon/0e4a4c62545b79220d1364887f25c25c00c77230/mod-api-features-v2-light.png" alt="The six features: Hooks, Host, injections, attachments, dependencies, renderer"> </picture> **Commit 2, file by file:** 1. `xtask/src/mod_api.rs`: the generator; 2. `crates/horizon-client/src/mod_api.rs`: the hand-written types and re-exports; 3. `native-hooks.json`: one `mod_api` contract per hook; 4. the dispatch lines in `game/native_hooks.rs`; 5. `entry.rs`; 6. `launcher/src/native_mods/api.rs`. Every commit from 2 on builds on its own, and passes `xtask mod-api --check` and `cargo check --all-targets` for the client and the launcher. **Generated, so you can skip them** (marked `linguist-generated` in `.gitattributes`, so GitHub collapses them in the diff; they are about 4k of the added lines): - `crates/horizon-client/src/mod_api/generated.rs`; - `crates/horizon-client/src/mod_api/generated_host.rs`; - `crates/horizon-client/src/game/mod_api_host_generated.rs`; - `crates/horizon-client-render/src/gfx/native_render/generated.rs`. `cargo run -p xtask -- mod-api --check` fails if any of them differs from the generator's output. `pr_ready.py` runs that check whenever `native-hooks.json`, `native_hooks.rs` or `xtask/` changes. **Contracts:** each hook has one of four (108 in all: 54 listen, 20 cancel, 18 collect, 16 replace). - **Cancel** stops at the first mod that cancels. - **Replace** takes the first `Some`. - **Collect** passes the value to every mod in order. Every hook keeps its receiver's access: `&Game` or `&mut Game`, and level hosts by mutability. **Hooks take one event struct.** Each trait hook method takes its host, where it has one, plus one generated `#[non_exhaustive]` event, named after the catalog hook in CamelCase plus `Event`, whose public fields are the hook point's arguments. A later version can add a field without breaking any mod, as NeoForge's event objects do. ```rust fn use_item_on(&self, host: &mut dyn Host, event: &mut UseItemOnEvent<'_>) -> Flow ``` - **Fields:** values are plain fields and borrowed arguments are references (`event.stack`). A number or `bool` a mod may change is a plain field (`event.amount *= 0.5`), written back once every mod has run, even after a `Cancel`. Anything else a mod may change, such as a game struct or a list, is a `&mut` field. One event goes through every mod, so each mod sees earlier changes. - **Unchanged:** `cancel` and `replace` answer with the return value; a `collect` hook's list is a field (`event.lines`). The `native_*` hook points, catalog contracts, API numbers and `dispatch_*` signatures don't change. - **Trait injections:** their bindings become the fields of `Inject<ModId><Method>Event`. - **Not covered yet:** a mod can't build an event in its own tests; see the test `Host` item in #6452. - **Types that can grow:** the mod API's enums and the structs a mod is handed are `#[non_exhaustive]` (610db0d288), so a later variant or field never breaks a mod; `Flow` and the structs mods build themselves stay open. **Not covered:** - **Pending hook fragments:** a small follow-up PR adds their contracts. A fragment carries its `mod_api` key, and the stamp tool keeps it. - **Helpers using gameplay-private types or generics:** `docs/native-mods.md` lists each one with the reason. ## NeoForge counterparts Every trait method maps an existing catalog hook. The PR that added each hook named its counterpart; the catalog's `neoforge` key records two (`server_about_to_start`, `server_stopping`). The new concepts follow NeoForge 26.3's public javadoc or docs, never its source: - **Attachments** follow `AttachmentType.Builder`: the default supplier, optional serialization and copy-on-death. They don't include holder-aware suppliers, custom copy handlers, conditional serialization or sync. - **Dependencies** follow `neoforge.mods.toml` `[[dependencies]]`: required or optional, with version ranges. - **`RenderHooks::world_drawn`** matches the after-level stage of `RenderLevelStageEvent`. - **`Host` and injections** have no counterpart; they're Horizon-specific. ## In-game proof The PR's commits up to and including commit 7 equal fork `mod-api` 4c3ccc0371, except for a fork-only CONTRIBUTING note, rustfmt line wrapping, and `native_attachments` moved from the hook points into the helpers block (the catalog test caught it). Commit 8 merges current main. The proofs ran on that fork at the heads listed. The launcher built and installed the game; horizon-ctl launched the installed build (the launcher's own Play needs a Microsoft sign-in). | Check | Fork head | Result | Time | |---|---|---|---| | Event structs: listen hook and scalar write-back, server thread | 246f47f20d | PASS: `level_tick_pre` logged the 200-tick clock line; a throwaway `living_incoming_damage` halved `event.amount` for cows and cancelled pigs: after `/damage <cow> 4` the cow read Health 8.0, and the pig stayed at 10.0 | — | | Event structs: cancel hook, server thread (ported machine sample) | 246f47f20d | PASS: sneaking with a stick on the machine put `[server] read #1` on the action bar and `MACHINE_SAMPLE server read #1` in the log, through `use_item_on` | — | | Event structs: trait mod on a dedicated server through `native-build --server-output` (#6462) | 246f47f20d | PASS: the launcher built the engine with `--no-default-features`; `horizon-server` ran it headless on 127.0.0.1 (`Starting Minecraft server on 127.0.0.1:25565`); the trait hook logged `MACHINE_SAMPLE server clock: 200 ticks in minecraft:overworld`; clean shutdown | — | | Trait mod on the server thread: tick, cancel, screen hook (API-only bundle) | 41ddf1689d | PASS: `SYNC2 level_tick_pre minecraft:overworld`, `SYNC2 use_item_on canceled [-25, 67, -1]`; a separate read of the target block returned `minecraft:air`; `SYNC2 screen_render_post creative` | launcher build 309 s | | `Host` on the server thread and on a dedicated server | 41ddf1689d | PASS on both: `HOST_PROOF before=minecraft:stone changed=true after=minecraft:air diamonds=1 message=true`; separate reads: block air, one diamond, action bar `Host proof: block removed, diamond given` | first native build 7 min 23 s | | Attachments, server thread | 41ddf1689d | PASS: after a save and reopen, `level=Ok(1) chunk=Ok(1) block=Ok(1) memory=Ok(0)`; after death, `player_keep=Some(Ok(2)) player_drop=Some(Ok(0))`; a cow's value goes 0 → 1, then 2 after a reopen | incremental 14 s | | Attachments, dedicated server | 41ddf1689d | PASS: after a clean `stop` (players, worlds and chunks saved), the reopen read the same values; the cow's value survived; a round trip through a build without the attachment mod read 2 everywhere, and the playerdata `horizon:attachments` bytes matched exactly | — | | HEAD injections, ordinary and trait | 41ddf1689d | PASS: `INJECT_PROOF ordinary HEAD fired`, then `INJECT_PROOF trait HEAD fired` | — | | Cancellable trait injection beside an API-only bundle | 4c3ccc0371 | PASS: `INJECT_PROOF cancel HEAD fired`; `catalog server tick fired` never appeared through tick 300 | 3 min 39 s | | Edit loop: one line in a trait mod | 41ddf1689d | PASS: `Reusing patched game source`, and the new line printed | **6.6 s** | | Edit loop: one line in a mixin | 41ddf1689d | PASS: `Applying the mixins`, and the new line printed | **1 min 46 s** | | Dependencies: order and optional feature | 41ddf1689d | PASS: user before base in folder order, but base dispatched first, `counter=1 extra=off`; with the extra mod, `extra=on` | **6.9 s** and **7.6 s** rebuilds | | Renderer hooks beside the machine sample | 4c3ccc0371 | PASS: magenta square and stone cube above the machine; `render-proof: render_needs`, `render-proof: world_drawn`, `render-proof: block_entity_submit`; output chest `{"count":4,"item":"minecraft:stone"}` | 122 s | | Machine sample, mixin bundle | 4c3ccc0371 | PASS: output chest `Items: [{count: 4, Slot: 0b, id: "minecraft:stone"}]` | 14 s | | Machine sample, trait bundle alone | 4c3ccc0371 | PARTIAL: only the clock was checked, `MACHINE_SAMPLE server clock: 200 ticks in minecraft:overworld`; the stick read, zoom and tooltip weren't rerun at this head | 124 s | | Machine sample, trait and mixin bundles together | 4c3ccc0371 | PASS for the machine and the clock: output chest `Items: [{count: 4, Slot: 0b, id: "minecraft:stone"}]`, and `MACHINE_SAMPLE server clock: 200 ticks in minecraft:overworld` | 226 s | | One folder with native code and content, laid out as #5909 (unpacked) | 41ddf1689d | PASS: the launcher built it (13 s); its hooks ran; `Horizon: content from 1 mod(s)`; `/give` of its item worked. Packed `.hmod` reading isn't part of this PR | 13 s | | Crate graph | 41ddf1689d | PASS: the generated game and the mod depend on `horizon-client`; neither depends on `horizon-client-lib` directly | — | <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/arismoko/horizon/9775ad3c6cf66e3e5c069295b5a10af05a03d7b4/mod-api-ingame-dark.png"> <img src="https://raw.githubusercontent.com/arismoko/horizon/9775ad3c6cf66e3e5c069295b5a10af05a03d7b4/mod-api-ingame-light.png" alt="In-game proofs: render hooks beside the machine sample, and the Host proof"> </picture> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/arismoko/horizon/9775ad3c6cf66e3e5c069295b5a10af05a03d7b4/mod-api-rebuild-dark.png"> <img src="https://raw.githubusercontent.com/arismoko/horizon/9775ad3c6cf66e3e5c069295b5a10af05a03d7b4/mod-api-rebuild-light.png" alt="Rebuild time after an edit: trait mod 6.6 s, mod order 6.9 s, optional dependency 7.6 s, mixin edit 1 min 46 s, first build 7 min 21 s"> </picture> Test route: https://github.com/arismoko/horizon-machine-sample (used) ## Self-check `python3 tools/pr_ready.py --quick --base upstream/main` at 0813600988: ``` pr_ready: 172 files changed since upstream/main; checks on local Public tree: pass (0 s) Python tests: pass (31 s) rustfmt: pass (7 s) clippy: pass (14 s) tests: workspace: fail (37 s) test result: FAILED. 550 passed; 2 failed; 4 ignored game::tnt::tests::a_soul_sand_bubble_column_lifts_primed_tnt game::tnt::tests::primed_tnt_follows_water_currents_after_moving doc tests: pass (16 s) content root tests: pass (3 min 7 s) mod API up to date: pass (6 s) tokens.css up to date: pass (0 s) launcher rustfmt: pass (1 s) launcher clippy: pass (1 min 37 s) launcher tests: stopped before it finished ``` - **The two TNT failures** also fail on main 975c07d915 when the vanilla data is present (#6046's tests), so this PR doesn't cause them. - **The launcher tests** were stopped here. Every commit passes `cargo check --tests` for the launcher. - **Trial merge** into main 1bd24e4042 is clean; `xtask mod-api --check` passes on it. Upstream CI covers the rest. **Keeping this current:** if main moves, merge it with a merge commit, never a rebase. Then run `cargo run -p xtask -- mod-api`, and the checks that cover what changed. The generated files regenerate, so they never need a hand merge. Please merge with a merge commit rather than squash, or merge this branch into #5446, so commit authorship is kept. 🤖 Generated with Dyna. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:25:31 +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#6724
No description provided.