[PR #6596] [MERGED] Native mods: energy in an item, Capabilities.Energy.ITEM #6974

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

📋 Pull Request Information

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

Base: main ← Head: mod-item-energy


📝 Commits (6)

  • 6519945 Native mods: energy in an item, Capabilities.Energy.ITEM
  • 0e3fbaf Native mods: the item energy hook's mod API contract
  • 4b0a92f Merge main into #6596
  • 0f1430f Merge main into #6596
  • 9c9cb89 Mod API entries and regenerated output for #6596
  • 5aceab8 Let the mod API name ItemEnergyBuffer

📊 Changes

7 files changed (+424 additions, -4 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+2 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+17 -0)
➕ crates/horizon-client/src/game/native_item_energy.rs (+341 -0)
📝 crates/horizon-client/src/mod_api.rs (+4 -4)
📝 crates/horizon-client/src/mod_api/generated.rs (+42 -0)
➕ tools/native-hook-fragments/item_energy_buffer.json (+16 -0)
📝 xtask/src/mod_api.rs (+2 -0)

📄 Description

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

What and why

Fixes #6553, part of #4953 ("Energy on items (Capabilities.Energy.ITEM)"). A tech mod's batteries and charged tools are items, and a mod had no way to say an item holds energy, nor to charge or drain one and get the changed item back. Horizon has energy in mod blocks (#6330) and fluid in items (#6331); this is the item half of energy, in the shape of both.

One hook point and three helpers, in a new game/native_item_energy.rs. API from a fragment (tools/native-hook-fragments/item_energy_buffer.json):

  • native_item_energy_buffer(item, buffer): a mod item (mod_id:name) holds energy; set its ItemEnergyBuffer as SimpleEnergyHandler is built: capacity, max_insert and max_extract per call. Checked as a block's buffer is (#6330): a capacity under 1 leaves it out, the limits are clamped to 0..=capacity. Asked at each call; minecraft: items have none.
  • The amount is kept in the stack's minecraft:custom_data as the int horizon_energy (the block keeps energy), so it is saved and sent with the stack, and the rest of the custom_data is kept. An emptied item carries nothing, so it stacks with a new one, also when the stack came from a server with its wire patch.
  • native_item_energy(stack): amount, capacity and limits of one item of the stack.
  • native_item_energy_insert(stack, amount) and native_item_energy_extract(stack, amount): the amount moved (the least of the room or what is held, the limit and the amount asked) and the single item it becomes; the item itself when nothing moved. The stack given is not changed: the caller shrinks it by one and hands out the result (NeoForge's ItemAccess acts on one item at a time). NeoForge runs a simulation as a transaction that isn't committed; here it is the result left unused.

They hold no world state and work on any side. Energy on entities (Capabilities.Energy.ENTITY) and a helper that moves energy between a block and an item (a charger slot) are separate.

Kind

  • feature: something new for players (mod authors)

Vanilla reference

Horizon-only: vanilla has no energy. The contract is NeoForge's: Capabilities.Energy.ITEM is an EnergyHandler over an ItemAccess, SimpleEnergyHandler a capacity with limits per insert and extract (names and behaviour from its public docs; no code read or copied).

Other projects' code

None.

Proof in the real game

Engine from this branch with a throwaway hook body (not committed): minecraft:stick given a buffer (capacity 1000, in 300, out 200; the minecraft: guard lifted for it, so the path a real mod item takes through the guard ran only in the unit tests), and native_use_item charging the held stick until full, then draining it, logging each call.

  • Server thread: right-click ×4: 0 → 300 → 600 → 900 → 1000 (the last took 100, the room left). data get entity @s SelectedItem: {"minecraft:custom_data": {horizon_energy: 1000}}. After quitting and reopening the world: still 1000. Right-click ×5: 1000 → 800 → … → 0, then the item reads {count: 1, id: "minecraft:stick"} with no component, and give @s minecraft:stick 1 stacked it to count: 2.
  • Dedicated server, a client joined: the client's right-click ×2 charged the stick on the server to 600; the client's own copy (horizon-ctl inventory) shows {"minecraft:custom_data":{horizon_energy:600}}. After a server restart and rejoin: 600, then 900 → 1000 (took 100) → drained to 800.

Self-check

pr_ready (tools/pr_ready.py --quick --base upstream/main on kased-h610mkddr4, against upstream/main at f8bfa06b9, 2 min 41 s)

Check Result
Scope ✅ 4 files, +371 −0
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 595 passed (26 s)
rustfmt ✅ (6 s)
clippy ✅ (1 min 7 s)
tests: horizon-client ✅ 600 passed (59 s)
doc tests ✅ 0 passed (1 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 ➖ 293 affected, to run in CI (--quick): armor_stand_frost, arrow, arrow_wind_charge, blending, block_particles, boat_authority, boat_paddle_reset, boat_submerged, breaking_model, datafix, datapack_worldgen, decorated_region and 281 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

After a rebase onto main at 6ed2d97cd: cargo check -p horizon-client --lib --bins --tests clean, no warnings.

🤖 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/6596 **Author:** [@ErikKalikas](https://github.com/ErikKalikas) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `mod-item-energy` --- ### 📝 Commits (6) - [`6519945`](https://github.com/horizon-rust/horizon/commit/65199450e480086db15bb0c19279f953ce178f1e) Native mods: energy in an item, Capabilities.Energy.ITEM - [`0e3fbaf`](https://github.com/horizon-rust/horizon/commit/0e3fbaf000fa6cf6c36add47c37f4ffe1931256d) Native mods: the item energy hook's mod API contract - [`4b0a92f`](https://github.com/horizon-rust/horizon/commit/4b0a92f9c19f848df76221b9ca229257c0b9f582) Merge main into #6596 - [`0f1430f`](https://github.com/horizon-rust/horizon/commit/0f1430f092529523b226b255411c772add67d372) Merge main into #6596 - [`9c9cb89`](https://github.com/horizon-rust/horizon/commit/9c9cb89f9876817e35448ef5242634144e1877f9) Mod API entries and regenerated output for #6596 - [`5aceab8`](https://github.com/horizon-rust/horizon/commit/5aceab866a54837e3194a2d1fdf58df95017e50b) Let the mod API name ItemEnergyBuffer ### 📊 Changes **7 files changed** (+424 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+2 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+17 -0) ➕ `crates/horizon-client/src/game/native_item_energy.rs` (+341 -0) 📝 `crates/horizon-client/src/mod_api.rs` (+4 -4) 📝 `crates/horizon-client/src/mod_api/generated.rs` (+42 -0) ➕ `tools/native-hook-fragments/item_energy_buffer.json` (+16 -0) 📝 `xtask/src/mod_api.rs` (+2 -0) </details> ### 📄 Description > 🤖 Opened by ErikKalikas's AI agent (Claude Code, Claude Opus 5.5), not by ErikKalikas personally. ## What and why Fixes #6553, part of #4953 ("Energy on items (`Capabilities.Energy.ITEM`)"). A tech mod's batteries and charged tools are items, and a mod had no way to say an item holds energy, nor to charge or drain one and get the changed item back. Horizon has energy in mod blocks (#6330) and fluid in items (#6331); this is the item half of energy, in the shape of both. One hook point and three helpers, in a new `game/native_item_energy.rs`. API from a fragment (`tools/native-hook-fragments/item_energy_buffer.json`): - `native_item_energy_buffer(item, buffer)`: a mod item (`mod_id:name`) holds energy; set its `ItemEnergyBuffer` as `SimpleEnergyHandler` is built: `capacity`, `max_insert` and `max_extract` per call. Checked as a block's buffer is (#6330): a capacity under 1 leaves it out, the limits are clamped to 0..=capacity. Asked at each call; `minecraft:` items have none. - The amount is kept in the stack's `minecraft:custom_data` as the int `horizon_energy` (the block keeps `energy`), so it is saved and sent with the stack, and the rest of the `custom_data` is kept. An emptied item carries nothing, so it stacks with a new one, also when the stack came from a server with its wire patch. - `native_item_energy(stack)`: amount, capacity and limits of one item of the stack. - `native_item_energy_insert(stack, amount)` and `native_item_energy_extract(stack, amount)`: the amount moved (the least of the room or what is held, the limit and the amount asked) and the single item it becomes; the item itself when nothing moved. The stack given is not changed: the caller shrinks it by one and hands out the result (NeoForge's `ItemAccess` acts on one item at a time). NeoForge runs a simulation as a transaction that isn't committed; here it is the result left unused. They hold no world state and work on any side. Energy on entities (`Capabilities.Energy.ENTITY`) and a helper that moves energy between a block and an item (a charger slot) are separate. ## Kind - [x] feature: something new for players (mod authors) ## Vanilla reference Horizon-only: vanilla has no energy. The contract is NeoForge's: `Capabilities.Energy.ITEM` is an `EnergyHandler` over an `ItemAccess`, `SimpleEnergyHandler` a capacity with limits per insert and extract (names and behaviour from its public docs; no code read or copied). ## Other projects' code None. ## Proof in the real game Engine from this branch with a throwaway hook body (not committed): `minecraft:stick` given a buffer (capacity 1000, in 300, out 200; the `minecraft:` guard lifted for it, so the path a real mod item takes through the guard ran only in the unit tests), and `native_use_item` charging the held stick until full, then draining it, logging each call. - **Server thread:** right-click ×4: 0 → 300 → 600 → 900 → 1000 (the last took 100, the room left). `data get entity @s SelectedItem`: `{"minecraft:custom_data": {horizon_energy: 1000}}`. After quitting and reopening the world: still 1000. Right-click ×5: 1000 → 800 → … → 0, then the item reads `{count: 1, id: "minecraft:stick"}` with no component, and `give @s minecraft:stick 1` stacked it to `count: 2`. - **Dedicated server, a client joined:** the client's right-click ×2 charged the stick on the server to 600; the client's own copy (`horizon-ctl inventory`) shows `{"minecraft:custom_data":{horizon_energy:600}}`. After a server restart and rejoin: 600, then 900 → 1000 (took 100) → drained to 800. ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base upstream/main` on kased-h610mkddr4, against upstream/main at f8bfa06b9, 2 min 41 s) | Check | Result | | --- | --- | | Scope | ✅ 4 files, +371 −0 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 595 passed (26 s) | | rustfmt | ✅ (6 s) | | clippy | ✅ (1 min 7 s) | | tests: horizon-client | ✅ 600 passed (59 s) | | doc tests | ✅ 0 passed (1 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 | ➖ 293 affected, to run in CI (--quick): armor_stand_frost, arrow, arrow_wind_charge, blending, block_particles, boat_authority, boat_paddle_reset, boat_submerged, breaking_model, datafix, datapack_worldgen, decorated_region and 281 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | After a rebase onto main at `6ed2d97cd`: `cargo check -p horizon-client --lib --bins --tests` clean, no warnings. 🤖 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:30:30 +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#6974
No description provided.