Use the team-formatted player name in chat

Chat, /say, /me and /tell now name the sender the way
Player.getDisplayName does: the team prefix, the name and the suffix
in the team colour. On a server the hub gets the scoreboard's teams
each tick; in-frame the existing player_display_name is used.
This commit is contained in:
Kacpherek
2026-10-25 15:27:51 +01:00
parent e28b453762
commit 3843a9a4aa
5 changed files with 34 additions and 11 deletions
+1 -1
View File
@@ -586,7 +586,7 @@ impl Game<'_> {
} else {
let line = Text::translate(
"chat.type.text",
&[Text::literal(PLAYER_NAME), Text::literal(message)],
&[self.player_display_name(), Text::literal(message)],
);
let focused = self.screen == Screen::Chat;
let heard = self.mods.running().then(|| line.plain());
+4 -4
View File
@@ -214,7 +214,7 @@ impl Game<'_> {
}
/// Player.getDisplayName: the name (team-formatted), suggesting a
/// `/tell`, showing the player on hover, inserting the name.
fn player_display_name(&self) -> Text {
pub(super) fn player_display_name(&self) -> Text {
let name = self.acting_player_name().to_owned();
let Some(uuid) = self.lan_acting_uuid else {
return self.target_name(Target::Player);
@@ -2809,7 +2809,7 @@ impl Game<'_> {
let name = match self.command_source.map(|s| s.entity) {
Some(Some(t)) => self.target_name(t),
Some(None) => Text::literal("Server"),
None => Text::literal(self.acting_player_name()),
None => self.player_display_name(),
};
let line = match id {
"say" => Text::translate("chat.type.announcement", &[name, message]),
@@ -2819,14 +2819,14 @@ impl Game<'_> {
// Whispering to yourself: the incoming and outgoing lines.
let out = Text::translate(
"commands.message.display.outgoing",
&[Text::literal(self.acting_player_name()), message.clone()],
&[self.player_display_name(), message.clone()],
)
.with_color(text::GRAY)
.italic();
self.system_message(out);
Text::translate(
"commands.message.display.incoming",
&[Text::literal(self.acting_player_name()), message],
&[self.player_display_name(), message],
)
.with_color(text::GRAY)
.italic()
@@ -332,6 +332,7 @@ impl Game<'_> {
return;
}
self.load_scoreboard();
host.hub.teams.clone_from(&self.score.board.teams);
let view = horizon_client::lan::scoreboard::View::of(&self.score.board);
for g in host.hub.guests.iter_mut().filter(|g| g.placed) {
if g.scoreboard != view {
+22
View File
@@ -149,6 +149,9 @@ pub struct Hub {
pub singleplayer_owner: Option<u128>,
/// Who rides what, set by the game before each tick.
pub riding: super::entity::Riding,
/// The scoreboard's teams, set by the game each tick, for players'
/// display names.
pub teams: Vec<crate::scoreboard::Team>,
last_clock: i64,
last_rate: f32,
ticking: (f32, bool),
@@ -206,6 +209,24 @@ pub fn display_name(uuid: u128, name: &str) -> Tag {
])
}
impl Hub {
/// Player.getDisplayName: the name formatted for the player's team
/// (PlayerTeam.formatNameForTeam), suggesting `/tell NAME `, showing the
/// player on hover and inserting the name.
pub fn player_display_name(&self, uuid: u128, name: &str) -> Tag {
let Some(team) = self.teams.iter().find(|t| t.players.contains(name)) else {
return display_name(uuid, name);
};
let mut c = team.formatted_name_comp(&crate::scoreboard::literal(name));
c["click_event"] =
serde_json::json!({"action": "suggest_command", "command": format!("/tell {name} ")});
c["hover_event"] = serde_json::json!({"action": "show_entity", "id": "minecraft:player",
"uuid": horizon_protocol::crypto::uuid_string(uuid), "name": name});
c["insertion"] = name.into();
crate::scoreboard::component_tag(&c)
}
}
/// A yellow translation of player names (`multiplayer.player.joined`).
fn yellow_names(key: &str, names: Vec<Tag>) -> Tag {
Tag::compound([
@@ -315,6 +336,7 @@ impl Hub {
entity_types: Default::default(),
entities: Default::default(),
entity_out: Vec::new(),
teams: Vec::new(),
}
}
+6 -6
View File
@@ -2,7 +2,7 @@
//! handle it: chat sessions validated against the services' keys, signed
//! messages checked link by link, the acknowledged window, and messages
//! relayed with each connection's numbering and signature cache.
use super::{HostEvent, Hub, display_name};
use super::{HostEvent, Hub};
use horizon_protocol::{
packets::play::{self, Clientbound as C},
secure_chat::{self, Body, Chain, ChatSession, DecodeError, ProfileKey, Signature, Unpacked},
@@ -214,7 +214,7 @@ impl Hub {
}
let bound = play::ChatTypeBound {
chat_type: self.chat_type,
name: display_name(uuid, &name),
name: self.player_display_name(uuid, &name),
target: None,
};
let line = format!("<{name}> {}", message.content);
@@ -427,7 +427,7 @@ impl Hub {
let (uuid, name) = (self.guests[i].uuid, self.guests[i].name.clone());
let bound = play::ChatTypeBound {
chat_type: self.chat_type_id(chat_type),
name: display_name(uuid, &name),
name: self.player_display_name(uuid, &name),
target: None,
};
let line = match chat_type {
@@ -467,15 +467,15 @@ impl Hub {
self.chat_type_id("msg_command_incoming"),
);
for &t in targets {
let target = display_name(self.guests[t].uuid, &self.guests[t].name);
let target = self.player_display_name(self.guests[t].uuid, &self.guests[t].name);
let out = play::ChatTypeBound {
chat_type: outgoing,
name: display_name(uuid, &name),
name: self.player_display_name(uuid, &name),
target: Some(target),
};
let inc = play::ChatTypeBound {
chat_type: incoming,
name: display_name(uuid, &name),
name: self.player_display_name(uuid, &name),
target: None,
};
match &message {