[PR #3996] [MERGED] Native mods: block entities for mod blocks (create, tick, save) #5344

Closed
opened 2026-11-03 05:11:56 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/3996
Author: @Sylviromi
Created: 10/29/2026
Status: ✅ Merged
Merged: 10/30/2026
Merged by: @levzzz5154

Base: main ← Head: native-mod-block-entities


📝 Commits (7)

  • 967ec25 Native mods: block entities for mod blocks (hook points API 3)
  • da98be3 Native mods: a save leaves out a mod block entity mid-tick
  • 52399eb Merge main into native-mod-block-entities: the hook catalog
  • 27c3c0b Native mods: the block entity hooks in the hook catalog
  • 8079010 Native mods: a mod block entity's tick marks its chunk changed
  • 2de673c Merge main; block entity hooks are API 4 (#3706 took 3)
  • f2d9c04 Merge main; block entity hooks are API 15 (main is at 14)

📊 Changes

11 files changed (+177 additions, -13 deletions)

View changed files

📝 crates/horizon-client-render/src/chunk/block_entity.rs (+10 -0)
📝 crates/horizon-client-render/src/containers.rs (+10 -5)
📝 crates/horizon-client/src/game.rs (+4 -1)
📝 crates/horizon-client/src/game/block_entity_ticks.rs (+2 -0)
📝 crates/horizon-client/src/game/blockctl.rs (+2 -2)
📝 crates/horizon-client/src/game/commands.rs (+1 -1)
📝 crates/horizon-client/src/game/dispenser.rs (+2 -1)
📝 crates/horizon-client/src/game/native_hooks.rs (+68 -1)
📝 crates/horizon-client/src/save.rs (+60 -2)
📝 docs/native-mods.md (+3 -0)
📝 native-hooks.json (+15 -0)

📄 Description

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

What and why

Fixes #3966. Part of #3911 (Mods): the first piece of the "ticking block entities with saved state" gap from #1854, which machine mods (GregTech-style) start from.

  • BlockEntity::Mod: a block entity whose id is a mod's (any namespace but minecraft:) now loads into the world as its id and saved compound, instead of staying raw chunk data nothing can reach. It saves back as it was loaded: same keys, same values.
  • Hook points, API 4 (game/native_hooks.rs, NATIVE_HOOKS_API 3 → 4; #3706 took 3):
    • native_block_entity(block, &mut Option<(String, Compound)>): a mod block gets its block entity (EntityBlock.newBlockEntity).
    • native_block_entity_ticks(block, &mut bool): whether it ticks (EntityBlock.getTicker), asked per state when the ticker table is built.
    • native_block_entity_tick(pos, id, &mut Compound): on the server, from tick_block_entities in vanilla's list order (Level.tickBlockEntities). The mod's data is out of the world during the call, so a mod that breaks its own block doesn't leave an orphaned block entity behind.
  • One way in: player placement, /setblock//fill, dispensers and horizon-ctl's setblock make block entities through Game::new_block_entity (Horizon's own first, then the hook), so the hook sees every placed block.

A mod block copies a template's facts, including whether it has a block entity; the hook's doc says the template must have one (a chest, a furnace...), so that breaking the block removes it.

Not in this PR (one small PR each, later): inventories, menus, sending mod block entities to clients, rendering.

Questions for @levzzz5154: is this the shape you want for the hooks (names, a Compound rather than a typed handle)? And should the tick hook sit on #3878's server tick work rather than beside it?

Kind

  • parity: Horizon now does what vanilla 26.3 does
  • bug: a crash or a breakage
  • perf: faster, or less memory
  • feature: something new for players (listed under New in the release notes)
  • extra: Horizon-only, off by default
  • tooling: tools, CI, tests, harnesses
  • docs

Vanilla reference

Horizon-only (the native mod API). Follows the shape of EntityBlock.newBlockEntity, EntityBlock.getTicker and Level.tickBlockEntities. Without a mod nothing changes: the hooks are empty, BlockEntity::for_block still runs first, and vanilla's block entity types never become Mod.

Self-check

New test save::tests::mod_block_entity_round_trips: a mod's block entity decodes to Mod without id/x/y/z, encodes back equal to the saved tag, and a vanilla type (minecraft:ender_chest) never becomes Mod. hook_points_stay_hookable and catalog_matches_hook_points pass with the new hooks (they're in native-hooks.json).

pr_ready (tools/pr_ready.py --quick, against origin/main at 829c4021a, 2 min 44 s)

Check Result
Scope ✅ 11 files, +174 −15
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 475 passed (32 s)
rustfmt ✅ (6 s)
clippy ✅ (52 s)
tests: horizon-client, horizon-client-render ❌ 688 passed, 1 failed: axolotl_nbt_tests::bucket_cooldown_and_brain_memories_survive_and_expired_cooldown_is_removed, the known main failure #3840 (it runs only with prepared resources; passes in CI)
doc tests ✅ (2 s)
Parity ➖ not run (--quick): CI's parity job picks the affected harnesses

Proof: a native mod on the hooks

A test mod built with horizon-mixins (0d6b103, the launcher's MIXINS_REV) on this branch (27c3c0be3):

  • a content mod adding testmachine:counter, which copies minecraft:furnace (so it has a block entity);
  • a native bundle with three HEAD hooks on native_block_entity, native_block_entity_ticks and native_block_entity_tick: a counter gets {ticks: 0L}, and each server tick adds 1.

horizon-mixins plan accepts all three injections, and the staged game builds with no warnings. Driven through horizon-ctl (offscreen, single player on the server thread, scratch homes), /data get block on the counter:

Step ticks
/setblock it, 1 s later 21
5 s later 112 (about 20 a second)
quit, then reopen the world 129 (kept)
3 s later 200 (still ticking after the load)
/setblock air there "The target block is not a block entity"

The mod's runtime, for reference:

pub fn new_entity(block: &str, entity: &mut Option<(String, Compound)>) {
    if block == "testmachine:counter" && entity.is_none() {
        let mut nbt = Compound::new();
        nbt.insert("ticks".into(), Tag::Long(0));
        *entity = Some(("testmachine:counter".to_owned(), nbt));
    }
}
pub fn ticks(block: &str, ticks: &mut bool) {
    if block == "testmachine:counter" {
        *ticks = true;
    }
}
pub fn tick(id: &str, nbt: &mut Compound) {
    if id == "testmachine:counter" {
        let n = match nbt.get(&"ticks".into()) { Some(Tag::Long(n)) => *n, _ => 0 };
        nbt.insert("ticks".into(), Tag::Long(n + 1));
    }
}

Not covered: LAN guests and dedicated servers (the ticks run in the server's Game, the same code, but I haven't run them), and a maintainer's local save proof for the saves label.

🤖 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/3996 **Author:** [@Sylviromi](https://github.com/Sylviromi) **Created:** 10/29/2026 **Status:** ✅ Merged **Merged:** 10/30/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `native-mod-block-entities` --- ### 📝 Commits (7) - [`967ec25`](https://github.com/horizon-rust/horizon/commit/967ec25579028318059ed085799af03c4d862cce) Native mods: block entities for mod blocks (hook points API 3) - [`da98be3`](https://github.com/horizon-rust/horizon/commit/da98be3276e06d6395e64e18ec8a09c89e5494c1) Native mods: a save leaves out a mod block entity mid-tick - [`52399eb`](https://github.com/horizon-rust/horizon/commit/52399eb28afb34dc47b1e12d0f1d7b21060b8a6b) Merge main into native-mod-block-entities: the hook catalog - [`27c3c0b`](https://github.com/horizon-rust/horizon/commit/27c3c0be3111f77cc2bef6e3d0f9f5d66f26b165) Native mods: the block entity hooks in the hook catalog - [`8079010`](https://github.com/horizon-rust/horizon/commit/807901034b6f4c3fdc6053d0aab7c9b7943acb8a) Native mods: a mod block entity's tick marks its chunk changed - [`2de673c`](https://github.com/horizon-rust/horizon/commit/2de673c4f8a881c81052c60a503430e8c7f68055) Merge main; block entity hooks are API 4 (#3706 took 3) - [`f2d9c04`](https://github.com/horizon-rust/horizon/commit/f2d9c0479421b3cf9377a478e1d2274e739d8b3d) Merge main; block entity hooks are API 15 (main is at 14) ### 📊 Changes **11 files changed** (+177 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client-render/src/chunk/block_entity.rs` (+10 -0) 📝 `crates/horizon-client-render/src/containers.rs` (+10 -5) 📝 `crates/horizon-client/src/game.rs` (+4 -1) 📝 `crates/horizon-client/src/game/block_entity_ticks.rs` (+2 -0) 📝 `crates/horizon-client/src/game/blockctl.rs` (+2 -2) 📝 `crates/horizon-client/src/game/commands.rs` (+1 -1) 📝 `crates/horizon-client/src/game/dispenser.rs` (+2 -1) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+68 -1) 📝 `crates/horizon-client/src/save.rs` (+60 -2) 📝 `docs/native-mods.md` (+3 -0) 📝 `native-hooks.json` (+15 -0) </details> ### 📄 Description > 🤖 Opened by Sylviromi's AI agent (Claude Code, Claude Opus 5.5), not by Sylviromi personally. ## What and why Fixes #3966. Part of #3911 (Mods): the first piece of the "ticking block entities with saved state" gap from #1854, which machine mods (GregTech-style) start from. - **`BlockEntity::Mod`**: a block entity whose id is a mod's (any namespace but `minecraft:`) now loads into the world as its id and saved compound, instead of staying raw chunk data nothing can reach. It saves back as it was loaded: same keys, same values. - **Hook points, API 4** (`game/native_hooks.rs`, `NATIVE_HOOKS_API` 3 → 4; #3706 took 3): - `native_block_entity(block, &mut Option<(String, Compound)>)`: a mod block gets its block entity (`EntityBlock.newBlockEntity`). - `native_block_entity_ticks(block, &mut bool)`: whether it ticks (`EntityBlock.getTicker`), asked per state when the ticker table is built. - `native_block_entity_tick(pos, id, &mut Compound)`: on the server, from `tick_block_entities` in vanilla's list order (`Level.tickBlockEntities`). The mod's data is out of the world during the call, so a mod that breaks its own block doesn't leave an orphaned block entity behind. - **One way in**: player placement, `/setblock`/`/fill`, dispensers and horizon-ctl's `setblock` make block entities through `Game::new_block_entity` (Horizon's own first, then the hook), so the hook sees every placed block. A mod block copies a template's facts, including whether it has a block entity; the hook's doc says the template must have one (a chest, a furnace...), so that breaking the block removes it. Not in this PR (one small PR each, later): inventories, menus, sending mod block entities to clients, rendering. **Questions for @levzzz5154:** is this the shape you want for the hooks (names, a `Compound` rather than a typed handle)? And should the tick hook sit on #3878's server tick work rather than beside it? ## Kind - [ ] parity: Horizon now does what vanilla 26.3 does - [ ] bug: a crash or a breakage - [ ] perf: faster, or less memory - [x] feature: something new for players (listed under New in the release notes) - [ ] extra: Horizon-only, off by default - [ ] tooling: tools, CI, tests, harnesses - [ ] docs ## Vanilla reference Horizon-only (the native mod API). Follows the shape of `EntityBlock.newBlockEntity`, `EntityBlock.getTicker` and `Level.tickBlockEntities`. Without a mod nothing changes: the hooks are empty, `BlockEntity::for_block` still runs first, and vanilla's block entity types never become `Mod`. ## Self-check New test `save::tests::mod_block_entity_round_trips`: a mod's block entity decodes to `Mod` without `id`/`x`/`y`/`z`, encodes back equal to the saved tag, and a vanilla type (`minecraft:ender_chest`) never becomes `Mod`. `hook_points_stay_hookable` and `catalog_matches_hook_points` pass with the new hooks (they're in `native-hooks.json`). **pr_ready** (`tools/pr_ready.py --quick`, against origin/main at 829c4021a, 2 min 44 s) | Check | Result | | --- | --- | | Scope | ✅ 11 files, +174 −15 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 475 passed (32 s) | | rustfmt | ✅ (6 s) | | clippy | ✅ (52 s) | | tests: horizon-client, horizon-client-render | ❌ 688 passed, 1 failed: `axolotl_nbt_tests::bucket_cooldown_and_brain_memories_survive_and_expired_cooldown_is_removed`, the known main failure #3840 (it runs only with prepared resources; passes in CI) | | doc tests | ✅ (2 s) | | Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses | ## Proof: a native mod on the hooks A test mod built with horizon-mixins (`0d6b103`, the launcher's `MIXINS_REV`) on this branch (`27c3c0be3`): - a content mod adding `testmachine:counter`, which copies `minecraft:furnace` (so it has a block entity); - a native bundle with three HEAD hooks on `native_block_entity`, `native_block_entity_ticks` and `native_block_entity_tick`: a counter gets `{ticks: 0L}`, and each server tick adds 1. `horizon-mixins plan` accepts all three injections, and the staged game builds with no warnings. Driven through `horizon-ctl` (offscreen, single player on the server thread, scratch homes), `/data get block` on the counter: | Step | `ticks` | | --- | --- | | `/setblock` it, 1 s later | 21 | | 5 s later | 112 (about 20 a second) | | `quit`, then reopen the world | 129 (kept) | | 3 s later | 200 (still ticking after the load) | | `/setblock` air there | "The target block is not a block entity" | The mod's runtime, for reference: ```rust pub fn new_entity(block: &str, entity: &mut Option<(String, Compound)>) { if block == "testmachine:counter" && entity.is_none() { let mut nbt = Compound::new(); nbt.insert("ticks".into(), Tag::Long(0)); *entity = Some(("testmachine:counter".to_owned(), nbt)); } } pub fn ticks(block: &str, ticks: &mut bool) { if block == "testmachine:counter" { *ticks = true; } } pub fn tick(id: &str, nbt: &mut Compound) { if id == "testmachine:counter" { let n = match nbt.get(&"ticks".into()) { Some(Tag::Long(n)) => *n, _ => 0 }; nbt.insert("ticks".into(), Tag::Long(n + 1)); } } ``` Not covered: LAN guests and dedicated servers (the ticks run in the server's `Game`, the same code, but I haven't run them), and a maintainer's local save proof for the `saves` label. 🤖 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 05:11:56 +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#5344
No description provided.