[GH-ISSUE #6300] Native mods: blocks, items and block-entity types registered from code, DeferredRegister / RegisterEvent (design proposal) #2044

Open
opened 2026-11-02 15:51:35 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @levzzz5154 on GitHub (Nov 1, 2026).
Original GitHub issue: https://github.com/horizon-rust/horizon/issues/6300

Originally assigned to: @levzzz5154 on GitHub.

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

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").

A design proposal: the options, a recommendation, and the steps as small PRs. Step 1 is self-contained and I claim it; the later steps need the decisions listed at the end.

The problem

A native mod's blocks and items exist only if a content mod declares each one in a file: data/<id>/horizon/block/<path>.json and item/<path>.json (docs/mods.md). A tech mod with 200 blocks (every machine tier, every cable colour, every casing) writes 200 files. It cannot loop over its tiers or colours, and its code and its declarations can drift apart. NeoForge mods do this in code: DeferredRegister.createBlocks(modid), registerSimpleBlock, registerSimpleBlockItem, and a DeferredRegister on Registries.BLOCK_ENTITY_TYPE, all drained by RegisterEvent.

How it works today

  • Declarations. horizon_mods::content::{Block, Item} (crates/horizon-mods/src/content.rs) are read from those files by Content::check. A block copies a vanilla template's behaviour, states and shapes under a new id, with properties, shapes, state_properties, default_state, item, components, connects_to and thickness.
  • IDs. mod_content::Plan::new (crates/horizon-client/src/mod_content.rs) sorts mods by id and each mod's blocks by id. Their states take ids after vanilla's (a run of template states × own property combinations each); items are sorted by id after vanilla's. registries.json gets protocol_id = max + 1 entries for minecraft:item, minecraft:block, and a minecraft:block_entity_type named after every mod block. Vanilla ids never move, and the ids depend only on the set of mods that are on.
  • Sync. Nothing is sent. content_identity.rs hashes the ordered rows; the server sends the hash in the configuration phase on horizon:block_registry and a client with another hash is disconnected ("install the same content mods as the server"). Native component types (#5426) are in the same hash.
  • Code registration that exists. native_component_types(types: &mut Vec<String>) (API 65) is a static hook asked once at start, before content loads; custom::install sorts the names, so ids don't depend on hook order. Mod particle types (#5792) need no registration: they go on the wire by name. Mob effects, thrown entity types and menu types are set-once statics filled from the content plan (game/mods.rs).
  • Block entities. A block gets one when native_block_entity(block, entity) answers for it (any template since #6117); native_block_entity_ticks says whether it ticks. There is no block-entity type object: the hook names the entity's id per block, and the wire type is the block's own name.
  • Saves. Chunks store states by name and properties; an unknown name loads as air. Numeric ids never reach disk.

Vanilla and NeoForge 26.3

  • Vanilla registers in static initialisers (Blocks.register → Registry.register(BuiltInRegistries.BLOCK, …)) and freezes the built-in registries at bootstrap. Block.BLOCK_STATE_REGISTRY numbers states in registry order; network ids are that order.
  • NeoForge posts RegisterEvent once per registry, on both sides, before the registries freeze; DeferredRegister is a per-namespace list drained by it. RegistryManager takes a snapshot of the synced registries and sends it to clients in the configuration phase (FrozenRegistryPayload); the client remaps its ids to the server's and refuses entries it lacks.

Options

A. Static collect hooks taking the content schema's own types (recommended).
native_blocks(blocks: &mut Vec<Block>) and native_items(items: &mut Vec<Item>), asked once at start beside native_component_types, before content_root. What they push joins the content plan as if it came from files: same validation (check_block, templates, components), same id order, same registries.json, same identity hash, same saves.

  • For: the smallest change; one schema for files and code, so every feature content blocks get (state properties, shapes, connecting blocks…) is in code at once; ids and sync need nothing new; it fits #6216, whose generator turns a collect hook into a Hooks method (component_types already is one).
  • Against: a struct literal per block, not NeoForge's builder (BlockBehaviour.Properties.of().strength(…)). A builder can be added over the structs later without changing the hook.

B. Virtual content files. The hook pushes (path, json bytes) pairs that Content::check reads as files.

  • For: no new validation path at all.
  • Against: JSON in strings, errors only at load, nothing typed. Worse than A for the same result.

C. A runtime registry with id-map sync, like NeoForge's RegistryManager. The server sends its id maps and clients remap.

  • For: a client could join with a different set or order of mods.
  • Against: much larger (every id table on the client becomes remappable); and it buys nothing here: a native mod is compiled into both games, and a client without the server's blocks has neither their models nor their behaviour. The hash check stays the right contract.

D. Wait for #6216 and register through a typed Registrar on Hooks.

  • For: NeoForge-like ergonomics.
  • Against: it needs A underneath anyway; A's hooks become trait methods by #6216's generator, and a Registrar builder can wrap them there.

Recommendation

A, now. Keep the identity hash (not C). A builder in the typed API later (D on top of A).

Steps

  1. native_blocks and native_items (this issue's first PR, ~300 lines). Two static hooks asked once at start. What they push is grouped by namespace and joins the content plan: into the content mod whose id is that namespace when one is on (a native mod's own .hmod with its models and textures), else a content mod with no files of its own. Same checks as files: a vanilla template, a namespaced id outside minecraft:, no id declared twice, no namespace another mod owns. A block with item: true gets its block item. The content root's stamp covers them. Docs, a fragment, unit tests.
  2. Block-entity types, Registries.BLOCK_ENTITY_TYPE. native_block_entity_types(types: &mut Vec<BlockEntityType>): an id, its valid blocks and whether it ticks, so a mod declares a type once for all its tiers instead of answering native_block_entity per block (which stays). Needs a decision: the wire type (minecraft:block_entity_type) becomes the declared type rather than one per block, which changes those ids for every mod block; and whether native_block_entity answers only the compound once a type is declared.
  3. A typed builder in the mod API, once #6216 lands: Registrar::block("cog").template("minecraft:stone").strength(2.0, 6.0).item(), building step 1's structs.
  4. Creative tabs, mob effects, menu types and entity types from code, the same way (native_creative_tabs, …): one small PR each, when a mod asks.

Decisions needed

  • Step 1: none beyond option A. I'm implementing it.
  • Step 2: whether a block-entity type's wire id may change from "one per block" to "one per declared type" (maintainers: Ludovic).
  • Step 3: the builder's shape belongs to #6216 (arismoko).
Originally created by @levzzz5154 on GitHub (Nov 1, 2026). Original GitHub issue: https://github.com/horizon-rust/horizon/issues/6300 Originally assigned to: @levzzz5154 on GitHub. > 🤖 Filed by levzzz5154's AI agent (Claude Code, Claude Opus 5.5), not by levzzz5154 personally. 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"). A design proposal: the options, a recommendation, and the steps as small PRs. Step 1 is self-contained and I claim it; the later steps need the decisions listed at the end. ## The problem A native mod's blocks and items exist only if a content mod declares each one in a file: `data/<id>/horizon/block/<path>.json` and `item/<path>.json` (docs/mods.md). A tech mod with 200 blocks (every machine tier, every cable colour, every casing) writes 200 files. It cannot loop over its tiers or colours, and its code and its declarations can drift apart. NeoForge mods do this in code: `DeferredRegister.createBlocks(modid)`, `registerSimpleBlock`, `registerSimpleBlockItem`, and a `DeferredRegister` on `Registries.BLOCK_ENTITY_TYPE`, all drained by `RegisterEvent`. ## How it works today - **Declarations.** `horizon_mods::content::{Block, Item}` (crates/horizon-mods/src/content.rs) are read from those files by `Content::check`. A block copies a vanilla `template`'s behaviour, states and shapes under a new id, with `properties`, `shapes`, `state_properties`, `default_state`, `item`, `components`, `connects_to` and `thickness`. - **IDs.** `mod_content::Plan::new` (crates/horizon-client/src/mod_content.rs) sorts mods by id and each mod's blocks by id. Their states take ids after vanilla's (a run of `template states × own property combinations` each); items are sorted by id after vanilla's. `registries.json` gets `protocol_id = max + 1` entries for `minecraft:item`, `minecraft:block`, and a `minecraft:block_entity_type` named after every mod block. Vanilla ids never move, and the ids depend only on the set of mods that are on. - **Sync.** Nothing is sent. `content_identity.rs` hashes the ordered rows; the server sends the hash in the configuration phase on `horizon:block_registry` and a client with another hash is disconnected ("install the same content mods as the server"). Native component types (#5426) are in the same hash. - **Code registration that exists.** `native_component_types(types: &mut Vec<String>)` (API 65) is a static hook asked once at start, before content loads; `custom::install` sorts the names, so ids don't depend on hook order. Mod particle types (#5792) need no registration: they go on the wire by name. Mob effects, thrown entity types and menu types are set-once statics filled from the content plan (`game/mods.rs`). - **Block entities.** A block gets one when `native_block_entity(block, entity)` answers for it (any template since #6117); `native_block_entity_ticks` says whether it ticks. There is no block-entity type object: the hook names the entity's id per block, and the wire type is the block's own name. - **Saves.** Chunks store states by name and properties; an unknown name loads as air. Numeric ids never reach disk. ## Vanilla and NeoForge 26.3 - Vanilla registers in static initialisers (`Blocks.register` → `Registry.register(BuiltInRegistries.BLOCK, …)`) and freezes the built-in registries at bootstrap. `Block.BLOCK_STATE_REGISTRY` numbers states in registry order; network ids are that order. - NeoForge posts `RegisterEvent` once per registry, on both sides, before the registries freeze; `DeferredRegister` is a per-namespace list drained by it. `RegistryManager` takes a snapshot of the synced registries and sends it to clients in the configuration phase (`FrozenRegistryPayload`); the client remaps its ids to the server's and refuses entries it lacks. ## Options **A. Static collect hooks taking the content schema's own types (recommended).** `native_blocks(blocks: &mut Vec<Block>)` and `native_items(items: &mut Vec<Item>)`, asked once at start beside `native_component_types`, before `content_root`. What they push joins the content plan as if it came from files: same validation (`check_block`, templates, components), same id order, same `registries.json`, same identity hash, same saves. - For: the smallest change; one schema for files and code, so every feature content blocks get (state properties, shapes, connecting blocks…) is in code at once; ids and sync need nothing new; it fits #6216, whose generator turns a collect hook into a `Hooks` method (`component_types` already is one). - Against: a struct literal per block, not NeoForge's builder (`BlockBehaviour.Properties.of().strength(…)`). A builder can be added over the structs later without changing the hook. **B. Virtual content files.** The hook pushes `(path, json bytes)` pairs that `Content::check` reads as files. - For: no new validation path at all. - Against: JSON in strings, errors only at load, nothing typed. Worse than A for the same result. **C. A runtime registry with id-map sync, like NeoForge's `RegistryManager`.** The server sends its id maps and clients remap. - For: a client could join with a different set or order of mods. - Against: much larger (every id table on the client becomes remappable); and it buys nothing here: a native mod is compiled into both games, and a client without the server's blocks has neither their models nor their behaviour. The hash check stays the right contract. **D. Wait for #6216 and register through a typed `Registrar` on `Hooks`.** - For: NeoForge-like ergonomics. - Against: it needs A underneath anyway; A's hooks become trait methods by #6216's generator, and a `Registrar` builder can wrap them there. ## Recommendation A, now. Keep the identity hash (not C). A builder in the typed API later (D on top of A). ## Steps 1. **`native_blocks` and `native_items`** (this issue's first PR, ~300 lines). Two static hooks asked once at start. What they push is grouped by namespace and joins the content plan: into the content mod whose id is that namespace when one is on (a native mod's own `.hmod` with its models and textures), else a content mod with no files of its own. Same checks as files: a vanilla template, a namespaced id outside `minecraft:`, no id declared twice, no namespace another mod owns. A block with `item: true` gets its block item. The content root's stamp covers them. Docs, a fragment, unit tests. 2. **Block-entity types, `Registries.BLOCK_ENTITY_TYPE`.** `native_block_entity_types(types: &mut Vec<BlockEntityType>)`: an id, its valid blocks and whether it ticks, so a mod declares a type once for all its tiers instead of answering `native_block_entity` per block (which stays). Needs a decision: the wire type (`minecraft:block_entity_type`) becomes the declared type rather than one per block, which changes those ids for every mod block; and whether `native_block_entity` answers only the compound once a type is declared. 3. **A typed builder in the mod API**, once #6216 lands: `Registrar::block("cog").template("minecraft:stone").strength(2.0, 6.0).item()`, building step 1's structs. 4. **Creative tabs, mob effects, menu types and entity types from code**, the same way (`native_creative_tabs`, …): one small PR each, when a mod asks. ## Decisions needed - Step 1: none beyond option A. I'm implementing it. - Step 2: whether a block-entity type's wire id may change from "one per block" to "one per declared type" (maintainers: Ludovic). - Step 3: the builder's shape belongs to #6216 (arismoko).
Author
Owner

@levzzz5154 commented on GitHub (Nov 1, 2026):

/claim

<!-- gh-comment-id:6562428248 --> @levzzz5154 commented on GitHub (Nov 1, 2026): /claim
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#2044
No description provided.