[PR #4677] [MERGED] Native mods: hook points for item behaviour (API next) #5776

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

📋 Pull Request Information

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

Base: main ← Head: hooks-items


📝 Commits (5)

  • 3c257d8 Native mods: hook points for item behaviour (API 21)
  • 4c88da0 Merge main; item behaviour hooks are API 22
  • fbb8f4e native_inventory_tick leaves the stack in its slot and runs when frozen
  • 429fda5 Merge main; item behaviour hooks are API 23
  • ae45f04 Merge hr/main; renumber item behaviour hooks to API 39

📊 Changes

7 files changed (+109 additions, -1 deletions)

View changed files

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

📄 Description

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

What and why

Fixes #4673. Part of #3911 (Mods), gap I of #1854 (item behaviour hooks), next to #4302 (using an item). A native mod can now react when a held item hits a mob, run code for an item while a player carries it, and tell when equipment changes. Weapons with an on-hit effect, items that recharge or tick down, and armour with an effect need these. Named after the vanilla calls they sit in, as on #4402.

  • native_hurt_enemy(&mut self, stack: &ItemStack, entity: u32, kind: &str): called in weapon_hit, where Player.itemAttackInteraction reaches Item.hurtEnemy, once the hit landed. It only hears. Another player as the target isn't told yet.
  • native_inventory_tick(&mut self, slot: i32, selected: bool): Item.inventoryTick from Inventory.tick, each server tick for every stack a player carries, /tick freeze included. slot is as Inventory saves it: 0 to 35, armour 100 (feet) to 103 (head), -106 the off hand. The helper native_carried_stack(slot) reads and changes the stack, which stays in its slot, so the item helpers see it; a count of 0 empties the slot.
  • native_on_equip(&mut self, slot: &str, new: Option<&ItemStack>): in player_equipped, the body of LivingEntity.onEquipItem once the item changed, before the equip sound. It only hears, and never for a spectator. Every path that calls onEquipItem goes through it.

All three are server-side, for every player of the world (the single player server thread, LAN guests, dedicated servers); the helpers (native_player...) read the acting player. Nothing changes without a native mod. NATIVE_HOOKS_API goes to the next free number: 23 in the code on this base. The title says "API next", as #4402 proposes, because the number moves with whatever merges first, with the native-hooks.json entries and an "API 23" paragraph in docs/native-mods.md.

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). They sit at Item.hurtEnemy (Player.itemAttackInteraction), Item.inventoryTick (Inventory.tick) and LivingEntity.onEquipItem. Without a native mod, gameplay is unchanged; CI's parity job picks the harnesses that cover these files. I didn't run parity here (no vanilla data on this machine).

Tested in the game

A throwaway horizon-mixins bundle (not in this pull request) hooked all three and logged each call, applied with horizon-mixins apply and built with --profile fast. A fresh survival world on the single player server thread, driven through horizon-ctl, on Windows with an RTX 5070:

Check Hook Result
Hit a pig with a netherite sword hurt_enemy once, with the sword, the pig's id and minecraft:pig
Hold the sword inventory_tick slot=0, selected=true
/give 5 sticks; the hook sets the count to 1 inventory_tick the window's inventory shows 1 stick
Hold apples; the hook gives 5 arrows with native_give_items from the held stack's tick inventory_tick 3 apples and 5 arrows in the window; the helper counts them all
/tick freeze, then /give 7 sticks inventory_tick cut to 1 while frozen
/give 2 dirt; the hook sets the count to 0 inventory_tick the slot is empty in the window
/item replace ... armor.head with an iron helmet, then air on_equip, inventory_tick head with the helmet, then head with none; slot 103 ticks while worn
Right-click a diamond chestplate on_equip chest with the chestplate

No errors in the log. On a dedicated server with two real clients: --dedicated --nogui and two Horizon clients (Alex, Steve) under horizon-ctl --offscreen, over TCP on localhost, on Windows, with a throwaway bundle hooking all of this. It ran on a local merge of #4669, #4677 and #4725 with main, not on this branch alone; all 56 checks passed.

