[PR #6359] [MERGED] Native mods: blocks and items declared in code, DeferredRegister #6824

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

📋 Pull Request Information

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

Base: main ← Head: mods/native-blocks-items


📝 Commits (1)

  • bed9c5f Native mods: blocks and items declared in code, DeferredRegister / RegisterEvent

📊 Changes

6 files changed (+467 additions, -32 deletions)

View changed files

📝 crates/horizon-client/src/game/mods.rs (+33 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+29 -0)
📝 crates/horizon-client/src/mod_content.rs (+76 -2)
📝 crates/horizon-client/src/mod_content_tests.rs (+98 -0)
📝 crates/horizon-mods/src/content.rs (+215 -30)
➕ tools/native-hook-fragments/native_blocks_items.json (+16 -0)

📄 Description

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

What and why

Step 1 of #6300 (code-side registration, a design proposal), part of #4953 ("Code-side registration of blocks, items and block-entity types, with network IDs, so a mod needs no content file per block"). #6300 stays open for block-entity types and a typed builder.

A native mod's blocks and items existed only when a content mod declared each in a file of its own (data/<id>/horizon/block/<path>.json). A tech mod with every tier and colour wrote hundreds of them, and couldn't loop over its tiers.

Two static hook points, API from a fragment (tools/native-hook-fragments/native_blocks_items.json), NeoForge's DeferredRegister.Blocks / .Items drained by RegisterEvent:

  • native_blocks(blocks: &mut Vec<horizon_mods::content::Block>) and native_items(items: &mut Vec<horizon_mods::content::Item>). They take the same declarations the files hold: a vanilla template and what differs (properties, state_properties, shapes, connects_to, components…; item: true gives a block its item). Block and Item now derive Default, so a declaration names only what it changes.
  • When they're asked: once at start, on every side, before any resource loads (game/mods.rs keeps the answer for the run, so a pack reload's content root has the same ones).
  • Where they go: mod_content::accept_declared joins them to the content plan by namespace.
    • Into the content mod whose id is that namespace, when it is on: the mod's own .hmod, with its models, textures, lang, loot and recipes.
    • Otherwise into a content mod with no files of its own (Content::declared). Its blocks look like their template (the content plan already copies a template's blockstate for a block that has none).
  • Checks: the files' checks, now shared (check_block and check_item take the declaration; Content::declare adds the namespace and duplicate checks; accept_mod's component and namespace-clash checks). A namespace whose declarations don't pass is logged and left out whole.
  • Ids and sync: state ids, network ids (registries.json), saves and the joining client's content-identity check are the files' plan, unchanged.
  • The cache: the content root's stamp counts the declarations, and resource_root builds a root for a mod with declarations but no files.

Kind

  • feature: something new for players (mod authors)

Vanilla reference

