diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index d85489f3a..005d40b86 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1030,6 +1030,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. @@ -10179,6 +10181,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 62b4f8624..8f535b14e 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -630,7 +630,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 a7385914b..e31c5ef3d 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -315,11 +315,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/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 52170de55..53d4a5906 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 db2cab0d0..2c5f2cb28 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..4f6443485 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,26 @@ def check_containers(host, guest, out): return {'ender': ender, 'double': double, 'taken': taken} +def check_feedback(host, guest, out): + x, y, z = 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') + 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}' + 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):