azalea_protocol/packets/game/
c_disguised_chat.rs

1use azalea_buf::AzBuf;
2use azalea_chat::{
3    FormattedText,
4    translatable_component::{StringOrComponent, TranslatableComponent},
5};
6use azalea_protocol_macros::ClientboundGamePacket;
7
8use super::c_player_chat::ChatTypeBound;
9
10// A disguised chat packet is basically the same as a normal
11// [`ClientboundPlayerChat`], except that it doesn't have any of the chat
12// signing things. Vanilla servers use this when messages are sent from the
13// console.
14#[derive(Clone, Debug, AzBuf, ClientboundGamePacket, PartialEq)]
15pub struct ClientboundDisguisedChat {
16    pub message: FormattedText,
17    pub chat_type: ChatTypeBound,
18}
19
20impl ClientboundDisguisedChat {
21    /// Get the full message, including the sender part.
22    #[must_use]
23    pub fn message(&self) -> FormattedText {
24        let sender = self.chat_type.name.clone();
25        let content = self.message.clone();
26        let target = self.chat_type.target_name.clone();
27
28        let mut args = vec![
29            StringOrComponent::FormattedText(sender),
30            StringOrComponent::FormattedText(content),
31        ];
32        if let Some(target) = target {
33            args.push(StringOrComponent::FormattedText(target));
34        }
35
36        let translation_key = self.chat_type.translation_key();
37        let component = TranslatableComponent::new(translation_key.to_string(), args);
38
39        FormattedText::Translatable(component)
40    }
41}