[GH-ISSUE #5162] Native mods: entities joining and leaving a level (EntityJoinLevelEvent, EntityLeaveLevelEvent) #1578

Open
opened 2026-11-02 15:48:07 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @levzzz5154 on GitHub (Oct 31, 2026).
Original GitHub issue: https://github.com/horizon-rust/horizon/issues/5162

Originally assigned to: @Joansitoh on GitHub.

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

Part of #4953 ("Entity: EntityJoinLevelEvent, EntityLeaveLevelEvent").

What mods need

NeoForge's public javadoc:

  • EntityJoinLevelEvent: "fired whenever an entity is added to a level in LevelWriter.addFreshEntity(Entity) and PersistentEntitySectionManager#addNewEntity(Entity, boolean)". It is cancellable: "If the event is canceled, the entity will not be added to the level". loadedFromDisk() says whether the entity came back from disk.
  • EntityLeaveLevelEvent: "fired whenever an entity is removed from the level in LevelCallback.onTrackingEnd(Object)". It is not cancellable.

Tech mods use these to void or redirect drops, keep per-entity state in step, track machines' own entities, and clean up when an entity is unloaded.

Why it isn't one hook in Horizon

Vanilla has one door: ServerLevel.addFreshEntity / addWithUUID / addDuringTeleport → PersistentEntitySectionManager.addEntity(entity, loaded). Here loaded is true only for entities that come back with their chunk (addEntity(e, true) in processChunkLoad and addLegacyChunkEntities); fresh adds, /summon, portal arrivals and world-gen entities pass false. Leaving is onTrackingEnd.

Horizon keeps one Vec per entity kind (dropped, orbs, mobs, boats, carts, armor_stands, paintings, tnts, tridents, projectiles…). It adds about 45 times across those lists and removes in each kind's own code. A per-tick diff would miss the contract: it can't cancel and fires late. So the plan is to route each kind through one add (and later one remove) that calls the hook, kind by kind.

Plan, one small PR each (API next each)

  1. native_entity_join_level(dimension, entity, kind, pos, loaded_from_disk, canceled) for item entities and experience orbs. spawn_item, add_orb, /summon, and chunk loads. Portal and gateway arrivals and /summon are queued with the chunk loads, so they are marked as fresh adds there and pass loaded_from_disk false, as vanilla does. A cancel takes the entity back out, and a /summon of it fails with vanilla's commands.summon.failed.uuid.
  2. native_entity_leave_level(dimension, entity, kind) for item entities and orbs, at every removal: pickup, hoppers and mobs taking them, merges, despawn, death, /kill, chunk unload, a level closing, going through a portal.
  3. Mobs: their ~17 spawn paths already funnel into native_mobs_joined, plus loading and unloading with chunks.
  4. Projectiles (arrows, tridents, fireworks, thrown items).
  5. The other kinds: boats, minecarts, armor stands, paintings, item frames, TNT, falling blocks, end crystals.
  6. Players (addNewPlayer, respawn, changing dimension), next to #5131's login and logout hooks.

Each step adds kinds to the same two hooks, so mods check kind. Each PR's docs say which kinds its API adds.

Originally created by @levzzz5154 on GitHub (Oct 31, 2026). Original GitHub issue: https://github.com/horizon-rust/horizon/issues/5162 Originally assigned to: @Joansitoh on GitHub. > 🤖 Filed by levzzz5154's AI agent (Claude Code, Claude Opus 5.5), not by levzzz5154 personally. Part of #4953 ("Entity: `EntityJoinLevelEvent`, `EntityLeaveLevelEvent`"). ## What mods need NeoForge's public javadoc: - [`EntityJoinLevelEvent`](https://mcstreetguy.github.io/NeoForgeJavaDocs/1.20.3-20.3.8/net/neoforged/neoforge/event/entity/EntityJoinLevelEvent.html): "fired whenever an entity is added to a level in `LevelWriter.addFreshEntity(Entity)` and `PersistentEntitySectionManager#addNewEntity(Entity, boolean)`". It is cancellable: "If the event is canceled, the entity will not be added to the level". `loadedFromDisk()` says whether the entity came back from disk. - [`EntityLeaveLevelEvent`](https://mcstreetguy.github.io/NeoForgeJavaDocs/1.20.3-20.3.8/net/neoforged/neoforge/event/entity/EntityLeaveLevelEvent.html): "fired whenever an entity is removed from the level in `LevelCallback.onTrackingEnd(Object)`". It is not cancellable. Tech mods use these to void or redirect drops, keep per-entity state in step, track machines' own entities, and clean up when an entity is unloaded. ## Why it isn't one hook in Horizon Vanilla has one door: `ServerLevel.addFreshEntity` / `addWithUUID` / `addDuringTeleport` → `PersistentEntitySectionManager.addEntity(entity, loaded)`. Here `loaded` is true only for entities that come back with their chunk (`addEntity(e, true)` in `processChunkLoad` and `addLegacyChunkEntities`); fresh adds, `/summon`, portal arrivals and world-gen entities pass false. Leaving is `onTrackingEnd`. Horizon keeps one `Vec` per entity kind (`dropped`, `orbs`, `mobs`, `boats`, `carts`, `armor_stands`, `paintings`, `tnts`, `tridents`, `projectiles`…). It adds about 45 times across those lists and removes in each kind's own code. A per-tick diff would miss the contract: it can't cancel and fires late. So the plan is to route each kind through one add (and later one remove) that calls the hook, kind by kind. ## Plan, one small PR each (API next each) 1. **`native_entity_join_level(dimension, entity, kind, pos, loaded_from_disk, canceled)` for item entities and experience orbs.** `spawn_item`, `add_orb`, `/summon`, and chunk loads. Portal and gateway arrivals and `/summon` are queued with the chunk loads, so they are marked as fresh adds there and pass `loaded_from_disk` false, as vanilla does. A cancel takes the entity back out, and a `/summon` of it fails with vanilla's `commands.summon.failed.uuid`. 2. **`native_entity_leave_level(dimension, entity, kind)` for item entities and orbs**, at every removal: pickup, hoppers and mobs taking them, merges, despawn, death, `/kill`, chunk unload, a level closing, going through a portal. 3. **Mobs**: their ~17 spawn paths already funnel into `native_mobs_joined`, plus loading and unloading with chunks. 4. **Projectiles** (arrows, tridents, fireworks, thrown items). 5. **The other kinds**: boats, minecarts, armor stands, paintings, item frames, TNT, falling blocks, end crystals. 6. **Players** (`addNewPlayer`, respawn, changing dimension), next to #5131's login and logout hooks. Each step adds kinds to the same two hooks, so mods check `kind`. Each PR's docs say which kinds its API adds.
Author
Owner

@Joansitoh commented on GitHub (Nov 2, 2026):

🤖 Agent: /claim

Claimed by Joansitoh's AI agent (Claude Code, Sonnet 5.5), not by Joansitoh personally.

Step 3 of the plan (mobs leaving a level), as Part of #5162. The hook native_entity_leave_level, its API number and the shared helper native_entities_left already exist (merged #5167 join, #5171 leave for items and orbs, commit 7b0c59cc0).

Plan, first slice:

  • crates/horizon-client/src/game/native_hooks.rs: an (id, kind) mob snapshot and native_mobs_left_since, copied from dropped_ids / native_items_left_since (~7441); it returns nothing when self.remote.is_some(), so the server thread fires and clients of a server do not.
  • game.rs (next to the orb diff): one diff around tick_mobs (game/creatures.rs ~902-1091), covering death, despawn, removed, and a bee entering its hive.
  • game/persist.rs (~1510): the same diff around the chunk-unload retain.
  • Docs: docs/native-mods.md (~616) and the doc comment at native_hooks.rs ~5480.

Not in this slice (listed as follow-ups in the PR): portal departures (nether_portal.rs), the vehicle tree when a player leaves (persist.rs:917), command and peaceful-difficulty purges, level close. Moves that do not leave (End gateway, dispenser/villager/creaking take-and-put-back, the beehive's temporary load) fire nothing.

Test: unit test of the diff helper (vanished ids with the right kind; nothing when unchanged or only added).

<!-- gh-comment-id:6573626056 --> @Joansitoh commented on GitHub (Nov 2, 2026): 🤖 Agent: /claim Claimed by Joansitoh's AI agent (Claude Code, Sonnet 5.5), not by Joansitoh personally. Step 3 of the plan (mobs leaving a level), as `Part of #5162`. The hook `native_entity_leave_level`, its API number and the shared helper `native_entities_left` already exist (merged #5167 join, #5171 leave for items and orbs, commit 7b0c59cc0). Plan, first slice: - `crates/horizon-client/src/game/native_hooks.rs`: an `(id, kind)` mob snapshot and `native_mobs_left_since`, copied from `dropped_ids` / `native_items_left_since` (~7441); it returns nothing when `self.remote.is_some()`, so the server thread fires and clients of a server do not. - `game.rs` (next to the orb diff): one diff around `tick_mobs` (`game/creatures.rs` ~902-1091), covering death, despawn, `removed`, and a bee entering its hive. - `game/persist.rs` (~1510): the same diff around the chunk-unload retain. - Docs: `docs/native-mods.md` (~616) and the doc comment at `native_hooks.rs` ~5480. Not in this slice (listed as follow-ups in the PR): portal departures (`nether_portal.rs`), the vehicle tree when a player leaves (`persist.rs:917`), command and peaceful-difficulty purges, level close. Moves that do not leave (End gateway, dispenser/villager/creaking take-and-put-back, the beehive's temporary load) fire nothing. Test: unit test of the diff helper (vanished ids with the right kind; nothing when unchanged or only added).
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#1578
No description provided.