[PR #6279] [MERGED] Native mods: a projectile's impact, ProjectileImpactEvent #6763

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/6279
Author: @levzzz5154
Created: 11/1/2026
Status: ✅ Merged
Merged: 11/1/2026
Merged by: @levzzz5154

Base: main ← Head: mods/projectile-impact


📝 Commits (2)

  • de0b65b Native mods: a projectile's impact a mod can cancel (ProjectileImpactEvent)
  • 2166052 Projectile impacts: a mod asks to hear them, so the tick copies nothing otherwise

📊 Changes

8 files changed (+514 additions, -41 deletions)

View changed files

📝 crates/horizon-client/src/arrows.rs (+35 -4)
📝 crates/horizon-client/src/game.rs (+1 -0)
📝 crates/horizon-client/src/game/creatures.rs (+58 -28)
📝 crates/horizon-client/src/game/dragon.rs (+1 -1)
📝 crates/horizon-client/src/game/native_hooks.rs (+55 -0)
➕ crates/horizon-client/src/game/native_projectiles.rs (+255 -0)
📝 crates/horizon-client/src/projectiles.rs (+91 -8)
➕ tools/native-hook-fragments/projectile_impact.json (+18 -0)

📄 Description

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

What and why

Fixes #6203, part of #4953 ("Entity: tick, mount, size, projectile impact"). A native mod that guards an area or lets arrows through a ghost had no point to say no to a projectile's impact: native_living_incoming_damage comes after the hit is decided.

One server hook point, API from a fragment (tools/native-hook-fragments/projectile_impact.json):

  • native_projectile_impact(dimension, projectile, kind, owner, entity, entity_kind, block, face, at, canceled) (ProjectileImpactEvent): the projectile's entity id, type (minecraft:arrow, minecraft:spectral_arrow...) and owner (a mob's or player's entity id, 0 for none), and what it hit: an entity (its id and type, a dragon part told as the dragon) or, with entity None, a block (position, face normal) at the point at. Set canceled: nothing of the impact happens (no damage, knockback, sound, block reaction, an arrow does not stick, a pearl does not teleport, a potion or fireball does not burst) and the projectile flies on.

How it works. The impact is found inside the library's Projectile::tick / arrows::tick, which have no Game, and tick_projectiles holds the level through MobWorld while it flies them. So the tick keeps a copy of each flying projectile, collects the impacts it finds, and once its worlds are let go asks the mods with the projectiles back in creatures.projectiles (the helpers find them by id). A projectile whose impact a mod canceled is put back to its copy and flown the tick again through what it hit (Projectile::tick_through, arrows::tick_through): an arrow ends at the point it would have hit and goes on, every other projectile goes the whole way of its motion; its hit and its arrow events are taken out of the tick, so no sound plays. A projectile a mod removed in its hook stays removed. Without a mod hooking, the outcome is the tick as before (the copy is the only added work, and not for arrows already stuck). native_projectiles.rs holds the settling; creatures.rs only collects.

Covers what tick_projectiles flies: arrows, tridents, snowballs, eggs, pearls, potions, fireballs, wither skulls, wind charges, llama spit and shulker bullets, from players, mobs and dispensers, on the single player server thread and on a dedicated server. The deprecated in-frame path and clients never fire it. Not covered, listed in the issue: fireworks rockets, fishing bobbers, a breeze's deflection, a hit on a wind charge.

Cost to know: on a server, every projectile that isn't stuck is cloned once a tick (before), so a canceled impact can fly again from where it was. Cloning only on a hit isn't possible, as the copy must be from before the flight; stuck arrows aren't cloned.

Kind

  • feature: something new for players (mod authors)

Vanilla reference

Vanilla 26.3 finds the impact in each projectile's own tick (ThrowableProjectile.tick, AbstractHurtingProjectile.tick, AbstractArrow.stepMoveAndHit, ShulkerBullet.tick, LlamaSpit.tick) and acts in Projectile.hitTargetOrDeflectSelf. NeoForge's ProjectileImpactEvent is posted from those tick methods, before the hit acts. Its contract: when canceled the impact is not processed and the projectile keeps flying. For an arrow, AbstractArrow.stepMoveAndHit has already set the arrow at the hit point and leaves the loop; for the others the hit result moves the projectile only when the event is not canceled. NeoForge's patch for ThrowableProjectile gates only the new position, so its later hitTargetOrDeflectSelf still runs; I followed the event's documented contract (no impact, flies on) for every kind. By AbstractArrow.tick, a canceled arrow left at a block's surface is inside the block's collision box at its next tick and sticks quietly, unless the mod moves it (the log shows no second impact for it; I read that from the code, I did not watch it stick). Where Horizon's structure differs (all projectiles flown in one place, the worlds borrowed during the tick) the replay follows Horizon's and says so in the hook's doc comment.

Other projects' code

None. NeoForge's names and contract only.

Proof in the real game

Engine from this branch with a throwaway eprintln! in the hook body (not in this PR) that cancels the first impact of snowballs and arrows, and every impact of eggs. A stone floor and a wall at x = 8 built around the player at y = 150; projectiles summoned with Motion toward the wall, two pigs (no AI) at x = 5.5 on the way.

Server thread (horizon-ctl launch --world L --creative --offscreen):

snowball at the wall:
TESTHOOK impact n=1 id=507 minecraft:snowball owner=0 entity=None/ block=[8, 150, 0] face=[-1, 0, 0] at=[8.0, 150.73, 0.5] canceled=true
TESTHOOK impact n=2 id=507 minecraft:snowball owner=0 entity=None/ block=[8, 150, 0] face=[-1, 0, 0] at=[8.226, 150.68, 0.5] canceled=false
arrow at the wall (canceled once; no second impact followed, as expected: from the face it is inside the block's box at its next tick, see Vanilla reference; I did not look at the arrow afterwards, and audio was off):
TESTHOOK impact n=1 id=536 minecraft:arrow owner=0 entity=None/ block=[8, 151, -2] face=[-1, 0, 0] at=[8.0, 151.12, -1.5] canceled=true
snowball at pig 1 (canceled: flies through, then meets the wall):
TESTHOOK impact n=1 id=545 minecraft:snowball owner=0 entity=Some(542)/minecraft:pig ... at=[4.75, 150.585, 2.5] canceled=true
TESTHOOK impact n=2 id=545 minecraft:snowball owner=0 entity=None/ block=[8, 150, 2] ... canceled=false
arrow at pig 1 (canceled once): the pig's Health stays 10.0f; then the wall, canceled=false, stuck.
small fireball at pig 2 (not canceled):
TESTHOOK impact n=1 id=551 minecraft:small_fireball owner=0 entity=Some(549)/minecraft:pig ... canceled=false
pig 2 Health: 3.0f (the fireball hurt it)
eggs, canceled every time: 86 impacts logged as the egg kept falling through the terrain.

A snowball thrown by the player with use-item (server thread): owner=2, the player's entity id.

Dedicated server (a client connected, commands from the console): the same results, e.g. arrow at pig: n=1 ... entity=Some(220)/minecraft:pig ... canceled=true, n=2 ... block=[8, 150, 2] ... canceled=false, pig Health 10.0f; small fireball on pig 2: canceled=false, Health 3.0f. The connected client printed nothing.

Unit tests (projectiles::tests): a tick through a wall flies the whole way of its motion for a snowball and a fireball and hits nothing, and the same tick without the cancel hits; a tick through an entity does not hit it.

Not run: summon of tridents and spectral arrows (they did not fly in a first try, a separate matter, so no tridents or spectral arrows were tested), potions, pearls, wind charges, llama spit and shulker bullets (same code path as the snowball and the fireball, not exercised), a dragon part or End crystal hit, mob- or dispenser-shot projectiles, a LAN guest's projectile (the owner id above is from the server thread's own player), parity harnesses (--quick).

Self-check

pr_ready (tools/pr_ready.py --quick --base hr/main on cachyos-x8664, against hr/main at 7311b1e17, 3 min 14 s)

Check Result
Scope ⚠️ 8 files, +493 −41; 534 lines of code: over 500 (a third is doc comments and tests; the replay and the hook are one piece)
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 586 passed (31 s)
rustfmt ✅ (6 s)
clippy ✅ (1 min 34 s)
tests: horizon-client ✅ 1804 passed (1 min 1 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

🤖 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/6279 **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/projectile-impact` --- ### 📝 Commits (2) - [`de0b65b`](https://github.com/horizon-rust/horizon/commit/de0b65b26b3b4161e6df3e9ab4c9cc096facf53d) Native mods: a projectile's impact a mod can cancel (ProjectileImpactEvent) - [`2166052`](https://github.com/horizon-rust/horizon/commit/2166052a666fa6f98d4a3351ba40fd438ef0c7a5) Projectile impacts: a mod asks to hear them, so the tick copies nothing otherwise ### 📊 Changes **8 files changed** (+514 additions, -41 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/arrows.rs` (+35 -4) 📝 `crates/horizon-client/src/game.rs` (+1 -0) 📝 `crates/horizon-client/src/game/creatures.rs` (+58 -28) 📝 `crates/horizon-client/src/game/dragon.rs` (+1 -1) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+55 -0) ➕ `crates/horizon-client/src/game/native_projectiles.rs` (+255 -0) 📝 `crates/horizon-client/src/projectiles.rs` (+91 -8) ➕ `tools/native-hook-fragments/projectile_impact.json` (+18 -0) </details> ### 📄 Description > 🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Sonnet 5.5), not by levzzz5154 personally. ## What and why Fixes #6203, part of #4953 ("Entity: tick, mount, size, projectile impact"). A native mod that guards an area or lets arrows through a ghost had no point to say no to a projectile's impact: `native_living_incoming_damage` comes after the hit is decided. One server hook point, API from a fragment (`tools/native-hook-fragments/projectile_impact.json`): - `native_projectile_impact(dimension, projectile, kind, owner, entity, entity_kind, block, face, at, canceled)` (`ProjectileImpactEvent`): the projectile's entity id, type (`minecraft:arrow`, `minecraft:spectral_arrow`...) and owner (a mob's or player's entity id, 0 for none), and what it hit: an entity (its id and type, a dragon part told as the dragon) or, with `entity` None, a block (position, face normal) at the point `at`. Set `canceled`: nothing of the impact happens (no damage, knockback, sound, block reaction, an arrow does not stick, a pearl does not teleport, a potion or fireball does not burst) and the projectile flies on. How it works. The impact is found inside the library's `Projectile::tick` / `arrows::tick`, which have no `Game`, and `tick_projectiles` holds the level through `MobWorld` while it flies them. So the tick keeps a copy of each flying projectile, collects the impacts it finds, and once its worlds are let go asks the mods with the projectiles back in `creatures.projectiles` (the helpers find them by id). A projectile whose impact a mod canceled is put back to its copy and flown the tick again through what it hit (`Projectile::tick_through`, `arrows::tick_through`): an arrow ends at the point it would have hit and goes on, every other projectile goes the whole way of its motion; its hit and its arrow events are taken out of the tick, so no sound plays. A projectile a mod removed in its hook stays removed. Without a mod hooking, the outcome is the tick as before (the copy is the only added work, and not for arrows already stuck). `native_projectiles.rs` holds the settling; `creatures.rs` only collects. Covers what `tick_projectiles` flies: arrows, tridents, snowballs, eggs, pearls, potions, fireballs, wither skulls, wind charges, llama spit and shulker bullets, from players, mobs and dispensers, on the single player server thread and on a dedicated server. The deprecated in-frame path and clients never fire it. Not covered, listed in the issue: fireworks rockets, fishing bobbers, a breeze's deflection, a hit on a wind charge. **Cost to know:** on a server, every projectile that isn't stuck is cloned once a tick (`before`), so a canceled impact can fly again from where it was. Cloning only on a hit isn't possible, as the copy must be from before the flight; stuck arrows aren't cloned. ## Kind - [x] feature: something new for players (mod authors) ## Vanilla reference Vanilla 26.3 finds the impact in each projectile's own tick (`ThrowableProjectile.tick`, `AbstractHurtingProjectile.tick`, `AbstractArrow.stepMoveAndHit`, `ShulkerBullet.tick`, `LlamaSpit.tick`) and acts in `Projectile.hitTargetOrDeflectSelf`. NeoForge's `ProjectileImpactEvent` is posted from those tick methods, before the hit acts. Its contract: when canceled the impact is not processed and the projectile keeps flying. For an arrow, `AbstractArrow.stepMoveAndHit` has already set the arrow at the hit point and leaves the loop; for the others the hit result moves the projectile only when the event is not canceled. NeoForge's patch for `ThrowableProjectile` gates only the new position, so its later `hitTargetOrDeflectSelf` still runs; I followed the event's documented contract (no impact, flies on) for every kind. By `AbstractArrow.tick`, a canceled arrow left at a block's surface is inside the block's collision box at its next tick and sticks quietly, unless the mod moves it (the log shows no second impact for it; I read that from the code, I did not watch it stick). Where Horizon's structure differs (all projectiles flown in one place, the worlds borrowed during the tick) the replay follows Horizon's and says so in the hook's doc comment. ## Other projects' code None. NeoForge's names and contract only. ## Proof in the real game Engine from this branch with a throwaway `eprintln!` in the hook body (not in this PR) that cancels the first impact of snowballs and arrows, and every impact of eggs. A stone floor and a wall at x = 8 built around the player at y = 150; projectiles summoned with `Motion` toward the wall, two pigs (no AI) at x = 5.5 on the way. Server thread (`horizon-ctl launch --world L --creative --offscreen`): ``` snowball at the wall: TESTHOOK impact n=1 id=507 minecraft:snowball owner=0 entity=None/ block=[8, 150, 0] face=[-1, 0, 0] at=[8.0, 150.73, 0.5] canceled=true TESTHOOK impact n=2 id=507 minecraft:snowball owner=0 entity=None/ block=[8, 150, 0] face=[-1, 0, 0] at=[8.226, 150.68, 0.5] canceled=false arrow at the wall (canceled once; no second impact followed, as expected: from the face it is inside the block's box at its next tick, see Vanilla reference; I did not look at the arrow afterwards, and audio was off): TESTHOOK impact n=1 id=536 minecraft:arrow owner=0 entity=None/ block=[8, 151, -2] face=[-1, 0, 0] at=[8.0, 151.12, -1.5] canceled=true snowball at pig 1 (canceled: flies through, then meets the wall): TESTHOOK impact n=1 id=545 minecraft:snowball owner=0 entity=Some(542)/minecraft:pig ... at=[4.75, 150.585, 2.5] canceled=true TESTHOOK impact n=2 id=545 minecraft:snowball owner=0 entity=None/ block=[8, 150, 2] ... canceled=false arrow at pig 1 (canceled once): the pig's Health stays 10.0f; then the wall, canceled=false, stuck. small fireball at pig 2 (not canceled): TESTHOOK impact n=1 id=551 minecraft:small_fireball owner=0 entity=Some(549)/minecraft:pig ... canceled=false pig 2 Health: 3.0f (the fireball hurt it) eggs, canceled every time: 86 impacts logged as the egg kept falling through the terrain. ``` A snowball thrown by the player with `use-item` (server thread): `owner=2`, the player's entity id. Dedicated server (a client connected, commands from the console): the same results, e.g. `arrow at pig`: `n=1 ... entity=Some(220)/minecraft:pig ... canceled=true`, `n=2 ... block=[8, 150, 2] ... canceled=false`, pig Health `10.0f`; small fireball on pig 2: `canceled=false`, Health `3.0f`. The connected client printed nothing. Unit tests (`projectiles::tests`): a tick through a wall flies the whole way of its motion for a snowball and a fireball and hits nothing, and the same tick without the cancel hits; a tick through an entity does not hit it. Not run: `summon` of tridents and spectral arrows (they did not fly in a first try, a separate matter, so no tridents or spectral arrows were tested), potions, pearls, wind charges, llama spit and shulker bullets (same code path as the snowball and the fireball, not exercised), a dragon part or End crystal hit, mob- or dispenser-shot projectiles, a LAN guest's projectile (the owner id above is from the server thread's own player), parity harnesses (`--quick`). ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base hr/main` on cachyos-x8664, against hr/main at 7311b1e17, 3 min 14 s) | Check | Result | | --- | --- | | Scope | ⚠️ 8 files, +493 −41; 534 lines of code: over 500 (a third is doc comments and tests; the replay and the hook are one piece) | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 586 passed (31 s) | | rustfmt | ✅ (6 s) | | clippy | ✅ (1 min 34 s) | | tests: horizon-client | ✅ 1804 passed (1 min 1 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 | 🤖 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#6763
No description provided.