Merge pull request #472 from ludovic111/fix/lan-guest-chat

LAN guest: command feedback names the guest and shows its numbers
This commit is contained in:
Ludovic Marie
2026-10-16 13:03:08 +02:00
committed by GitHub
8 changed files with 88 additions and 12 deletions
+3
View File
@@ -1044,6 +1044,8 @@ struct Game<'a> {
/// have each chest open.
lid_openers: HashMap<(i32, i32, i32), i32>,
lan_acting: Option<u32>,
/// The name of the guest `lan_acting` acts for (`Player.getName`).
lan_acting_name: Option<String>,
/// Command feedback to forward to a guest.
chat_capture: Option<Vec<horizon_client::text::Text>>,
/// 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(),
+1 -1
View File
@@ -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(),
+9 -5
View File
@@ -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
+12
View File
@@ -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(());
@@ -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();
+6
View File
@@ -372,6 +372,12 @@ impl Game<'_> {
}
}
/// The server's last `n` chat lines, newest first.
pub(super) fn remote_chat(&self, n: usize) -> Option<Vec<String>> {
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<()> {
+32 -3
View File
@@ -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<String> = 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();
+23 -3
View File
@@ -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):