[PR #5637] [MERGED] Native mods: living_knockback, change or cancel the knockback an entity takes (API next) #6406

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

📋 Pull Request Information

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

Base: main ← Head: feat/native-knockback-hook


📝 Commits (3)

  • be5d818 Native mods: a hook at LivingEntity.knockback to change or cancel a push
  • 7a2ea36 Merge remote-tracking branch 'hr/main' into pr-5637
  • 4b57957 Merge remote-tracking branch 'hr/main' into pr-5637

📊 Changes

14 files changed (+484 additions, -76 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+2 -1)
📝 crates/horizon-client/src/game/creatures.rs (+168 -28)
📝 crates/horizon-client/src/game/explosion.rs (+6 -1)
📝 crates/horizon-client/src/game/lan_pvp.rs (+12 -2)
📝 crates/horizon-client/src/game/mace.rs (+1 -1)
📝 crates/horizon-client/src/game/native_hooks.rs (+71 -1)
📝 crates/horizon-client/src/game/shield.rs (+15 -24)
📝 crates/horizon-client/src/game/spear.rs (+3 -2)
📝 crates/horizon-client/src/health.rs (+151 -1)
📝 crates/horizon-client/src/mobs.rs (+24 -6)
📝 crates/horizon-client/src/mobs/goat.rs (+2 -0)
📝 crates/horizon-client/src/mobs/spear_use.rs (+18 -8)
📝 crates/horizon-client/src/mobs/sulfur_cube.rs (+1 -1)
➕ tools/native-hook-fragments/living_knockback.json (+10 -0)

📄 Description

🤖 Opened by mariuslozo-boop's AI agent (Claude Code, Claude Sonnet 5.5 + Claude Opus 5.5 review), not by mariuslozo-boop personally.

What and why

Closes #5496 (part of #4953). A mod couldn't change the knockback an entity takes. This adds the hook the issue asks for, in the shape of the merged hook PRs (an empty Game hook, a OnceLock the game sets, a fragment instead of an API number, #5161):

  • Hook native_living_knockback(entity, kind, damage_type, causing, direct, &mut strength, &mut x, &mut z, &mut canceled). It runs in LivingEntity.knockback (LivingEntity.java:1647) once the knockback resistance took its share, before the <= 0 guard, the random-direction draws and the motion change. A mod may change the strength or the direction, or cancel the push (the motion is left alone). A full resistance hands it a strength of 0, which a mod may raise. A non-finite number a mod leaves is put back.

  • Source: in 26.3 every knockback call has a DamageSource, so every push names one. The pushed entity and its source are named exactly as native_living_incoming_damage names the same hit (Hurting, hurt_server_args), with the same ids still 0. A player's blow on another player names no attacker yet, as its damage doesn't. A goat's ram pushes with minecraft:mob_attack and hurts with mob_attack_no_aggro, as in vanilla.

  • Where it fires (players, including LAN guests and a dedicated server's, and mobs):

    Vanilla caller Horizon
    dealDefaultKnockback (a hit's own push) Mob::damage, hurt_player_from, sulfur_cube::deal_default_knockback (no causing entity)
    Player.causeExtraKnockback (sprint, Knockback) game.rs attack, spear.rs, lan_pvp.rs
    Player.doSweepAttack mace.rs (mobs), lan_pvp.rs::sweep_players
    LivingEntity.stabAttack (a mob's spear) MobEvent::Knockback, which now carries its source
    RamTarget.tick (goat) MobEvent::Knockback, with mobAttack(goat)
    LivingEntity.blockedByItem (a shield's push on its holder) shield.rs called knock_back_player(0.5, ...). It had open-coded the push, past the hook, the knockback resistance and the random direction
    Creaking / SulfurCube.knockback overrides a creaking that can't move and a sulfur cube with a block hit by an entity don't reach it, as they don't call super
  • Not reached yet, listed in the hook's docs: a mob's melee push from its attack knockback or a Knockback weapon (Mob.doHurtTarget's causeExtraKnockback), which Horizon makes as a plain push (mobs/ai.rs); an armor stand's sweep push (open-coded in mace.rs); the ender dragon, which Horizon doesn't knock back. The pushes vanilla doesn't make with knockback don't fire it: the dragon's wings, Punch, explosions, wind charges, a mace smash's area, a hoglin's throw.

  • Unhooked, the game calls an empty function. The server_knockback harness (below) shows the pushes are unchanged.

#5606 (shield on Easy) touches the same lines of shield.rs and the same files (creatures.rs, explosion.rs, health.rs); whichever lands second needs a rebase.

NeoForge's public LivingKnockBackEvent javadoc (read, no source): fired when a living entity is about to be knocked back (in hurt, blockUsingShield, Mob.doHurtTarget, Player.attack), with a strength and an x/z ratio, cancelable. It doesn't say whether the resistance is applied first. The issue asks for after, as here.

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

LivingEntity.knockback (1647), dealDefaultKnockback, blockedByItem, causeExtraKnockback, stabAttack; Player.causeExtraKnockback, Player.doSweepAttack; Mob.doHurtTarget; RamTarget.tick; the overrides Creaking.knockback, SulfurCube.knockback, EnderDragon.knockback; MaceItem.knockback (a push, so not covered).

Harness: tools/test_server_knockback_parity.py, run on Windows on this branch with a temporary print in the empty hook. Result: 22 of 23 cases identical; the one that differs is the listed pvp-mace. The hook fired 30 times, on cows, zombies and players (player_attack, mob_attack, the sprint and Knockback II pushes). The print was removed before committing.

Other projects' code

none (NeoForge: only the public javadoc of LivingKnockBackEvent, no source)

Self-check

New tests, each failing with the hook call stubbed out and passing with it:

  • health::tests::the_native_knockback_hook_changes_or_cancels_a_mobs_push: vanilla's push with no mod; a cancel leaves the motion and needs_sync; doubled and turned around; non-finite values put back; after a 0.5 and a full resistance (raised by the mod); the game's own hit reaches it with its damage.
  • game::creatures::knockback_tests::the_native_knockback_hook_changes_or_cancels_a_players_push: the same on a player (cancel, doubled, after resistance). It needs .local/horizon-client/26.3 and is skipped without it.
  • The fragment stamps cleanly (tools/stamp_native_hooks.py run locally, not committed; it gave API 63 with the other pending fragments set aside). The pending server_tag_reads.json on main fails the stamp by itself (invalid comment path: docs/native-mods.md), which is not this branch.

pr_ready (tools/pr_ready.py --base upstream/main on PCDKBLENDER, against upstream/main at f99f8feaa, 5 min 45 s)

Check Result
Scope ⚠️ 14 files, +484 −76; 560 lines of code: over 500, split it if it can ship in parts
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 510 passed (58 s)
rustfmt ✅ (10 s)
clippy ✅ (18 s)
tests: horizon-client ✅ 1576 passed (4 min 13 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 ❌ 373 affected, (1 s)
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)
  • Scope: about 200 of the 560 lines are the two tests. It is not split: the shield, goat and spear sources are what let every push name its hit.
  • Parity: this was run on Windows, where the runner needs fcntl and can't start. The affected harnesses run in CI (run-parity).

Not tested:

  • The shield push. On Horizon a partly blocking item never gets there. In temporary harness cases (a shield with damage_reductions factor 0.5, against a player and against a zombie), vanilla knocks the holder back and Horizon sends no motion at all. A print in the hook never fired for those hits. The !fully branch is decided by code this change doesn't touch, so the open-coded push it replaces wasn't reached either. That is a separate parity gap (seen on this branch only).
  • The goat's ram and a mob's spear naming their source: no test.
  • lan_e2e with a LAN guest: not run (the guest path is the same knock_back_player as the host's).
  • A real native mod patching the hook in a running game: not run.

🤖 Generated with Claude Code

https://claude.ai/code/session_01RodDbPWCByh96WKcbiEQpB


🔄 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/5637 **Author:** [@mariuslozo-boop](https://github.com/mariuslozo-boop) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `feat/native-knockback-hook` --- ### 📝 Commits (3) - [`be5d818`](https://github.com/horizon-rust/horizon/commit/be5d818a5876f538ff3be3c0ff6786d287ab4226) Native mods: a hook at LivingEntity.knockback to change or cancel a push - [`7a2ea36`](https://github.com/horizon-rust/horizon/commit/7a2ea363ce93e1477a489719ae54af4407db1858) Merge remote-tracking branch 'hr/main' into pr-5637 - [`4b57957`](https://github.com/horizon-rust/horizon/commit/4b5795718084af9f8df6f2a1331c8b93649ab60b) Merge remote-tracking branch 'hr/main' into pr-5637 ### 📊 Changes **14 files changed** (+484 additions, -76 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+2 -1) 📝 `crates/horizon-client/src/game/creatures.rs` (+168 -28) 📝 `crates/horizon-client/src/game/explosion.rs` (+6 -1) 📝 `crates/horizon-client/src/game/lan_pvp.rs` (+12 -2) 📝 `crates/horizon-client/src/game/mace.rs` (+1 -1) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+71 -1) 📝 `crates/horizon-client/src/game/shield.rs` (+15 -24) 📝 `crates/horizon-client/src/game/spear.rs` (+3 -2) 📝 `crates/horizon-client/src/health.rs` (+151 -1) 📝 `crates/horizon-client/src/mobs.rs` (+24 -6) 📝 `crates/horizon-client/src/mobs/goat.rs` (+2 -0) 📝 `crates/horizon-client/src/mobs/spear_use.rs` (+18 -8) 📝 `crates/horizon-client/src/mobs/sulfur_cube.rs` (+1 -1) ➕ `tools/native-hook-fragments/living_knockback.json` (+10 -0) </details> ### 📄 Description > 🤖 Opened by mariuslozo-boop's AI agent (Claude Code, Claude Sonnet 5.5 + Claude Opus 5.5 review), not by mariuslozo-boop personally. ## What and why Closes #5496 (part of #4953). A mod couldn't change the knockback an entity takes. This adds the hook the issue asks for, in the shape of the merged hook PRs (an empty `Game` hook, a `OnceLock` the game sets, a fragment instead of an API number, #5161): - **Hook** `native_living_knockback(entity, kind, damage_type, causing, direct, &mut strength, &mut x, &mut z, &mut canceled)`. It runs in `LivingEntity.knockback` (LivingEntity.java:1647) once the knockback resistance took its share, before the `<= 0` guard, the random-direction draws and the motion change. A mod may change the strength or the direction, or cancel the push (the motion is left alone). A full resistance hands it a strength of 0, which a mod may raise. A non-finite number a mod leaves is put back. - **Source:** in 26.3 every `knockback` call has a `DamageSource`, so every push names one. The pushed entity and its source are named exactly as `native_living_incoming_damage` names the same hit (`Hurting`, `hurt_server_args`), with the same ids still 0. A player's blow on another player names no attacker yet, as its damage doesn't. A goat's ram pushes with `minecraft:mob_attack` and hurts with `mob_attack_no_aggro`, as in vanilla. - **Where it fires** (players, including LAN guests and a dedicated server's, and mobs): | Vanilla caller | Horizon | | --- | --- | | `dealDefaultKnockback` (a hit's own push) | `Mob::damage`, `hurt_player_from`, `sulfur_cube::deal_default_knockback` (no causing entity) | | `Player.causeExtraKnockback` (sprint, Knockback) | `game.rs` attack, `spear.rs`, `lan_pvp.rs` | | `Player.doSweepAttack` | `mace.rs` (mobs), `lan_pvp.rs::sweep_players` | | `LivingEntity.stabAttack` (a mob's spear) | `MobEvent::Knockback`, which now carries its source | | `RamTarget.tick` (goat) | `MobEvent::Knockback`, with `mobAttack(goat)` | | `LivingEntity.blockedByItem` (a shield's push on its holder) | `shield.rs` called `knock_back_player(0.5, ...)`. It had open-coded the push, past the hook, the knockback resistance and the random direction | | `Creaking` / `SulfurCube.knockback` overrides | a creaking that can't move and a sulfur cube with a block hit by an entity don't reach it, as they don't call `super` | - **Not reached yet**, listed in the hook's docs: a mob's melee push from its attack knockback or a Knockback weapon (`Mob.doHurtTarget`'s `causeExtraKnockback`), which Horizon makes as a plain push (`mobs/ai.rs`); an armor stand's sweep push (open-coded in `mace.rs`); the ender dragon, which Horizon doesn't knock back. The pushes vanilla doesn't make with `knockback` don't fire it: the dragon's wings, Punch, explosions, wind charges, a mace smash's area, a hoglin's throw. - **Unhooked**, the game calls an empty function. The `server_knockback` harness (below) shows the pushes are unchanged. #5606 (shield on Easy) touches the same lines of `shield.rs` and the same files (`creatures.rs`, `explosion.rs`, `health.rs`); whichever lands second needs a rebase. NeoForge's public `LivingKnockBackEvent` javadoc (read, no source): fired when a living entity is about to be knocked back (in `hurt`, `blockUsingShield`, `Mob.doHurtTarget`, `Player.attack`), with a strength and an x/z ratio, cancelable. It doesn't say whether the resistance is applied first. The issue asks for after, as here. ## 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 `LivingEntity.knockback` (1647), `dealDefaultKnockback`, `blockedByItem`, `causeExtraKnockback`, `stabAttack`; `Player.causeExtraKnockback`, `Player.doSweepAttack`; `Mob.doHurtTarget`; `RamTarget.tick`; the overrides `Creaking.knockback`, `SulfurCube.knockback`, `EnderDragon.knockback`; `MaceItem.knockback` (a `push`, so not covered). Harness: `tools/test_server_knockback_parity.py`, run on Windows on this branch with a temporary print in the empty hook. Result: **22 of 23 cases identical**; the one that differs is the listed `pvp-mace`. The hook fired 30 times, on cows, zombies and players (`player_attack`, `mob_attack`, the sprint and Knockback II pushes). The print was removed before committing. ## Other projects' code none (NeoForge: only the public javadoc of `LivingKnockBackEvent`, no source) ## Self-check New tests, each failing with the hook call stubbed out and passing with it: - `health::tests::the_native_knockback_hook_changes_or_cancels_a_mobs_push`: vanilla's push with no mod; a cancel leaves the motion and `needs_sync`; doubled and turned around; non-finite values put back; after a 0.5 and a full resistance (raised by the mod); the game's own hit reaches it with its damage. - `game::creatures::knockback_tests::the_native_knockback_hook_changes_or_cancels_a_players_push`: the same on a player (cancel, doubled, after resistance). It needs `.local/horizon-client/26.3` and is skipped without it. - The fragment stamps cleanly (`tools/stamp_native_hooks.py` run locally, not committed; it gave API 63 with the other pending fragments set aside). The pending `server_tag_reads.json` on main fails the stamp by itself (`invalid comment path: docs/native-mods.md`), which is not this branch. **pr_ready** (`tools/pr_ready.py --base upstream/main` on PCDKBLENDER, against upstream/main at f99f8feaa, 5 min 45 s) | Check | Result | | --- | --- | | Scope | ⚠️ 14 files, +484 −76; 560 lines of code: over 500, split it if it can ship in parts | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 510 passed (58 s) | | rustfmt | ✅ (10 s) | | clippy | ✅ (18 s) | | tests: horizon-client | ✅ 1576 passed (4 min 13 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 | ❌ 373 affected, (1 s) | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | - Scope: about 200 of the 560 lines are the two tests. It is not split: the shield, goat and spear sources are what let every push name its hit. - Parity: this was run on Windows, where the runner needs `fcntl` and can't start. The affected harnesses run in CI (`run-parity`). Not tested: - **The shield push.** On Horizon a partly blocking item never gets there. In temporary harness cases (a shield with `damage_reductions` factor 0.5, against a player and against a zombie), vanilla knocks the holder back and Horizon sends no motion at all. A print in the hook never fired for those hits. The `!fully` branch is decided by code this change doesn't touch, so the open-coded push it replaces wasn't reached either. That is a separate parity gap (seen on this branch only). - The goat's ram and a mob's spear naming their source: no test. - `lan_e2e` with a LAN guest: not run (the guest path is the same `knock_back_player` as the host's). - A real native mod patching the hook in a running game: not run. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01RodDbPWCByh96WKcbiEQpB --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:20:01 +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#6406
No description provided.