azalea_protocol/packets/game/
c_player_chat.rs

1use std::io::{Cursor, Write};
2
3use azalea_buf::{AzBuf, AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError};
4use azalea_chat::{
5    translatable_component::{StringOrComponent, TranslatableComponent},
6    FormattedText,
7};
8use azalea_core::bitset::BitSet;
9use azalea_crypto::MessageSignature;
10use azalea_protocol_macros::ClientboundGamePacket;
11use azalea_registry::{ChatType, OptionalRegistry};
12use uuid::Uuid;
13
14#[derive(Clone, Debug, AzBuf, ClientboundGamePacket, PartialEq)]
15pub struct ClientboundPlayerChat {
16    pub sender: Uuid,
17    #[var]
18    pub index: u32,
19    pub signature: Option<MessageSignature>,
20    pub body: PackedSignedMessageBody,
21    pub unsigned_content: Option<FormattedText>,
22    pub filter_mask: FilterMask,
23    pub chat_type: ChatTypeBound,
24}
25
26#[derive(Clone, Debug, PartialEq, AzBuf)]
27pub struct PackedSignedMessageBody {
28    // the error is here, for some reason it skipped a byte earlier and here
29    // it's reading `0` when it should be `11`
30    pub content: String,
31    pub timestamp: u64,
32    pub salt: u64,
33    pub last_seen: PackedLastSeenMessages,
34}
35
36#[derive(Clone, Debug, PartialEq, AzBuf)]
37pub struct PackedLastSeenMessages {
38    pub entries: Vec<PackedMessageSignature>,
39}
40
41/// Messages can be deleted by either their signature or message id.
42#[derive(Clone, Debug, PartialEq)]
43pub enum PackedMessageSignature {
44    Signature(Box<MessageSignature>),
45    Id(u32),
46}
47
48#[derive(Clone, Debug, PartialEq, AzBuf)]
49pub enum FilterMask {
50    PassThrough,
51    FullyFiltered,
52    PartiallyFiltered(BitSet),
53}
54
55#[derive(Clone, Debug, PartialEq)]
56pub struct ChatTypeBound {
57    pub chat_type: ChatType,
58    pub name: FormattedText,
59    pub target_name: Option<FormattedText>,
60}
61impl AzaleaRead for ChatTypeBound {
62    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
63        let Some(chat_type) = OptionalRegistry::<ChatType>::azalea_read(buf)?.0 else {
64            return Err(BufReadError::Custom("ChatType cannot be None".to_owned()));
65        };
66        let name = FormattedText::azalea_read(buf)?;
67        let target_name = Option::<FormattedText>::azalea_read(buf)?;
68
69        Ok(ChatTypeBound {
70            chat_type,
71            name,
72            target_name,
73        })
74    }
75}
76impl AzaleaWrite for ChatTypeBound {
77    fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
78        OptionalRegistry(Some(self.chat_type)).azalea_write(buf)?;
79        self.name.azalea_write(buf)?;
80        self.target_name.azalea_write(buf)?;
81        Ok(())
82    }
83}
84
85// must be in Client
86#[derive(Clone, Debug, PartialEq)]
87pub struct MessageSignatureCache {
88    pub entries: Vec<Option<MessageSignature>>,
89}
90
91impl ClientboundPlayerChat {
92    /// Returns the content of the message. If you want to get the FormattedText
93    /// for the whole message including the sender part, use
94    /// [`ClientboundPlayerChat::message`].
95    #[must_use]
96    pub fn content(&self) -> FormattedText {
97        self.unsigned_content
98            .clone()
99            .unwrap_or_else(|| FormattedText::from(self.body.content.clone()))
100    }
101
102    /// Get the full message, including the sender part.
103    #[must_use]
104    pub fn message(&self) -> FormattedText {
105        let sender = self.chat_type.name.clone();
106        let content = self.content();
107        let target = self.chat_type.target_name.clone();
108
109        let translation_key = self.chat_type.chat_type.chat_translation_key();
110
111        let mut args = vec![
112            StringOrComponent::FormattedText(sender),
113            StringOrComponent::FormattedText(content),
114        ];
115        if let Some(target) = target {
116            args.push(StringOrComponent::FormattedText(target));
117        }
118
119        let component = TranslatableComponent::new(translation_key.to_string(), args);
120
121        FormattedText::Translatable(component)
122    }
123}
124
125impl AzaleaRead for PackedMessageSignature {
126    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
127        let id = u32::azalea_read_var(buf)?;
128        if id == 0 {
129            let full_signature = MessageSignature::azalea_read(buf)?;
130
131            Ok(PackedMessageSignature::Signature(Box::new(full_signature)))
132        } else {
133            Ok(PackedMessageSignature::Id(id - 1))
134        }
135    }
136}
137impl AzaleaWrite for PackedMessageSignature {
138    fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
139        match self {
140            PackedMessageSignature::Signature(full_signature) => {
141                0u32.azalea_write_var(buf)?;
142                full_signature.azalea_write(buf)?;
143            }
144            PackedMessageSignature::Id(id) => {
145                (id + 1).azalea_write_var(buf)?;
146            }
147        }
148        Ok(())
149    }
150}