diff options
| author | mat <github@matdoes.dev> | 2022-10-21 21:44:39 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-10-21 21:44:39 -0500 |
| commit | b95e69be8f4fcf3c1069739cf29f66cfe2a74d6b (patch) | |
| tree | ab05da83c814b6b3296a4e109c33007d412b7ebd /azalea-protocol/src/packets | |
| parent | aa1f2a55e047f528dcb78509b025548696e42543 (diff) | |
| download | azalea-drasl-b95e69be8f4fcf3c1069739cf29f66cfe2a74d6b.tar.xz | |
add function that gets full message content
Diffstat (limited to 'azalea-protocol/src/packets')
| -rw-r--r-- | azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs | 61 |
1 files changed, 58 insertions, 3 deletions
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", + } } } |
