[PR #5905] [MERGED] feat(mods): native mods act on an item's use tick and release #6553

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

📋 Pull Request Information

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

Base: main ← Head: feat/native-item-use-tick-release


📝 Commits (2)

  • 57bac61 feat(mods): native mods act on an item's use tick and release
  • 4bcfd21 Merge main into native item use tick and release

📊 Changes

10 files changed (+423 additions, -52 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+33 -23)
📝 crates/horizon-client/src/game/crossbow.rs (+4 -3)
📝 crates/horizon-client/src/game/fov.rs (+5 -5)
📝 crates/horizon-client/src/game/item_draw.rs (+1 -1)
📝 crates/horizon-client/src/game/item_use.rs (+216 -8)
📝 crates/horizon-client/src/game/lan_actions.rs (+6 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+90 -4)
📝 crates/horizon-client/src/game/potions.rs (+18 -3)
📝 crates/horizon-client/src/game/remote.rs (+24 -5)
➕ tools/native-hook-fragments/item_use_tick_release.json (+26 -0)

📄 Description

What and why

Part of #5523

Follows #5649 (an item's use over time: its duration, animation, start and finish)
A mod's item still could not act while used or when let go: no tool that charges and fires on release, no tool that cooks while held
Vanilla calls Item.onUseTick each tick of a use (LivingEntity.updateUsingItem)
It calls Item.releaseUsing with the ticks left when the player lets go early (releaseUsingItem)
This adds four hook points for that, on the server thread, LAN and the in-frame path alike

native_living_entity_use_item_tick(stack, entity, hand, &mut duration, &mut canceled) fires where updatingUsingItem starts, each tick of every use
duration is the ticks left, and changing it moves the end
The client's crumbs and first person pose follow the moved end too, as they follow #5649's started duration
A cancel sets it to -1, as NeoForge's code does
Once a mod set it, the use goes on only while it is above 0
At 0 or less the item's tick is skipped, and the server finishes the use (completeUsingItem: finishUsingItem, the food eaten, the finish hook)
A potion run out this way is drunk without its own last tick (no drink sound, no random draw for it)
An item used on release (a crossbow) stays frozen until let go instead
NeoForge's javadoc says a cancel stops the use, but its code finishes it, and this follows the code
On a server's client a run-out use stays in use, frozen, so letting go still sends RELEASE_USE_ITEM
A use no mod touched goes on as vanilla's do, so an eye of ender's use of no time is still held until let go
native_on_use_tick(stack, entity, remaining) is Item.onUseTick, what a mod's own item does each tick it is used, with ticks left above 0
It comes where ItemStack.onUseTick calls the item, after a consumable's crumbs, for consumables (potions included), shields and mods' items
Vanilla's other items do their own, and a kinetic weapon has none on the server, as in vanilla
native_living_entity_use_item_stop(stack, entity, hand, duration, &mut canceled) fires in releaseUsingItem, before the item's release
canceled skips the release (a bow doesn't shoot, a spyglass makes no sound), and the use stops anyway
native_release_using(stack, entity, time_left, &mut used) is Item.releaseUsing, after the vanilla item's own release
A bow-like item is held for its native_use_duration minus time_left, so it can act by its charge
A bow's, a crossbow's and a trident's charge count the same way, getUseDuration less the ticks left, so a Start or Tick that moves the ticks left moves the charge too
Without a mod that is the ticks the item was held, as before
used is vanilla's return value: set it and the stack's use cooldown starts, as ItemStack.releaseUsing applies it
entity is a mob's Entity.getId, None for a player, and hand the hand in use (first_person::Hand), as in #5649
Only players fire them so far

The events and the item methods are split as NeoForge splits them
LivingEntityUseItemEvent.Tick and .Stop are events any mod hears for any item, and take their names in snake case (#4402)
Their getters are the entity, the item, the hand and the duration, with setDuration on the base event
onUseTick and releaseUsing are the item's own methods, with no NeoForge event, so they keep Horizon names

All four run on both sides, as vanilla's updatingUsingItem and releaseUsingItem do
On a server's client they run for the use it predicts, so a mod should answer the same on both
It should change the world only where this Game runs the world, not on a server's client
No helper tells the two apart yet: native_window is also true in-frame and on an in-frame LAN host
Switching the held item stops a use without a release, as updatingUsingItem does, so neither the stop nor the release hook fires then
They go in as the item_use_tick_release fragment, "API next"
The fragment gives Tick and Stop their neoforge event and cancellable, as #5649's does for Start

Four points from the #5649 review are fixed here
A Start that leaves a duration below 0 refuses the use, as NeoForge's code does (its LivingEntity.java:3120 in 21.1, if (duration < 0) return;)
A Start that leaves 0 starts it, and its first tick runs it out, as NeoForge's --useItemRemaining <= 0 completes it, so it no longer runs forever
An item whose own duration is 0 (an eye of ender) is still held until let go, as vanilla's
A potion and a spyglass now count to the duration the use started with (32 and 1200 untouched), as useItemRemaining does
A spyglass that runs its time now completes as the others do: its use cooldown, finishUsingItem lowering it, and native_living_entity_use_item_finish
The spyglass sound plays before the use starts, as SpyglassItem.use plays it before startUsingInstantly, so a Start cancel no longer silences it
The first person pose already counts from the started duration on main (game/item_draw.rs:277)

Without a mod, Horizon's own uses go on as before
I think the empty hooks are inlined away as #5649's are, since their bodies are the same
This branch's build wasn't checked for their symbols
Tests: a tick that leaves fewer or more ticks moves the end of a use, a cancel or 0 or less runs it out while an untouched use goes on, what a release runs (the item's and a mod's release, a spyglass's sound) for a cancel or a changed hand, the time left at a release gives back how long the item was held, and a moved end moves the charge

Left as is: bows, crossbows, tridents, brushes, bundles and goat horns keep Horizon's own use times, so a changed duration ends only consumables, potions, spyglasses and mods' items (it still moves the bow, crossbow and trident charge)
Left as is: a release that a mod marks used starts the use cooldown, but no use remainder
Left as is: mobs using items don't fire the hooks
Left as is: a mod that shortens a bow's, shield's or other Horizon-timed item's use sees it finish one tick later than NeoForge, as those items keep their own counters
Left as is: a potion whose time a mod changed plays its drink sounds over its last 25 ticks, as a 32 tick drink does
Left as is: a frozen use reports the same ticks left to the next tick, stop and release, where NeoForge keeps counting down
Left as is: the frozen-until-release case is the vanilla crossbow only, so a mod's own release-used item doesn't freeze
Left as is: Horizon loads a crossbow on its use tick, so a frozen crossbow let go doesn't load, and a Stop cancel doesn't keep one from loading

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

world/entity/LivingEntity.java:3442-3451 (updatingUsingItem: a changed item stops the use without a release)
world/entity/LivingEntity.java:3488-3493 (updateUsingItem: onUseTick with the ticks left, then the countdown)
world/entity/LivingEntity.java:3620-3631 (releaseUsingItem: releaseUsing with the ticks left, then stopUsingItem)
world/entity/LivingEntity.java:3533-3550 (onSyncedDataUpdated: useItemRemaining for a use the server's flags start)
server/level/ServerPlayer.java:2144-2147 (USING_ITEM before the tick)
world/item/ItemStack.java:737-745 (releaseUsing: a used release applies the after-use effects)
world/item/ItemStack.java:1115-1127 (onUseTick: the consumable's crumbs, then the item's tick, none for a kinetic weapon on the server)
world/item/CrossbowItem.java:270-272 (useOnRelease)
world/item/Item.java:178, :337-339 (onUseTick, releaseUsing)
world/item/BowItem.java:32, CrossbowItem.java:87 and :206, TridentItem.java:64 (the charge: getUseDuration less the ticks left)
world/item/SpyglassItem.java:31-33 (use: the sound, then startUsingInstantly) and :37-38 (finishUsingItem lowers it)

Other projects' code

none

Self-check

save_roundtrip passes on this branch (run locally against the 26.3 server jar)

Check Result
Scope ✅ 10 files, +423 −52
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 515 passed (1 min 5 s)
rustfmt ✅ (10 s)
clippy ✅ (2 min 58 s)
tests: horizon-client ✅ 1674 passed (7 min 19 s)
doc tests ✅ 0 passed (2 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)

🔄 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/5905 **Author:** [@reservedbytes](https://github.com/reservedbytes) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `feat/native-item-use-tick-release` --- ### 📝 Commits (2) - [`57bac61`](https://github.com/horizon-rust/horizon/commit/57bac6127120cfb0b62731b4e98c008e81e2cb7b) feat(mods): native mods act on an item's use tick and release - [`4bcfd21`](https://github.com/horizon-rust/horizon/commit/4bcfd21e9ad9cb1bdbd3649ad05e0850672a2ba0) Merge main into native item use tick and release ### 📊 Changes **10 files changed** (+423 additions, -52 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+33 -23) 📝 `crates/horizon-client/src/game/crossbow.rs` (+4 -3) 📝 `crates/horizon-client/src/game/fov.rs` (+5 -5) 📝 `crates/horizon-client/src/game/item_draw.rs` (+1 -1) 📝 `crates/horizon-client/src/game/item_use.rs` (+216 -8) 📝 `crates/horizon-client/src/game/lan_actions.rs` (+6 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+90 -4) 📝 `crates/horizon-client/src/game/potions.rs` (+18 -3) 📝 `crates/horizon-client/src/game/remote.rs` (+24 -5) ➕ `tools/native-hook-fragments/item_use_tick_release.json` (+26 -0) </details> ### 📄 Description ## What and why Part of #5523 Follows #5649 (an item's use over time: its duration, animation, start and finish) A mod's item still could not act while used or when let go: no tool that charges and fires on release, no tool that cooks while held Vanilla calls `Item.onUseTick` each tick of a use (`LivingEntity.updateUsingItem`) It calls `Item.releaseUsing` with the ticks left when the player lets go early (`releaseUsingItem`) This adds four hook points for that, on the server thread, LAN and the in-frame path alike `native_living_entity_use_item_tick(stack, entity, hand, &mut duration, &mut canceled)` fires where `updatingUsingItem` starts, each tick of every use `duration` is the ticks left, and changing it moves the end The client's crumbs and first person pose follow the moved end too, as they follow #5649's started duration A cancel sets it to -1, as NeoForge's code does Once a mod set it, the use goes on only while it is above 0 At 0 or less the item's tick is skipped, and the server finishes the use (`completeUsingItem`: `finishUsingItem`, the food eaten, the finish hook) A potion run out this way is drunk without its own last tick (no drink sound, no random draw for it) An item used on release (a crossbow) stays frozen until let go instead NeoForge's javadoc says a cancel stops the use, but its code finishes it, and this follows the code On a server's client a run-out use stays in use, frozen, so letting go still sends `RELEASE_USE_ITEM` A use no mod touched goes on as vanilla's do, so an eye of ender's use of no time is still held until let go `native_on_use_tick(stack, entity, remaining)` is `Item.onUseTick`, what a mod's own item does each tick it is used, with ticks left above 0 It comes where `ItemStack.onUseTick` calls the item, after a consumable's crumbs, for consumables (potions included), shields and mods' items Vanilla's other items do their own, and a kinetic weapon has none on the server, as in vanilla `native_living_entity_use_item_stop(stack, entity, hand, duration, &mut canceled)` fires in `releaseUsingItem`, before the item's release `canceled` skips the release (a bow doesn't shoot, a spyglass makes no sound), and the use stops anyway `native_release_using(stack, entity, time_left, &mut used)` is `Item.releaseUsing`, after the vanilla item's own release A bow-like item is held for its `native_use_duration` minus `time_left`, so it can act by its charge A bow's, a crossbow's and a trident's charge count the same way, `getUseDuration` less the ticks left, so a Start or Tick that moves the ticks left moves the charge too Without a mod that is the ticks the item was held, as before `used` is vanilla's return value: set it and the stack's use cooldown starts, as `ItemStack.releaseUsing` applies it `entity` is a mob's `Entity.getId`, None for a player, and `hand` the hand in use (`first_person::Hand`), as in #5649 Only players fire them so far The events and the item methods are split as NeoForge splits them `LivingEntityUseItemEvent.Tick` and `.Stop` are events any mod hears for any item, and take their names in snake case (#4402) Their getters are the entity, the item, the hand and the duration, with `setDuration` on the base event `onUseTick` and `releaseUsing` are the item's own methods, with no NeoForge event, so they keep Horizon names All four run on both sides, as vanilla's `updatingUsingItem` and `releaseUsingItem` do On a server's client they run for the use it predicts, so a mod should answer the same on both It should change the world only where this `Game` runs the world, not on a server's client No helper tells the two apart yet: `native_window` is also true in-frame and on an in-frame LAN host Switching the held item stops a use without a release, as `updatingUsingItem` does, so neither the stop nor the release hook fires then They go in as the `item_use_tick_release` fragment, "API next" The fragment gives Tick and Stop their `neoforge` event and `cancellable`, as #5649's does for Start Four points from the #5649 review are fixed here A Start that leaves a duration below 0 refuses the use, as NeoForge's code does (its `LivingEntity.java:3120` in 21.1, `if (duration < 0) return;`) A Start that leaves 0 starts it, and its first tick runs it out, as NeoForge's `--useItemRemaining <= 0` completes it, so it no longer runs forever An item whose own duration is 0 (an eye of ender) is still held until let go, as vanilla's A potion and a spyglass now count to the duration the use started with (32 and 1200 untouched), as `useItemRemaining` does A spyglass that runs its time now completes as the others do: its use cooldown, `finishUsingItem` lowering it, and `native_living_entity_use_item_finish` The spyglass sound plays before the use starts, as `SpyglassItem.use` plays it before `startUsingInstantly`, so a Start cancel no longer silences it The first person pose already counts from the started duration on `main` (`game/item_draw.rs:277`) Without a mod, Horizon's own uses go on as before I think the empty hooks are inlined away as #5649's are, since their bodies are the same This branch's build wasn't checked for their symbols Tests: a tick that leaves fewer or more ticks moves the end of a use, a cancel or 0 or less runs it out while an untouched use goes on, what a release runs (the item's and a mod's release, a spyglass's sound) for a cancel or a changed hand, the time left at a release gives back how long the item was held, and a moved end moves the charge Left as is: bows, crossbows, tridents, brushes, bundles and goat horns keep Horizon's own use times, so a changed duration ends only consumables, potions, spyglasses and mods' items (it still moves the bow, crossbow and trident charge) Left as is: a release that a mod marks used starts the use cooldown, but no use remainder Left as is: mobs using items don't fire the hooks Left as is: a mod that shortens a bow's, shield's or other Horizon-timed item's use sees it finish one tick later than NeoForge, as those items keep their own counters Left as is: a potion whose time a mod changed plays its drink sounds over its last 25 ticks, as a 32 tick drink does Left as is: a frozen use reports the same ticks left to the next tick, stop and release, where NeoForge keeps counting down Left as is: the frozen-until-release case is the vanilla crossbow only, so a mod's own release-used item doesn't freeze Left as is: Horizon loads a crossbow on its use tick, so a frozen crossbow let go doesn't load, and a Stop cancel doesn't keep one from loading ## 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 `world/entity/LivingEntity.java:3442-3451` (`updatingUsingItem`: a changed item stops the use without a release) `world/entity/LivingEntity.java:3488-3493` (`updateUsingItem`: `onUseTick` with the ticks left, then the countdown) `world/entity/LivingEntity.java:3620-3631` (`releaseUsingItem`: `releaseUsing` with the ticks left, then `stopUsingItem`) `world/entity/LivingEntity.java:3533-3550` (`onSyncedDataUpdated`: `useItemRemaining` for a use the server's flags start) `server/level/ServerPlayer.java:2144-2147` (`USING_ITEM` before the tick) `world/item/ItemStack.java:737-745` (`releaseUsing`: a used release applies the after-use effects) `world/item/ItemStack.java:1115-1127` (`onUseTick`: the consumable's crumbs, then the item's tick, none for a kinetic weapon on the server) `world/item/CrossbowItem.java:270-272` (`useOnRelease`) `world/item/Item.java:178`, `:337-339` (`onUseTick`, `releaseUsing`) `world/item/BowItem.java:32`, `CrossbowItem.java:87` and `:206`, `TridentItem.java:64` (the charge: `getUseDuration` less the ticks left) `world/item/SpyglassItem.java:31-33` (`use`: the sound, then `startUsingInstantly`) and `:37-38` (`finishUsingItem` lowers it) ## Other projects' code none ## Self-check `save_roundtrip` passes on this branch (run locally against the 26.3 server jar) | Check | Result | | --- | --- | | Scope | ✅ 10 files, +423 −52 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 515 passed (1 min 5 s) | | rustfmt | ✅ (10 s) | | clippy | ✅ (2 min 58 s) | | tests: horizon-client | ✅ 1674 passed (7 min 19 s) | | doc tests | ✅ 0 passed (2 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) | --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:22:53 +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#6553
No description provided.