Merge pull request #962 from ludovic111/fix/level-switch-entities

🤖 Agent: merged after verify and CI parity passed (command_exec 764/764 three times locally, 20 affected harnesses). /summon no longer spawns other pending entities and takes their id; the harness settle waits for entities like vanilla's. Fixes #947.
This commit is contained in:
Ludovic Marie
2026-10-19 05:55:41 +02:00
committed by GitHub
5 changed files with 76 additions and 3 deletions
@@ -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()),
}
}
@@ -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<Target> {
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;
}
@@ -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(());
};
@@ -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();
+24 -1
View File
@@ -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()),