[PR #82] [MERGED] Game rules: save them with the world once, as they are now; save on Quit #2313

Closed
opened 2026-11-02 15:54:52 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: main ← Head: fix/gamerules-persist-74


📝 Commits (1)

  • 9bbb674 Game rules: save them with the world once, as they are now; save on Quit

📊 Changes

5 files changed (+82 additions, -20 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+28 -7)
📝 crates/horizon-client/src/game/chat.rs (+3 -4)
📝 crates/horizon-client/src/game/control.rs (+1 -8)
📝 crates/horizon-client/src/gamerules.rs (+37 -0)
📝 tools/test_save_roundtrip_parity.py (+13 -1)

📄 Description

What was wrong

Game rules set with /gamerule (for example advance_time false, advance_weather false) could come back with their old values after leaving and rejoining the world.

On Linux I could not reproduce it with Save and Quit to Title, closing the window or horizon-ctl quit (current main and the v2026.10.14 release behave the same). Reading the save path turned up two ways rules get lost:

  1. game_rules.dat was written twice on every save. Level::write wrote it first from the rules loaded when the world opened (save::Level::game_rules, never updated afterwards), then save_world_state wrote it again from the live rules with a plain fs::write over the file that had just been renamed into place. A failure of that second write was only logged. On Windows, a file that was just renamed can be held briefly by the search indexer or an antivirus, so the second write can fail. The file then keeps the rules from when the world opened, and the world opens with them next time.
  2. Quit on macOS (Cmd+Q) did not save. winit ends the application through applicationWillTerminate → LoopExiting → ApplicationHandler::exiting, with no CloseRequested, and Horizon saved only on CloseRequested. Everything since the last autosave was lost, game rules included.

What vanilla does

  • 26.3 keeps game rules as saved data (data/minecraft/game_rules.dat, {data: {"minecraft:<rule>": …}, DataVersion}). It is written once per save from the live GameRules, through SavedDataStorage: write _new, then rename over the file and keep _old.
  • On macOS, GLFW's application delegate turns Quit into window-close events, so Minecraft.stop() runs and the integrated server saves the world as on any other exit.

The fix

  • sync_level copies the live rules into save::Level (GameRules::by_name), and Level::write is now the only writer of game_rules.dat, using the same _new + rename path as vanilla. save_world_state no longer writes the file.
  • ApplicationHandler::exiting saves the world if one is still open. Window close, horizon-ctl quit and exiting share save_for_exit, which closes the container, counts leave_game and saves exactly once.

Tests

  • Unit test gamerules::tests::level_save_keeps_changed_rules: changes advance_time, advance_weather and random_tick_speed, saves through Level::write, and reads the rules back with both GameRules::load and Level::read.
  • Save round-trip harness (tools/test_save_roundtrip_parity.py, vanilla world → Horizon → vanilla): Horizon's player now runs /gamerule max_entity_cramming 30 before saving, and the comparison requires that new value in Horizon's game_rules.dat with nothing else unexplained. Result: "save round-trip parity ok (0 unexplained differences)".
  • cargo fmt --check, cargo clippy --workspace --all-targets -D warnings and cargo test --workspace pass; tools/check_public_tree.py passes.

Left open

  • Neither cause has been seen on the reporter's machine. If it still happens, the OS and how the world was left (Save and Quit, window close, Cmd+Q, killed from the launcher) would narrow it down.
  • A kill from outside (SIGTERM, a crash) still loses changes since the last autosave, as it does in vanilla.

Fixes #74

🤖 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/82 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/14/2026 **Status:** ✅ Merged **Merged:** 10/14/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `fix/gamerules-persist-74` --- ### 📝 Commits (1) - [`9bbb674`](https://github.com/horizon-rust/horizon/commit/9bbb674184d4a92823db37ee2ebb4cce14311931) Game rules: save them with the world once, as they are now; save on Quit ### 📊 Changes **5 files changed** (+82 additions, -20 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+28 -7) 📝 `crates/horizon-client/src/game/chat.rs` (+3 -4) 📝 `crates/horizon-client/src/game/control.rs` (+1 -8) 📝 `crates/horizon-client/src/gamerules.rs` (+37 -0) 📝 `tools/test_save_roundtrip_parity.py` (+13 -1) </details> ### 📄 Description ## What was wrong Game rules set with `/gamerule` (for example `advance_time false`, `advance_weather false`) could come back with their old values after leaving and rejoining the world. On Linux I could not reproduce it with Save and Quit to Title, closing the window or `horizon-ctl quit` (current main and the v2026.10.14 release behave the same). Reading the save path turned up two ways rules get lost: 1. **`game_rules.dat` was written twice on every save.** `Level::write` wrote it first from the rules loaded when the world opened (`save::Level::game_rules`, never updated afterwards), then `save_world_state` wrote it again from the live rules with a plain `fs::write` over the file that had just been renamed into place. A failure of that second write was only logged. On Windows, a file that was just renamed can be held briefly by the search indexer or an antivirus, so the second write can fail. The file then keeps the rules from when the world opened, and the world opens with them next time. 2. **Quit on macOS (Cmd+Q) did not save.** winit ends the application through `applicationWillTerminate` → `LoopExiting` → `ApplicationHandler::exiting`, with no `CloseRequested`, and Horizon saved only on `CloseRequested`. Everything since the last autosave was lost, game rules included. ## What vanilla does - 26.3 keeps game rules as saved data (`data/minecraft/game_rules.dat`, `{data: {"minecraft:<rule>": …}, DataVersion}`). It is written once per save from the live `GameRules`, through `SavedDataStorage`: write `_new`, then rename over the file and keep `_old`. - On macOS, GLFW's application delegate turns Quit into window-close events, so `Minecraft.stop()` runs and the integrated server saves the world as on any other exit. ## The fix - `sync_level` copies the live rules into `save::Level` (`GameRules::by_name`), and `Level::write` is now the only writer of `game_rules.dat`, using the same `_new` + rename path as vanilla. `save_world_state` no longer writes the file. - `ApplicationHandler::exiting` saves the world if one is still open. Window close, `horizon-ctl quit` and exiting share `save_for_exit`, which closes the container, counts `leave_game` and saves exactly once. ## Tests - Unit test `gamerules::tests::level_save_keeps_changed_rules`: changes `advance_time`, `advance_weather` and `random_tick_speed`, saves through `Level::write`, and reads the rules back with both `GameRules::load` and `Level::read`. - Save round-trip harness (`tools/test_save_roundtrip_parity.py`, vanilla world → Horizon → vanilla): Horizon's player now runs `/gamerule max_entity_cramming 30` before saving, and the comparison requires that new value in Horizon's `game_rules.dat` with nothing else unexplained. Result: **"save round-trip parity ok (0 unexplained differences)"**. - `cargo fmt --check`, `cargo clippy --workspace --all-targets -D warnings` and `cargo test --workspace` pass; `tools/check_public_tree.py` passes. ## Left open - Neither cause has been seen on the reporter's machine. If it still happens, the OS and how the world was left (Save and Quit, window close, Cmd+Q, killed from the launcher) would narrow it down. - A kill from outside (SIGTERM, a crash) still loses changes since the last autosave, as it does in vanilla. Fixes #74 🤖 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-02 15:54:52 +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#2313
No description provided.