[PR #6299] [MERGED] Native mods: breathing, LivingBreatheEvent #6781

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

📋 Pull Request Information

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

Base: main ← Head: mods/living-breathe


📝 Commits (2)

📊 Changes

6 files changed (+216 additions, -23 deletions)

View changed files

➕ crates/horizon-client/src/breathing.rs (+130 -0)
📝 crates/horizon-client/src/client_lib.rs (+1 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+21 -0)
📝 crates/horizon-client/src/mobs/ai.rs (+15 -6)
📝 crates/horizon-client/src/motion/environment.rs (+33 -17)
➕ tools/native-hook-fragments/living_breathe.json (+16 -0)

📄 Description

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

What and why

Fixes #6248, part of #4953 ("Living: breathing, and equipment changes of mobs (a player's are heard by on_equip)"), the breathing half. A mod with a breathing apparatus, an air-tight armour set, a gas that drowns the player or a mob that never needs air had no way to change how air runs down and refills; the rules were hard-coded in the player's motion and in the mob tick.

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

  • native_living_breathe(entity, kind, can_breathe, consume, refill) (LivingBreatheEvent): entity is the entity's ID and kind its type (minecraft:player, minecraft:zombie...). The three out parameters come in as Horizon does it today and the mod may change them: with can_breathe the air rises by refill (up to 300); without, it falls by consume, and at -20 it is set to 0 and the entity takes 2 drowning damage. Negative amounts count as 0. It does not cancel; several mods can hook it.

It is a free function in a new lib module, breathing.rs, like native_loot_function and native_menu_slot_rules, because the player's motion (motion/environment.rs) and the mob tick (mobs/ai.rs) have no Game to call a method on. The module also holds Breath, breathe (calls the hook, clamps) and apply (the air arithmetic, unit tested). native_hooks.rs's catalog test reads the new file for free hooks (native_breathing_stays_hookable checks it stays hookable).

Where it fires, each tick:

  • Players: tick_environment_damage, on the server's copy of the player (not a client's prediction, client_side). Vanilla's answer: eyes in water and able to drown means can_breathe false, consume 1 (0 when the oxygen bonus spares the tick; its random draw happens at the same point as before, so the random stream is unchanged), refill 0; otherwise can_breathe true with refill 4, or 0 for a submerged player held by the nautilus' breath.
  • Mobs: the generic air block of base_tick (eyes in water and no breathes_water means drowning, else refill 4). Fish and other mobs that need water (needs_water) and axolotls keep their own air logic, as their handleAirSupply overrides the generic block in vanilla too.

