[PR #5167] [MERGED] Native mods: entities joining a level, for item entities and orbs (API next) #6112

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

📋 Pull Request Information

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

Base: main ← Head: mods/native-entity-join


📝 Commits (2)

  • 2af60d6 Native mods: entities joining a level, for item entities and orbs
  • 94027c2 Merge origin/main into mods/native-entity-join; API number as a fragment, review fixes

📊 Changes

10 files changed (+189 additions, -5 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+18 -1)
📝 crates/horizon-client/src/game/allays.rs (+1 -1)
📝 crates/horizon-client/src/game/commands.rs (+11 -1)
📝 crates/horizon-client/src/game/commands/data.rs (+7 -0)
📝 crates/horizon-client/src/game/dispenser.rs (+1 -1)
📝 crates/horizon-client/src/game/end.rs (+3 -0)
📝 crates/horizon-client/src/game/native_hooks.rs (+49 -0)
📝 crates/horizon-client/src/game/nether_portal.rs (+2 -0)
📝 crates/horizon-client/src/game/persist.rs (+87 -1)
➕ tools/native-hook-fragments/entity_join_level.json (+10 -0)

📄 Description

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

What and why

Step 1 of #5162 (the plan for entities joining and leaving a level). Part of #4953 and #3911. NATIVE_HOOKS_API 40 → 41 (API next: the number is set at merge).

native_entity_join_level(dimension, entity, kind, pos, loaded_from_disk, canceled) is NeoForge's EntityJoinLevelEvent (javadoc: cancellable, "If the event is canceled, the entity will not be added to the level"; loadedFromDisk()). In the catalog it has neoforge, side: server and cancellable: true. This PR covers item entities and experience orbs, at every place Horizon adds them:

Where loaded_from_disk
spawn_item (every drop: blocks, mobs, players, dispensers… 64 callers) false
add_orb (XP from blocks, mobs, smelting, breeding…) false
/summon experience_orb false
/summon item … and other NBT spawns (spawn_entity_from_nbt) false
Nether portal and End gateway arrivals false (vanilla's addDuringTeleport)
Entities loading with their chunk (spawn_loaded_entities) true

How it works:

  • The hook runs right after the entity is put in its list, so helpers that take an entity ID find it. A cancel pops it back out (and forgets its saved compound), as if it were never added.
  • A refused /summon fails with vanilla's commands.summon.failed.uuid, which is what SummonCommand throws when tryAddFreshEntityWithPassengers returns false.
  • Portal arrivals and /summon reach the level through the same queue as chunk loads (world.loaded_entities). They are marked there with a transient horizon:fresh_add key, the way horizon:finalize already works. The key comes off before the compound is kept or saved (unit test a_fresh_add_is_told_apart_from_a_load_and_unmarked).
  • On a client of a server nothing fires.

EntityLeaveLevelEvent is step 2, in a separate PR. Mobs, projectiles, vehicles and players are steps 3–6 of #5162; each adds kinds to this same hook.

Kind

  • feature: something new for players (listed under New in the release notes)
  • parity: Horizon now does what vanilla 26.3 does
  • bug: a crash or a breakage
  • extra: Horizon-only, off by default
  • tooling: tools, CI, tests, harnesses
  • docs

Vanilla reference

26.3's PersistentEntitySectionManager:

  • addNewEntity(entity) is addEntity(entity, false). ServerLevel.addFreshEntity, addWithUUID and addDuringTeleport reach it.
  • processPendingLoads and addLegacyChunkEntities call addEntity(e, true). These are the only loaded = true callers.
  • SummonCommand: if (!level.tryAddFreshEntityWithPassengers(entity)) throw ERROR_DUPLICATE_UUID.

With no mod installed the hook is empty and nothing changes: the mark only travels through the load queue.

Other projects' code

none

Tested in the game

A throwaway probe in the hook (removed before the commit) logs every call and cancels any entity that joins above y=200. Fresh mktemp -d homes, HORIZON_NO_AUDIO=1, --profile fast.

Single player (server thread)

Action Hook Game
summon item 0.5 90 0.5 {Item:{id:diamond,count:2}…} #56 minecraft:item from_disk=false "Summoned new Diamond"
summon item 0.5 250 0.5 {…dirt…} #59 … canceled=true "Unable to summon entity due to duplicate UUIDs"
summon experience_orb 1.5 90 1.5 / … 2.5 90 2.5 {Value:7} both from_disk=false "Summoned new Experience Orb"
summon experience_orb 1.5 250 1.5 canceled=true "Unable to summon entity due to duplicate UUIDs"
setblock … dirt then setblock … air destroy, at y 95 and y 250 drop joins; the one at y 250 is canceled and never appears
Emerald dropped into a nether portal built with /fill joins the overworld, then minecraft:the_nether with from_disk=false
Reopen the world the diamond, the dirt and coal drops and both orbs: from_disk=true; the canceled ones never come back

The world's own drops (leaves, eggs, mob loot) all showed up as fresh joins too.

Dedicated server (--dedicated --nogui) with a horizon-ctl client, Alex

Action Hook Alex sees
console summon item … diamond from_disk=false the diamond
console summon item 1.5 250 1.5 … and summon experience_orb 2.5 250 2.5 canceled=true; both say "duplicate UUIDs" nothing
setblock 3 100 3 lapis_ore, then air destroy (always drops XP) lapis item + orb join (add_orb) the lapis
The same at y 250 the item and all three orbs canceled nothing
Server restart, Alex rejoins diamond and lapis from_disk=true
Alex's own client nothing fires

Parity, locally (tools/parity.py nether_portal server_entities held_drop): 3 passed. The other harnesses that cover these files are CI's.

Self-check

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

Check Result
Scope ✅ 9 files, +157 −2
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 495 passed (25 s)
rustfmt ✅ (5 s)
clippy ✅ (12 s)
tests: horizon-client ✅ 1341 passed (17 s)
doc tests ✅ 0 passed (1 s)
Parity ➖ not run (--quick): CI's parity job picks the affected harnesses; nether_portal, server_entities and held_drop passed locally

🤖 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/5167 **Author:** [@levzzz5154](https://github.com/levzzz5154) **Created:** 10/31/2026 **Status:** ✅ Merged **Merged:** 10/31/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `mods/native-entity-join` --- ### 📝 Commits (2) - [`2af60d6`](https://github.com/horizon-rust/horizon/commit/2af60d68a2b838d53e7322c16a54174325b60dd7) Native mods: entities joining a level, for item entities and orbs - [`94027c2`](https://github.com/horizon-rust/horizon/commit/94027c278ec94204b014823c79352e7e36169cdd) Merge origin/main into mods/native-entity-join; API number as a fragment, review fixes ### 📊 Changes **10 files changed** (+189 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+18 -1) 📝 `crates/horizon-client/src/game/allays.rs` (+1 -1) 📝 `crates/horizon-client/src/game/commands.rs` (+11 -1) 📝 `crates/horizon-client/src/game/commands/data.rs` (+7 -0) 📝 `crates/horizon-client/src/game/dispenser.rs` (+1 -1) 📝 `crates/horizon-client/src/game/end.rs` (+3 -0) 📝 `crates/horizon-client/src/game/native_hooks.rs` (+49 -0) 📝 `crates/horizon-client/src/game/nether_portal.rs` (+2 -0) 📝 `crates/horizon-client/src/game/persist.rs` (+87 -1) ➕ `tools/native-hook-fragments/entity_join_level.json` (+10 -0) </details> ### 📄 Description > 🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Opus 5.5), not by levzzz5154 personally. ## What and why Step 1 of #5162 (the plan for entities joining and leaving a level). Part of #4953 and #3911. `NATIVE_HOOKS_API` 40 → 41 (**API next**: the number is set at merge). **`native_entity_join_level(dimension, entity, kind, pos, loaded_from_disk, canceled)`** is NeoForge's `EntityJoinLevelEvent` ([javadoc](https://mcstreetguy.github.io/NeoForgeJavaDocs/1.20.3-20.3.8/net/neoforged/neoforge/event/entity/EntityJoinLevelEvent.html): cancellable, "If the event is canceled, the entity will not be added to the level"; `loadedFromDisk()`). In the catalog it has `neoforge`, `side: server` and `cancellable: true`. This PR covers item entities and experience orbs, at every place Horizon adds them: | Where | `loaded_from_disk` | |---|---| | `spawn_item` (every drop: blocks, mobs, players, dispensers… 64 callers) | false | | `add_orb` (XP from blocks, mobs, smelting, breeding…) | false | | `/summon experience_orb` | false | | `/summon item …` and other NBT spawns (`spawn_entity_from_nbt`) | false | | Nether portal and End gateway arrivals | false (vanilla's `addDuringTeleport`) | | Entities loading with their chunk (`spawn_loaded_entities`) | true | How it works: - The hook runs right after the entity is put in its list, so helpers that take an entity ID find it. A cancel pops it back out (and forgets its saved compound), as if it were never added. - A refused `/summon` fails with vanilla's `commands.summon.failed.uuid`, which is what `SummonCommand` throws when `tryAddFreshEntityWithPassengers` returns false. - Portal arrivals and `/summon` reach the level through the same queue as chunk loads (`world.loaded_entities`). They are marked there with a transient `horizon:fresh_add` key, the way `horizon:finalize` already works. The key comes off before the compound is kept or saved (unit test `a_fresh_add_is_told_apart_from_a_load_and_unmarked`). - On a client of a server nothing fires. `EntityLeaveLevelEvent` is step 2, in a separate PR. Mobs, projectiles, vehicles and players are steps 3–6 of #5162; each adds kinds to this same hook. ## Kind - [x] feature: something new for players (listed under New in the release notes) - [ ] parity: Horizon now does what vanilla 26.3 does - [ ] bug: a crash or a breakage - [ ] extra: Horizon-only, off by default - [ ] tooling: tools, CI, tests, harnesses - [ ] docs ## Vanilla reference 26.3's `PersistentEntitySectionManager`: - `addNewEntity(entity)` is `addEntity(entity, false)`. `ServerLevel.addFreshEntity`, `addWithUUID` and `addDuringTeleport` reach it. - `processPendingLoads` and `addLegacyChunkEntities` call `addEntity(e, true)`. These are the only `loaded = true` callers. - `SummonCommand`: `if (!level.tryAddFreshEntityWithPassengers(entity)) throw ERROR_DUPLICATE_UUID`. With no mod installed the hook is empty and nothing changes: the mark only travels through the load queue. ## Other projects' code none ## Tested in the game A throwaway probe in the hook (removed before the commit) logs every call and cancels any entity that joins above y=200. Fresh `mktemp -d` homes, `HORIZON_NO_AUDIO=1`, `--profile fast`. **Single player (server thread)** | Action | Hook | Game | |---|---|---| | `summon item 0.5 90 0.5 {Item:{id:diamond,count:2}…}` | `#56 minecraft:item from_disk=false` | "Summoned new Diamond" | | `summon item 0.5 250 0.5 {…dirt…}` | `#59 … canceled=true` | "Unable to summon entity due to duplicate UUIDs" | | `summon experience_orb 1.5 90 1.5` / `… 2.5 90 2.5 {Value:7}` | both `from_disk=false` | "Summoned new Experience Orb" | | `summon experience_orb 1.5 250 1.5` | `canceled=true` | "Unable to summon entity due to duplicate UUIDs" | | `setblock … dirt` then `setblock … air destroy`, at y 95 and y 250 | drop joins; the one at y 250 is canceled and never appears | | | Emerald dropped into a nether portal built with `/fill` | joins the overworld, then `minecraft:the_nether` with `from_disk=false` | | | Reopen the world | the diamond, the dirt and coal drops and both orbs: `from_disk=true`; the canceled ones never come back | | The world's own drops (leaves, eggs, mob loot) all showed up as fresh joins too. **Dedicated server (`--dedicated --nogui`) with a `horizon-ctl` client, Alex** | Action | Hook | Alex sees | |---|---|---| | console `summon item … diamond` | `from_disk=false` | the diamond | | console `summon item 1.5 250 1.5 …` and `summon experience_orb 2.5 250 2.5` | `canceled=true`; both say "duplicate UUIDs" | nothing | | `setblock 3 100 3 lapis_ore`, then `air destroy` (always drops XP) | lapis item + orb join (`add_orb`) | the lapis | | The same at y 250 | the item and all three orbs canceled | nothing | | Server restart, Alex rejoins | diamond and lapis `from_disk=true` | | | Alex's own client | nothing fires | | Parity, locally (`tools/parity.py nether_portal server_entities held_drop`): 3 passed. The other harnesses that cover these files are CI's. ## Self-check **pr_ready** (`tools/pr_ready.py --quick --base hr/main` on cachyos-x8664, against hr/main at 01bbb4c65, 1 min 0 s) | Check | Result | | --- | --- | | Scope | ✅ 9 files, +157 −2 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 495 passed (25 s) | | rustfmt | ✅ (5 s) | | clippy | ✅ (12 s) | | tests: horizon-client | ✅ 1341 passed (17 s) | | doc tests | ✅ 0 passed (1 s) | | Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses; `nether_portal`, `server_entities` and `held_drop` passed locally | 🤖 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:12: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#6112
No description provided.