[PR #6296] [MERGED] Native mods: a player opening and closing a container, PlayerContainerEvent #6779

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

📋 Pull Request Information

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

Base: main ← Head: mods/container-events


📝 Commits (3)

  • 7a88ad6 Native mods: a player opening and closing a menu (PlayerContainerEvent)
  • ccbc2d1 Native mods: compare a player's open menu without building it each tick
  • f3959d2 Merge main into #6296

📊 Changes

5 files changed (+211 additions, -4 deletions)

View changed files

📝 crates/horizon-client/src/game/lan_actions.rs (+5 -0)
📝 crates/horizon-client/src/game/lan_stations.rs (+3 -3)
📝 crates/horizon-client/src/game/native_hooks.rs (+42 -0)
📝 crates/horizon-client/src/game/native_menus.rs (+141 -1)
➕ tools/native-hook-fragments/player_container_events.json (+20 -0)

📄 Description

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

What and why

Fixes #6188, part of #4953 ("Player opens and closes a container (PlayerContainerEvent.Open / Close)"). native_mod_container_viewers (#6049) counts the players with a mod container open; it cannot say which player, and it never sees vanilla menus. A mod that logs who looked into a chest, locks a machine to its first user or starts a process when a menu opens had no hook.

Two server hook points, API from a fragment (tools/native-hook-fragments/player_container_events.json):

  • native_player_container_open(player, name, dimension, menu, pos) (PlayerContainerEvent.Open)
  • native_player_container_close(player, name, dimension, menu, pos) (PlayerContainerEvent.Close)

menu is the menu type's name (minecraft:generic_9x3, minecraft:furnace, minecraft:crafting, minecraft:enchantment, minecraft:merchant, minecraft:lectern, a mod's own), pos the block for a block's menu. They run with the player's state swapped in (as_guest), so the helpers reach that player.

Horizon keeps a guest's open menu in several places (the hub's container menu, the trade, crafting table, workstation, lectern and mount windows), so tick_player_menus (game/native_menus.rs, called from tick_guests) reads what each player has open, once a tick, and compares it with what the mods last heard (GuestPlay::menu_heard): a change closes the old and opens the new. guest_close_all (a menu replaced by another, a player leaving) also tells the close at once, so a leaving player's close comes before native_player_logged_out. Each open has exactly one close.

Choices a reviewer may want to look at:

  • A menu opened and closed again inside one tick is not heard (a mod that opens one for a player and closes it in the same hook).
  • The player's own inventory menu is not one: vanilla posts Close for it when the client sends a close with window 0, but it has no Open.
  • A mount's inventory has no menu type in vanilla (HorseInventoryMenu), so it is told as horizon:mount_inventory.
  • A leaving player's close is Horizon's own: vanilla/NeoForge post no Close on a disconnect, but a mod that keeps who has what open needs each open to end, so guest_close_all tells it, before native_player_logged_out.
  • Not told when the server stops (everything goes away with it).
  • Not cancellable, as in NeoForge. The deprecated in-frame path never fires them (no hub guests).

Kind

  • feature: something new for players (mod authors)

Vanilla reference

ServerPlayer.openMenu sets the new containerMenu after initMenu (the same in openHorseInventory and openNautilusInventory); doCloseContainer runs containerMenu.removed(player) and goes back to the inventory menu. A close packet, a death, stillValid failing and a new openMenu all end there; a disconnect does not (vanilla posts nothing, and the openers counter catches it up with recheckOpeners). NeoForge's ServerPlayer patch posts Open after containerMenu is set in those three methods and Close in doCloseContainer after removed.

Other projects' code

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

Proof in the real game

Engine from this branch with throwaway eprintln!s in the hook bodies (not in this PR); a creative player at a platform with a chest, a furnace, a crafting table and an enchanting table, each opened with horizon-ctl interact and closed with close. A first version of this branch closed every tick; the output below is from the fixed one.

Server thread (horizon-ctl launch --world L --creative --offscreen):

TESTHOOK open Player91704 minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=118
TESTHOOK close Player91704 minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=149
TESTHOOK open Player91704 minecraft:overworld minecraft:furnace Some([-2, 100, 0]) t=181
TESTHOOK close Player91704 minecraft:overworld minecraft:furnace Some([-2, 100, 0]) t=213
TESTHOOK open Player91704 minecraft:overworld minecraft:crafting Some([0, 100, 2]) t=244
TESTHOOK close Player91704 minecraft:overworld minecraft:crafting Some([0, 100, 2]) t=275
TESTHOOK open Player91704 minecraft:overworld minecraft:enchantment Some([0, 100, -2]) t=306
TESTHOOK close Player91704 minecraft:overworld minecraft:enchantment Some([0, 100, -2]) t=337
TESTHOOK open Player91704 minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=460
TESTHOOK close Player91704 minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=492

