From c12f5cf41adfc96551da6715b2f086203e8d7a72 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:46:30 +0200 Subject: [PATCH] Server titles: /title reaches the guest, and its HUD shows and clears them TitleCommand sends the targeted player ClientboundSetTitleText, SetSubtitleText, SetActionBarText, SetTitlesAnimation and ClearTitles. A guest's /title (the window's own player on the server thread too) only changed the server's HUD; it is now sent to the guest. The remote client showed titles and action bars as a bare line and ignored subtitles, times and clearing; it now feeds the HUD's titles as ClientPacketListener does (the action bar of a SystemChat overlay too) and ticks them (Gui.tick). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 3 ++ .../src/game/commands/text_commands.rs | 43 +++++++++++++---- crates/horizon-client/src/game/lan_actions.rs | 3 ++ crates/horizon-client/src/game/remote.rs | 46 +++++++++---------- tools/lan_e2e.py | 21 ++++++++- 5 files changed, 80 insertions(+), 36 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 38edb3605..789479710 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1152,6 +1152,8 @@ struct Game<'a> { lan_acting_uuid: Option, /// Command feedback to forward to a guest. chat_capture: Option>, + /// Packets for the guest acting now, sent once its action returns. + guest_packets: Vec, /// `broadcastToAdmins` lines a guest's command queued, sent once it /// returns: the line, whether operators get it and whether the console /// logs it. @@ -10374,6 +10376,7 @@ impl<'a> Game<'a> { lan_entity_events: Vec::new(), lid_openers: HashMap::new(), lan_acting: None, + guest_packets: Vec::new(), lan_acting_name: None, lan_acting_uuid: None, chat_capture: None, diff --git a/crates/horizon-client/src/game/commands/text_commands.rs b/crates/horizon-client/src/game/commands/text_commands.rs index da8485849..dead07d71 100644 --- a/crates/horizon-client/src/game/commands/text_commands.rs +++ b/crates/horizon-client/src/game/commands/text_commands.rs @@ -1,6 +1,7 @@ //! `/tellraw` (TellRawCommand) and `/title` (TitleCommand). use super::{Arg, Context, Game, R, Target, err}; -use horizon_client::text::Text; +use horizon_client::{hud_overlays::Titles, lan::data::component_nbt, text::Text}; +use horizon_protocol::packets::play::{self, Clientbound as C}; impl Game<'_> { /// ComponentArgument with selectors resolved for the source @@ -153,30 +154,45 @@ impl Game<'_> { let name = self.target_name(Target::Player); let single = n == 1; let key = if ctx.has("clear") { - self.titles.clear(); + self.titles_or_packet(Titles::clear, C::ClearTitles(play::Bool1 { value: false })); "cleared" } else if ctx.has("reset") { - self.titles.clear(); - self.titles.reset_times(); + self.titles_or_packet( + |t| { + t.clear(); + t.reset_times(); + }, + C::ClearTitles(play::Bool1 { value: true }), + ); "reset" } else if ctx.has("times") { let get = |k: &str| match ctx.get(k) { Some(Arg::Int(v)) => *v, _ => -1, }; - self.titles - .set_times(get("fadeIn"), get("stay"), get("fadeOut")); + let (fade_in, stay, fade_out) = (get("fadeIn"), get("stay"), get("fadeOut")); + self.titles_or_packet( + |t| t.set_times(fade_in, stay, fade_out), + C::SetTitlesAnimation(play::TitlesAnimation { + fade_in, + stay, + fade_out, + }), + ); "times" } else { let text = self.component_arg(ctx, "title")?; + let sent = play::Text { + text: component_nbt(&text.component()), + }; if ctx.has("subtitle") { - self.titles.set_subtitle(text); + self.titles_or_packet(|t| t.set_subtitle(text), C::SetSubtitleText(sent)); "show.subtitle" } else if ctx.has("actionbar") { - self.titles.set_overlay(text, false); + self.titles_or_packet(|t| t.set_overlay(text, false), C::SetActionBarText(sent)); "show.actionbar" } else { - self.titles.set_title(text); + self.titles_or_packet(|t| t.set_title(text), C::SetTitleText(sent)); "show.title" } }; @@ -191,4 +207,13 @@ impl Game<'_> { self.feedback(message); Ok(n as i32) } + /// The player the title is for: the guest acting now is sent the packet + /// (TitleCommand's `connection.send`), the window's own player shows it. + fn titles_or_packet(&mut self, local: impl FnOnce(&mut Titles), packet: C) { + if self.lan_acting.is_some() { + self.guest_packets.push(packet); + } else { + local(&mut self.titles); + } + } } diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 68b29a360..9c0f0ec88 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -203,6 +203,9 @@ impl Game<'_> { self.lan_acting_uuid = Some(g.uuid); let result = f(self); + for packet in std::mem::take(&mut self.guest_packets) { + host.hub.send_to(conn, &packet); + } self.lan_acting = None; self.lan_acting_name = None; self.lan_acting_uuid = None; diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 991bc42b2..ef7fad329 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -40,13 +40,15 @@ use horizon_protocol::{ text::{Language, format_java, plain}, }; use horizon_world::{nbt::Tag, registry::BlockStateId}; -use std::{ - collections::HashMap, - sync::Arc, - time::{Duration, Instant}, -}; +use std::{collections::HashMap, sync::Arc, time::Instant}; use winit::{event::MouseButton, keyboard::KeyCode}; +/// A text component the server sent, for the HUD. +fn remote_text(tag: &Tag) -> horizon_client::text::Text { + use horizon_client::scoreboard::{comp_text, tag_to_json}; + comp_text(&tag_to_json(tag)) +} + /// Streaming needs a generator; a server world never generates. struct NoTerrain; impl ChunkGenerator for NoTerrain { @@ -120,7 +122,6 @@ pub(super) struct Remote { pub chat: Vec<(String, Instant)>, /// The server's command tree (ClientboundCommandsPacket). pub commands: Option, - action_bar: Option<(String, Instant)>, players: HashMap, /// `ClientPacketListener.playerInfoMap` as player-list entries, and /// which of them are listed. @@ -306,7 +307,6 @@ impl Game<'_> { game_mode: 0, chat: Vec::new(), commands: None, - action_bar: None, players: HashMap::new(), tab_players: HashMap::new(), skin_textures: HashMap::new(), @@ -1659,10 +1659,9 @@ impl Game<'_> { .map(|r| r.lang.clone()) .unwrap_or_default(), ); + // ChatListener.handleOverlay. if p.overlay { - if let Some(r) = self.remote.as_mut() { - r.action_bar = Some((text, Instant::now())); - } + self.titles.set_overlay(remote_text(&p.content), false); } else { self.chat_line(text); } @@ -1893,15 +1892,16 @@ impl Game<'_> { self.stats.values.entry(ty).or_default().insert(key, count); } } - C::SetActionBarText(p) | C::SetTitleText(p) => { - let lang = self - .remote - .as_ref() - .map(|r| r.lang.clone()) - .unwrap_or_default(); - let text = plain(&p.text, &lang); - if let Some(r) = self.remote.as_mut() { - r.action_bar = Some((text, Instant::now())); + // ClientPacketListener.setActionBarText, setTitleText, + // setSubtitleText, setTitlesAnimation and handleTitlesClear. + C::SetActionBarText(p) => self.titles.set_overlay(remote_text(&p.text), false), + C::SetTitleText(p) => self.titles.set_title(remote_text(&p.text)), + C::SetSubtitleText(p) => self.titles.set_subtitle(remote_text(&p.text)), + C::SetTitlesAnimation(p) => self.titles.set_times(p.fade_in, p.stay, p.fade_out), + C::ClearTitles(p) => { + self.titles.clear(); + if p.value { + self.titles.reset_times(); } } _ => {} @@ -2252,6 +2252,8 @@ impl Game<'_> { self.tick_client_effects(); // Player.tick's sleep counter (the darkening and the wake-up fade). self.sleep_timer = super::tick_sleep_timer(self.sleep_timer, self.sleeping.is_some()); + // Gui.tick: the titles, the action bar and the held item's name. + self.tick_hud(); let ghast_collisions = self.happy_ghast_player_collisions(); let pushed = self.remote_pushes(); let Some(player) = self.player.as_mut() else { @@ -3507,12 +3509,6 @@ impl Game<'_> { batch.text(atlas, text, 4.0, y, rgba(255, 255, 255, a), true); y -= 9.0; } - if let Some((text, at)) = &r.action_bar - && now.duration_since(*at) < Duration::from_secs(3) - { - let tw = HudBatch::text_width(atlas, text); - batch.text(atlas, text, (w - tw) / 2.0, h - 68.0, u32::MAX, true); - } if self.screen == Screen::Dead && let Some(message) = &r.death_message { diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index b1e7ec098..1532d1cf5 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -74,7 +74,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'effects', 'portals'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'titles', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'effects', 'portals'] class Game: @@ -775,6 +775,22 @@ def check_recipes(host, guest, out): return {'known': known, 'grid': grid} +def check_titles(host, guest, out): + # TitleCommand sends the guest its title packets; its HUD shows and clears them. + for command in ('title @s times 10 400 10', 'title @s subtitle "Under"', 'title @s title "Over"', + 'title @s actionbar "Bar"'): + guest('command', command=command) + guest.wait(5) + shown = {k: guest.raw('messages', 1)[k] for k in ('title', 'subtitle', 'actionbar')} + guest.shot(out, 'titles') + guest('command', command='title @s clear') + guest.wait(5) + cleared = {k: guest.raw('messages', 1)[k] for k in ('title', 'subtitle')} + assert shown == {'title': 'Over', 'subtitle': 'Under', 'actionbar': 'Bar'}, f'titles shown: {shown}' + assert cleared == {'title': None, 'subtitle': None}, f'titles cleared: {cleared}' + return {'shown': shown, 'cleared': cleared} + + def png_rows(path): """The rows of an 8-bit RGB or RGBA PNG, unfiltered, as bytes of 4 (or 3) per pixel.""" data = Path(path).read_bytes() @@ -1047,7 +1063,8 @@ def check_xp(host, guest, out): CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, 'chest': check_chest, 'crafting': check_crafting, 'sign': check_sign, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, - 'containers': check_containers, 'feedback': check_feedback, 'commands': check_commands, + 'containers': check_containers, 'feedback': check_feedback, 'titles': check_titles, + 'commands': check_commands, 'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls, 'effects': check_effects, 'portals': check_portals, 'clicks': check_clicks, 'sleep': check_sleep, 'push': check_push, 'eat': check_eat,