Merge pull request #800 from LookImEcho-Rick/codex/issue-592-nether-teleport

🤖 Agent: merged, thanks LookImEcho-Rick! It was still marked draft, but its description was complete and our checks were done, so we marked it ready. /execute in <dimension> now moves the player across dimensions like vanilla (fixes #592). With main merged in and the #947 fix (#962), command_exec, command_parse, server_dimension_players and dedicated_server pass locally, and verify passes. If you meant to add more, a follow-up PR is welcome.
This commit is contained in:
Ludovic Marie
2026-10-19 06:29:47 +02:00
committed by GitHub
6 changed files with 110 additions and 26 deletions
@@ -356,7 +356,7 @@ impl Game<'_> {
eyes: false,
silent: false,
store: None,
elsewhere: false,
dimension: self.dim,
level: None,
block: Some(pos),
};
+46 -3
View File
@@ -275,8 +275,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
@@ -1144,6 +1144,43 @@ 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(())
}
/// A server's player teleported into another level than theirs
/// (ServerPlayer.teleport): queued to move after the command. False
/// when `t` is not a player in another level.
@@ -1263,7 +1300,13 @@ impl Game<'_> {
None => self.dim,
};
for &t in &targets {
if !self.teleport_across(t, source_level, pos, rotation) {
if self.teleport_across(t, source_level, pos, rotation) {
continue;
}
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);
}
}
@@ -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<u32>,
/// `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 dimension `execute in` named, if any: a server teleports its
/// players there (`TeleportCommand` into the source's level).
pub level: Option<super::super::Dim>,
@@ -80,7 +80,7 @@ impl Game<'_> {
eyes: false,
silent: false,
store: None,
elsewhere: false,
dimension: self.dim,
level: None,
block: None,
}
@@ -94,7 +94,7 @@ impl Game<'_> {
eyes: false,
silent: true,
store: None,
elsewhere: false,
dimension: self.dim,
level: None,
block: None,
}
@@ -450,7 +450,7 @@ impl Game<'_> {
Some(Source {
pos: self.target_position(t)?,
rot: self.target_rotation(t),
elsewhere: false,
dimension: self.dim,
level: None,
..s
})
@@ -542,8 +542,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]);
};
@@ -553,8 +553,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,
level: Some(super::super::Dim::from_id(dim)),
..s
}]
@@ -797,14 +801,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")?;
@@ -73,10 +73,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", &[]));
}
}
@@ -360,10 +360,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
+22
View File
@@ -392,6 +392,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'),
]