From b95e69be8f4fcf3c1069739cf29f66cfe2a74d6b Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 21 Oct 2022 21:44:39 -0500 Subject: add function that gets full message content --- .../packets/game/clientbound_player_chat_packet.rs | 61 ++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs index e961828e..3f75e74b 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs @@ -1,5 +1,8 @@ use azalea_buf::McBuf; -use azalea_chat::component::Component; +use azalea_chat::{ + component::Component, + translatable_component::{StringOrComponent, TranslatableComponent}, +}; use azalea_core::BitSet; use azalea_crypto::{MessageSignature, SignedMessageHeader}; use azalea_protocol_macros::ClientboundGamePacket; @@ -47,7 +50,10 @@ pub struct SignedMessageBody { } impl PlayerChatMessage { - pub fn message(&self, only_secure_chat: bool) -> Component { + /// Returns the content of the message. If you want to get the Component + /// for the whole message including the sender part, use + /// [`ClientboundPlayerChatPacket::message`]. + pub fn content(&self, only_secure_chat: bool) -> Component { if only_secure_chat { return self .signed_body @@ -58,7 +64,56 @@ impl PlayerChatMessage { } self.unsigned_content .clone() - .unwrap_or_else(|| self.message(true)) + .unwrap_or_else(|| self.content(true)) + } +} + +impl ClientboundPlayerChatPacket { + /// Get the full message, including the sender part. + pub fn message(&self, only_secure_chat: bool) -> Component { + let sender = self.chat_type.name.clone(); + let content = self.message.content(only_secure_chat); + let target = self.chat_type.target_name.clone(); + + let translation_key = self.chat_type.chat_type.chat_translation_key(); + + let mut args = vec![ + StringOrComponent::Component(sender), + StringOrComponent::Component(content), + ]; + if let Some(target) = target { + args.push(StringOrComponent::Component(target)); + } + + let component = TranslatableComponent::new(translation_key.to_string(), args); + + Component::Translatable(component) + } +} + +impl ChatType { + pub fn chat_translation_key(&self) -> &'static str { + match self { + ChatType::Chat => "chat.type.text", + ChatType::SayCommand => "chat.type.announcement", + ChatType::MsgCommandIncoming => "commands.message.display.incoming", + ChatType::MsgCommandOutgoing => "commands.message.display.outgoing", + ChatType::TeamMsgCommandIncoming => "chat.type.team.text", + ChatType::TeamMsgCommandOutgoing => "chat.type.team.sent", + ChatType::EmoteCommand => "chat.type.emote", + } + } + + pub fn narrator_translation_key(&self) -> &'static str { + match self { + ChatType::Chat => "chat.type.text.narrate", + ChatType::SayCommand => "chat.type.text.narrate", + ChatType::MsgCommandIncoming => "chat.type.text.narrate", + ChatType::MsgCommandOutgoing => "chat.type.text.narrate", + ChatType::TeamMsgCommandIncoming => "chat.type.text.narrate", + ChatType::TeamMsgCommandOutgoing => "chat.type.text.narrate", + ChatType::EmoteCommand => "chat.type.emote", + } } } -- cgit v1.2.3