The last pair: the chest left open and the player teleported 20 blocks away (out of reach, closed by stillValid).

Dedicated server, a client connected as Alex (horizon-ctl connect), the same sequence, then the client disconnected with the chest open:

TESTHOOK open Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=280
TESTHOOK close Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=312
TESTHOOK open Alex minecraft:overworld minecraft:furnace Some([-2, 100, 0]) t=343
TESTHOOK close Alex minecraft:overworld minecraft:furnace Some([-2, 100, 0]) t=376
TESTHOOK open Alex minecraft:overworld minecraft:crafting Some([0, 100, 2]) t=407
TESTHOOK close Alex minecraft:overworld minecraft:crafting Some([0, 100, 2]) t=439
TESTHOOK open Alex minecraft:overworld minecraft:enchantment Some([0, 100, -2]) t=469
TESTHOOK close Alex minecraft:overworld minecraft:enchantment Some([0, 100, -2]) t=501
TESTHOOK open Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=531
TESTHOOK close Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=593
TESTHOOK open Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=624
TESTHOOK close Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=656

The connected client printed nothing. Not run: a trade, a lectern, a mount's inventory, a mod's own menu type and one menu replacing another (horizon-ctl cannot open a second while one is open; the replacing path is guest_close_all before open_*, and a closing player at disconnect shows the same path); the server stopping.

Self-check

pr_ready (tools/pr_ready.py --quick --base hr/main on cachyos-x8664, against hr/main at 7311b1e17, 5 min 48 s)

