[PR #5130] [MERGED] Native mods: read and take a block's items through a side, and move them (API next) #6093

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

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/5130
Author: @Qwerner88880
Created: 10/31/2026
Status: ✅ Merged
Merged: 10/31/2026
Merged by: @levzzz5154

Base: main ← Head: hooks-item-handler


📝 Commits (4)

  • 7fc7fc4 Native mods: read and take a block's items through a side, and move them
  • 1992ad0 Merge main; the block items helpers as an API fragment
  • b8e9257 Merge remote-tracking branch 'hr/main' into qw-5130
  • 4c7b07a Merge remote-tracking branch 'hr/main' into qw-5130

📊 Changes

6 files changed (+376 additions, -45 deletions)

View changed files

📝 crates/horizon-client/src/game/composter.rs (+3 -1)
📝 crates/horizon-client/src/game/machines.rs (+71 -41)
➕ crates/horizon-client/src/game/machines/native_items.rs (+278 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+4 -3)
📝 docs/native-mods.md (+12 -0)
➕ tools/native-hook-fragments/block_items.json (+8 -0)

📄 Description

🤖 Opened by Qwerner88880's AI agent (ZCode, GLM-5.3), not by Qwerner88880 personally.

What and why

Fixes #5029. Part of #3911 (Mods), of #4953 (Capabilities: the item handler on blocks). NATIVE_HOOKS_API 40 → 41 — the number is the next free one and moves if another branch merges first.

Three helpers and a level name, so a native mod can do what a hopper does to a chest without knowing every block entity's rules:

  • native_container_slots(dimension, pos, face): the slots open through that side — a furnace's top is the input, its bottom the output and fuel, a double chest is both halves — each slot with its stack and whether a hopper could take it. None for a block that isn't a container there, a chunk that isn't loaded, another level than the one in place.
  • native_extract_item(dimension, pos, face, slot, count): takes up to count from one slot through that side, with the side's rules: fuel won't leave a furnace's bottom, a composter holds its bone meal.
  • native_move_items(dimension, from, from_face, to, to_face, item, max): moves up to max items between two containers the way a hopper does — one item at a time, and an item the target refuses goes back to its slot, so nothing is lost when the target fills up.
  • native_dimension(): the level a machine's tick runs in; the tick isn't told it.

native_insert_item changes with them, fixing what #5029 listed: it goes one item at a time and tells the container once, so an empty slot fills to the stack's own limit and no further; a client's copy of the world is left alone; and a machine can't reach its own slots during its tick — what went in there used to land on the placeholder and vanish.

Real hoppers change too, as vanilla has them

  • A shulker box no longer goes into a shulker box (HopperBlockEntity refuses it).
  • A stack's max_stack_size component sets its limit: 99-slot stacks move and stack to 99, 16-limit stacks stop at 16.
  • A comparator's signal reads the component's limit too, instead of clamping every stack to 64: one full slot of 99 in a hopper reads 3, not 5.
  • A slot holding an empty stack (count == 0, which a mod can leave) counts as free.
  • If the target's block entity went away between the look and the move, the item comes back instead of disappearing (move_in_item, composter_container_insert).

A composter takes one item per call, as in vanilla; its bone meal stays hopper-only.

Kind

  • parity: Horizon now does what vanilla 26.3 does
  • bug: a crash or a breakage
  • 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

Checked against the game jar (server-game.jar, javap):

  • HopperBlockEntity.addItem / tryMoveInItem: one item at a time; isFullContainer, isEmptyContainer and the shulker refusal in HopperBlockEntity.
  • WorldlyContainer.getSlotsForFace / canPlaceItemThroughFace: the sides each container opens.
  • ItemStack.getMaxStackSize reads minecraft:max_stack_size; BaseContainerBlockEntity.setItem calls ItemStack.limitSize(getMaxStackSize(stack)) and then setChanged().
  • AbstractContainerMenu.getRedstoneSignalFromContainer: the fill ratio uses each stack's own max.

Other projects' code

none

Known gaps, knowingly left (all pre-existing, none touched by this branch)

  • /item replace block writes a chest slot without the setChanged part of BaseContainerBlockEntity.setItem (verified in the jar: setItem ends with setChanged), so comparators never re-read after it. The live scenario inserts through native_insert_item, which does tell the container. A fix belongs in commands/items.rs, not in this branch.
  • The player's menus ignore the component: menu.rs reads the registry's max only, so a 99-stack merges up to 64 in the player's inventory (vanilla's Inventory.add reads the component and takes 99). The possible overflow at menu.rs:1822 is unreachable: both paths that pick the slot (slot_with_space, the first empty one) guarantee count below the limit or zero. All merge paths use guarded or saturating arithmetic.
  • game/minecarts.rs:255: a comparator reading a chest minecart still clamps to 64 and ignores the component.
  • max_stack reads only Tag::Int and not a removed [!minecraft:max_stack_size] component (commands/items.rs:625 is the same).
  • /item ... block reaches only chest-like block entities (game/commands/items.rs:93); a furnace it silently skips.
  • Minecarts and boats with chests aren't reachable by these helpers.
  • Sending a message to a player who already left answers Ok, as API 40 documents.

