[PR #6330] [MERGED] Native mods: energy in a mod block, Capabilities.Energy.BLOCK #6801

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

📋 Pull Request Information

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

Base: main ← Head: mods/block-energy


📝 Commits (1)

  • b8dc376 Native mods: energy handler on blocks (Capabilities.Energy.BLOCK)

📊 Changes

8 files changed (+362 additions, -3 deletions)

View changed files

📝 crates/horizon-client-render/src/block_entities.rs (+11 -1)
📝 crates/horizon-client-render/src/chunk/block_entity.rs (+17 -0)
📝 crates/horizon-client-render/src/containers.rs (+1 -1)
📝 crates/horizon-client/src/game/block_entity_ticks.rs (+2 -0)
📝 crates/horizon-client/src/game/machines.rs (+1 -0)
➕ crates/horizon-client/src/game/machines/native_energy.rs (+275 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+47 -1)
➕ tools/native-hook-fragments/block_entity_energy.json (+8 -0)

📄 Description

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

What and why

Fixes #6227, part of #4953 ("Energy handler on blocks (Capabilities.Energy.BLOCK)"). A tech mod's generators and machines pass energy between blocks; Horizon had item and fluid handlers on blocks but nothing for energy, so each mod invented its own storage, saving and transfer rules.

One hook point and four server helpers, in the shape of the fluid handler (machines/native_fluids.rs). API from a fragment (tools/native-hook-fragments/block_entity_energy.json):

  • native_block_entity_energy(block, energy): a mod block declares a ModEnergyBuffer: capacity, max_insert and max_extract per call, and the faces a fill may come through (insert_faces) and a drain may leave by (extract_faces). Asked once per mod block when the ticking table is built. A capacity under 1 leaves the buffer out; the limits are clamped to 0..=capacity.
  • The amount lives in the block entity's saved compound as the int energy (as SimpleEnergyHandler.serialize), so it is saved with the chunk and the block's own tick reads and writes it there.
  • native_energy(dimension, pos, face): amount, capacity, limits and whether this side may fill or drain; none where there is no buffer, the side reaches neither, or the block entity is the one ticking now.
  • native_energy_insert(pos, face, amount, simulate) and native_energy_extract(dimension, pos, face, amount, simulate): the amount moved, SimpleEnergyHandler's rule (room or stored, call limit, offer, least of them); simulate changes nothing.
  • native_move_energy(dimension, from, from_face, to, to_face, max): EnergyHandlerUtil.move: ask what the source can give, offer it to the target, take from the source exactly what the target accepted. Nothing within one block.

Server side only (the single player's server thread and a dedicated server); the deprecated in-frame path and clients of a server get nothing. Energy on items and entities are separate items. Unlike the fluid helpers, insert and extract answer the amount moved (NeoForge's contract), not what was left over.

The helpers' parameters follow the fluid helpers' (#5188) on purpose: native_energy_insert acts on the level in place without a dimension, as native_fluid_insert does, and the others take one. If the pair should be made uniform, that is for both at once.

Kind

  • feature: something new for players (mod authors)

Vanilla reference

Vanilla has no energy. The contract is NeoForge's javadoc for EnergyHandler, SimpleEnergyHandler and EnergyHandlerUtil (names and behaviour only; no code copied). Mod block entities and the table built for them follow native_block_entity_fluids.

Other projects' code

None.

Proof in the real game

Engine from this branch plus a throwaway probe (a content mod testmod with a generator block and two battery blocks, eprintln!s in native_tick; not in this PR). Generator: capacity 500, extract 80/call, no fill. Battery: capacity 1000, insert 100, extract 40, fill on every side but the bottom.

Server thread (horizon-ctl launch --world L --creative --offscreen --dev-mod …, blocks put with setblock), and the same on a dedicated server with a client connected (identical output):

TESTHOOK found: gen=Some(NativeEnergy { amount: 0, capacity: 500, max_insert: 0, max_extract: 80, can_insert: false, can_extract: true }) bat=Some(NativeEnergy { amount: 0, capacity: 1000, max_insert: 100, max_extract: 40, can_insert: true, can_extract: true }) bat2=Some(0)
TESTHOOK gen=300 set; gen insert -> 0
TESTHOOK move 0: 80 gen=220 bat=80
TESTHOOK move 1: 80 gen=140 bat=160
TESTHOOK move 2: 80 gen=60 bat=240
TESTHOOK move 3: 60 gen=0 bat=300
TESTHOOK move 4: 0 gen=0 bat=300
TESTHOOK extract sim 1000 -> 40 (bat 300)
TESTHOOK extract 1000 -> 40 (bat 260)
TESTHOOK insert Down (no face) -> 0; native_energy Down = Some(false)
TESTHOOK insert sim 100 -> 100
TESTHOOK 12 inserts of 100 -> 740, bat 1000 cap 1000
TESTHOOK move bat->bat2 30
TESTHOOK move same block 0; other dim 0
TESTHOOK final bat=970 bat2=30

After save-all, quit and reopening the same world on the server thread: bat=Some(NativeEnergy { amount: 970 … }) bat2=Some(30), so the amount is saved. Not run: a client reading a server's energy (helpers are server-only), a block entity that ticks (the helpers return none for it while it ticks, as the fluid helpers do), energy in a Nether/End level.

Self-check

pr_ready (tools/pr_ready.py --quick --base hr/main on cachyos-x8664, against hr/main at 1bd24e404, 2 min 51 s)

Check Result
Scope ✅ 8 files, +362 −3
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 586 passed (34 s)
rustfmt ✅ (7 s)
clippy ✅ (52 s)
tests: horizon-client, horizon-client-render ✅ 2724 passed (1 min 16 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/6330 **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/block-energy` --- ### 📝 Commits (1) - [`b8dc376`](https://github.com/horizon-rust/horizon/commit/b8dc376f865593b4282597fa763f152359b98ddb) Native mods: energy handler on blocks (Capabilities.Energy.BLOCK) ### 📊 Changes **8 files changed** (+362 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client-render/src/block_entities.rs` (+11 -1) 📝 `crates/horizon-client-render/src/chunk/block_entity.rs` (+17 -0) 📝 `crates/horizon-client-render/src/containers.rs` (+1 -1) 📝 `crates/horizon-client/src/game/block_entity_ticks.rs` (+2 -0) 📝 `crates/horizon-client/src/game/machines.rs` (+1 -0) ➕ `crates/horizon-client/src/game/machines/native_energy.rs` (+275 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+47 -1) ➕ `tools/native-hook-fragments/block_entity_energy.json` (+8 -0) </details> ### 📄 Description > 🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Sonnet 5.5), not by levzzz5154 personally. ## What and why Fixes #6227, part of #4953 ("Energy handler on blocks (`Capabilities.Energy.BLOCK`)"). A tech mod's generators and machines pass energy between blocks; Horizon had item and fluid handlers on blocks but nothing for energy, so each mod invented its own storage, saving and transfer rules. One hook point and four server helpers, in the shape of the fluid handler (`machines/native_fluids.rs`). API from a fragment (`tools/native-hook-fragments/block_entity_energy.json`): - `native_block_entity_energy(block, energy)`: a mod block declares a `ModEnergyBuffer`: `capacity`, `max_insert` and `max_extract` per call, and the faces a fill may come through (`insert_faces`) and a drain may leave by (`extract_faces`). Asked once per mod block when the ticking table is built. A capacity under 1 leaves the buffer out; the limits are clamped to `0..=capacity`. - The amount lives in the block entity's saved compound as the int `energy` (as `SimpleEnergyHandler.serialize`), so it is saved with the chunk and the block's own tick reads and writes it there. - `native_energy(dimension, pos, face)`: amount, capacity, limits and whether this side may fill or drain; none where there is no buffer, the side reaches neither, or the block entity is the one ticking now. - `native_energy_insert(pos, face, amount, simulate)` and `native_energy_extract(dimension, pos, face, amount, simulate)`: the amount moved, `SimpleEnergyHandler`'s rule (room or stored, call limit, offer, least of them); `simulate` changes nothing. - `native_move_energy(dimension, from, from_face, to, to_face, max)`: `EnergyHandlerUtil.move`: ask what the source can give, offer it to the target, take from the source exactly what the target accepted. Nothing within one block. Server side only (the single player's server thread and a dedicated server); the deprecated in-frame path and clients of a server get nothing. Energy on items and entities are separate items. Unlike the fluid helpers, insert and extract answer the amount moved (NeoForge's contract), not what was left over. The helpers' parameters follow the fluid helpers' (#5188) on purpose: `native_energy_insert` acts on the level in place without a `dimension`, as `native_fluid_insert` does, and the others take one. If the pair should be made uniform, that is for both at once. ## Kind - [x] feature: something new for players (mod authors) ## Vanilla reference Vanilla has no energy. The contract is NeoForge's javadoc for `EnergyHandler`, `SimpleEnergyHandler` and `EnergyHandlerUtil` (names and behaviour only; no code copied). Mod block entities and the table built for them follow `native_block_entity_fluids`. ## Other projects' code None. ## Proof in the real game Engine from this branch plus a throwaway probe (a content mod `testmod` with a generator block and two battery blocks, `eprintln!`s in `native_tick`; not in this PR). Generator: capacity 500, extract 80/call, no fill. Battery: capacity 1000, insert 100, extract 40, fill on every side but the bottom. Server thread (`horizon-ctl launch --world L --creative --offscreen --dev-mod …`, blocks put with `setblock`), and the same on a dedicated server with a client connected (identical output): ``` TESTHOOK found: gen=Some(NativeEnergy { amount: 0, capacity: 500, max_insert: 0, max_extract: 80, can_insert: false, can_extract: true }) bat=Some(NativeEnergy { amount: 0, capacity: 1000, max_insert: 100, max_extract: 40, can_insert: true, can_extract: true }) bat2=Some(0) TESTHOOK gen=300 set; gen insert -> 0 TESTHOOK move 0: 80 gen=220 bat=80 TESTHOOK move 1: 80 gen=140 bat=160 TESTHOOK move 2: 80 gen=60 bat=240 TESTHOOK move 3: 60 gen=0 bat=300 TESTHOOK move 4: 0 gen=0 bat=300 TESTHOOK extract sim 1000 -> 40 (bat 300) TESTHOOK extract 1000 -> 40 (bat 260) TESTHOOK insert Down (no face) -> 0; native_energy Down = Some(false) TESTHOOK insert sim 100 -> 100 TESTHOOK 12 inserts of 100 -> 740, bat 1000 cap 1000 TESTHOOK move bat->bat2 30 TESTHOOK move same block 0; other dim 0 TESTHOOK final bat=970 bat2=30 ``` After `save-all`, quit and reopening the same world on the server thread: `bat=Some(NativeEnergy { amount: 970 … }) bat2=Some(30)`, so the amount is saved. Not run: a client reading a server's energy (helpers are server-only), a block entity that ticks (the helpers return none for it while it ticks, as the fluid helpers do), energy in a Nether/End level. ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base hr/main` on cachyos-x8664, against hr/main at 1bd24e404, 2 min 51 s) | Check | Result | | --- | --- | | Scope | ✅ 8 files, +362 −3 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 586 passed (34 s) | | rustfmt | ✅ (7 s) | | clippy | ✅ (52 s) | | tests: horizon-client, horizon-client-render | ✅ 2724 passed (1 min 16 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:27:00 +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#6801
No description provided.