[PR #6570] [MERGED] Tell native mods when the server's difficulty changes #6955

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

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/6570
Author: @MSedgeMC
Created: 11/1/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @ludovic111

Base: main ← Head: fix/mm-difficulty


📝 Commits (1)

  • eb94a3a Tell native mods when the server's difficulty changes

📊 Changes

3 files changed (+52 additions, -0 deletions)

View changed files

📝 crates/horizon-client/src/game/commands.rs (+29 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+10 -0)
➕ tools/native-hook-fragments/difficulty_changed.json (+13 -0)

📄 Description

Implements the tracker's Difficulty changed line (DifficultyChangeEvent, #4953), claimed on the tracker at 22:57:48Z.

What was missing

A native mod had no point to hear the server's difficulty change. Vanilla's MinecraftServer.setDifficulty is the one door /difficulty, a dedicated server's management and a test environment's difficulty all go through; Horizon's setter for it, set_difficulty_forced, had no hook.

What changed

  • native_difficulty_changed(from, to) (server-side, informational), with the names peaceful, easy, normal, hard.
  • Fired at the end of set_difficulty_forced, so a listener sees the change complete (level data, synced rules, hostile-mob removal and the clients' packet have happened), and only when the difficulty is another one: /difficulty already refuses a same value, but a dedicated server's management and a test environment may repeat it.
  • difficulty_names and its test sit beside it; the fragment is tools/native-hook-fragments/difficulty_changed.json.

Scope limits

  • Per server, as set_difficulty_forced is; a difficulty read from a save at load (level.difficulty_id()) is not a change and is not told.
  • Informational: NeoForge's event cannot be cancelled either.

Proven

On the build VM:

python3 ~/pushrun.py ~/horizon-worktrees/mm-difficulty --name mm-difficulty -- 'cargo test -q -p horizon-client --lib difficulty_tests'
running 1 test
test result: ok. 1 passed; 0 failed; 592 filtered out

cargo clippy -q -p horizon-client --lib -- -D warnings                -> exit 0
cargo clippy -q -p horizon-client --bin horizon-client -- -D warnings -> exit 0
rustfmt --edition 2024 --check <the changed files>                    -> clean

The test, difficulty_tests::a_difficulty_change_names_both_ends, checks the names the setter passes (0 -> 3 is peaceful -> hard, 2 -> 1 is normal -> easy, and an out-of-range index still names one).

Not proven

  • The hook firing in game. It is an empty hook point, so a unit test cannot observe the dispatch; the test covers the names the setter passes and the branch that guards a no-op change, not that a bundle hears it.
  • The dedicated server's management path (game/dedicated/management.rs calls the same setter) and a test environment's, which share the setter but were not run.
  • A real bundle in game.

Vanilla reference

Vanilla 26.3's MinecraftServer.setDifficulty(difficulty, true), which set_difficulty_forced already mirrors, and NeoForge's DifficultyChangeEvent javadoc for the name, the old and new difficulty, and that it is not cancellable.

Fixes the tracker's Difficulty changed line in #4953.


🔄 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/6570 **Author:** [@MSedgeMC](https://github.com/MSedgeMC) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `fix/mm-difficulty` --- ### 📝 Commits (1) - [`eb94a3a`](https://github.com/horizon-rust/horizon/commit/eb94a3af3747fe1342865b3d0f2ec0ea8a8c16e4) Tell native mods when the server's difficulty changes ### 📊 Changes **3 files changed** (+52 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/commands.rs` (+29 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+10 -0) ➕ `tools/native-hook-fragments/difficulty_changed.json` (+13 -0) </details> ### 📄 Description Implements the tracker's `Difficulty changed` line (`DifficultyChangeEvent`, #4953), claimed on the tracker at 22:57:48Z. ## What was missing A native mod had no point to hear the server's difficulty change. Vanilla's `MinecraftServer.setDifficulty` is the one door `/difficulty`, a dedicated server's management and a test environment's difficulty all go through; Horizon's setter for it, `set_difficulty_forced`, had no hook. ## What changed - `native_difficulty_changed(from, to)` (server-side, informational), with the names `peaceful`, `easy`, `normal`, `hard`. - Fired at the end of `set_difficulty_forced`, so a listener sees the change complete (level data, synced rules, hostile-mob removal and the clients' packet have happened), and only when the difficulty is another one: `/difficulty` already refuses a same value, but a dedicated server's management and a test environment may repeat it. - `difficulty_names` and its test sit beside it; the fragment is `tools/native-hook-fragments/difficulty_changed.json`. ## Scope limits - Per server, as `set_difficulty_forced` is; a difficulty read from a save at load (`level.difficulty_id()`) is not a change and is not told. - Informational: NeoForge's event cannot be cancelled either. ## Proven On the build VM: ``` python3 ~/pushrun.py ~/horizon-worktrees/mm-difficulty --name mm-difficulty -- 'cargo test -q -p horizon-client --lib difficulty_tests' running 1 test test result: ok. 1 passed; 0 failed; 592 filtered out cargo clippy -q -p horizon-client --lib -- -D warnings -> exit 0 cargo clippy -q -p horizon-client --bin horizon-client -- -D warnings -> exit 0 rustfmt --edition 2024 --check <the changed files> -> clean ``` The test, `difficulty_tests::a_difficulty_change_names_both_ends`, checks the names the setter passes (`0 -> 3` is `peaceful -> hard`, `2 -> 1` is `normal -> easy`, and an out-of-range index still names one). ## Not proven - The hook firing in game. It is an empty hook point, so a unit test cannot observe the dispatch; the test covers the names the setter passes and the branch that guards a no-op change, not that a bundle hears it. - The dedicated server's management path (`game/dedicated/management.rs` calls the same setter) and a test environment's, which share the setter but were not run. - A real bundle in game. ## Vanilla reference Vanilla 26.3's `MinecraftServer.setDifficulty(difficulty, true)`, which `set_difficulty_forced` already mirrors, and NeoForge's `DifficultyChangeEvent` javadoc for the name, the old and new difficulty, and that it is not cancellable. Fixes the tracker's `Difficulty changed` line in #4953. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:30:06 +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#6955
No description provided.