[PR #5170] [MERGED] feat(mods): native mods change or cancel damage #6116

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

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/5170
Author: @reservedbytes
Created: 10/31/2026
Status: ✅ Merged
Merged: 10/31/2026
Merged by: @ludovic111

Base: main ← Head: feat/native-hurt-server-v2


📝 Commits (2)

  • ea26d1a feat(mods): native mods change or cancel damage
  • 4ba9e95 Merge main; native damage hook as a fragment; canceled hits credit no attacker

📊 Changes

15 files changed (+363 additions, -6 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+7 -0)
📝 crates/horizon-client/src/game/combat.rs (+12 -0)
📝 crates/horizon-client/src/game/creatures.rs (+27 -0)
📝 crates/horizon-client/src/game/explosion.rs (+2 -0)
📝 crates/horizon-client/src/game/fangs.rs (+1 -1)
📝 crates/horizon-client/src/game/lan_actions.rs (+1 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+102 -0)
📝 crates/horizon-client/src/health.rs (+156 -3)
📝 crates/horizon-client/src/hunger.rs (+5 -0)
📝 crates/horizon-client/src/mobs.rs (+15 -0)
📝 crates/horizon-client/src/mobs/armadillo.rs (+2 -2)
📝 crates/horizon-client/src/motion.rs (+3 -0)
📝 crates/horizon-world/src/living_hurt.rs (+12 -0)
📝 crates/horizon-world/src/server_player_combat.rs (+8 -0)
➕ tools/native-hook-fragments/living_incoming_damage.json (+10 -0)

📄 Description

What and why

Native mods can't change or cancel damage yet, which protection mods, armour effects and damage multipliers need

native_living_incoming_damage(entity, kind, damage_type, causing, direct, amount, canceled) fires where LivingEntity.hurtServer starts, after its guards, for players and mobs
I was going to use the vanilla name (LivingEntity.hurtServer), but levz named it living_incoming_damage in the docs (#4402)
causing and direct are the entity ids of vanilla's DamageSource.getEntity and getDirectEntity (a skeleton and its arrow, or the attacker itself in melee), 0 for none
A mod can change amount or set canceled, and a canceled hit is a refused one: no damage, hurt sound, knockback, invulnerable time, statistics or criteria
The caller acts as on any refused hit, so a blast still pushes a mob whose hit was canceled, as vanilla pushes one that refused it
A ghast's canceled hit by a fireball sent back at it counts as landed, as Ghast.hurtServer returns true for it
A negative or NaN amount from a mod is clamped as vanilla does (0, or Float.MAX_VALUE)
A curled-up armadillo's cut now leaves vanilla's negative amount for that clamp, so the mod sees what vanilla's hurtServer gets
Fangs whose evoker is gone no longer name it as the attacker of a mob they bite, as vanilla's EvokerFangs.dealDamageTo uses plain magic then
The ender dragon doesn't reach the hook yet
A follow-up adds it

The damage code is in the lib, which can't reach Game, so the hook has no self and goes through a function pointer the game sets at start
A projectile owner the game keeps from an earlier hit is used only when the damage is a projectile's, so a zombie's hit after a skeleton's stray arrow doesn't name the skeleton
Rule 5 of docs/native-hooks-neoforge.md would give a server-side hook &mut self, but that isn't possible here
Every call runs while the hurt entity is mutably borrowed out of Game (a mob out of its list, the player's body)
A function pointer can't carry &mut Game without aliasing that borrow, which is unsound
So the hook takes the event's data as parameters, which fits rule 5's intent as closely as Horizon's borrow structure allows

A player's damage type is its source's own
A mob's is worked out from Horizon's damage kind for the hit (source_damage_type), which merges some of vanilla's types:
A trident's hit on a mob is minecraft:arrow
Vanilla's is minecraft:trident
A wither skull's hit on a mob is minecraft:magic
Vanilla's is minecraft:wither_skull
Evoker fangs on a mob are minecraft:magic
Vanilla's are minecraft:indirect_magic
A fireball nobody shot is minecraft:fireball
Vanilla's is minecraft:unattributed_fireball
A shulker bullet's hit is never minecraft:mob_projectile
A bed or respawn anchor blast on a mob is minecraft:explosion
Vanilla's is minecraft:bad_respawn_point, which players do get
A blast from TNT a player lit is minecraft:explosion on a mob
Vanilla's is minecraft:player_explosion
The docs list these, and new damage kinds for them are left for another pull request

causing and direct are filled where the game already knows them, and are 0 for now in these cases:
A player's direct entity, always
A player's causing entity, except for a mob that shot the projectile, the mob /damage names, an evoker that summoned the fangs, a slime or magma cube that touched the player, and a mob that exploded
A sulfur cube's causing id is guessed until its blast names its source
A mob's causing and direct entities when a player is behind the hit (melee, arrows, tridents, blasts, TNT a player lit)
A mob's direct entity when it isn't the causing one (a projectile, a blast, a potion)
A falling block on a mob, whose causing and direct entity is the block in vanilla
/damage on a mob by an entity with a damage type that isn't melee leaves the direct entity 0
/damage on a mob by one entity from another with a melee type gives the from entity as the direct one
Vanilla gives the by one

Horizon's mobs don't check the fire resistance effect among the guards yet, so a mob with it still reaches the hook for fire damage
/kill and native_kill_mob don't reach the hook yet
They set the health to 0 instead of hurting with minecraft:generic_kill
Falling out of the world does reach it, as minecraft:out_of_world
It runs on the thread that ticks the world (the server thread, the window's on the deprecated in-frame path), so a mod that shares state with its render thread hooks needs to synchronise it
On the in-frame path the player's id is 0, or the host's once the world is open to LAN

Without a mod nothing changes: the empty adapter compiles to xor eax, eax; ret (the same OnceLock, function pointer and adapter built on their own with -O)
That leaves about 1.7 ns per hit for the pointer's load and the indirect call, and a mod adds only its own code on top

This bumps the hook points API to 41 (renumbered at merge if needed)

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.hurtServer (LivingEntity.java:1192: the guards to 1199, the hook where 1200 starts, the clamps at 1206 and 1224)
Ghast.hurtServer (Ghast.java:101, true for a reflected fireball at 104)
Armadillo.hurtServer (Armadillo.java:294)
DamageSources.fireball (DamageSources.java:231)

Other projects' code

none

Self-check

Check Result
Scope ✅ 15 files, +382 −7
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 495 passed (57 s)
rustfmt ✅ (8 s)
clippy ✅ (1 min 54 s)
tests: horizon-client, horizon-world ✅ 1659 passed (6 min 26 s)
doc tests ✅ 0 passed (3 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/5170 **Author:** [@reservedbytes](https://github.com/reservedbytes) **Created:** 10/31/2026 **Status:** ✅ Merged **Merged:** 10/31/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `feat/native-hurt-server-v2` --- ### 📝 Commits (2) - [`ea26d1a`](https://github.com/horizon-rust/horizon/commit/ea26d1a145ece7797c428fffd300e3a9bfeda4c1) feat(mods): native mods change or cancel damage - [`4ba9e95`](https://github.com/horizon-rust/horizon/commit/4ba9e95bf9a9ed4ebb91940081995a8b6c13c05a) Merge main; native damage hook as a fragment; canceled hits credit no attacker ### 📊 Changes **15 files changed** (+363 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+7 -0) 📝 `crates/horizon-client/src/game/combat.rs` (+12 -0) 📝 `crates/horizon-client/src/game/creatures.rs` (+27 -0) 📝 `crates/horizon-client/src/game/explosion.rs` (+2 -0) 📝 `crates/horizon-client/src/game/fangs.rs` (+1 -1) 📝 `crates/horizon-client/src/game/lan_actions.rs` (+1 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+102 -0) 📝 `crates/horizon-client/src/health.rs` (+156 -3) 📝 `crates/horizon-client/src/hunger.rs` (+5 -0) 📝 `crates/horizon-client/src/mobs.rs` (+15 -0) 📝 `crates/horizon-client/src/mobs/armadillo.rs` (+2 -2) 📝 `crates/horizon-client/src/motion.rs` (+3 -0) 📝 `crates/horizon-world/src/living_hurt.rs` (+12 -0) 📝 `crates/horizon-world/src/server_player_combat.rs` (+8 -0) ➕ `tools/native-hook-fragments/living_incoming_damage.json` (+10 -0) </details> ### 📄 Description ## What and why Native mods can't change or cancel damage yet, which protection mods, armour effects and damage multipliers need `native_living_incoming_damage(entity, kind, damage_type, causing, direct, amount, canceled)` fires where `LivingEntity.hurtServer` starts, after its guards, for players and mobs I was going to use the vanilla name (`LivingEntity.hurtServer`), but levz named it `living_incoming_damage` in the docs (#4402) `causing` and `direct` are the entity ids of vanilla's `DamageSource.getEntity` and `getDirectEntity` (a skeleton and its arrow, or the attacker itself in melee), 0 for none A mod can change `amount` or set `canceled`, and a canceled hit is a refused one: no damage, hurt sound, knockback, invulnerable time, statistics or criteria The caller acts as on any refused hit, so a blast still pushes a mob whose hit was canceled, as vanilla pushes one that refused it A ghast's canceled hit by a fireball sent back at it counts as landed, as `Ghast.hurtServer` returns true for it A negative or NaN amount from a mod is clamped as vanilla does (0, or `Float.MAX_VALUE`) A curled-up armadillo's cut now leaves vanilla's negative amount for that clamp, so the mod sees what vanilla's `hurtServer` gets Fangs whose evoker is gone no longer name it as the attacker of a mob they bite, as vanilla's `EvokerFangs.dealDamageTo` uses plain magic then The ender dragon doesn't reach the hook yet A follow-up adds it The damage code is in the lib, which can't reach `Game`, so the hook has no `self` and goes through a function pointer the game sets at start A projectile owner the game keeps from an earlier hit is used only when the damage is a projectile's, so a zombie's hit after a skeleton's stray arrow doesn't name the skeleton Rule 5 of `docs/native-hooks-neoforge.md` would give a server-side hook `&mut self`, but that isn't possible here Every call runs while the hurt entity is mutably borrowed out of `Game` (a mob out of its list, the player's body) A function pointer can't carry `&mut Game` without aliasing that borrow, which is unsound So the hook takes the event's data as parameters, which fits rule 5's intent as closely as Horizon's borrow structure allows A player's damage type is its source's own A mob's is worked out from Horizon's damage kind for the hit (`source_damage_type`), which merges some of vanilla's types: A trident's hit on a mob is `minecraft:arrow` Vanilla's is `minecraft:trident` A wither skull's hit on a mob is `minecraft:magic` Vanilla's is `minecraft:wither_skull` Evoker fangs on a mob are `minecraft:magic` Vanilla's are `minecraft:indirect_magic` A fireball nobody shot is `minecraft:fireball` Vanilla's is `minecraft:unattributed_fireball` A shulker bullet's hit is never `minecraft:mob_projectile` A bed or respawn anchor blast on a mob is `minecraft:explosion` Vanilla's is `minecraft:bad_respawn_point`, which players do get A blast from TNT a player lit is `minecraft:explosion` on a mob Vanilla's is `minecraft:player_explosion` The docs list these, and new damage kinds for them are left for another pull request `causing` and `direct` are filled where the game already knows them, and are 0 for now in these cases: A player's direct entity, always A player's causing entity, except for a mob that shot the projectile, the mob `/damage` names, an evoker that summoned the fangs, a slime or magma cube that touched the player, and a mob that exploded A sulfur cube's causing id is guessed until its blast names its source A mob's causing and direct entities when a player is behind the hit (melee, arrows, tridents, blasts, TNT a player lit) A mob's direct entity when it isn't the causing one (a projectile, a blast, a potion) A falling block on a mob, whose causing and direct entity is the block in vanilla `/damage` on a mob `by` an entity with a damage type that isn't melee leaves the direct entity 0 `/damage` on a mob `by` one entity `from` another with a melee type gives the `from` entity as the direct one Vanilla gives the `by` one Horizon's mobs don't check the fire resistance effect among the guards yet, so a mob with it still reaches the hook for fire damage `/kill` and `native_kill_mob` don't reach the hook yet They set the health to 0 instead of hurting with `minecraft:generic_kill` Falling out of the world does reach it, as `minecraft:out_of_world` It runs on the thread that ticks the world (the server thread, the window's on the deprecated in-frame path), so a mod that shares state with its render thread hooks needs to synchronise it On the in-frame path the player's id is 0, or the host's once the world is open to LAN Without a mod nothing changes: the empty adapter compiles to `xor eax, eax; ret` (the same OnceLock, function pointer and adapter built on their own with `-O`) That leaves about 1.7 ns per hit for the pointer's load and the indirect call, and a mod adds only its own code on top This bumps the hook points API to 41 (renumbered at merge if needed) ## 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.hurtServer` (`LivingEntity.java:1192`: the guards to 1199, the hook where 1200 starts, the clamps at 1206 and 1224) `Ghast.hurtServer` (`Ghast.java:101`, true for a reflected fireball at 104) `Armadillo.hurtServer` (`Armadillo.java:294`) `DamageSources.fireball` (`DamageSources.java:231`) ## Other projects' code none ## Self-check | Check | Result | | --- | --- | | Scope | ✅ 15 files, +382 −7 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 495 passed (57 s) | | rustfmt | ✅ (8 s) | | clippy | ✅ (1 min 54 s) | | tests: horizon-client, horizon-world | ✅ 1659 passed (6 min 26 s) | | doc tests | ✅ 0 passed (3 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:12:08 +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#6116
No description provided.