fix(server): move players to another dimension with execute in ... run tp

On a dedicated server, `execute in <dimension> run tp @s ...` from a player
moved them inside the level they were in. It now does what vanilla's
TeleportCommand does: ServerPlayer.teleport into the other level, with the
Respawn packet keeping all data and no portal sound, then the feedback. The
players there are tracked to them and they are removed from the ones they
left.

tools/test_server_dimension_players_parity.py joins two players to a vanilla
and a Horizon server and compares, step by step, the players each is sent and
loses and the dimensions it respawns into, while they go to the Nether and
the End by command and through an End portal. LocatorBarClient gains
`players on` and `still NAME` (the position reminder a still client sends).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-18 02:14:03 +02:00
parent ce571f789c
commit 9a2d87ec97
9 changed files with 287 additions and 12 deletions
+3
View File
@@ -984,6 +984,8 @@ struct Game<'a> {
/// A server's players on their way to another level, each waiting for
/// the destination to load around them.
server_trips: Vec<server_travel::Trip>,
/// Players a command sends to another level, once it has run.
level_teleports: Vec<server_travel::LevelTeleport>,
/// A server's levels other than the one in place (game/levels.rs).
levels: levels::Levels,
/// Which pass of a server tick runs (`tick_server`): the first one reads
@@ -10295,6 +10297,7 @@ impl<'a> Game<'a> {
world_root: PathBuf::new(),
travelling: None,
server_trips: Vec::new(),
level_teleports: Vec::new(),
levels: Default::default(),
level_pass: Default::default(),
portals: Default::default(),
@@ -357,6 +357,7 @@ impl Game<'_> {
silent: false,
store: None,
elsewhere: false,
level: None,
block: Some(pos),
};
// performPrefixedCommand: a leading slash is allowed.
@@ -1124,7 +1124,23 @@ impl Game<'_> {
(-(dy.atan2(horizontal).to_degrees())) as f32,
]);
}
// `execute in` another dimension: a server's player moves to that
// level (ServerPlayer.teleport into another level), after the command.
let level = self
.command_source
.and_then(|s| s.level)
.filter(|d| *d != self.dim && self.options.dedicated.is_some());
for &t in &targets {
if let (Some(dim), Target::Player, Some(conn)) = (level, t, self.lan_acting) {
self.level_teleports
.push(super::server_travel::LevelTeleport {
conn,
dim,
at: pos,
rotation,
});
continue;
}
self.teleport_to(t, pos, rotation);
}
let [x, y, z] = pos.map(|v| Text::literal(format_double(v)));
@@ -26,6 +26,9 @@ pub(in crate::game) struct Source {
/// `execute in` another dimension than the one Horizon has loaded: none
/// of its chunks are loaded.
pub elsewhere: bool,
/// The dimension `execute in` named, if any: a server teleports its
/// players there (`TeleportCommand` into the source's level).
pub level: Option<super::super::Dim>,
/// The command block whose output this source is
/// (`BaseCommandBlock.CloseableCommandBlockSource`).
pub block: Option<(i32, i32, i32)>,
@@ -77,6 +80,7 @@ impl Game<'_> {
silent: false,
store: None,
elsewhere: false,
level: None,
block: None,
}
}
@@ -90,6 +94,7 @@ impl Game<'_> {
silent: true,
store: None,
elsewhere: false,
level: None,
block: None,
}
}
@@ -441,6 +446,7 @@ impl Game<'_> {
pos: self.target_position(t)?,
rot: self.target_rotation(t),
elsewhere: false,
level: None,
..s
})
})
@@ -544,6 +550,7 @@ impl Game<'_> {
}
vec![Source {
elsewhere: dim != self.dim.id(),
level: Some(super::super::Dim::from_id(dim)),
..s
}]
}
@@ -928,6 +928,8 @@ impl Game<'_> {
.map(|t| horizon_client::lan::data::component_nbt(&t.component()))
.collect::<Vec<_>>()
});
// A teleport into another level happens before its feedback is sent.
self.run_level_teleports(host);
for content in lines.unwrap_or_default() {
host.hub.send_to(
conn,
@@ -22,6 +22,17 @@ pub(super) struct Trip {
end: bool,
/// The Nether portal plan, whose exit is found or built on arrival.
plan: Option<Plan>,
/// Sent by a command: the player is already where it goes.
placed: bool,
}
/// `TeleportCommand` into another level for a server's player: where, and
/// the rotation it sets (none keeps the player's).
pub(super) struct LevelTeleport {
pub conn: u32,
pub dim: Dim,
pub at: [f64; 3],
pub rotation: Option<[f32; 2]>,
}
/// A player's box: Player's standing dimensions.
@@ -177,7 +188,8 @@ impl Game<'_> {
};
let at = [destination.x, destination.y, destination.z];
let ctx = self.server_ctx();
host.hub.change_level(i, at, yaw, &ctx);
// TeleportTransition.PLAY_PORTAL_SOUND.
host.hub.change_level(i, at, yaw, None, true, &ctx);
self.as_guest(host, conn, |g| g.trigger_dimension(source, target));
self.server_trips.push(Trip {
conn,
@@ -185,6 +197,7 @@ impl Game<'_> {
target: destination,
end,
plan,
placed: false,
});
if let Err(e) = self.enter_level(Some(host), source) {
log::error!("could not reopen {}: {e}", source.id());
@@ -214,6 +227,9 @@ impl Game<'_> {
continue;
}
let trip = self.server_trips.remove(k);
if trip.placed {
continue;
}
self.resolve_arrivals();
let yaw = host.hub.guests[i].yaw;
let (position, yaw) = if trip.end {
@@ -248,6 +264,43 @@ impl Game<'_> {
}
}
/// The players the command just run sends to another level
/// (`ServerPlayer.teleport` from TeleportCommand): a Respawn packet
/// keeping all data and the teleport, with no portal sound. The server
/// leaves the body alone until the level has loaded around it.
pub(super) fn run_level_teleports(&mut self, host: &mut LanHost) {
for t in std::mem::take(&mut self.level_teleports) {
let Some(i) = host.hub.guests.iter().position(|g| g.conn == t.conn) else {
continue;
};
let source = self.dim;
if let Err(e) = self.enter_level(Some(host), t.dim) {
log::error!("could not open {}: {e}", t.dim.id());
continue;
}
let g = &mut host.hub.guests[i];
let [yaw, pitch] = t.rotation.unwrap_or([g.yaw, g.pitch]);
// A new body there: no fall distance or motion carried over.
g.motion = None;
let ctx = self.server_ctx();
host.hub
.change_level(i, t.at, yaw, Some(pitch), false, &ctx);
self.as_guest(host, t.conn, |g| g.trigger_dimension(source, t.dim));
self.server_trips.retain(|trip| !trip.is(t.conn));
self.server_trips.push(Trip {
conn: t.conn,
dim: t.dim,
target: Vec3::new(t.at[0], t.at[1], t.at[2]),
end: false,
plan: None,
placed: true,
});
if let Err(e) = self.enter_level(Some(host), source) {
log::error!("could not reopen {}: {e}", source.id());
}
}
}
/// The hub's view of the server for packets sent outside its own tick.
pub(super) fn server_ctx(&mut self) -> TickCtx<'_> {
let spawn = self.spawn.map_or([0.5, 80.0, 0.5], |s| [s.x, s.y, s.z]);
+20 -7
View File
@@ -2199,7 +2199,15 @@ impl Hub {
/// names (`set_dimension`): ClientboundRespawnPacket keeping all data,
/// then vanilla's packet order. Nothing tracked in the old level is
/// removed one by one: the client drops its whole level.
pub fn change_level(&mut self, i: usize, at: [f64; 3], yaw: f32, ctx: &TickCtx) {
pub fn change_level(
&mut self,
i: usize,
at: [f64; 3],
yaw: f32,
pitch: Option<f32>,
portal_sound: bool,
ctx: &TickCtx,
) {
let mode = self.guests[i].game_mode;
let info = self.spawn_info(mode);
self.close_container_for(i);
@@ -2210,6 +2218,9 @@ impl Hub {
g.chunk_queue.clear();
g.center = None;
g.yaw = yaw;
if let Some(pitch) = pitch {
g.pitch = pitch;
}
g.position = at;
g.previous_position = at;
g.send(&C::Respawn(play::Respawn {
@@ -2241,12 +2252,14 @@ impl Hub {
// ServerPlayer.sendPostEffects.
g.send(&raw(ids::POST_EFFECTS, vec![0]));
// TeleportTransition.PLAY_PORTAL_SOUND.
g.send(&C::LevelEvent(play::LevelEvent {
kind: 1032,
pos: [0, 0, 0],
data: 0,
global: false,
}));
if portal_sound {
g.send(&C::LevelEvent(play::LevelEvent {
kind: 1032,
pos: [0, 0, 0],
data: 0,
global: false,
}));
}
// lastSentExp, lastSentHealth and lastSentFood.
g.sent_health = None;
g.sent_xp = None;