Choices a reviewer may want to look at:

  • Without a mod nothing changes: the arithmetic is the old one, with -20 now tested as <= -20 (vanilla's shouldTakeDrowningDamage), which only matters when a mod consumes more than 1 per tick.
  • NeoForge's LivingDrownEvent and the nautilus are not part of this step.
  • The hook runs for every ticking mob each tick, like other per-entity hooks; it is a no-op call without a mod.

Kind

  • feature: something new for players (mod authors)

Vanilla reference

LivingEntity.baseTick: on the server, an entity that isEyeInFluid(WATER) (not in a bubble column) and cannot breathe there has decreaseAirSupply applied (1, or 0 by the oxygen bonus chance), and shouldTakeDrowningDamage (air at -20 or less) sets it to 0 and hurts it for 2; any other entity gets increaseAirSupply (+4, up to getMaxAirSupply, 300). NeoForge posts LivingBreatheEvent(entity, canBreathe, consumeAirAmount, refillAirAmount) there, with vanilla's answers as defaults, every tick; it cannot be cancelled.

Other projects' code

None. Names and contracts only from NeoForge's public LivingBreatheEvent.

Proof in the real game

Engine from this branch with a throwaway probe in the hook body (not in this PR): it printed every 400th call and, when an environment variable was set, answered can_breathe = false, consume = 5 for the player, or consume = 20 for a cow. A survival player on a stone platform, a 4x4x4 water pool, a cow and a zombie in the pool.

Server thread (horizon-ctl launch --world L --creative --offscreen, then gamemode survival), no override. Called for every mob and the player (TESTHOOK breathe call ... kind=minecraft:cow can=true consume=1 refill=4); the player standing in the water, state every 4 seconds: air 300, 270, 190, 110, 29, -11 with health 20 until the last (18). A cow in the water, a line per 10 ticks from the mob branch: air 290 down to 0 and -10, then -19 -> 0 drowned five times until it died.

Server thread, player override (can_breathe=false, consume=5), standing on dry ground: air 0 within 4 seconds, health 1.5, then 0 (the player died of drowning).

Server thread, cow override (consume=20), cow in water: air 120 -> 100, then 0 -> 0 drowned five times; dead within seconds.

Dedicated server (--dedicated) with a client connected as Alex: with no player override the player's air fell (201, 141, 81 at 3 s steps; the water had spread to where it stood); a cow in the pool with the override: air 120 -> 100 then 0 -> 0 drowned five times, and the server log had 38 gas lines and "Alex drowned" in a separate run with the player override.

Not run: a mod bundle using the hook; the nautilus' breath; the oxygen bonus (respiration helmet); water breathing; a mob with its own air logic (fish, axolotl) to see the hook is not called for it; creative or spectator players. The .local mob tests were not part of the self-check below (see next paragraph).

Note on the self-check: entities::armor_stand_motion_tests::a_tick_keeps_the_previous_position_for_interpolation fails on hr/main itself (455b6916b) when the prepared resources are present in .local/horizon-client, whether or not this branch is applied; it skips without them. The table below is from a run without them. With them the 17 tests with air, breath or drown in the name pass.

Self-check

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

Check Result
Scope ✅ 6 files, +216 −23
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 586 passed (30 s)
rustfmt ✅ (6 s)
clippy ✅ (14 s)
tests: horizon-client ✅ 1809 passed (14 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/6299 **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/living-breathe` --- ### 📝 Commits (2) - [`38f3208`](https://github.com/horizon-rust/horizon/commit/38f320860a21b1c2de3cf771b5e54e3e0d09bf49) Native mods: breathing, LivingBreatheEvent - [`5706ce9`](https://github.com/horizon-rust/horizon/commit/5706ce95c720911dc10a2b9cf468cfb1f7beeff6) Merge main into #6299 ### 📊 Changes **6 files changed** (+216 additions, -23 deletions) <details> <summary>View changed files</summary> ➕ `crates/horizon-client/src/breathing.rs` (+130 -0) 📝 `crates/horizon-client/src/client_lib.rs` (+1 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+21 -0) 📝 `crates/horizon-client/src/mobs/ai.rs` (+15 -6) 📝 `crates/horizon-client/src/motion/environment.rs` (+33 -17) ➕ `tools/native-hook-fragments/living_breathe.json` (+16 -0) </details> ### 📄 Description > 🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Sonnet 5.5), not by levzzz5154 personally. ## What and why Fixes #6248, part of #4953 ("Living: breathing, and equipment changes of mobs (a player's are heard by `on_equip`)"), the breathing half. A mod with a breathing apparatus, an air-tight armour set, a gas that drowns the player or a mob that never needs air had no way to change how air runs down and refills; the rules were hard-coded in the player's motion and in the mob tick. One server hook point, API from a fragment (`tools/native-hook-fragments/living_breathe.json`): - `native_living_breathe(entity, kind, can_breathe, consume, refill)` (`LivingBreatheEvent`): `entity` is the entity's ID and `kind` its type (`minecraft:player`, `minecraft:zombie`...). The three out parameters come in as Horizon does it today and the mod may change them: with `can_breathe` the air rises by `refill` (up to 300); without, it falls by `consume`, and at -20 it is set to 0 and the entity takes 2 drowning damage. Negative amounts count as 0. It does not cancel; several mods can hook it. It is a free function in a new lib module, `breathing.rs`, like `native_loot_function` and `native_menu_slot_rules`, because the player's motion (`motion/environment.rs`) and the mob tick (`mobs/ai.rs`) have no `Game` to call a method on. The module also holds `Breath`, `breathe` (calls the hook, clamps) and `apply` (the air arithmetic, unit tested). `native_hooks.rs`'s catalog test reads the new file for free hooks (`native_breathing_stays_hookable` checks it stays hookable). Where it fires, each tick: - Players: `tick_environment_damage`, on the server's copy of the player (not a client's prediction, `client_side`). Vanilla's answer: eyes in water and able to drown means `can_breathe` false, `consume` 1 (0 when the oxygen bonus spares the tick; its random draw happens at the same point as before, so the random stream is unchanged), `refill` 0; otherwise `can_breathe` true with `refill` 4, or 0 for a submerged player held by the nautilus' breath. - Mobs: the generic air block of `base_tick` (eyes in water and no `breathes_water` means drowning, else refill 4). Fish and other mobs that need water (`needs_water`) and axolotls keep their own air logic, as their `handleAirSupply` overrides the generic block in vanilla too. Choices a reviewer may want to look at: - Without a mod nothing changes: the arithmetic is the old one, with `-20` now tested as `<= -20` (vanilla's `shouldTakeDrowningDamage`), which only matters when a mod consumes more than 1 per tick. - NeoForge's `LivingDrownEvent` and the nautilus are not part of this step. - The hook runs for every ticking mob each tick, like other per-entity hooks; it is a no-op call without a mod. ## Kind - [x] feature: something new for players (mod authors) ## Vanilla reference `LivingEntity.baseTick`: on the server, an entity that `isEyeInFluid(WATER)` (not in a bubble column) and cannot breathe there has `decreaseAirSupply` applied (1, or 0 by the oxygen bonus chance), and `shouldTakeDrowningDamage` (air at -20 or less) sets it to 0 and hurts it for 2; any other entity gets `increaseAirSupply` (+4, up to `getMaxAirSupply`, 300). NeoForge posts `LivingBreatheEvent(entity, canBreathe, consumeAirAmount, refillAirAmount)` there, with vanilla's answers as defaults, every tick; it cannot be cancelled. ## Other projects' code None. Names and contracts only from NeoForge's public `LivingBreatheEvent`. ## Proof in the real game Engine from this branch with a throwaway probe in the hook body (not in this PR): it printed every 400th call and, when an environment variable was set, answered `can_breathe = false, consume = 5` for the player, or `consume = 20` for a cow. A survival player on a stone platform, a 4x4x4 water pool, a cow and a zombie in the pool. Server thread (`horizon-ctl launch --world L --creative --offscreen`, then `gamemode survival`), no override. Called for every mob and the player (`TESTHOOK breathe call ... kind=minecraft:cow can=true consume=1 refill=4`); the player standing in the water, `state` every 4 seconds: air 300, 270, 190, 110, 29, -11 with health 20 until the last (18). A cow in the water, a line per 10 ticks from the mob branch: air 290 down to 0 and -10, then `-19 -> 0 drowned` five times until it died. Server thread, player override (`can_breathe=false`, `consume=5`), standing on dry ground: air 0 within 4 seconds, health 1.5, then 0 (the player died of drowning). Server thread, cow override (`consume=20`), cow in water: `air 120 -> 100`, then `0 -> 0 drowned` five times; dead within seconds. Dedicated server (`--dedicated`) with a client connected as Alex: with no player override the player's air fell (201, 141, 81 at 3 s steps; the water had spread to where it stood); a cow in the pool with the override: `air 120 -> 100` then `0 -> 0 drowned` five times, and the server log had 38 `gas` lines and "Alex drowned" in a separate run with the player override. Not run: a mod bundle using the hook; the nautilus' breath; the oxygen bonus (respiration helmet); water breathing; a mob with its own air logic (fish, axolotl) to see the hook is not called for it; creative or spectator players. The `.local` mob tests were not part of the self-check below (see next paragraph). Note on the self-check: `entities::armor_stand_motion_tests::a_tick_keeps_the_previous_position_for_interpolation` fails on hr/main itself (455b6916b) when the prepared resources are present in `.local/horizon-client`, whether or not this branch is applied; it skips without them. The table below is from a run without them. With them the 17 tests with `air`, `breath` or `drown` in the name pass. ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base hr/main` on cachyos-x8664, against hr/main at 455b6916b, 1 min 6 s) | Check | Result | | --- | --- | | Scope | ✅ 6 files, +216 −23 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 586 passed (30 s) | | rustfmt | ✅ (6 s) | | clippy | ✅ (14 s) | | tests: horizon-client | ✅ 1809 passed (14 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:30 +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#6781
No description provided.