diff --git a/crates/horizon-client/src/chat_tests.rs b/crates/horizon-client/src/chat_tests.rs index 228e8d097..2bbb6ad0b 100644 --- a/crates/horizon-client/src/chat_tests.rs +++ b/crates/horizon-client/src/chat_tests.rs @@ -120,7 +120,7 @@ fn click_targets_match_rendered_spans_when_scaled_wrapped_and_scrolled() { } #[test] -fn suggest_command_replaces_input_and_keeps_chat_open() { +fn locate_coordinates_fill_chat_with_teleport_without_submitting() { let font = font(); let m = Measure(&font); let registries = Registries::default(); @@ -128,10 +128,37 @@ fn suggest_command_replaces_input_and_keeps_chat_open() { dispatcher: crate::command::vanilla::game_dispatcher(), registries: ®istries, }; - let mut screen = ChatScreen::new(&m, &commands, "draft", 0, 640, 360); - screen.suggest_command(&m, &commands, "/seed"); - assert_eq!(screen.value(), "/seed"); - assert_eq!(screen.input.cursor, 5); - assert_eq!(screen.input.highlight, 5); - assert_eq!(screen.normalized(), "/seed"); + // Structures and POIs use relative Y; biome results include absolute Y. + for (y, expected) in [ + (None, "/tp @s -1136 ~ 848"), + (Some(73), "/tp @s -1136 73 848"), + ] { + let link = Text::teleport_link(-1136, y, 848); + assert_eq!(link.component()["click_event"]["action"], "suggest_command"); + assert_eq!(link.component()["click_event"]["command"], expected); + let mut chat = Chat::default(); + add(&mut chat, &font, Text::literal("Found at ").append(link)); + let mut screen = ChatScreen::new(&m, &commands, "draft", 0, 640, 360); + let x = 4.0 + f64::from(font.width("Found at ")); + assert!(!screen.click(&m, &commands, x, 315.0)); + let Some(ClickEvent::SuggestCommand(command)) = chat.click_at(&font, 360, x, 315.0) else { + panic!("locate coordinates must suggest a command"); + }; + screen.suggest_command(&m, &commands, &command); + assert_eq!(screen.value(), expected); + assert_eq!(screen.input.cursor, expected.chars().count()); + assert_eq!(screen.input.highlight, screen.input.cursor); + // Submission is a separate, explicit Enter press. + assert_eq!( + screen.key( + &m, + &commands, + &[], + winit::keyboard::KeyCode::Enter, + false, + false + ), + ChatAction::Submit(expected.to_owned()) + ); + } } diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 494b2d51c..fdc093dd6 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -2334,25 +2334,7 @@ impl Game<'_> { &[&poi], ))); }; - let coords = Text::translate( - "chat.coordinates", - &[ - Text::literal(p[0].to_string()), - Text::literal("~"), - Text::literal(p[2].to_string()), - ], - ); - let link = Text::literal("[") - .append(coords) - .append("]") - .with_style(&Style { - color: Some(text::GREEN), - click: Some(text::ClickEvent::SuggestCommand(format!( - "/tp @s {} ~ {}", - p[0], p[2] - ))), - ..Style::default() - }); + let link = Text::teleport_link(p[0], None, p[2]); let (dx, dz) = (p[0] - from[0], p[2] - from[2]); let distance = ((dx * dx + dz * dz) as f32).sqrt().floor() as i32; let shown = if poi.starts_with('#') { @@ -2408,18 +2390,7 @@ impl Game<'_> { } else { biome.clone() }; - let coords = Text::tr("chat.coordinates", &[&p[0], &p[1], &p[2]]); - let link = Text::literal("[") - .append(coords) - .append("]") - .with_style(&Style { - color: Some(text::GREEN), - click: Some(text::ClickEvent::SuggestCommand(format!( - "/tp @s {} {} {}", - p[0], p[1], p[2] - ))), - ..Style::default() - }); + let link = Text::teleport_link(p[0], Some(p[1]), p[2]); let distance = (d2 as f32).sqrt().floor() as i32; self.feedback(Text::translate( "commands.locate.biome.success", @@ -2447,25 +2418,7 @@ impl Game<'_> { &[&s], ))); }; - let coords = Text::translate( - "chat.coordinates", - &[ - Text::literal(p[0].to_string()), - Text::literal("~"), - Text::literal(p[2].to_string()), - ], - ); - let link = Text::literal("[") - .append(coords) - .append("]") - .with_style(&Style { - color: Some(text::GREEN), - click: Some(text::ClickEvent::SuggestCommand(format!( - "/tp @s {} ~ {}", - p[0], p[2] - ))), - ..Style::default() - }); + let link = Text::teleport_link(p[0], None, p[2]); let (dx, dz) = (f64::from(p[0] - from[0]), f64::from(p[2] - from[2])); let distance = (dx * dx + dz * dz).sqrt().floor() as i32; let shown = if s.starts_with('#') { diff --git a/crates/horizon-client/src/text.rs b/crates/horizon-client/src/text.rs index b43aa32f3..bb2bd6b48 100644 --- a/crates/horizon-client/src/text.rs +++ b/crates/horizon-client/src/text.rs @@ -151,6 +151,20 @@ impl Text { ..Style::color(GREEN) }) } + /// LocateCommand's coordinate link: suggest a teleport without executing it. + /// Structure and POI results preserve the player's current height (`~`). + pub fn teleport_link(x: i32, y: Option, z: i32) -> Self { + let y = y.map_or_else(|| "~".to_owned(), |y| y.to_string()); + let coords = Self::tr("chat.coordinates", &[&x, &y, &z]); + Self::literal("[") + .append(coords) + .append("]") + .with_style(&Style { + click: Some(ClickEvent::SuggestCommand(format!("/tp @s {x} {y} {z}"))), + hover: Some(Box::new(Self::translate("chat.coordinates.tooltip", &[]))), + ..Style::color(GREEN) + }) + } /// Component.translatable(key, args) in the current language. pub fn translate(key: &str, args: &[Text]) -> Self { let mut t = format_translation(&lang::get(key), args);