Tests

Live, on this branch (HEAD 7fc7fc473): a throwaway mixin bundle on a dedicated server with two horizon-ctl --offscreen clients (Alex and Steve) and a content mod's machine block, then a single-player world; fresh temp folders, HORIZON_NO_AUDIO=1. RESULT: 85 checks, 0 failures (was 83/1 before the comparator scenario was moved to the notifying path — /item replace block doesn't setChanged, see the gaps below). Covers: reading each side of a furnace, a double chest, a mod block; extracts and moves with refusals (nothing lost); the hopper under a machine still fed by it; shulker-in-shulker; a 16-limit stack moving whole; a 99-limit stack filling one slot to 99 through an insert and a comparator reading it as 3; a composter taking one; restarts keeping the mod's slots; the client's copy staying out; a machine in the Nether naming its level.

Not verified live: hundreds of containers at once (perf), a real vanilla client beside the host.

tools/pr_ready.py --quick --base origin/main: inserted below after the final run.

Self-check

pr_ready (tools/pr_ready.py --quick on DESKTOP-H7P5E7K, against origin/main at 13ce6bcc6, 4 min 11 s)

Check Result
Scope ✅ 5 files, +377 −46
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 495 passed (1 min 5 s)
rustfmt ✅ (10 s)
clippy ✅ (53 s)
tests: horizon-client ✅ 1339 passed (1 min 58 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 ➖ not run (--quick): CI's parity job picks the affected harnesses
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

🤖 Generated with ZCode (GLM-5.3)


🔄 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/5130 **Author:** [@Qwerner88880](https://github.com/Qwerner88880) **Created:** 10/31/2026 **Status:** ✅ Merged **Merged:** 10/31/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `hooks-item-handler` --- ### 📝 Commits (4) - [`7fc7fc4`](https://github.com/horizon-rust/horizon/commit/7fc7fc4735cb698e3806274ebd8d085fa7b26dbe) Native mods: read and take a block's items through a side, and move them - [`1992ad0`](https://github.com/horizon-rust/horizon/commit/1992ad03ba964b7b73f4fe91b05332f8b697c183) Merge main; the block items helpers as an API fragment - [`b8e9257`](https://github.com/horizon-rust/horizon/commit/b8e9257861db027b1433ff9b9b509cb2ec3d9b42) Merge remote-tracking branch 'hr/main' into qw-5130 - [`4c7b07a`](https://github.com/horizon-rust/horizon/commit/4c7b07a6ea89c0df811d1bdf4d37afd6373bcaa5) Merge remote-tracking branch 'hr/main' into qw-5130 ### 📊 Changes **6 files changed** (+376 additions, -45 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/composter.rs` (+3 -1) 📝 `crates/horizon-client/src/game/machines.rs` (+71 -41) ➕ `crates/horizon-client/src/game/machines/native_items.rs` (+278 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+4 -3) 📝 `docs/native-mods.md` (+12 -0) ➕ `tools/native-hook-fragments/block_items.json` (+8 -0) </details> ### 📄 Description > 🤖 Opened by Qwerner88880's AI agent (ZCode, GLM-5.3), not by Qwerner88880 personally. ## What and why Fixes #5029. Part of #3911 (Mods), of #4953 (Capabilities: the item handler on blocks). `NATIVE_HOOKS_API` 40 → **41** — the number is the next free one and moves if another branch merges first. Three helpers and a level name, so a native mod can do what a hopper does to a chest without knowing every block entity's rules: - **`native_container_slots(dimension, pos, face)`:** the slots open through that side — a furnace's top is the input, its bottom the output and fuel, a double chest is both halves — each slot with its stack and whether a hopper could take it. `None` for a block that isn't a container there, a chunk that isn't loaded, another level than the one in place. - **`native_extract_item(dimension, pos, face, slot, count)`:** takes up to `count` from one slot through that side, with the side's rules: fuel won't leave a furnace's bottom, a composter holds its bone meal. - **`native_move_items(dimension, from, from_face, to, to_face, item, max)`:** moves up to `max` items between two containers the way a hopper does — one item at a time, and an item the target refuses goes back to its slot, so nothing is lost when the target fills up. - **`native_dimension()`:** the level a machine's tick runs in; the tick isn't told it. `native_insert_item` changes with them, fixing what #5029 listed: it goes one item at a time and tells the container once, so an empty slot fills to the stack's own limit and no further; a client's copy of the world is left alone; and a machine can't reach its own slots during its tick — what went in there used to land on the placeholder and vanish. ## Real hoppers change too, as vanilla has them - A shulker box no longer goes into a shulker box (`HopperBlockEntity` refuses it). - A stack's `max_stack_size` component sets its limit: 99-slot stacks move and stack to 99, 16-limit stacks stop at 16. - A comparator's signal reads the component's limit too, instead of clamping every stack to 64: one full slot of 99 in a hopper reads 3, not 5. - A slot holding an empty stack (`count == 0`, which a mod can leave) counts as free. - If the target's block entity went away between the look and the move, the item comes back instead of disappearing (`move_in_item`, `composter_container_insert`). A composter takes one item per call, as in vanilla; its bone meal stays hopper-only. ## Kind - [ ] parity: Horizon now does what vanilla 26.3 does - [x] bug: a crash or a breakage - [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 Checked against the game jar (`server-game.jar`, `javap`): - `HopperBlockEntity.addItem` / `tryMoveInItem`: one item at a time; `isFullContainer`, `isEmptyContainer` and the shulker refusal in `HopperBlockEntity`. - `WorldlyContainer.getSlotsForFace` / `canPlaceItemThroughFace`: the sides each container opens. - `ItemStack.getMaxStackSize` reads `minecraft:max_stack_size`; `BaseContainerBlockEntity.setItem` calls `ItemStack.limitSize(getMaxStackSize(stack))` and then `setChanged()`. - `AbstractContainerMenu.getRedstoneSignalFromContainer`: the fill ratio uses each stack's own max. ## Other projects' code none ## Known gaps, knowingly left (all pre-existing, none touched by this branch) - **`/item replace block` writes a chest slot without the `setChanged` part of `BaseContainerBlockEntity.setItem`** (verified in the jar: `setItem` ends with `setChanged`), so comparators never re-read after it. The live scenario inserts through `native_insert_item`, which does tell the container. A fix belongs in `commands/items.rs`, not in this branch. - **The player's menus ignore the component:** `menu.rs` reads the registry's max only, so a 99-stack merges up to 64 in the player's inventory (vanilla's `Inventory.add` reads the component and takes 99). The possible overflow at `menu.rs:1822` is unreachable: both paths that pick the slot (`slot_with_space`, the first empty one) guarantee `count` below the limit or zero. All merge paths use guarded or saturating arithmetic. - `game/minecarts.rs:255`: a comparator reading a chest minecart still clamps to 64 and ignores the component. - `max_stack` reads only `Tag::Int` and not a removed `[!minecraft:max_stack_size]` component (`commands/items.rs:625` is the same). - `/item ... block` reaches only chest-like block entities (`game/commands/items.rs:93`); a furnace it silently skips. - Minecarts and boats with chests aren't reachable by these helpers. - Sending a message to a player who already left answers `Ok`, as API 40 documents. ## Tests **Live, on this branch (HEAD `7fc7fc473`):** a throwaway mixin bundle on a dedicated server with two `horizon-ctl --offscreen` clients (Alex and Steve) and a content mod's machine block, then a single-player world; fresh temp folders, `HORIZON_NO_AUDIO=1`. RESULT: 85 checks, 0 failures (was 83/1 before the comparator scenario was moved to the notifying path — `/item replace block` doesn't `setChanged`, see the gaps below). Covers: reading each side of a furnace, a double chest, a mod block; extracts and moves with refusals (nothing lost); the hopper under a machine still fed by it; shulker-in-shulker; a 16-limit stack moving whole; a 99-limit stack filling one slot to 99 through an insert and a comparator reading it as 3; a composter taking one; restarts keeping the mod's slots; the client's copy staying out; a machine in the Nether naming its level. **Not verified live:** hundreds of containers at once (perf), a real vanilla client beside the host. **`tools/pr_ready.py --quick --base origin/main`:** inserted below after the final run. ## Self-check **pr_ready** (`tools/pr_ready.py --quick` on DESKTOP-H7P5E7K, against origin/main at 13ce6bcc6, 4 min 11 s) | Check | Result | | --- | --- | | Scope | ✅ 5 files, +377 −46 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 495 passed (1 min 5 s) | | rustfmt | ✅ (10 s) | | clippy | ✅ (53 s) | | tests: horizon-client | ✅ 1339 passed (1 min 58 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 | ➖ not run (--quick): CI's parity job picks the affected harnesses | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | 🤖 Generated with ZCode (GLM-5.3) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:11:36 +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#6093
No description provided.