diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 03be601ee..45aa085e8 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1044,6 +1044,8 @@ struct Game<'a> { /// have each chest open. lid_openers: HashMap<(i32, i32, i32), i32>, lan_acting: Option, + /// The name of the guest `lan_acting` acts for (`Player.getName`). + lan_acting_name: Option, /// Command feedback to forward to a guest. chat_capture: Option>, /// State of `/spectate`, `/posteffect`, `/debug`, `/waypoint` and friends. @@ -10223,6 +10225,7 @@ pub fn run() -> Result<()> { lan_entity_events: Vec::new(), lid_openers: HashMap::new(), lan_acting: None, + lan_acting_name: None, chat_capture: None, cmds: Default::default(), ui: Default::default(), diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index b117f423f..0493a5d7d 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -638,7 +638,7 @@ impl Game<'_> { fn messages_value(&self, n: usize) -> Value { let titles = &self.titles; json!({ - "chat": self.ui.chat.recent_lines(n), + "chat": self.remote_chat(n).unwrap_or_else(|| self.ui.chat.recent_lines(n)), "order": "newest first", "title": (titles.time > 0).then(|| titles.title.as_ref().map(|t| t.plain())).flatten(), "subtitle": (titles.time > 0).then(|| titles.subtitle.as_ref().map(|t| t.plain())).flatten(), diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 91247b85f..29befe606 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -324,11 +324,15 @@ impl Game<'_> { /// Entity.getDisplayName. pub(super) fn target_name(&self, t: Target) -> Text { match t { - Target::Player => match self.score.board.players_team(PLAYER_NAME) { - // PlayerTeam.formatNameForTeam. - Some(team) => team.formatted_name(Text::literal(PLAYER_NAME)), - None => Text::literal(PLAYER_NAME), - }, + Target::Player => { + // A guest's commands name the guest. + let name = self.lan_acting_name.as_deref().unwrap_or(PLAYER_NAME); + match self.score.board.players_team(name) { + // PlayerTeam.formatNameForTeam. + Some(team) => team.formatted_name(Text::literal(name)), + None => Text::literal(name), + } + } Target::Item(id) => { let item = self .dropped diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 7d8de6e2f..08d21e6cd 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -590,6 +590,18 @@ impl Game<'_> { fn control_key_down(&mut self, key: KeyCode) -> Result<()> { self.physical_keys.insert(key); + // A server's chat input takes the keys first, as remote_chat_key does. + if self.remote_chat_open() { + use winit::keyboard::NamedKey; + let named = match key { + KeyCode::Escape => Some(NamedKey::Escape), + KeyCode::Enter | KeyCode::NumpadEnter => Some(NamedKey::Enter), + KeyCode::Backspace => Some(NamedKey::Backspace), + _ => None, + }; + self.remote_chat_input(named, None); + return Ok(()); + } if self.screen == Screen::Chat { self.chat_key_code(key, None); return Ok(()); diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 051c38e18..9f1137ab7 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -154,9 +154,11 @@ impl Game<'_> { self.close_ender_chest(); std::mem::swap(&mut self.ender_items, &mut g.ender_items); self.lan_acting = Some(conn); + self.lan_acting_name = Some(g.name.clone()); let result = f(self); self.lan_acting = None; + self.lan_acting_name = None; // An ender chest the guest opened: the menu shows their inventory // itself (lan_open_container), not a stand-in block entity. self.close_ender_chest(); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 8df1c5324..a7b1c4410 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -372,6 +372,12 @@ impl Game<'_> { } } + /// The server's last `n` chat lines, newest first. + pub(super) fn remote_chat(&self, n: usize) -> Option> { + let r = self.remote.as_ref()?; + Some(r.chat.iter().rev().take(n).map(|l| l.0.clone()).collect()) + } + /// Process everything the network thread received (once per frame, like /// the client's packet processor). pub(super) fn poll_remote(&mut self) -> Result<()> { diff --git a/crates/horizon-protocol/src/text.rs b/crates/horizon-protocol/src/text.rs index 7a23f16a8..cc44430fb 100644 --- a/crates/horizon-protocol/src/text.rs +++ b/crates/horizon-protocol/src/text.rs @@ -47,9 +47,10 @@ fn append(tag: &Tag, lang: &Language, out: &mut String, depth: usize) { Tag::Compound(_) => { if let Some(text) = get(tag, "text").and_then(string) { out.push_str(&text); - } else if let Some(text) = get(tag, "").and_then(string) { - // Mixed-list wrapper for a bare string. - out.push_str(&text); + } else if let Some(value) = get(tag, "") { + // NbtOps' wrapper for a bare value in a mixed list (a + // translation's number and string arguments). + append(value, lang, out, depth + 1); } else if let Some(key) = get(tag, "translate").and_then(string) { let args: Vec = match get(tag, "with") { Some(Tag::List(args)) => args @@ -154,6 +155,34 @@ mod tests { assert_eq!(format_java("100%% sure", &[]), "100% sure"); } + #[test] + fn unwraps_mixed_list_arguments() { + let mut lang = Language::new(); + lang.insert( + "commands.item.entity.set.success.single".into(), + "Replaced %s slot(s) on %s with %s".into(), + ); + // `with: [1, {text: "Alex"}, ...]` in NBT: the int comes wrapped. + let replaced = Tag::compound([ + ( + "translate", + Tag::String("commands.item.entity.set.success.single".into()), + ), + ( + "with", + Tag::List(vec![ + Tag::compound([("", Tag::Int(1))]), + Tag::compound([("text", Tag::String("Alex".into()))]), + Tag::compound([("", Tag::String("[Emerald]".into()))]), + ]), + ), + ]); + assert_eq!( + plain(&replaced, &lang), + "Replaced 1 slot(s) on Alex with [Emerald]" + ); + } + #[test] fn flattens_components() { let mut lang = Language::new(); diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 37a220e52..6f0a79b81 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -18,7 +18,8 @@ address. The script then checks, from both sides, what a guest's game used to ge drowning and the void (then respawns); - pvp: the guest and the host hit each other with a sword; with the pvp game rule off, nothing; - containers: the guest's ender chest holds the guest's items, not the host's, and a double chest - opens as 9x6 with the right half first. + opens as 9x6 with the right half first; +- feedback: the guest's command feedback names the guest and shows the numbers. Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -45,7 +46,7 @@ from agent_control_e2e import Cli, Failed, game_env # noqa: E402 ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' -CHECKS = ['tab', 'self', 'armour', 'nametags', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback'] class Game: @@ -453,10 +454,29 @@ def check_containers(host, guest, out): return {'ender': ender, 'double': double, 'taken': taken} +def check_feedback(host, guest, out): + x, y, z = (round(v, 1) for v in pos_of(guest)) + guest('command', command='item replace entity @s weapon.mainhand with minecraft:stick') + guest('command', command=f'tp @s {x:.1f} {y:.1f} {z:.1f}') + guest.wait(10) + chat = guest.raw('messages', 5)['chat'] + guest.raw('key', 't', 'tap') + guest.wait(5) + guest.shot(out, 'feedback') + guest.raw('key', 'esc', 'tap') + guest.wait(5) + screen = guest('state').get('screen') + replaced = f'Replaced 1 slot(s) on {GUEST_NAME} with [Stick]' + assert any(replaced in line for line in chat), f'no "{replaced}" in {chat}' + assert any(line.startswith(f'Teleported {GUEST_NAME} to {x:.6f}') for line in chat), f'teleport feedback: {chat}' + assert screen in (None, 'playing'), f'the chat did not close: {screen}' + return chat + + CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, 'chest': check_chest, 'crafting': check_crafting, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, - 'containers': check_containers} + 'containers': check_containers, 'feedback': check_feedback} def run(ctl, out, resources, checks):