From c0f0c17ed4b05dd72d9ecda26ae6a925e0018f0c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 19 Oct 2026 03:07:17 +0200 Subject: [PATCH] Keep restored chunk entities out of /summon's result after a level switch Coming back to a level, the entities saved with its chunks wait in World::loaded_entities until the next tick spawns them, as vanilla's PersistentEntitySectionManager.processPendingLoads does. /summon pushed its compound onto that queue and drained all of it, so the first waiting entity took the id counted as the summon's root: after a Nether round trip `/summon minecraft:armor_stand ...` reported "Summoned new Pig". Summon now spawns only its own stack and leaves the queue to the tick. The command harness's settle now waits for the entities of the chunks around the player as well, like the oracle's settle (areEntitiesActuallyLoadedAndTicking), and a `portal` op sends the player through the nether portal it stands in (Entity.handlePortal: NetherPortalBlock.getPortalDestination, teleportToPortalDestination). The corpus's new `levels` group makes the round trip and checks the Overworld's pig and a summon's feedback on the way back. Fixes #947 Co-Authored-By: Claude Opus 5.5 --- .../src/game/command_harness.rs | 21 +++++++++++++++- .../horizon-client/src/game/commands/data.rs | 9 ++++++- .../horizon-client/src/game/nether_portal.rs | 6 +++++ .../parity/VanillaCommandExecOracle.java | 18 +++++++++++++ tools/command_exec_corpus.py | 25 ++++++++++++++++++- 5 files changed, 76 insertions(+), 3 deletions(-) diff --git a/crates/horizon-client/src/game/command_harness.rs b/crates/horizon-client/src/game/command_harness.rs index ed3f8b336..096ba1c74 100644 --- a/crates/horizon-client/src/game/command_harness.rs +++ b/crates/horizon-client/src/game/command_harness.rs @@ -4,7 +4,8 @@ //! command as the player (Commands.performPrefixedCommand) and records every //! system message it produced, as styled spans, with the results reported to //! the player's `CommandResultCallback`; `tick` advances the game; `settle` -//! waits until the terrain around the player is loaded. +//! waits until the terrain and entities around the player are loaded; +//! `portal` sends the player through the nether portal it stands in. use super::Game; use horizon_client::{Result, text::Text}; use serde_json::{Value, json}; @@ -40,6 +41,19 @@ pub(super) fn spans(text: &Text) -> Value { } impl Game<'_> { + /// The player's portal process finished in the nether portal it stands + /// in (the oracle's player is not ticked, so neither is the wait). + fn harness_portal(&mut self) -> Result<()> { + let Some(p) = &self.player else { + return Err("command harness: portal without a player".into()); + }; + let entry = horizon_core::position::BlockPos::containing(p.position); + let block = self.world.block(entry.x, entry.y, entry.z); + if &*self.res.facts().get(block).name != "minecraft:nether_portal" { + return Err("command harness: the player is not in a nether portal".into()); + } + self.player_through_nether_portal(entry) + } /// Load terrain until the player stands in lit chunks. fn harness_settle(&mut self, deadline: Instant) -> Result<()> { loop { @@ -51,6 +65,10 @@ impl Game<'_> { (p.position.z.floor() as i32) >> 4, ); if (-1..=1).all(|dz| (-1..=1).all(|dx| self.streaming.is_lit(cx + dx, cz + dz))) { + // The oracle's settle also waits for the entities saved + // in those chunks (areEntitiesActuallyLoadedAndTicking): + // after a dimension change they come back from the save. + self.spawn_loaded_entities(); return Ok(()); } } @@ -130,6 +148,7 @@ impl Game<'_> { } } "settle" => self.harness_settle(deadline)?, + "portal" => self.harness_portal()?, other => return Err(format!("command harness: unknown op {other}").into()), } } diff --git a/crates/horizon-client/src/game/commands/data.rs b/crates/horizon-client/src/game/commands/data.rs index bbb388deb..89e3d6397 100644 --- a/crates/horizon-client/src/game/commands/data.rs +++ b/crates/horizon-client/src/game/commands/data.rs @@ -666,13 +666,20 @@ impl Game<'_> { /// Spawn an entity from its compound (`EntityType.loadEntityRecursive`); /// returns what was created. pub(super) fn spawn_entity_from_nbt(&mut self, tag: Tag) -> Option { - let before = self.next_entity; let key = { let p = entity_nbt::position(&tag)?; ((p.x.floor() as i32) >> 4, (p.z.floor() as i32) >> 4) }; + // Only this stack spawns now: the entities of chunks that finished + // loading wait for the level's entity tick, as in vanilla + // (PersistentEntitySectionManager.processPendingLoads), and must not + // take the ids counted as this stack's. + let mut waiting = std::mem::take(&mut self.world.loaded_entities); + let before = self.next_entity; self.world.loaded_entities.push((key, vec![tag])); self.spawn_loaded_entities(); + waiting.append(&mut self.world.loaded_entities); + self.world.loaded_entities = waiting; if self.next_entity == before { return None; } diff --git a/crates/horizon-client/src/game/nether_portal.rs b/crates/horizon-client/src/game/nether_portal.rs index 94df10ec4..11c32f701 100644 --- a/crates/horizon-client/src/game/nether_portal.rs +++ b/crates/horizon-client/src/game/nether_portal.rs @@ -298,6 +298,12 @@ impl Game<'_> { else { return Ok(()); }; + self.player_through_nether_portal(entry) + } + /// `Entity.handlePortal` once the player's portal process at `entry` + /// finished: `NetherPortalBlock.getPortalDestination`, then + /// `teleportToPortalDestination` (`isAllowedToEnterPortal`). + pub(super) fn player_through_nether_portal(&mut self, entry: BlockPos) -> Result<()> { let Some(p) = self.player.as_ref() else { return Ok(()); }; diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java index 4b61558d8..8c6225401 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java @@ -222,6 +222,7 @@ public final class VanillaCommandExecOracle { } } case "settle" -> settle(); + case "portal" -> portal(); default -> throw new IllegalArgumentException(line); } } @@ -278,6 +279,23 @@ public final class VanillaCommandExecOracle { } /** Force-load the chunks around the player (Horizon waits for its streamed terrain). */ + /** + * Entity.handlePortal once the player's portal process finished in the + * nether portal it stands in (the oracle does not tick the player). + */ + void portal() { + ServerLevel level = player.level(); + net.minecraft.core.BlockPos entry = player.blockPosition(); + if (!level.getBlockState(entry).is(net.minecraft.world.level.block.Blocks.NETHER_PORTAL)) { + throw new IllegalStateException("portal: the player is not in a nether portal"); + } + var block = (net.minecraft.world.level.block.NetherPortalBlock) net.minecraft.world.level.block.Blocks.NETHER_PORTAL; + var transition = block.getPortalDestination(level, player, entry); + if (transition != null) { + player.teleportToPortalDestination(level, transition); + } + } + void settle() { ServerLevel level = player.level(); int cx = player.chunkPosition().x(), cz = player.chunkPosition().z(); diff --git a/tools/command_exec_corpus.py b/tools/command_exec_corpus.py index eb99e1f1a..4757e66f0 100644 --- a/tools/command_exec_corpus.py +++ b/tools/command_exec_corpus.py @@ -2,7 +2,8 @@ Each group is a list of operations: `cmd` runs a command as the player and records its messages and results (`record: false` runs setup silently), -`tick` advances the game and `settle` loads the terrain around the player. +`tick` advances the game, `settle` loads the terrain and entities around the +player and `portal` sends the player through the nether portal it stands in. Everything happens high above the terrain (y 200 and up, where both worlds are air) after the world's entities are removed, so the results do not depend on world generation. @@ -394,6 +395,27 @@ def execute(): ] +def levels(): + """A nether portal round trip: on the way back the Overworld's entities (the + pig left by `entities`) are found and a summon names what it summoned.""" + return [ + *setup('/fill 2 231 12 5 236 12 minecraft:obsidian', '/fill 3 232 12 4 235 12 minecraft:nether_portal[axis=x]', + '/tp @s 3.5 232 12.5 0 0'), + {'op': 'portal'}, + {'op': 'settle'}, + cmd('/execute if dimension minecraft:the_nether run say through the portal'), + {'op': 'portal'}, + {'op': 'settle'}, + cmd('/execute if dimension minecraft:overworld run say back through the portal'), + cmd('/data get entity @s Pos'), + cmd('/data get entity @s Rotation'), + cmd('/execute if entity @e[type=minecraft:pig]'), + cmd('/summon minecraft:armor_stand 4.5 210 4.5 {NoGravity:1b,Tags:["lv"],CustomName:"Back"}'), + *setup('/kill @e[tag=lv]', '/fill 2 231 12 5 236 12 minecraft:air', '/tp @s 8.5 230 8.5 0 0'), + {'op': 'settle'}, + ] + + def items_and_loot(): return [ *setup('/clear @s', '/gamemode survival'), @@ -918,6 +940,7 @@ def groups(): ('entities', entities()), ('scores, teams and bossbars', scores()), ('execute', execute()), + ('levels', levels()), ('items and loot', items_and_loot()), ('world', world_misc()), ('progression', progression()),