[PR #7716] Native hooks: can_player_sleep, player_xp_change #7782

Open
opened 2026-11-03 07:24:06 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/7716
Author: @levzzz5154
Created: 11/2/2026
Status: 🔄 Open

Base: main ← Head: native/player-sleep-xp-enchant


📝 Commits (1)

  • b11f571 Native mods: sleeping problem and experience change (can_player_sleep, player_xp_change)

📊 Changes

7 files changed (+232 additions, -40 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+59 -38)
📝 crates/horizon-client/src/game/advancements.rs (+2 -1)
📝 crates/horizon-client/src/game/commands.rs (+16 -1)
📝 crates/horizon-client/src/game/native_hooks.rs (+36 -0)
📝 crates/horizon-client/src/mod_api/generated.rs (+86 -0)
➕ tools/native-hook-fragments/can_player_sleep.json (+16 -0)
➕ tools/native-hook-fragments/player_xp_change.json (+17 -0)

📄 Description

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

Part of #4953: CanPlayerSleepEvent, PlayerXpEvent.XpChange. Two lines of the Not started list are not added (see below).

What it adds

can_player_sleep (CanPlayerSleepEvent, server). Game::try_sleep (vanilla ServerPlayer.startSleepInBed) now finds vanilla's first problem without acting on it, in vanilla's order: bed out of reach, obstructed, then (after the spawn point is set, only when nothing failed yet) the bed rule's "only at night", then monsters nearby (not creative). The hook gets player, name, the bed head pos and problem: &mut Option<String>. A mod sets None to let the player sleep (day, monsters, far away) or sets a name to refuse: too_far_away, obstructed, not_possible_now, not_safe show vanilla's message; other_problem, and any other name, refuses without a message. Vanilla 26.3 has no NOT_POSSIBLE_HERE/NOT_POSSIBLE_NOW constants (BedSleepingProblem is a record: too far, obstructed, other, not safe, and the bed rule's own error), so not_possible_now stands for the bed rule's no_sleep message. With no mod the flow and messages are unchanged.
Relation to player_bed_enter (Bukkit-style): that one fires after all checks pass and can only cancel. can_player_sleep fires before it, sees the check result and can change it. player_bed_enter still fires afterwards and may cancel.

player_xp_change (PlayerXpEvent.XpChange, server, cancellable). Fires at Horizon's Player.giveExperiencePoints sites: orb pickup (after mending), advancement experience rewards, and /xp add <n> points. A mod may change amount or cancel. Orbs and advancement rewards treat a negative amount as 0; /xp add keeps the sign, as vanilla does. Experience bottles and furnace XP spawn orbs, so they arrive through the orb path. With no mods the helper returns early (no allocation).
Relation to player_exp_change (existing): that one hears orb pickups only, after mending, and cannot cancel. It is kept; for an orb pickup it fires first, then player_xp_change.

Not added

  • PlayerEnchantItemEvent: enchant_item already exists (native_enchant_item(player, name, pos, item, levels)), fired in lan_station_enchant after the levels are charged and the stat and criterion are done (vanilla EnchantmentMenu.clickMenuButton after onEnchantmentPerformed), with the finished stack and levels charged. Same contract as NeoForge's listen-only event, so a second hook would be a duplicate.
  • PlayerEvent.ItemSmeltedEvent: furnace_extract already exists (native_furnace_extract(player, name, pos, item, count)), fired where vanilla runs FurnaceResultSlot.checkTakeAchievements when the output count drops (click and shift-click). Same contract, so no second hook.

Proof

Fast build, horizon-ctl launch --world W --creative --offscreen --wait, scratch homes, throwaway eprintln! plus env-driven overrides in the hook bodies (removed; git diff has none). A flat stone floor at y=99, red bed at 101/102,100,100, horizon-ctl interact 101 100 100.

Sleep (server thread):

  • No mod, day: TESTHOOK sleep ... [102,100,100] problem=Some("not_possible_now"); the bed stays occupied=false, as before.
  • No mod, night: problem=None; the bed became occupied=true.
  • Day with the override none: hook saw not_possible_now; the player was moved to the bed (Pos 102.5, 100.6875, 100.5), i.e. sleep started.
  • Night with the override not_safe: hook saw None; the bed stayed occupied=false, sleep refused.

XP (xp set @p 0 points, xp add @p 10 points, then a 5-point orb summoned on the player):

  • No mod: TESTHOOK xp ... amount=10, amount=5; points 3 (level 1) then 8.
  • Cancel: both hook lines printed; points stay 0.
  • Set to 25: xp add 10 gave 25 (level 2, 9 points), the orb gave 25 more (level 4).

Not run: a dedicated-server run; the advancement-reward site (no easy reward-with-xp advancement to trigger by command); a monster for the real not_safe path (forced with the override instead); a real mod crate.

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

Check Result
Scope ✅ 7 files, +232 −40
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 614 passed (31 s)
rustfmt ✅ (7 s)
clippy ✅ (1 min 29 s)
tests: horizon-client, horizon-client-lib ✅ 2175 passed (1 min 21 s)
doc tests ✅ 0 passed (4 s)
content root tests ➖ content root untouched
mod API up to date ✅ (14 s)
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)

🤖 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/7716 **Author:** [@levzzz5154](https://github.com/levzzz5154) **Created:** 11/2/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `native/player-sleep-xp-enchant` --- ### 📝 Commits (1) - [`b11f571`](https://github.com/horizon-rust/horizon/commit/b11f571aab7a35554762904d9125718235b924d3) Native mods: sleeping problem and experience change (can_player_sleep, player_xp_change) ### 📊 Changes **7 files changed** (+232 additions, -40 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+59 -38) 📝 `crates/horizon-client/src/game/advancements.rs` (+2 -1) 📝 `crates/horizon-client/src/game/commands.rs` (+16 -1) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+36 -0) 📝 `crates/horizon-client/src/mod_api/generated.rs` (+86 -0) ➕ `tools/native-hook-fragments/can_player_sleep.json` (+16 -0) ➕ `tools/native-hook-fragments/player_xp_change.json` (+17 -0) </details> ### 📄 Description > 🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Sonnet 5.5), not by levzzz5154 personally. Part of #4953: `CanPlayerSleepEvent`, `PlayerXpEvent.XpChange`. Two lines of the Not started list are not added (see below). ## What it adds **`can_player_sleep`** (`CanPlayerSleepEvent`, server). `Game::try_sleep` (vanilla `ServerPlayer.startSleepInBed`) now finds vanilla's first problem without acting on it, in vanilla's order: bed out of reach, obstructed, then (after the spawn point is set, only when nothing failed yet) the bed rule's "only at night", then monsters nearby (not creative). The hook gets `player`, `name`, the bed head `pos` and `problem: &mut Option<String>`. A mod sets `None` to let the player sleep (day, monsters, far away) or sets a name to refuse: `too_far_away`, `obstructed`, `not_possible_now`, `not_safe` show vanilla's message; `other_problem`, and any other name, refuses without a message. Vanilla 26.3 has no `NOT_POSSIBLE_HERE`/`NOT_POSSIBLE_NOW` constants (`BedSleepingProblem` is a record: too far, obstructed, other, not safe, and the bed rule's own error), so `not_possible_now` stands for the bed rule's `no_sleep` message. With no mod the flow and messages are unchanged. Relation to `player_bed_enter` (Bukkit-style): that one fires after all checks pass and can only cancel. `can_player_sleep` fires before it, sees the check result and can change it. `player_bed_enter` still fires afterwards and may cancel. **`player_xp_change`** (`PlayerXpEvent.XpChange`, server, cancellable). Fires at Horizon's `Player.giveExperiencePoints` sites: orb pickup (after mending), advancement experience rewards, and `/xp add <n> points`. A mod may change `amount` or cancel. Orbs and advancement rewards treat a negative amount as 0; `/xp add` keeps the sign, as vanilla does. Experience bottles and furnace XP spawn orbs, so they arrive through the orb path. With no mods the helper returns early (no allocation). Relation to `player_exp_change` (existing): that one hears orb pickups only, after mending, and cannot cancel. It is kept; for an orb pickup it fires first, then `player_xp_change`. ## Not added - `PlayerEnchantItemEvent`: `enchant_item` already exists (`native_enchant_item(player, name, pos, item, levels)`), fired in `lan_station_enchant` after the levels are charged and the stat and criterion are done (vanilla `EnchantmentMenu.clickMenuButton` after `onEnchantmentPerformed`), with the finished stack and levels charged. Same contract as NeoForge's listen-only event, so a second hook would be a duplicate. - `PlayerEvent.ItemSmeltedEvent`: `furnace_extract` already exists (`native_furnace_extract(player, name, pos, item, count)`), fired where vanilla runs `FurnaceResultSlot.checkTakeAchievements` when the output count drops (click and shift-click). Same contract, so no second hook. ## Proof Fast build, `horizon-ctl launch --world W --creative --offscreen --wait`, scratch homes, throwaway `eprintln!` plus env-driven overrides in the hook bodies (removed; `git diff` has none). A flat stone floor at y=99, red bed at 101/102,100,100, `horizon-ctl interact 101 100 100`. Sleep (server thread): - No mod, day: `TESTHOOK sleep ... [102,100,100] problem=Some("not_possible_now")`; the bed stays `occupied=false`, as before. - No mod, night: `problem=None`; the bed became `occupied=true`. - Day with the override `none`: hook saw `not_possible_now`; the player was moved to the bed (`Pos` 102.5, 100.6875, 100.5), i.e. sleep started. - Night with the override `not_safe`: hook saw `None`; the bed stayed `occupied=false`, sleep refused. XP (`xp set @p 0 points`, `xp add @p 10 points`, then a 5-point orb summoned on the player): - No mod: `TESTHOOK xp ... amount=10`, `amount=5`; points 3 (level 1) then 8. - Cancel: both hook lines printed; points stay 0. - Set to 25: `xp add 10` gave 25 (level 2, 9 points), the orb gave 25 more (level 4). Not run: a dedicated-server run; the advancement-reward site (no easy reward-with-xp advancement to trigger by command); a monster for the real `not_safe` path (forced with the override instead); a real mod crate. **pr_ready** (`tools/pr_ready.py --quick --base hr/main` on cachyos-x8664, against hr/main at f4a9e85d7, 3 min 47 s) | Check | Result | | --- | --- | | Scope | ✅ 7 files, +232 −40 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 614 passed (31 s) | | rustfmt | ✅ (7 s) | | clippy | ✅ (1 min 29 s) | | tests: horizon-client, horizon-client-lib | ✅ 2175 passed (1 min 21 s) | | doc tests | ✅ 0 passed (4 s) | | content root tests | ➖ content root untouched | | mod API up to date | ✅ (14 s) | | 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) | 🤖 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>
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#7782
No description provided.