Vanilla registers blocks and items in static initialisers into BuiltInRegistries and freezes them at bootstrap; Block.BLOCK_STATE_REGISTRY numbers states in registry order. NeoForge posts RegisterEvent per registry on both sides before the freeze, and DeferredRegister drains into it. Horizon's content plan already gives mod blocks and items ids after vanilla's in a fixed order (by mod, then by id), so the code path only feeds that plan. Unlike NeoForge, no id map is synced to clients: the identity hash check stays the contract (#6300 explains why).

Other projects' code

None. NeoForge's names and javadoc only.

Proof in the real game

Engine from this branch with throwaway lines in the hook bodies (not in this PR). The hooks declared:

  • lanef:cog: template minecraft:oak_log, hardness 9, with its item. Namespace lanef is also a dev content mod that is on, with a file-declared lanef:machine and lanef:spanner.
  • gears:gear: template minecraft:iron_block, a namespace with no content mod.
  • bad:post: template lanef:cog, which must be refused.
  • lanef:wrench: template minecraft:stick, max_stack_size: 1.

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

TESTHOOK native_blocks asked: ["lanef:cog", "gears:gear", "bad:post"]
TESTHOOK native_items asked: ["lanef:wrench"]
Horizon: native blocks and items of bad: block bad:post: template "lanef:cog" is not a vanilla block (minecraft:...)
Horizon: content from 2 mod(s) in …/.horizon-cache/mod-content
> setblock 0 119 5 lanef:cog[axis=x]    → Changed the block at 0, 119, 5
> setblock 1 119 5 gears:gear           → Changed the block at 1, 119, 5
> setblock 2 119 5 bad:post             → Unknown block type 'bad:post'
> execute if block 0 119 5 lanef:cog[axis=x] run say cog_is_there → [Player…] cog_is_there
> give @s lanef:wrench 3 / lanef:cog / gears:gear / lanef:spanner 2 / lanef:machine / minecraft:stick
inventory hotbar: lanef:wrench ×1 in slots 0, 1, 2 (stack size 1), lanef:cog, gears:gear, lanef:spanner, lanef:spanner, lanef:machine, minecraft:stick

The screenshot shows the cog as a sideways oak log and the gear as an iron block.

Dedicated server (port 25761, the dev content mod in its horizon-mods/) and an offscreen client with the same build and mod, which joined: its content identity matched. Blocks were set and items given from the server's console:

client:
TESTHOOK native_blocks asked: ["lanef:cog", "gears:gear", "bad:post"]
TESTHOOK native_items asked: ["lanef:wrench"]
Horizon: native blocks and items of bad: block bad:post: template "lanef:cog" is not a vanilla block (minecraft:...)
Horizon: content from 2 mod(s) in …
server:
TESTHOOK native_blocks asked: ["lanef:cog", "gears:gear", "bad:post"]
TESTHOOK native_items asked: ["lanef:wrench"]
Horizon: native blocks and items of bad: block bad:post: template "lanef:cog" is not a vanilla block (minecraft:...)
[Server thread/INFO]: System chat: Alex joined the game
[Server thread/INFO]: System chat: Changed the block at 0, 119, 5
[Server thread/INFO]: System chat: Changed the block at 1, 119, 5
[Server thread/INFO]: System chat: Gave 2 [block.lanef.wrench] to Alex
[Server thread/INFO]: System chat: Gave 1 [block.gears.gear] to Alex
client looking-at: lanef:cog[axis=x] at [0, 119, 5]; inventory counts: {gears:gear: 1, lanef:wrench: 2}

(block.lanef.wrench is the missing lang key; the file-declared lanef:spanner shows block.lanef.spanner the same way, so this PR doesn't change it.)

Not run: a client whose native mods differ from the server's. Its content hash differs, so the existing check refuses it with "install the same content mods". I had no second build to show it.

Tests:

  • content::tests::blocks_and_items_declared_in_code_pass_the_files_checks (horizon-mods): accepted declarations, and every refusal leaving the content as it was. Refusals: another namespace, minecraft:, no namespace, a duplicate, an item clashing with a block's item, a modded template, light 16, stack 100, a foreign remainder.
  • a_connecting_block_file_names_its_tag_with_a_hash: the file check kept after check_block moved to declarations.
  • mod_content_tests::blocks_and_items_declared_in_code_get_ids (ignored, a content-root test pr_ready runs):
    • Declarations join the ruby content mod or make gears, and a clashing namespace is refused whole.
    • gears:shaft's states come first after vanilla's.
    • The block items and gears:wrench (stack 1) are in registries.json and Items.
    • The same declarations reuse the cached root, and a changed one rebuilds it.

Self-check

pr_ready (tools/pr_ready.py --quick --base hr/main on cachyos-x8664, against hr/main at ff8e46848, 3 min 13 s)

Check Result
Scope ✅ 6 files, +467 −32
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 594 passed (32 s)
rustfmt ✅ (6 s)
clippy ✅ (48 s)
tests: horizon-client, horizon-client-lib, horizon-mods ❌ 556 passed, 2 failed (22 s)
doc tests ✅ 0 passed (3 s)
content root tests ✅ 22 passed (1 min 11 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ✅ (1 s)
launcher clippy ✅ (3 s)
launcher tests ✅ 396 passed (7 s)
Parity ➖ not run (--quick): CI's parity job picks the affected harnesses
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)
  • The two TNT failures (game::tnt::tests::primed_tnt_follows_water_currents_after_moving, a_soul_sand_bubble_column_lifts_primed_tnt) also fail on hr/main ff8e46848 in the same worktree. I ran them there. They fail only when the vanilla data (.local/) is present, as #6216 noted. This PR doesn't touch them.

🤖 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/6359 **Author:** [@levzzz5154](https://github.com/levzzz5154) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `mods/native-blocks-items` --- ### 📝 Commits (1) - [`bed9c5f`](https://github.com/horizon-rust/horizon/commit/bed9c5fd86c899ac28f114f688b5022c906b049e) Native mods: blocks and items declared in code, DeferredRegister / RegisterEvent ### 📊 Changes **6 files changed** (+467 additions, -32 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/mods.rs` (+33 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+29 -0) 📝 `crates/horizon-client/src/mod_content.rs` (+76 -2) 📝 `crates/horizon-client/src/mod_content_tests.rs` (+98 -0) 📝 `crates/horizon-mods/src/content.rs` (+215 -30) ➕ `tools/native-hook-fragments/native_blocks_items.json` (+16 -0) </details> ### 📄 Description > 🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Opus 5.5), not by levzzz5154 personally. ## What and why Step 1 of #6300 (code-side registration, a design proposal), part of #4953 ("Code-side registration of blocks, items and block-entity types, with network IDs, so a mod needs no content file per block"). #6300 stays open for block-entity types and a typed builder. A native mod's blocks and items existed only when a content mod declared each in a file of its own (`data/<id>/horizon/block/<path>.json`). A tech mod with every tier and colour wrote hundreds of them, and couldn't loop over its tiers. Two static hook points, API from a fragment (`tools/native-hook-fragments/native_blocks_items.json`), NeoForge's `DeferredRegister.Blocks` / `.Items` drained by `RegisterEvent`: - `native_blocks(blocks: &mut Vec<horizon_mods::content::Block>)` and `native_items(items: &mut Vec<horizon_mods::content::Item>)`. They take the same declarations the files hold: a vanilla template and what differs (`properties`, `state_properties`, `shapes`, `connects_to`, `components`…; `item: true` gives a block its item). `Block` and `Item` now derive `Default`, so a declaration names only what it changes. - **When they're asked:** once at start, on every side, before any resource loads (`game/mods.rs` keeps the answer for the run, so a pack reload's content root has the same ones). - **Where they go:** `mod_content::accept_declared` joins them to the content plan by namespace. - Into the content mod whose id is that namespace, when it is on: the mod's own `.hmod`, with its models, textures, lang, loot and recipes. - Otherwise into a content mod with no files of its own (`Content::declared`). Its blocks look like their template (the content plan already copies a template's blockstate for a block that has none). - **Checks:** the files' checks, now shared (`check_block` and `check_item` take the declaration; `Content::declare` adds the namespace and duplicate checks; `accept_mod`'s component and namespace-clash checks). A namespace whose declarations don't pass is logged and left out whole. - **Ids and sync:** state ids, network ids (`registries.json`), saves and the joining client's content-identity check are the files' plan, unchanged. - **The cache:** the content root's stamp counts the declarations, and `resource_root` builds a root for a mod with declarations but no files. ## Kind - [x] feature: something new for players (mod authors) ## Vanilla reference Vanilla registers blocks and items in static initialisers into `BuiltInRegistries` and freezes them at bootstrap; `Block.BLOCK_STATE_REGISTRY` numbers states in registry order. NeoForge posts `RegisterEvent` per registry on both sides before the freeze, and `DeferredRegister` drains into it. Horizon's content plan already gives mod blocks and items ids after vanilla's in a fixed order (by mod, then by id), so the code path only feeds that plan. Unlike NeoForge, no id map is synced to clients: the identity hash check stays the contract (#6300 explains why). ## Other projects' code None. NeoForge's names and javadoc only. ## Proof in the real game Engine from this branch with throwaway lines in the hook bodies (not in this PR). The hooks declared: - `lanef:cog`: template `minecraft:oak_log`, hardness 9, with its item. Namespace `lanef` is also a dev content mod that is on, with a file-declared `lanef:machine` and `lanef:spanner`. - `gears:gear`: template `minecraft:iron_block`, a namespace with no content mod. - `bad:post`: template `lanef:cog`, which must be refused. - `lanef:wrench`: template `minecraft:stick`, `max_stack_size: 1`. Server thread (`horizon-ctl launch --world L --creative --offscreen --dev-mod …`): ``` TESTHOOK native_blocks asked: ["lanef:cog", "gears:gear", "bad:post"] TESTHOOK native_items asked: ["lanef:wrench"] Horizon: native blocks and items of bad: block bad:post: template "lanef:cog" is not a vanilla block (minecraft:...) Horizon: content from 2 mod(s) in …/.horizon-cache/mod-content > setblock 0 119 5 lanef:cog[axis=x] → Changed the block at 0, 119, 5 > setblock 1 119 5 gears:gear → Changed the block at 1, 119, 5 > setblock 2 119 5 bad:post → Unknown block type 'bad:post' > execute if block 0 119 5 lanef:cog[axis=x] run say cog_is_there → [Player…] cog_is_there > give @s lanef:wrench 3 / lanef:cog / gears:gear / lanef:spanner 2 / lanef:machine / minecraft:stick inventory hotbar: lanef:wrench ×1 in slots 0, 1, 2 (stack size 1), lanef:cog, gears:gear, lanef:spanner, lanef:spanner, lanef:machine, minecraft:stick ``` The screenshot shows the cog as a sideways oak log and the gear as an iron block. Dedicated server (port 25761, the dev content mod in its `horizon-mods/`) and an offscreen client with the same build and mod, which joined: its content identity matched. Blocks were set and items given from the server's console: ``` client: TESTHOOK native_blocks asked: ["lanef:cog", "gears:gear", "bad:post"] TESTHOOK native_items asked: ["lanef:wrench"] Horizon: native blocks and items of bad: block bad:post: template "lanef:cog" is not a vanilla block (minecraft:...) Horizon: content from 2 mod(s) in … server: TESTHOOK native_blocks asked: ["lanef:cog", "gears:gear", "bad:post"] TESTHOOK native_items asked: ["lanef:wrench"] Horizon: native blocks and items of bad: block bad:post: template "lanef:cog" is not a vanilla block (minecraft:...) [Server thread/INFO]: System chat: Alex joined the game [Server thread/INFO]: System chat: Changed the block at 0, 119, 5 [Server thread/INFO]: System chat: Changed the block at 1, 119, 5 [Server thread/INFO]: System chat: Gave 2 [block.lanef.wrench] to Alex [Server thread/INFO]: System chat: Gave 1 [block.gears.gear] to Alex client looking-at: lanef:cog[axis=x] at [0, 119, 5]; inventory counts: {gears:gear: 1, lanef:wrench: 2} ``` (`block.lanef.wrench` is the missing lang key; the file-declared `lanef:spanner` shows `block.lanef.spanner` the same way, so this PR doesn't change it.) Not run: a client whose native mods differ from the server's. Its content hash differs, so the existing check refuses it with "install the same content mods". I had no second build to show it. Tests: - `content::tests::blocks_and_items_declared_in_code_pass_the_files_checks` (horizon-mods): accepted declarations, and every refusal leaving the content as it was. Refusals: another namespace, `minecraft:`, no namespace, a duplicate, an item clashing with a block's item, a modded template, light 16, stack 100, a foreign remainder. - `a_connecting_block_file_names_its_tag_with_a_hash`: the file check kept after `check_block` moved to declarations. - `mod_content_tests::blocks_and_items_declared_in_code_get_ids` (ignored, a content-root test pr_ready runs): - Declarations join the `ruby` content mod or make `gears`, and a clashing namespace is refused whole. - `gears:shaft`'s states come first after vanilla's. - The block items and `gears:wrench` (stack 1) are in `registries.json` and `Items`. - The same declarations reuse the cached root, and a changed one rebuilds it. ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base hr/main` on cachyos-x8664, against hr/main at ff8e46848, 3 min 13 s) | Check | Result | | --- | --- | | Scope | ✅ 6 files, +467 −32 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 594 passed (32 s) | | rustfmt | ✅ (6 s) | | clippy | ✅ (48 s) | | tests: horizon-client, horizon-client-lib, horizon-mods | ❌ 556 passed, 2 failed (22 s) | | doc tests | ✅ 0 passed (3 s) | | content root tests | ✅ 22 passed (1 min 11 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ✅ (1 s) | | launcher clippy | ✅ (3 s) | | launcher tests | ✅ 396 passed (7 s) | | Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | - **The two TNT failures** (`game::tnt::tests::primed_tnt_follows_water_currents_after_moving`, `a_soul_sand_bubble_column_lifts_primed_tnt`) also fail on hr/main ff8e46848 in the same worktree. I ran them there. They fail only when the vanilla data (`.local/`) is present, as #6216 noted. This PR doesn't touch them. 🤖 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:27:35 +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#6824
No description provided.