diff --git a/crates/horizon-client/src/game/command_blocks.rs b/crates/horizon-client/src/game/command_blocks.rs index 5a5fa11c8..434c72e72 100644 --- a/crates/horizon-client/src/game/command_blocks.rs +++ b/crates/horizon-client/src/game/command_blocks.rs @@ -356,7 +356,7 @@ impl Game<'_> { eyes: false, silent: false, store: None, - elsewhere: false, + dimension: self.dim, block: Some(pos), }; // performPrefixedCommand: a leading slash is allowed. diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 1995efbf4..aca4d3002 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -231,8 +231,8 @@ impl Game<'_> { .ok_or_else(|| err("argument.pos.missing.int", &[]))?; // BlockPos.containing(getPosition) let p = self.resolve_coords(&c).map(|v| v.floor() as i32); - let elsewhere = self.command_source.is_some_and(|s| s.elsewhere); - if elsewhere || !self.world.chunks.contains_key(&(p[0] >> 4, p[2] >> 4)) { + let dimension = self.command_source.map_or(self.dim, |s| s.dimension); + if dimension != self.dim || !self.world.chunks.contains_key(&(p[0] >> 4, p[2] >> 4)) { return Err(err("argument.pos.unloaded", &[])); } if p[1] < horizon_client::chunk::MIN_Y @@ -1056,6 +1056,42 @@ impl Game<'_> { Target::Frame(_) | Target::Painting(_) => {} } } + /// Teleport the local player into the command source's dimension. Unlike + /// portal travel, `/tp` keeps the coordinates given by the command. + fn teleport_player_to_dimension( + &mut self, + dimension: Dim, + pos: [f64; 3], + rotation: Option<[f32; 2]>, + ) -> R<()> { + let Some(mut player) = self.player.take() else { + return Ok(()); + }; + let from = self.dim; + if self.switch_level(dimension, player.position).is_err() { + self.player = Some(player); + return Err(err("command.failed", &[])); + } + self.trigger_dimension(from, dimension); + let target = Vec3::new(pos[0], pos[1], pos[2]); + player.position = target; + player.velocity = Vec3::ZERO; + player.fall_distance = 0.0; + player.ground.on_ground = false; + if let Some([yaw, pitch]) = rotation { + let pitch = pitch.clamp(-90.0, 90.0); + self.yaw = yaw; + self.pitch = pitch; + player.yaw = yaw; + player.head_yaw = yaw; + player.pitch = pitch; + } + self.travelling = Some((player, target)); + self.portals.travel = None; + self.end_travel = false; + self.end_portal_travel = false; + Ok(()) + } fn cmd_teleport(&mut self, id: &str, ctx: &Context) -> R { let targets = if id.starts_with("teleport.targets") { self.entities(ctx, "targets", false)? @@ -1125,7 +1161,12 @@ impl Game<'_> { ]); } for &t in &targets { - self.teleport_to(t, pos, rotation); + let dimension = self.command_source.map_or(self.dim, |s| s.dimension); + if t == Target::Player && dimension != self.dim { + self.teleport_player_to_dimension(dimension, pos, rotation)?; + } else { + self.teleport_to(t, pos, rotation); + } } let [x, y, z] = pos.map(|v| Text::literal(format_double(v))); let message = if targets.len() == 1 { diff --git a/crates/horizon-client/src/game/commands/execute.rs b/crates/horizon-client/src/game/commands/execute.rs index 1fa67c8b6..9db5654d6 100644 --- a/crates/horizon-client/src/game/commands/execute.rs +++ b/crates/horizon-client/src/game/commands/execute.rs @@ -1,7 +1,7 @@ //! `/execute` (ExecuteCommand): walk the parsed redirect chain, forking and //! filtering command sources, then run the final command once per source, //! reporting each result to the `store` callbacks of its source. -use super::{Arg, Context, Game, R, Target, data::DataRef, err}; +use super::{Arg, Context, Dim, Game, R, Target, data::DataRef, err}; use horizon_client::{ command::{CommandError, vanilla}, nbt_path::NbtPath, @@ -23,9 +23,9 @@ pub(in crate::game) struct Source { pub silent: bool, /// Index of the newest `store` in `ExecState::stores`. pub store: Option, - /// `execute in` another dimension than the one Horizon has loaded: none - /// of its chunks are loaded. - pub elsewhere: bool, + /// The level this command source uses. Horizon has only this dimension's + /// chunks loaded in the single-player client. + pub dimension: Dim, /// The command block whose output this source is /// (`BaseCommandBlock.CloseableCommandBlockSource`). pub block: Option<(i32, i32, i32)>, @@ -76,7 +76,7 @@ impl Game<'_> { eyes: false, silent: false, store: None, - elsewhere: false, + dimension: self.dim, block: None, } } @@ -89,7 +89,7 @@ impl Game<'_> { eyes: false, silent: true, store: None, - elsewhere: false, + dimension: self.dim, block: None, } } @@ -440,7 +440,7 @@ impl Game<'_> { Some(Source { pos: self.target_position(t)?, rot: self.target_rotation(t), - elsewhere: false, + dimension: self.dim, ..s }) }) @@ -531,8 +531,8 @@ impl Game<'_> { ..s }], "in" => { - // Horizon keeps one dimension loaded: in another one, no - // chunk is loaded (as in vanilla without a player there). + // CommandSourceStack.withLevel scales the source position; + // absolute coordinates passed to /tp are unaffected. let Some(Arg::Resource(dim)) = c.get("dimension") else { return Ok(vec![s]); }; @@ -542,8 +542,12 @@ impl Game<'_> { ) { return Err(err("argument.dimension.invalid", &[dim])); } + let dimension = Dim::from_id(dim); + let coordinate_scale = |dim| if dim == Dim::Nether { 8.0 } else { 1.0 }; + let scale = coordinate_scale(s.dimension) / coordinate_scale(dimension); vec![Source { - elsewhere: dim != self.dim.id(), + pos: [s.pos[0] * scale, s.pos[1], s.pos[2] * scale], + dimension, ..s }] } @@ -785,14 +789,16 @@ impl Game<'_> { let p = self .coords(c, "pos") .map(|co| co.block(self.source_position(), self.source_rotation())); - return Ok(flag(p.is_some_and(|p| { - self.world.chunks.contains_key(&(p[0] >> 4, p[2] >> 4)) - }))); + let dimension = self.command_source.map_or(self.dim, |s| s.dimension); + return Ok(flag( + dimension == self.dim + && p.is_some_and(|p| self.world.chunks.contains_key(&(p[0] >> 4, p[2] >> 4))), + )); } if c.has("dimension") { - return Ok(flag( - matches!(c.get("dimension"), Some(Arg::Resource(d)) if d == self.dim.id()), - )); + let dimension = self.command_source.map_or(self.dim, |s| s.dimension); + return Ok(flag(matches!(c.get("dimension"), Some(Arg::Resource(d)) + if d.as_str() == dimension.id()))); } if c.has("block") { let p = self.block_pos_arg(c, "pos")?; diff --git a/crates/horizon-client/src/game/commands/place.rs b/crates/horizon-client/src/game/commands/place.rs index 3e447a7fb..9037304b0 100644 --- a/crates/horizon-client/src/game/commands/place.rs +++ b/crates/horizon-client/src/game/commands/place.rs @@ -79,10 +79,10 @@ impl Game<'_> { } /// `PlaceCommand.checkLoaded`: every chunk of the inclusive range. fn place_check_loaded(&self, min: [i32; 2], max: [i32; 2]) -> R<()> { - let elsewhere = self.command_source.is_some_and(|s| s.elsewhere); + let dimension = self.command_source.map_or(self.dim, |s| s.dimension); for cx in min[0]..=max[0] { for cz in min[1]..=max[1] { - if elsewhere || !self.world.chunks.contains_key(&(cx, cz)) { + if dimension != self.dim || !self.world.chunks.contains_key(&(cx, cz)) { return Err(err("argument.pos.unloaded", &[])); } } diff --git a/crates/horizon-client/src/game/nether_portal.rs b/crates/horizon-client/src/game/nether_portal.rs index 9ff9e8421..582092f57 100644 --- a/crates/horizon-client/src/game/nether_portal.rs +++ b/crates/horizon-client/src/game/nether_portal.rs @@ -352,10 +352,23 @@ impl Game<'_> { ) -> Result<()> { self.resolve_arrivals(); let plan = self.portals.travel.take(); - let exit = plan.as_ref().and_then(|plan| self.nether_exit_portal(plan)); - let (position, rotation) = match (plan, exit) { - (Some(plan), Some(exit)) => self.arrival(&plan, exit), - _ => (target, 0.0), + let Some(plan) = plan else { + // A command teleport can change dimensions without going through + // a portal. It has no portal placement, rotation, or sound effect. + player.position = target; + player.velocity = Vec3::ZERO; + player.fall_distance = 0.0; + self.previous_position = target; + self.player = Some(player); + self.pad.player_replaced(); + self.sway = Default::default(); + self.apply_equipment(); + return Ok(()); + }; + let exit = self.nether_exit_portal(&plan); + let (position, rotation) = match exit { + Some(exit) => self.arrival(&plan, exit), + None => (target, 0.0), }; player.position = position; // Relative.ROTATION and Relative.DELTA: the yaw turns, the motion diff --git a/tools/command_exec_corpus.py b/tools/command_exec_corpus.py index eb99e1f1a..4b9d9b9e6 100644 --- a/tools/command_exec_corpus.py +++ b/tools/command_exec_corpus.py @@ -391,6 +391,28 @@ def execute(): *setup('/kill @e[type=minecraft:armor_stand]', '/setblock 4 210 4 minecraft:air', '/setblock 5 210 5 minecraft:air', '/scoreboard objectives remove exec', '/tp @s 8.5 230 8.5 0 0'), {'op': 'tick', 'n': 25}, + *setup('/gamemode spectator'), + cmd('/execute in minecraft:the_nether if dimension minecraft:the_nether run say source dimension'), + cmd('/execute in minecraft:the_nether at @s if dimension minecraft:overworld run say entity dimension'), + cmd('/execute in minecraft:the_nether positioned as @s if dimension minecraft:the_nether run say position only'), + cmd('/execute in minecraft:the_nether if loaded 0 100 0'), + cmd('/execute in minecraft:the_nether run tp @s 0 100 0'), + {'op': 'settle'}, + cmd('/execute if dimension minecraft:the_nether run say dimension switched'), + cmd('/data get entity @s Pos'), + cmd('/execute in minecraft:the_nether positioned 80 100 -40 in minecraft:overworld run tp @s ~ ~ ~'), + {'op': 'settle'}, + cmd('/execute if dimension minecraft:overworld run say relative dimension'), + cmd('/data get entity @s Pos'), + cmd('/execute in minecraft:overworld positioned 80 100 -40 in minecraft:the_nether run tp @s ~ ~ ~ 45 30'), + {'op': 'settle'}, + cmd('/data get entity @s Pos'), + cmd('/data get entity @s Rotation'), + cmd('/execute in minecraft:overworld run tp @s 8.5 230 8.5'), + {'op': 'settle'}, + cmd('/execute if dimension minecraft:overworld run say dimension restored'), + cmd('/data get entity @s Pos'), + *setup('/gamemode creative'), ]