Check Result
Each player hits a mob with a sword hurt_enemy once, for the acting player
/give Alex sticks and Steve dirt; the hook changes them only that player's stack changes, and that player's client shows it
Each player holds apples; the hook gives 5 arrows from the tick 3 apples and 5 arrows each, none lost
/tick freeze, then /give the stack still ticks
Alex and Steve equip armour by right click on_equip names only that player; slot 103 ticks while worn

Not tested: taking armour off on a dedicated server (done in single player, by /item), the swap-hands key, a menu click on an armour slot, the host's own path under /tick freeze, and a real mod using these hooks.

Self-check

tools/pr_ready.py --quick --base origin/main on Windows.

horizon-client doesn't currently build on Windows on main, because #4732 added unsafe to save.rs under #![forbid(unsafe_code)], so I ran this and the game with that one line relaxed locally. It isn't in this pull request, and the table counts it.

pr_ready (tools/pr_ready.py --quick on DESKTOP-H7P5E7K, against origin/main at 672b204c0, 6 min 1 s)

Check Result
Scope ✅ 8 files, +110 −2
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 481 passed (1 min 5 s)
rustfmt ✅ (7 s)
clippy ✅ (1 min 10 s)
tests: horizon-client ✅ 1211 passed (2 min 40 s)
doc tests ✅ 0 passed (56 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 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/4677 **Author:** [@Qwerner88880](https://github.com/Qwerner88880) **Created:** 10/30/2026 **Status:** ✅ Merged **Merged:** 10/31/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `hooks-items` --- ### 📝 Commits (5) - [`3c257d8`](https://github.com/horizon-rust/horizon/commit/3c257d870c492467b6409fd89d5331f6afdb0682) Native mods: hook points for item behaviour (API 21) - [`4c88da0`](https://github.com/horizon-rust/horizon/commit/4c88da00961a6165a42167f14d7f714ec88f3b9e) Merge main; item behaviour hooks are API 22 - [`fbb8f4e`](https://github.com/horizon-rust/horizon/commit/fbb8f4e6d78d3f2c321b72c0469ad2fbbdf536c6) native_inventory_tick leaves the stack in its slot and runs when frozen - [`429fda5`](https://github.com/horizon-rust/horizon/commit/429fda5680aa8d4c59aaa3b11bca66ef714b5533) Merge main; item behaviour hooks are API 23 - [`ae45f04`](https://github.com/horizon-rust/horizon/commit/ae45f040b76d94cf9a287f9400e4c5fe72616b49) Merge hr/main; renumber item behaviour hooks to API 39 ### 📊 Changes **7 files changed** (+109 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+1 -0) 📝 `crates/horizon-client/src/game/lan_actions.rs` (+1 -0) 📝 `crates/horizon-client/src/game/mace.rs` (+1 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+85 -1) 📝 `crates/horizon-client/src/game/player_equip.rs` (+1 -0) 📝 `docs/native-mods.md` (+5 -0) 📝 `native-hooks.json` (+15 -0) </details> ### 📄 Description > 🤖 Opened by Qwerner88880's AI agent (Claude Code, Claude Opus 5.5), not by Qwerner88880 personally. ## What and why Fixes #4673. Part of #3911 (Mods), gap **I** of #1854 (item behaviour hooks), next to #4302 (using an item). A native mod can now react when a held item hits a mob, run code for an item while a player carries it, and tell when equipment changes. Weapons with an on-hit effect, items that recharge or tick down, and armour with an effect need these. Named after the vanilla calls they sit in, as on #4402. - **`native_hurt_enemy(&mut self, stack: &ItemStack, entity: u32, kind: &str)`**: called in `weapon_hit`, where `Player.itemAttackInteraction` reaches `Item.hurtEnemy`, once the hit landed. It only hears. Another player as the target isn't told yet. - **`native_inventory_tick(&mut self, slot: i32, selected: bool)`**: `Item.inventoryTick` from `Inventory.tick`, each server tick for every stack a player carries, `/tick freeze` included. `slot` is as `Inventory` saves it: 0 to 35, armour 100 (feet) to 103 (head), -106 the off hand. The helper `native_carried_stack(slot)` reads and changes the stack, which stays in its slot, so the item helpers see it; a count of 0 empties the slot. - **`native_on_equip(&mut self, slot: &str, new: Option<&ItemStack>)`**: in `player_equipped`, the body of `LivingEntity.onEquipItem` once the item changed, before the equip sound. It only hears, and never for a spectator. Every path that calls `onEquipItem` goes through it. All three are server-side, for every player of the world (the single player server thread, LAN guests, dedicated servers); the helpers (`native_player`...) read the acting player. Nothing changes without a native mod. `NATIVE_HOOKS_API` goes to the next free number: 23 in the code on this base. The title says "API next", as #4402 proposes, because the number moves with whatever merges first, with the `native-hooks.json` entries and an "API 23" paragraph in `docs/native-mods.md`. ## 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). They sit at `Item.hurtEnemy` (`Player.itemAttackInteraction`), `Item.inventoryTick` (`Inventory.tick`) and `LivingEntity.onEquipItem`. Without a native mod, gameplay is unchanged; CI's parity job picks the harnesses that cover these files. I didn't run parity here (no vanilla data on this machine). ## Tested in the game A throwaway horizon-mixins bundle (not in this pull request) hooked all three and logged each call, applied with `horizon-mixins apply` and built with `--profile fast`. A fresh survival world on the single player server thread, driven through `horizon-ctl`, on Windows with an RTX 5070: | Check | Hook | Result | | --- | --- | --- | | Hit a pig with a netherite sword | `hurt_enemy` | once, with the sword, the pig's id and `minecraft:pig` | | Hold the sword | `inventory_tick` | `slot=0`, `selected=true` | | `/give` 5 sticks; the hook sets the count to 1 | `inventory_tick` | the window's inventory shows 1 stick | | Hold apples; the hook gives 5 arrows with `native_give_items` from the held stack's tick | `inventory_tick` | 3 apples and 5 arrows in the window; the helper counts them all | | `/tick freeze`, then `/give` 7 sticks | `inventory_tick` | cut to 1 while frozen | | `/give` 2 dirt; the hook sets the count to 0 | `inventory_tick` | the slot is empty in the window | | `/item replace ... armor.head` with an iron helmet, then air | `on_equip`, `inventory_tick` | `head` with the helmet, then `head` with none; slot 103 ticks while worn | | Right-click a diamond chestplate | `on_equip` | `chest` with the chestplate | No errors in the log. **On a dedicated server with two real clients:** `--dedicated --nogui` and two Horizon clients (Alex, Steve) under `horizon-ctl --offscreen`, over TCP on localhost, on Windows, with a throwaway bundle hooking all of this. It ran on a local merge of #4669, #4677 and #4725 with main, not on this branch alone; all 56 checks passed. | Check | Result | | --- | --- | | Each player hits a mob with a sword | `hurt_enemy` once, for the acting player | | `/give` Alex sticks and Steve dirt; the hook changes them | only that player's stack changes, and that player's client shows it | | Each player holds apples; the hook gives 5 arrows from the tick | 3 apples and 5 arrows each, none lost | | `/tick freeze`, then `/give` | the stack still ticks | | Alex and Steve equip armour by right click | `on_equip` names only that player; slot 103 ticks while worn | **Not tested:** taking armour off on a dedicated server (done in single player, by `/item`), the swap-hands key, a menu click on an armour slot, the host's own path under `/tick freeze`, and a real mod using these hooks. ## Self-check `tools/pr_ready.py --quick --base origin/main` on Windows. `horizon-client` doesn't currently build on Windows on main, because #4732 added `unsafe` to `save.rs` under `#![forbid(unsafe_code)]`, so I ran this and the game with that one line relaxed locally. It isn't in this pull request, and the table counts it. **pr_ready** (`tools/pr_ready.py --quick` on DESKTOP-H7P5E7K, against origin/main at 672b204c0, 6 min 1 s) | Check | Result | | --- | --- | | Scope | ✅ 8 files, +110 −2 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 481 passed (1 min 5 s) | | rustfmt | ✅ (7 s) | | clippy | ✅ (1 min 10 s) | | tests: horizon-client | ✅ 1211 passed (2 min 40 s) | | doc tests | ✅ 0 passed (56 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 [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 05:14:55 +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#5776
No description provided.