[PR #6280] [MERGED] Native mods: an entity's tick, EntityTickEvent.Pre / Post #6762

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

📋 Pull Request Information

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

Base: main ← Head: mods/entity-tick


📝 Commits (4)

  • a6a3867 Native mods: entities ticking, with a Pre that can cancel (EntityTickEvent)
  • 84a5b49 Native mods: tell entities' ticks only while a mod listens (native_entity_tick_listened)
  • fff6517 Native mods: an item merged before its tick hears no Post; a minecart's Post after it is stored
  • cfbae5b Merge remote-tracking branch 'hr/main' into mods/entity-tick

📊 Changes

7 files changed (+320 additions, -2 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+28 -1)
📝 crates/horizon-client/src/game/boats.rs (+25 -1)
📝 crates/horizon-client/src/game/creatures.rs (+23 -0)
📝 crates/horizon-client/src/game/minecarts.rs (+14 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+35 -0)
➕ crates/horizon-client/src/game/native_ticks.rs (+169 -0)
➕ tools/native-hook-fragments/entity_tick.json (+26 -0)

📄 Description

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

What and why

Fixes #6249, part of #4953 ("Entity: tick, mount, size, projectile impact"). A native mod that makes machines out of entities (a conveyor that carries item entities, a field that freezes mobs) had no point that runs for each entity as it ticks, and none to stop an entity's tick.

Two server hook points, API from a fragment (tools/native-hook-fragments/entity_tick.json):

  • native_entity_tick_pre(dimension, entity, kind, canceled) (EntityTickEvent.Pre): the entity's id and type (minecraft:zombie, minecraft:item), before it ticks. Set canceled and it does not tick this tick: no AI, physics or effects for a mob, no movement, ageing or merging for an item, no motion for a minecart or boat, and its Post is not told.
  • native_entity_tick_post(dimension, entity, kind) (EntityTickEvent.Post): after it ticked (one that died or was removed in its tick is told too).

Families in this PR: mobs, item entities, minecarts and boats, those that tick (in an entity-ticking chunk, not a boat or cart a player rides: it ticks with the player). Not yet, listed in the issue: projectiles, experience orbs, primed TNT, falling blocks, armor stands, End crystals, the ender dragon and the ridden vehicle. kind tells which family: later APIs add kinds.

Where. The mob and item loops hold their entity lists out of Game (and a MobWorld that borrows it), so a hook with &mut Game can't run between one entity's tick and the next. Mobs and items therefore hear every Pre before the family's first tick (a pass over the entities that will tick, which the loop then skips when canceled) and every Post after its last. Minecarts and boats can call the hooks inside their loops, so they hear theirs around each one's tick (a canceled cart's or boat's lava and bubble steps are skipped too; a cart's Post comes before its own lava step). native_ticks.rs holds the TickRound the families share.

Cost. Two empty calls per ticking entity per tick; nothing allocated per tick on a server (the list a round keeps is reused between ticks, a mob's type is shared, not copied; a boat builds its type name each tick, which its clone already costs more than). A client and the deprecated in-frame path do none of it.

Kind

  • feature: something new for players (mod authors)

Vanilla reference

Vanilla ticks an entity in ServerLevel.tickNonPassenger (commonTick, tick) for each entity of the entity-tick list in an entity-ticking chunk. NeoForge's patch posts EntityTickEvent.Pre there, skips the tick when canceled, and posts EntityTickEvent.Post after it only if the Pre was not canceled. Horizon's families tick in their own loops (tick_creature_entities, tick_items, tick_carts, tick_boats), so the placement follows those, and for mobs and items the order is batched as above: a mod sees one entity's Post after the other entities of its family have ticked, where NeoForge's comes before the next entity's Pre.

Other projects' code

None. NeoForge's names and contracts only.

Proof in the real game

Engine from this branch with a throwaway eprintln! in each hook body (not in this PR): it cancels the Pre of the types named in an environment variable and counts Pre and Post per type. A stone floor at y = 149; a pig, a zombie, an item (stone), a minecart and an oak boat summoned at y = 160 above it; their Pos read 3 s later with data get entity. A frozen entity stays at 160, a free one falls to the floor (150).

Server thread, freezing minecraft:pig,minecraft:minecart: pig y = 160.0, minecart y = 160.0 (frozen); zombie 150.0, stone 150.0, oak boat 150.0 (fell).
Server thread, freezing minecraft:item,minecraft:oak_boat: stone y = 160.0, oak boat y = 160.0 (frozen); zombie 150.0, pig 150.0, minecart 150.0 (fell). The log showed pre ... oak_boat ... canceled=true and no Post for it.

Dedicated server (a client connected, commands from the console), freezing minecraft:pig,minecraft:item,minecraft:minecart: pig 160.0, stone 160.0, minecart 160.0; zombie 150.0, oak boat 150.0. Posts counted for the zombie and the oak boat (TESTHOOK post ... oak_boat id=179 n=201); the connected client printed nothing.

Not run: a rider on a cart or boat, entities outside the ticking range, a mod that removes an entity from its Pre (the boat and cart loops re-find their entity by id after the hook, the mob and item loops run their pass before they take their list), Post counts for every frozen type in every run, a LAN guest.

Self-check

pr_ready (tools/pr_ready.py --quick --base hr/main on cachyos-x8664, against hr/main at 455b6916b, 2 min 14 s)

Check Result
Scope ✅ 7 files, +255 −2
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 586 passed (35 s)
rustfmt ✅ (7 s)
clippy ✅ (49 s)
tests: horizon-client ✅ 1805 passed (41 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 (a game-binary change reruns about 25)

🤖 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/6280 **Author:** [@levzzz5154](https://github.com/levzzz5154) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `mods/entity-tick` --- ### 📝 Commits (4) - [`a6a3867`](https://github.com/horizon-rust/horizon/commit/a6a386723a4ce22bce52f10c707b2b27ad84b68d) Native mods: entities ticking, with a Pre that can cancel (EntityTickEvent) - [`84a5b49`](https://github.com/horizon-rust/horizon/commit/84a5b49f986dfe61b16da3877290d7bba82d740f) Native mods: tell entities' ticks only while a mod listens (native_entity_tick_listened) - [`fff6517`](https://github.com/horizon-rust/horizon/commit/fff651702e0b2aca657a646e35f69f99c605b5ba) Native mods: an item merged before its tick hears no Post; a minecart's Post after it is stored - [`cfbae5b`](https://github.com/horizon-rust/horizon/commit/cfbae5bd0d6a1a0c38f5cd5d05b495e525546661) Merge remote-tracking branch 'hr/main' into mods/entity-tick ### 📊 Changes **7 files changed** (+320 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+28 -1) 📝 `crates/horizon-client/src/game/boats.rs` (+25 -1) 📝 `crates/horizon-client/src/game/creatures.rs` (+23 -0) 📝 `crates/horizon-client/src/game/minecarts.rs` (+14 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+35 -0) ➕ `crates/horizon-client/src/game/native_ticks.rs` (+169 -0) ➕ `tools/native-hook-fragments/entity_tick.json` (+26 -0) </details> ### 📄 Description > 🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Sonnet 5.5), not by levzzz5154 personally. ## What and why Fixes #6249, part of #4953 ("Entity: tick, mount, size, projectile impact"). A native mod that makes machines out of entities (a conveyor that carries item entities, a field that freezes mobs) had no point that runs for each entity as it ticks, and none to stop an entity's tick. Two server hook points, API from a fragment (`tools/native-hook-fragments/entity_tick.json`): - `native_entity_tick_pre(dimension, entity, kind, canceled)` (`EntityTickEvent.Pre`): the entity's id and type (`minecraft:zombie`, `minecraft:item`), before it ticks. Set `canceled` and it does not tick this tick: no AI, physics or effects for a mob, no movement, ageing or merging for an item, no motion for a minecart or boat, and its Post is not told. - `native_entity_tick_post(dimension, entity, kind)` (`EntityTickEvent.Post`): after it ticked (one that died or was removed in its tick is told too). Families in this PR: mobs, item entities, minecarts and boats, those that tick (in an entity-ticking chunk, not a boat or cart a player rides: it ticks with the player). Not yet, listed in the issue: projectiles, experience orbs, primed TNT, falling blocks, armor stands, End crystals, the ender dragon and the ridden vehicle. `kind` tells which family: later APIs add kinds. Where. The mob and item loops hold their entity lists out of `Game` (and a `MobWorld` that borrows it), so a hook with `&mut Game` can't run between one entity's tick and the next. Mobs and items therefore hear every Pre before the family's first tick (a pass over the entities that will tick, which the loop then skips when canceled) and every Post after its last. Minecarts and boats can call the hooks inside their loops, so they hear theirs around each one's tick (a canceled cart's or boat's lava and bubble steps are skipped too; a cart's Post comes before its own lava step). `native_ticks.rs` holds the `TickRound` the families share. Cost. Two empty calls per ticking entity per tick; nothing allocated per tick on a server (the list a round keeps is reused between ticks, a mob's type is shared, not copied; a boat builds its type name each tick, which its clone already costs more than). A client and the deprecated in-frame path do none of it. ## Kind - [x] feature: something new for players (mod authors) ## Vanilla reference Vanilla ticks an entity in `ServerLevel.tickNonPassenger` (`commonTick`, `tick`) for each entity of the entity-tick list in an entity-ticking chunk. NeoForge's patch posts `EntityTickEvent.Pre` there, skips the tick when canceled, and posts `EntityTickEvent.Post` after it only if the Pre was not canceled. Horizon's families tick in their own loops (`tick_creature_entities`, `tick_items`, `tick_carts`, `tick_boats`), so the placement follows those, and for mobs and items the order is batched as above: a mod sees one entity's Post after the other entities of its family have ticked, where NeoForge's comes before the next entity's Pre. ## Other projects' code None. NeoForge's names and contracts only. ## Proof in the real game Engine from this branch with a throwaway `eprintln!` in each hook body (not in this PR): it cancels the Pre of the types named in an environment variable and counts Pre and Post per type. A stone floor at y = 149; a pig, a zombie, an item (stone), a minecart and an oak boat summoned at y = 160 above it; their `Pos` read 3 s later with `data get entity`. A frozen entity stays at 160, a free one falls to the floor (150). Server thread, freezing `minecraft:pig,minecraft:minecart`: pig y = 160.0, minecart y = 160.0 (frozen); zombie 150.0, stone 150.0, oak boat 150.0 (fell). Server thread, freezing `minecraft:item,minecraft:oak_boat`: stone y = 160.0, oak boat y = 160.0 (frozen); zombie 150.0, pig 150.0, minecart 150.0 (fell). The log showed `pre ... oak_boat ... canceled=true` and no Post for it. Dedicated server (a client connected, commands from the console), freezing `minecraft:pig,minecraft:item,minecraft:minecart`: pig 160.0, stone 160.0, minecart 160.0; zombie 150.0, oak boat 150.0. Posts counted for the zombie and the oak boat (`TESTHOOK post ... oak_boat id=179 n=201`); the connected client printed nothing. Not run: a rider on a cart or boat, entities outside the ticking range, a mod that removes an entity from its Pre (the boat and cart loops re-find their entity by id after the hook, the mob and item loops run their pass before they take their list), Post counts for every frozen type in every run, a LAN guest. ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base hr/main` on cachyos-x8664, against hr/main at 455b6916b, 2 min 14 s) | Check | Result | | --- | --- | | Scope | ✅ 7 files, +255 −2 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 586 passed (35 s) | | rustfmt | ✅ (7 s) | | clippy | ✅ (49 s) | | tests: horizon-client | ✅ 1805 passed (41 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 (a game-binary change reruns about 25) | 🤖 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 06:26:16 +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#6762
No description provided.