[PR #7624] [MERGED] Native mods: items in an item, Capabilities.Item.ITEM #7722

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

📋 Pull Request Information

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

Base: main ← Head: mod-item-container


📝 Commits (1)

  • a298731 Native mods: items in an item, Capabilities.Item.ITEM

📊 Changes

7 files changed (+500 additions, -0 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+1 -0)
📝 crates/horizon-client/src/game/mod_api_host_generated.rs (+18 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+12 -0)
➕ crates/horizon-client/src/game/native_item_container.rs (+370 -0)
📝 crates/horizon-client/src/mod_api/generated.rs (+38 -0)
📝 crates/horizon-client/src/mod_api/generated_host.rs (+40 -0)
➕ tools/native-hook-fragments/item_container.json (+21 -0)

📄 Description

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

What and why

Fixes #6768, part of #4953 ("Item handler on items (Capabilities.Item.ITEM)"). A tech mod's backpacks, storage cells and toolboxes are items that hold items. Horizon had items in blocks (#5130), fluid in items (#6331) and energy in items (#6596), but a mod had no way to say an item holds items, nor to fill or empty one and get the changed item back.

One hook point and three helpers, in a new game/native_item_container.rs, in the shape of the item fluid and energy helpers. API from a fragment (tools/native-hook-fragments/item_container.json), with the typed mod API's contract (replace, out: slots) and the three helpers in the generated Host; cargo run -p xtask -- mod-api filled the dispatch and the generated files.

  • native_item_container(item, slots): a mod item (mod_id:name) holds slots slots, clamped to 1..=256 (ItemContainerContents' limit); 0 leaves it without. Asked at each call; minecraft: items have none.
  • The contents live in the stack's minecraft:container, as a shulker box item keeps them, so they are saved and sent with the stack, and the rest of its 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_container_slots(stack): the slots of one item of the stack, empty ones included.
  • native_item_container_insert(stack, item): as much of item as fits, topping up the slots that hold the same item and components first, then the first empty slots (SimpleContainer.addItem's order). No slot takes more than its limit (Container.getMaxStackSize(stack): 99, or the stack's own when less, its max_stack_size component included). Unlike SimpleContainer, which puts the whole rest into the first empty slot and cuts it there (setItem → limitSize), what doesn't fit stays out, so nothing is lost. A shulker box never goes in (Item.canFitInsideContainerItems, Horizon's menu::fits_inside_container_items).
  • native_item_container_extract(stack, slot, max): up to max from a slot; none from an empty slot or one past the item's slots.
  • Both answer what moved and the single item it becomes, without changing the stack given: the caller shrinks it by one and hands out the result (NeoForge's ItemAccess acts on one item at a time); a result left unused is the simulation.

They hold no world state and work on any side. Not here (the open questions in #6768): vanilla's shulker boxes as built-in 27-slot containers, bundles (they hold by weight, BundleContents), and item handlers on entities.

Kind

  • feature: something new for players (mod authors)

Vanilla reference

SimpleContainer.addItem (moveItemToOccupiedSlotsWithSameType, moveItemsBetweenStacks, moveItemToEmptySlots), Container.getMaxStackSize, ShulkerBoxSlot.mayPlace → BlockItem.canFitInsideContainerItems, ItemContainerContents (the minecraft:container component). NeoForge's Capabilities.Item.ITEM is a ResourceHandler<ItemResource> over an ItemAccess (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 3 slots (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 putting the offhand stack into the held stick, or, with an empty offhand, taking the first filled slot out into it, logging each call.

  • Server thread: stone 64 then 6: [stone 64, stone 6, empty]; ender pearls 16: the last slot; 16 more: moved=0 (no room); a red shulker box: moved=0. data get entity @s SelectedItem shows the three slots in minecraft:container. After quitting and reopening the world they are still there; three takes give stone 64, stone 6, pearls 16, the item reads {count: 1, id: "minecraft:stick"} with no component, and give @s minecraft:stick 1 stacks it to count: 2.
  • Dedicated server, a client joined: the client's clicks put 40 stone and 10 apples in on the server; the client's own copy (horizon-ctl inventory) holds the same minecraft:container. After a server restart and rejoin both slots are there, and a take gives the 40 stone back.

Self-check

pr_ready (tools/pr_ready.py --quick --base upstream/main on kased-h610mkddr4, against upstream/main at 97dc1b553, 3 min 49 s)

Check Result
Scope ✅ 7 files, +500 −0
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 614 passed (28 s)
rustfmt ✅ (6 s)
clippy ✅ (9 s)
tests: horizon-client, horizon-client-lib ✅ 2171 passed (2 min 50 s)
doc tests ✅ 0 passed (2 s)
content root tests ➖ content root untouched
mod API up to date ✅ (11 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 298 affected, to run in CI (--quick): anvil_turtle_egg, armor_stand_frost, armor_stand_turtle_egg, arrow, arrow_wind_charge, blending, block_particles, boat_authority, boat_paddle_reset, boat_submerged, breaking_model, datafix and 286 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 26c83240d: cargo run -p xtask -- mod-api --check clean.

🤖 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/7624 **Author:** [@ErikKalikas](https://github.com/ErikKalikas) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `mod-item-container` --- ### 📝 Commits (1) - [`a298731`](https://github.com/horizon-rust/horizon/commit/a29873128541133197dbb1c7682a6c4095071cff) Native mods: items in an item, Capabilities.Item.ITEM ### 📊 Changes **7 files changed** (+500 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+1 -0) 📝 `crates/horizon-client/src/game/mod_api_host_generated.rs` (+18 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+12 -0) ➕ `crates/horizon-client/src/game/native_item_container.rs` (+370 -0) 📝 `crates/horizon-client/src/mod_api/generated.rs` (+38 -0) 📝 `crates/horizon-client/src/mod_api/generated_host.rs` (+40 -0) ➕ `tools/native-hook-fragments/item_container.json` (+21 -0) </details> ### 📄 Description > 🤖 Opened by ErikKalikas's AI agent (Claude Code, Claude Opus 5.5), not by ErikKalikas personally. ## What and why Fixes #6768, part of #4953 ("Item handler on items (`Capabilities.Item.ITEM`)"). A tech mod's backpacks, storage cells and toolboxes are items that hold items. Horizon had items in blocks (#5130), fluid in items (#6331) and energy in items (#6596), but a mod had no way to say an item holds items, nor to fill or empty one and get the changed item back. One hook point and three helpers, in a new `game/native_item_container.rs`, in the shape of the item fluid and energy helpers. API from a fragment (`tools/native-hook-fragments/item_container.json`), with the typed mod API's contract (`replace`, `out: slots`) and the three helpers in the generated `Host`; `cargo run -p xtask -- mod-api` filled the dispatch and the generated files. - `native_item_container(item, slots)`: a mod item (`mod_id:name`) holds `slots` slots, clamped to 1..=256 (`ItemContainerContents`' limit); 0 leaves it without. Asked at each call; `minecraft:` items have none. - The contents live in the stack's `minecraft:container`, as a shulker box item keeps them, so they are saved and sent with the stack, and the rest of its 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_container_slots(stack)`: the slots of one item of the stack, empty ones included. - `native_item_container_insert(stack, item)`: as much of `item` as fits, topping up the slots that hold the same item and components first, then the first empty slots (`SimpleContainer.addItem`'s order). No slot takes more than its limit (`Container.getMaxStackSize(stack)`: 99, or the stack's own when less, its `max_stack_size` component included). Unlike `SimpleContainer`, which puts the whole rest into the first empty slot and cuts it there (`setItem` → `limitSize`), what doesn't fit stays out, so nothing is lost. A shulker box never goes in (`Item.canFitInsideContainerItems`, Horizon's `menu::fits_inside_container_items`). - `native_item_container_extract(stack, slot, max)`: up to `max` from a slot; none from an empty slot or one past the item's slots. - Both answer what moved and the single item it becomes, without changing the stack given: the caller shrinks it by one and hands out the result (NeoForge's `ItemAccess` acts on one item at a time); a result left unused is the simulation. They hold no world state and work on any side. Not here (the open questions in #6768): vanilla's shulker boxes as built-in 27-slot containers, bundles (they hold by weight, `BundleContents`), and item handlers on entities. ## Kind - [x] feature: something new for players (mod authors) ## Vanilla reference `SimpleContainer.addItem` (`moveItemToOccupiedSlotsWithSameType`, `moveItemsBetweenStacks`, `moveItemToEmptySlots`), `Container.getMaxStackSize`, `ShulkerBoxSlot.mayPlace` → `BlockItem.canFitInsideContainerItems`, `ItemContainerContents` (the `minecraft:container` component). NeoForge's `Capabilities.Item.ITEM` is a `ResourceHandler<ItemResource>` over an `ItemAccess` (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 3 slots (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` putting the offhand stack into the held stick, or, with an empty offhand, taking the first filled slot out into it, logging each call. - **Server thread:** stone 64 then 6: `[stone 64, stone 6, empty]`; ender pearls 16: the last slot; 16 more: `moved=0` (no room); a red shulker box: `moved=0`. `data get entity @s SelectedItem` shows the three slots in `minecraft:container`. After quitting and reopening the world they are still there; three takes give stone 64, stone 6, pearls 16, the item reads `{count: 1, id: "minecraft:stick"}` with no component, and `give @s minecraft:stick 1` stacks it to `count: 2`. - **Dedicated server, a client joined:** the client's clicks put 40 stone and 10 apples in on the server; the client's own copy (`horizon-ctl inventory`) holds the same `minecraft:container`. After a server restart and rejoin both slots are there, and a take gives the 40 stone back. ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base upstream/main` on kased-h610mkddr4, against upstream/main at 97dc1b553, 3 min 49 s) | Check | Result | | --- | --- | | Scope | ✅ 7 files, +500 −0 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 614 passed (28 s) | | rustfmt | ✅ (6 s) | | clippy | ✅ (9 s) | | tests: horizon-client, horizon-client-lib | ✅ 2171 passed (2 min 50 s) | | doc tests | ✅ 0 passed (2 s) | | content root tests | ➖ content root untouched | | mod API up to date | ✅ (11 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 298 affected, to run in CI (--quick): anvil_turtle_egg, armor_stand_frost, armor_stand_turtle_egg, arrow, arrow_wind_charge, blending, block_particles, boat_authority, boat_paddle_reset, boat_submerged, breaking_model, datafix and 286 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 `26c83240d`: `cargo run -p xtask -- mod-api --check` clean. 🤖 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 07:23:02 +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#7722
No description provided.