[PR #6297] [MERGED] Native mods: a player starting and stopping tracking an entity, PlayerEvent.StartTracking #6778

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/6297
Author: @levzzz5154
Created: 11/1/2026
Status: ✅ Merged
Merged: 11/1/2026
Merged by: @levzzz5154

Base: main ← Head: mods/player-tracking


📝 Commits (1)

  • 0fd8c97 Native mods: a player starting and stopping tracking an entity (PlayerEvent)

📊 Changes

6 files changed (+253 additions, -11 deletions)

View changed files

📝 crates/horizon-client/src/game/lan.rs (+9 -0)
📝 crates/horizon-client/src/game/lan_entities.rs (+36 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+40 -0)
📝 crates/horizon-client/src/lan/entity.rs (+113 -11)
📝 crates/horizon-client/src/lan/hub.rs (+35 -0)
➕ tools/native-hook-fragments/player_tracking.json (+20 -0)

📄 Description

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

What and why

Fixes #6231, part of #4953 ("Player: start tracking (#5816)") and a line of #5816 (hook points a survival server needs). A mod that adds data to an entity (a mod mob's state, an owner, a name) must send it to each player that gets the entity, and drop its per-player bookkeeping when they lose it. Horizon pairs entities with clients (Entities::tick in lan/entity.rs) but told the mods nothing.

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

  • native_player_start_tracking(player, name, entity, uuid, kind) (PlayerEvent.StartTracking)
  • native_player_stop_tracking(player, name, entity, uuid, kind) (PlayerEvent.StopTracking)

entity is the entity's ID (the one native_entity_join_level uses), uuid its UUID, kind its type; another player is an entity too (a player never tracks their own).

How: the per-client Tracker (the set of entities a client was paired with) now keeps a list of starts and stops, with the entity's identity, from every place that changes the set: the tick's range checks (Entities::tick: start when it pairs, stop when it drops out of range or the level), forget (a respawned player), clear (a level change, a respawn) and a leaving player (remove_guest). Its seen set became a map to the entity's Tracked { uuid, kind }, which the level's ServerEntity makes once. Hub::collect_tracking moves the lists into native_tracking, and tick_lan hands them to run_native_tracking (game/lan_entities.rs), which tells the mods in order. Each start has one stop.

Choices a reviewer may want to look at:

  • No player's state is swapped in (the stops of a leaving player have no body): a mod sends what the player needs with native_net_send_to_player. A leaving player's stops come after native_player_logged_out, as in vanilla (PlayerList.remove posts the logout first).
  • Told after the tick's packets and entity pass (the end of tick_lan), not inside the pairing, so a hook that runs commands cannot change the entities mid-pass. Start and stop for an entity inside one tick both arrive, in order.
  • No dimension or position of the target: the ID and UUID find it.
  • Cost: one Arc per pair and a list push per start or stop, drained every tick. The tracker tests and the existing tests are untouched; a new one checks the events.

Kind

  • feature: something new for players (mod authors)

Vanilla reference

ChunkMap.TrackedEntity.updatePlayer calls ServerEntity.addPairing(player) (add packets, startSeenByPlayer) when an entity comes into range and removePairing(player) (stopSeenByPlayer, ClientboundRemoveEntitiesPacket) when it leaves; leaving the game, changing level, respawning and an entity being removed all end in removePairing. NeoForge's ServerEntity patch posts PlayerEvent.StartTracking at the end of addPairing and PlayerEvent.StopTracking at the end of removePairing.

Other projects' code

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

Proof in the real game

Engine from this branch with throwaway eprintln!s in the hook bodies (not in this PR). Both runs are in the default world, so natural mobs start and stop around the player too; the lines below are the cows /summoned with {NoAI:1b} and what the steps did.

Server thread (horizon-ctl launch --world L --creative --offscreen), a platform at 0,100,0:

summon minecraft:cow 3 100 3 {NoAI:1b}
TESTHOOK start Player11952 455 minecraft:cow e03e0c59f3754ffc8ddab5ce747f8df7
kill @e[type=minecraft:cow]
TESTHOOK stop Player11952 455 minecraft:cow e03e0c59f3754ffc8ddab5ce747f8df7
summon minecraft:cow 3 100 -3 {NoAI:1b}
TESTHOOK start Player11952 587 minecraft:cow 82d93387048e4208aff42fa6fa0a0da4
tp @s 600.5 100 0.5
TESTHOOK stop Player11952 587 minecraft:cow 82d93387048e4208aff42fa6fa0a0da4
tp @s 0.5 100 0.5
TESTHOOK start Player11952 977 minecraft:cow 82d93387048e4208aff42fa6fa0a0da4
execute in minecraft:the_nether run tp @s 0 100 0
TESTHOOK stop Player11952 977 minecraft:cow 82d93387048e4208aff42fa6fa0a0da4

(The cow is a new entity with a new ID when its chunk comes back; its UUID is the same. In the Nether trip 190 stops and 87 starts came in the same step as the level change.)

Dedicated server, a client connected as Alex (horizon-ctl connect), the same sequence:

TESTHOOK start Alex 439 minecraft:cow f4da740baf2f41d385805273b40ac822
TESTHOOK stop Alex 439 minecraft:cow f4da740baf2f41d385805273b40ac822
TESTHOOK start Alex 451 minecraft:cow 70b8a2ef37aa4eb6893a5ba47bf6a1a3
TESTHOOK stop Alex 451 minecraft:cow 70b8a2ef37aa4eb6893a5ba47bf6a1a3      (tp 600 blocks away)
TESTHOOK start Alex 659 minecraft:cow 70b8a2ef37aa4eb6893a5ba47bf6a1a3      (tp back)
[Server thread/INFO]: System chat: Alex left the game                       (client disconnected)
TESTHOOK stop Alex ...   (80 stops, one for each entity still tracked, none of them a start)

The connected client printed nothing. Not run: two players tracking each other, a respawn after death, a spectator going in or out of tracking, a mount with passengers.

Self-check

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

Check Result
Scope ✅ 6 files, +253 −11
Cargo.lock ✅ unchanged
Public tree ✅ (1 s)
Python tests ✅ 586 passed (32 s)
rustfmt ✅ (6 s)
clippy ✅ (17 s)
tests: horizon-client ✅ 1805 passed (17 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/6297 **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/player-tracking` --- ### 📝 Commits (1) - [`0fd8c97`](https://github.com/horizon-rust/horizon/commit/0fd8c9706659a7487938dde71b2a5c51b95acfa3) Native mods: a player starting and stopping tracking an entity (PlayerEvent) ### 📊 Changes **6 files changed** (+253 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/lan.rs` (+9 -0) 📝 `crates/horizon-client/src/game/lan_entities.rs` (+36 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+40 -0) 📝 `crates/horizon-client/src/lan/entity.rs` (+113 -11) 📝 `crates/horizon-client/src/lan/hub.rs` (+35 -0) ➕ `tools/native-hook-fragments/player_tracking.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 #6231, part of #4953 ("Player: start tracking (#5816)") and a line of #5816 (hook points a survival server needs). A mod that adds data to an entity (a mod mob's state, an owner, a name) must send it to each player that gets the entity, and drop its per-player bookkeeping when they lose it. Horizon pairs entities with clients (`Entities::tick` in `lan/entity.rs`) but told the mods nothing. Two server hook points, API from a fragment (`tools/native-hook-fragments/player_tracking.json`): - `native_player_start_tracking(player, name, entity, uuid, kind)` (`PlayerEvent.StartTracking`) - `native_player_stop_tracking(player, name, entity, uuid, kind)` (`PlayerEvent.StopTracking`) `entity` is the entity's ID (the one `native_entity_join_level` uses), `uuid` its UUID, `kind` its type; another player is an entity too (a player never tracks their own). How: the per-client `Tracker` (the set of entities a client was paired with) now keeps a list of starts and stops, with the entity's identity, from every place that changes the set: the tick's range checks (`Entities::tick`: start when it pairs, stop when it drops out of range or the level), `forget` (a respawned player), `clear` (a level change, a respawn) and a leaving player (`remove_guest`). Its `seen` set became a map to the entity's `Tracked { uuid, kind }`, which the level's `ServerEntity` makes once. `Hub::collect_tracking` moves the lists into `native_tracking`, and `tick_lan` hands them to `run_native_tracking` (`game/lan_entities.rs`), which tells the mods in order. Each start has one stop. Choices a reviewer may want to look at: - No player's state is swapped in (the stops of a leaving player have no body): a mod sends what the player needs with `native_net_send_to_player`. A leaving player's stops come after `native_player_logged_out`, as in vanilla (`PlayerList.remove` posts the logout first). - Told after the tick's packets and entity pass (the end of `tick_lan`), not inside the pairing, so a hook that runs commands cannot change the entities mid-pass. Start and stop for an entity inside one tick both arrive, in order. - No dimension or position of the target: the ID and UUID find it. - Cost: one `Arc` per pair and a list push per start or stop, drained every tick. The tracker tests and the existing tests are untouched; a new one checks the events. ## Kind - [x] feature: something new for players (mod authors) ## Vanilla reference `ChunkMap.TrackedEntity.updatePlayer` calls `ServerEntity.addPairing(player)` (add packets, `startSeenByPlayer`) when an entity comes into range and `removePairing(player)` (`stopSeenByPlayer`, `ClientboundRemoveEntitiesPacket`) when it leaves; leaving the game, changing level, respawning and an entity being removed all end in `removePairing`. NeoForge's `ServerEntity` patch posts `PlayerEvent.StartTracking` at the end of `addPairing` and `PlayerEvent.StopTracking` at the end of `removePairing`. ## Other projects' code None. Names and contracts only from NeoForge's public `PlayerEvent`. ## Proof in the real game Engine from this branch with throwaway `eprintln!`s in the hook bodies (not in this PR). Both runs are in the default world, so natural mobs start and stop around the player too; the lines below are the cows `/summon`ed with `{NoAI:1b}` and what the steps did. Server thread (`horizon-ctl launch --world L --creative --offscreen`), a platform at 0,100,0: ``` summon minecraft:cow 3 100 3 {NoAI:1b} TESTHOOK start Player11952 455 minecraft:cow e03e0c59f3754ffc8ddab5ce747f8df7 kill @e[type=minecraft:cow] TESTHOOK stop Player11952 455 minecraft:cow e03e0c59f3754ffc8ddab5ce747f8df7 summon minecraft:cow 3 100 -3 {NoAI:1b} TESTHOOK start Player11952 587 minecraft:cow 82d93387048e4208aff42fa6fa0a0da4 tp @s 600.5 100 0.5 TESTHOOK stop Player11952 587 minecraft:cow 82d93387048e4208aff42fa6fa0a0da4 tp @s 0.5 100 0.5 TESTHOOK start Player11952 977 minecraft:cow 82d93387048e4208aff42fa6fa0a0da4 execute in minecraft:the_nether run tp @s 0 100 0 TESTHOOK stop Player11952 977 minecraft:cow 82d93387048e4208aff42fa6fa0a0da4 ``` (The cow is a new entity with a new ID when its chunk comes back; its UUID is the same. In the Nether trip 190 stops and 87 starts came in the same step as the level change.) Dedicated server, a client connected as Alex (`horizon-ctl connect`), the same sequence: ``` TESTHOOK start Alex 439 minecraft:cow f4da740baf2f41d385805273b40ac822 TESTHOOK stop Alex 439 minecraft:cow f4da740baf2f41d385805273b40ac822 TESTHOOK start Alex 451 minecraft:cow 70b8a2ef37aa4eb6893a5ba47bf6a1a3 TESTHOOK stop Alex 451 minecraft:cow 70b8a2ef37aa4eb6893a5ba47bf6a1a3 (tp 600 blocks away) TESTHOOK start Alex 659 minecraft:cow 70b8a2ef37aa4eb6893a5ba47bf6a1a3 (tp back) [Server thread/INFO]: System chat: Alex left the game (client disconnected) TESTHOOK stop Alex ... (80 stops, one for each entity still tracked, none of them a start) ``` The connected client printed nothing. Not run: two players tracking each other, a respawn after death, a spectator going in or out of tracking, a mount with passengers. ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base hr/main` on cachyos-x8664, against hr/main at 1bd24e404, 1 min 15 s) | Check | Result | | --- | --- | | Scope | ✅ 6 files, +253 −11 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ (1 s) | | Python tests | ✅ 586 passed (32 s) | | rustfmt | ✅ (6 s) | | clippy | ✅ (17 s) | | tests: horizon-client | ✅ 1805 passed (17 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#6778
No description provided.