Check Result
Scope ✅ 5 files, +211 −4
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 586 passed (1 min 11 s)
rustfmt ✅ (8 s)
clippy ✅ (2 min 23 s)
tests: horizon-client ✅ 1802 passed (2 min 4 s)
doc tests ✅ 0 passed (2 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/6296 **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/container-events` --- ### 📝 Commits (3) - [`7a88ad6`](https://github.com/horizon-rust/horizon/commit/7a88ad618a66d0d49a7897a348e2fd5ce35cae6f) Native mods: a player opening and closing a menu (PlayerContainerEvent) - [`ccbc2d1`](https://github.com/horizon-rust/horizon/commit/ccbc2d1fe5f2de58dbfcb3ea66f86cebf5f034f0) Native mods: compare a player's open menu without building it each tick - [`f3959d2`](https://github.com/horizon-rust/horizon/commit/f3959d23a992b577eaa7cf19f4e10912763b32f1) Merge main into #6296 ### 📊 Changes **5 files changed** (+211 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/lan_actions.rs` (+5 -0) 📝 `crates/horizon-client/src/game/lan_stations.rs` (+3 -3) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+42 -0) 📝 `crates/horizon-client/src/game/native_menus.rs` (+141 -1) ➕ `tools/native-hook-fragments/player_container_events.json` (+20 -0) </details> ### 📄 Description > 🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Sonnet 5.5), not by levzzz5154 personally. ## What and why Fixes #6188, part of #4953 ("Player opens and closes a container (`PlayerContainerEvent.Open` / `Close`)"). `native_mod_container_viewers` (#6049) counts the players with a mod container open; it cannot say which player, and it never sees vanilla menus. A mod that logs who looked into a chest, locks a machine to its first user or starts a process when a menu opens had no hook. Two server hook points, API from a fragment (`tools/native-hook-fragments/player_container_events.json`): - `native_player_container_open(player, name, dimension, menu, pos)` (`PlayerContainerEvent.Open`) - `native_player_container_close(player, name, dimension, menu, pos)` (`PlayerContainerEvent.Close`) `menu` is the menu type's name (`minecraft:generic_9x3`, `minecraft:furnace`, `minecraft:crafting`, `minecraft:enchantment`, `minecraft:merchant`, `minecraft:lectern`, a mod's own), `pos` the block for a block's menu. They run with the player's state swapped in (`as_guest`), so the helpers reach that player. Horizon keeps a guest's open menu in several places (the hub's container menu, the trade, crafting table, workstation, lectern and mount windows), so `tick_player_menus` (`game/native_menus.rs`, called from `tick_guests`) reads what each player has open, once a tick, and compares it with what the mods last heard (`GuestPlay::menu_heard`): a change closes the old and opens the new. `guest_close_all` (a menu replaced by another, a player leaving) also tells the close at once, so a leaving player's close comes before `native_player_logged_out`. Each open has exactly one close. Choices a reviewer may want to look at: - A menu opened and closed again inside one tick is not heard (a mod that opens one for a player and closes it in the same hook). - The player's own inventory menu is not one: vanilla posts `Close` for it when the client sends a close with window 0, but it has no `Open`. - A mount's inventory has no menu type in vanilla (`HorseInventoryMenu`), so it is told as `horizon:mount_inventory`. - A leaving player's close is Horizon's own: vanilla/NeoForge post no `Close` on a disconnect, but a mod that keeps who has what open needs each open to end, so `guest_close_all` tells it, before `native_player_logged_out`. - Not told when the server stops (everything goes away with it). - Not cancellable, as in NeoForge. The deprecated in-frame path never fires them (no hub guests). ## Kind - [x] feature: something new for players (mod authors) ## Vanilla reference `ServerPlayer.openMenu` sets the new `containerMenu` after `initMenu` (the same in `openHorseInventory` and `openNautilusInventory`); `doCloseContainer` runs `containerMenu.removed(player)` and goes back to the inventory menu. A close packet, a death, `stillValid` failing and a new `openMenu` all end there; a disconnect does not (vanilla posts nothing, and the openers counter catches it up with `recheckOpeners`). NeoForge's `ServerPlayer` patch posts `Open` after `containerMenu` is set in those three methods and `Close` in `doCloseContainer` after `removed`. ## Other projects' code None. Names and contracts only from NeoForge's public `PlayerContainerEvent`. ## Proof in the real game Engine from this branch with throwaway `eprintln!`s in the hook bodies (not in this PR); a creative player at a platform with a chest, a furnace, a crafting table and an enchanting table, each opened with `horizon-ctl interact` and closed with `close`. A first version of this branch closed every tick; the output below is from the fixed one. Server thread (`horizon-ctl launch --world L --creative --offscreen`): ``` TESTHOOK open Player91704 minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=118 TESTHOOK close Player91704 minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=149 TESTHOOK open Player91704 minecraft:overworld minecraft:furnace Some([-2, 100, 0]) t=181 TESTHOOK close Player91704 minecraft:overworld minecraft:furnace Some([-2, 100, 0]) t=213 TESTHOOK open Player91704 minecraft:overworld minecraft:crafting Some([0, 100, 2]) t=244 TESTHOOK close Player91704 minecraft:overworld minecraft:crafting Some([0, 100, 2]) t=275 TESTHOOK open Player91704 minecraft:overworld minecraft:enchantment Some([0, 100, -2]) t=306 TESTHOOK close Player91704 minecraft:overworld minecraft:enchantment Some([0, 100, -2]) t=337 TESTHOOK open Player91704 minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=460 TESTHOOK close Player91704 minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=492 ``` The last pair: the chest left open and the player teleported 20 blocks away (out of reach, closed by `stillValid`). Dedicated server, a client connected as Alex (`horizon-ctl connect`), the same sequence, then the client disconnected with the chest open: ``` TESTHOOK open Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=280 TESTHOOK close Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=312 TESTHOOK open Alex minecraft:overworld minecraft:furnace Some([-2, 100, 0]) t=343 TESTHOOK close Alex minecraft:overworld minecraft:furnace Some([-2, 100, 0]) t=376 TESTHOOK open Alex minecraft:overworld minecraft:crafting Some([0, 100, 2]) t=407 TESTHOOK close Alex minecraft:overworld minecraft:crafting Some([0, 100, 2]) t=439 TESTHOOK open Alex minecraft:overworld minecraft:enchantment Some([0, 100, -2]) t=469 TESTHOOK close Alex minecraft:overworld minecraft:enchantment Some([0, 100, -2]) t=501 TESTHOOK open Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=531 TESTHOOK close Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=593 TESTHOOK open Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=624 TESTHOOK close Alex minecraft:overworld minecraft:generic_9x3 Some([2, 100, 0]) t=656 ``` The connected client printed nothing. Not run: a trade, a lectern, a mount's inventory, a mod's own menu type and one menu replacing another (`horizon-ctl` cannot open a second while one is open; the replacing path is `guest_close_all` before `open_*`, and a closing player at disconnect shows the same path); the server stopping. ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base hr/main` on cachyos-x8664, against hr/main at 7311b1e17, 5 min 48 s) | Check | Result | | --- | --- | | Scope | ✅ 5 files, +211 −4 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 586 passed (1 min 11 s) | | rustfmt | ✅ (8 s) | | clippy | ✅ (2 min 23 s) | | tests: horizon-client | ✅ 1802 passed (2 min 4 s) | | doc tests | ✅ 0 passed (2 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:29 +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#6779
No description provided.