azalea_protocol/packets/game/
c_player_chat.rs1use std::{
2 io::{self, Cursor, Write},
3 sync::LazyLock,
4};
5
6use azalea_buf::{AzBuf, AzBufVar, BufReadError};
7use azalea_chat::{
8 FormattedText,
9 component::translatable::{PrimitiveOrComponent, TranslatableComponent},
10};
11use azalea_core::{bitset::JavaBitSet, identifier::Identifier};
12use azalea_crypto::signing::MessageSignature;
13use azalea_protocol_macros::ClientboundGamePacket;
14use azalea_registry::{
15 Holder,
16 data::ChatKind,
17 registry_holder::{RegistryHolder, RegistryType},
18};
19use simdnbt::owned::NbtCompound;
20use uuid::Uuid;
21
22#[derive(AzBuf, ClientboundGamePacket, Clone, Debug, PartialEq)]
23pub struct ClientboundPlayerChat {
24 #[var]
25 pub global_index: u32,
26 pub sender: Uuid,
27 #[var]
28 pub index: u32,
29 pub signature: Option<MessageSignature>,
30 pub body: PackedSignedMessageBody,
31 pub unsigned_content: Option<FormattedText>,
32 pub filter_mask: FilterMask,
33 pub chat_type: ChatTypeBound,
34}
35
36#[derive(AzBuf, Clone, Debug, PartialEq)]
37pub struct PackedSignedMessageBody {
38 pub content: String,
41 pub timestamp: u64,
42 pub salt: u64,
43 pub last_seen: PackedLastSeenMessages,
44}
45
46#[derive(AzBuf, Clone, Debug, PartialEq)]
47pub struct PackedLastSeenMessages {
48 pub entries: Vec<PackedMessageSignature>,
49}
50
51#[derive(Clone, Debug, PartialEq)]
53pub enum PackedMessageSignature {
54 Signature(Box<MessageSignature>),
55 Id(u32),
56}
57
58#[derive(AzBuf, Clone, Debug, PartialEq)]
59pub enum FilterMask {
60 PassThrough,
61 FullyFiltered,
62 PartiallyFiltered(JavaBitSet),
63}
64
65#[derive(AzBuf, Clone, Debug, PartialEq)]
66pub struct ChatTypeBound {
67 pub chat_type: Holder<ChatKind, DirectChatType>,
68 pub name: FormattedText,
69 pub target_name: Option<FormattedText>,
70}
71
72#[derive(AzBuf, Clone, Debug, PartialEq)]
73pub struct DirectChatType {
74 pub chat: ChatTypeDecoration,
75 pub narration: ChatTypeDecoration,
76}
77#[derive(AzBuf, Clone, Debug, PartialEq)]
78pub struct ChatTypeDecoration {
79 pub translation_key: String,
80 pub parameters: Vec<ChatTypeDecorationParameter>,
81 pub style: NbtCompound,
82}
83
84#[derive(AzBuf, Clone, Copy, Debug, PartialEq)]
85pub enum ChatTypeDecorationParameter {
86 Sender = 0,
87 Target = 1,
88 Content = 2,
89}
90
91#[derive(Clone, Debug, PartialEq)]
93pub struct MessageSignatureCache {
94 pub entries: Vec<Option<MessageSignature>>,
95}
96
97pub static GUESSED_DEFAULT_REGISTRIES_FOR_CHAT: LazyLock<RegistryHolder> =
103 LazyLock::new(|| RegistryHolder {
104 extra: [(
105 Identifier::new("chat_type"),
106 RegistryType {
107 map: ChatKind::ALL
108 .iter()
109 .map(|k| (Identifier::from(k.clone()), NbtCompound::new()))
110 .collect(),
111 },
112 )]
113 .into_iter()
114 .collect(),
115 ..Default::default()
116 });
117
118impl ClientboundPlayerChat {
119 #[must_use]
124 pub fn content(&self) -> FormattedText {
125 self.unsigned_content
126 .clone()
127 .unwrap_or_else(|| FormattedText::from(self.body.content.clone()))
128 }
129
130 #[must_use]
132 pub fn message(&self) -> FormattedText {
133 let sender = self.chat_type.name.clone();
134 let content = self.content();
135 let target = self.chat_type.target_name.clone();
136
137 let mut args = vec![
138 PrimitiveOrComponent::FormattedText(sender),
139 PrimitiveOrComponent::FormattedText(content),
140 ];
141 if let Some(target) = target {
142 args.push(PrimitiveOrComponent::FormattedText(target));
143 }
144
145 let translation_key = self.chat_type.translation_key();
147 let component = TranslatableComponent::new(translation_key.to_owned(), args);
148
149 FormattedText::Translatable(component)
150 }
151}
152
153impl ChatTypeBound {
154 pub fn translation_key(&self) -> &str {
155 match &self.chat_type {
156 Holder::Reference(r) => r.chat_translation_key(),
157 Holder::Direct(d) => d.chat.translation_key.as_str(),
158 }
159 }
160}
161
162impl AzBuf for PackedMessageSignature {
163 fn azalea_read(ctx: azalea_buf::Ctx, buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
164 let id = u32::azalea_read_var(ctx, buf)?;
165 if id == 0 {
166 let full_signature = MessageSignature::azalea_read(ctx, buf)?;
167
168 Ok(PackedMessageSignature::Signature(Box::new(full_signature)))
169 } else {
170 Ok(PackedMessageSignature::Id(id - 1))
171 }
172 }
173 fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
174 match self {
175 PackedMessageSignature::Signature(full_signature) => {
176 0u32.azalea_write_var(ctx, buf)?;
177 full_signature.azalea_write(ctx, buf)?;
178 }
179 PackedMessageSignature::Id(id) => {
180 (id + 1).azalea_write_var(ctx, buf)?;
181 }
182 }
183 Ok(())
184 }
185}