[PR #4302] [MERGED] Native mods: hook points for using an item (API 5) #5549

Closed
opened 2026-11-03 05:13:20 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/4302
Author: @ludovicthenot
Created: 10/30/2026
Status: ✅ Merged
Merged: 10/30/2026
Merged by: @levzzz5154

Base: main ← Head: native-use-item-hook


📝 Commits (2)

  • 4ee9816 Native mods: hook points for using an item (API 5)
  • 0e060bb Merge main: use-item hooks are API 13

📊 Changes

4 files changed (+57 additions, -2 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+15 -1)
📝 crates/horizon-client/src/game/native_hooks.rs (+27 -1)
📝 docs/native-mods.md (+5 -0)
📝 native-hooks.json (+10 -0)

📄 Description

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

Stacked on #4284 (API 4): review only the last commit (4ee9816b8, +57 −2).

What and why

Fixes #4290. Part of #3911 (Mods), gap I of #1854 (item behaviour hooks): a native mod can now react when a player uses an item (right click), which wands, staffs, tools with a right-click action and items that open a mod's screen need.

Two hook points, one per vanilla call, so each hears a click once:

  • native_use_item_on(&mut self, stack: &ItemStack, pos: [i32; 3], face: [i32; 3], claimed: &mut bool): a right click with an item on a block, once the block let it through (a chest still opens with a wand in hand, as vanilla orders it). Where ItemStack.useOn starts.
  • native_use_item(&mut self, stack: &ItemStack, claimed: &mut bool): the item's own use, in the air or on a block after native_use_item_on let it through. Where Item.use starts.
  • A mod that sets claimed takes the use: the hand swings (swing_arm, sent back to a guest by lan_swing_back) and the item's use doesn't run (claiming use_item_on also skips use_item).
  • Called in use_hand (game.rs), after the cooldown check (an item on cooldown is never used). Every player goes through it on the server (the single player server thread, LAN guests, dedicated servers), so it is authoritative; self.hand is the hand, and the helpers (native_player...) read the acting player. A guest's UseItemOn packet reaches only native_use_item_on and its UseItem only native_use_item, so a guest's click is heard once too.
  • Not claimed, the use goes on exactly as before: nothing changes without a native mod.
  • NATIVE_HOOKS_API 4 → 5, its native-hooks.json entries (use_item_on and use_item, version 1), and an "API 5" paragraph in docs/native-mods.md. The number follows #4284 and moves with it.

The first version of this pull request had one hook, native_use_item with an optional block, which a guest's click on a block reached twice (once per packet). It is split into the two vanilla calls instead.

Kind

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

Horizon-only (hook points; they do nothing in Horizon). native_use_item_on sits where ServerPlayerGameMode.useItemOn reaches ItemStack.useOn (the block's useItemOn/useWithoutItem passed), native_use_item where ServerPlayerGameMode.useItem reaches Item.use, both after ItemCooldowns.isOnCooldown. Without a native mod the item uses are unchanged; CI's parity job picks the harnesses that cover game.rs.

Tested in the game

A throwaway local change (not in this pull request) logged both hooks and made native_use_item claim when an environment variable was set. Fresh survival worlds on the server thread, through horizon-ctl (--offscreen), /give @s minecraft:snowball 16, then use-item:

Click Hooks called Snowballs after
in the air, no claim use_item once 15 (thrown)
on the ground, no claim use_item_on (top face), then use_item, once each 15 (thrown)
on the ground, use_item claims use_item_on, then use_item 16 (not thrown)

A LAN guest's two packets were not tested with a second client.

Self-check

pr_ready (tools/pr_ready.py --base native-tooltip-hook on nemo, on the first version of this pull request, 2 min 48 s)

Check Result
Scope ✅ 4 files
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 480 passed (1 min 43 s)
rustfmt ✅ (17 s)
clippy ✅ (16 s)
tests: horizon-client ❌ 727 passed, 1 failed (22 s)
doc tests ✅ 0 passed (7 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 (no vanilla data here): 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)

The one failure, title::tests::each_world_preset_writes_its_own_generator (Os { code: 267, kind: NotADirectory }), fails the same way on upstream/main on this Windows machine (see #4284): not from this pull request.

After the split, on this commit: cargo fmt --check ✅, cargo clippy -p horizon-client --bin horizon-client ✅ (no warnings), native_hooks::tests ✅ 4 passed (the catalog test checks both entries).


🔄 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/4302 **Author:** [@ludovicthenot](https://github.com/ludovicthenot) **Created:** 10/30/2026 **Status:** ✅ Merged **Merged:** 10/30/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `native-use-item-hook` --- ### 📝 Commits (2) - [`4ee9816`](https://github.com/horizon-rust/horizon/commit/4ee9816b820c8e90a54192cb57f57f66c302266c) Native mods: hook points for using an item (API 5) - [`0e060bb`](https://github.com/horizon-rust/horizon/commit/0e060bb2acc91662046da7c561bad680abeb3b6c) Merge main: use-item hooks are API 13 ### 📊 Changes **4 files changed** (+57 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+15 -1) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+27 -1) 📝 `docs/native-mods.md` (+5 -0) 📝 `native-hooks.json` (+10 -0) </details> ### 📄 Description > 🤖 Opened by ludovicthenot's AI agent (Claude Code, Claude Opus 5.5), not by ludovicthenot personally. **Stacked on #4284 (API 4): review only the last commit** (`4ee9816b8`, +57 −2). ## What and why Fixes #4290. Part of #3911 (Mods), gap **I** of #1854 (item behaviour hooks): a native mod can now react when a player uses an item (right click), which wands, staffs, tools with a right-click action and items that open a mod's screen need. Two hook points, one per vanilla call, so each hears a click once: - **`native_use_item_on(&mut self, stack: &ItemStack, pos: [i32; 3], face: [i32; 3], claimed: &mut bool)`**: a right click with an item on a block, once the block let it through (a chest still opens with a wand in hand, as vanilla orders it). Where `ItemStack.useOn` starts. - **`native_use_item(&mut self, stack: &ItemStack, claimed: &mut bool)`**: the item's own use, in the air or on a block after `native_use_item_on` let it through. Where `Item.use` starts. - A mod that sets `claimed` takes the use: the hand swings (`swing_arm`, sent back to a guest by `lan_swing_back`) and the item's use doesn't run (claiming `use_item_on` also skips `use_item`). - Called in `use_hand` (game.rs), after the cooldown check (an item on cooldown is never used). Every player goes through it on the server (the single player server thread, LAN guests, dedicated servers), so it is authoritative; `self.hand` is the hand, and the helpers (`native_player`...) read the acting player. A guest's `UseItemOn` packet reaches only `native_use_item_on` and its `UseItem` only `native_use_item`, so a guest's click is heard once too. - Not claimed, the use goes on exactly as before: nothing changes without a native mod. - `NATIVE_HOOKS_API` 4 → 5, its `native-hooks.json` entries (`use_item_on` and `use_item`, version 1), and an "API 5" paragraph in `docs/native-mods.md`. The number follows #4284 and moves with it. The first version of this pull request had one hook, `native_use_item` with an optional block, which a guest's click on a block reached twice (once per packet). It is split into the two vanilla calls instead. ## Kind - [ ] parity: Horizon now does what vanilla 26.3 does - [ ] bug: a crash or a breakage - [ ] perf: faster, or less memory - [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 Horizon-only (hook points; they do nothing in Horizon). `native_use_item_on` sits where `ServerPlayerGameMode.useItemOn` reaches `ItemStack.useOn` (the block's `useItemOn`/`useWithoutItem` passed), `native_use_item` where `ServerPlayerGameMode.useItem` reaches `Item.use`, both after `ItemCooldowns.isOnCooldown`. Without a native mod the item uses are unchanged; CI's parity job picks the harnesses that cover `game.rs`. ## Tested in the game A throwaway local change (not in this pull request) logged both hooks and made `native_use_item` claim when an environment variable was set. Fresh survival worlds on the server thread, through `horizon-ctl` (`--offscreen`), `/give @s minecraft:snowball 16`, then `use-item`: | Click | Hooks called | Snowballs after | | --- | --- | --- | | in the air, no claim | `use_item` once | 15 (thrown) | | on the ground, no claim | `use_item_on` (top face), then `use_item`, once each | 15 (thrown) | | on the ground, `use_item` claims | `use_item_on`, then `use_item` | 16 (not thrown) | A LAN guest's two packets were not tested with a second client. ## Self-check **pr_ready** (`tools/pr_ready.py --base native-tooltip-hook` on nemo, on the first version of this pull request, 2 min 48 s) | Check | Result | | --- | --- | | Scope | ✅ 4 files | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 480 passed (1 min 43 s) | | rustfmt | ✅ (17 s) | | clippy | ✅ (16 s) | | tests: horizon-client | ❌ 727 passed, 1 failed (22 s) | | doc tests | ✅ 0 passed (7 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 (no vanilla data here): 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) | The one failure, `title::tests::each_world_preset_writes_its_own_generator` (`Os { code: 267, kind: NotADirectory }`), fails the same way on upstream/main on this Windows machine (see #4284): not from this pull request. After the split, on this commit: `cargo fmt --check` ✅, `cargo clippy -p horizon-client --bin horizon-client` ✅ (no warnings), `native_hooks::tests` ✅ 4 passed (the catalog test checks both entries). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 05:13:20 +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#5549
No description provided.