diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2025-12-12 00:56:02 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-12-12 00:56:02 -0600 |
| commit | f9c25665c203d6377ace62f1e95381d037d8fd9e (patch) | |
| tree | 8b4131d20fe661d3cc1175ec27f801fe61df41ea /azalea-protocol/src/packets/game/c_player_chat.rs | |
| parent | 82ad975242292d5875780b4398b62637674bf50a (diff) | |
| download | azalea-drasl-f9c25665c203d6377ace62f1e95381d037d8fd9e.tar.xz | |
Refactor azalea-registry (#294)
* move registries in azalea-registry into separate modules
* rename Item and Block to ItemKind and BlockKind
* remove 'extra' registries from azalea-registry
* hide deprecated items from docs
* use DamageKindKey instead of Identifier when parsing registries
* store tag entries as a Vec instead of a HashSet
* sort tag values by protocol id
* update changelog
Diffstat (limited to 'azalea-protocol/src/packets/game/c_player_chat.rs')
| -rw-r--r-- | azalea-protocol/src/packets/game/c_player_chat.rs | 64 |
1 files changed, 57 insertions, 7 deletions
diff --git a/azalea-protocol/src/packets/game/c_player_chat.rs b/azalea-protocol/src/packets/game/c_player_chat.rs index f3201e3f..3904d0d9 100644 --- a/azalea-protocol/src/packets/game/c_player_chat.rs +++ b/azalea-protocol/src/packets/game/c_player_chat.rs @@ -1,14 +1,25 @@ -use std::io::{self, Cursor, Write}; +use std::{ + io::{self, Cursor, Write}, + sync::LazyLock, +}; use azalea_buf::{AzBuf, AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError}; use azalea_chat::{ FormattedText, translatable_component::{PrimitiveOrComponent, TranslatableComponent}, }; -use azalea_core::bitset::BitSet; +use azalea_core::{ + bitset::BitSet, + data_registry::DataRegistryWithKey, + registry_holder::{RegistryHolder, RegistryType}, +}; use azalea_crypto::signing::MessageSignature; use azalea_protocol_macros::ClientboundGamePacket; -use azalea_registry::Holder; +use azalea_registry::{ + DataRegistryKey, Holder, + data::{ChatKind, ChatKindKey}, + identifier::Identifier, +}; use simdnbt::owned::NbtCompound; use uuid::Uuid; @@ -57,7 +68,7 @@ pub enum FilterMask { #[derive(Clone, Debug, PartialEq, AzBuf)] pub struct ChatTypeBound { - pub chat_type: Holder<azalea_registry::ChatType, DirectChatType>, + pub chat_type: Holder<ChatKind, DirectChatType>, pub name: FormattedText, pub target_name: Option<FormattedText>, } @@ -87,6 +98,27 @@ pub struct MessageSignatureCache { pub entries: Vec<Option<MessageSignature>>, } +/// A `RegistryHolder` that only has the `chat_type` registry (without values), +/// with the keys being in the default order for vanilla servers. +/// +/// This is used when we call [`ClientboundPlayerChat::message`] without also +/// passing registries. +pub static GUESSED_DEFAULT_REGISTRIES_FOR_CHAT: LazyLock<RegistryHolder> = + LazyLock::new(|| RegistryHolder { + extra: [( + Identifier::new("chat_type"), + RegistryType { + map: ChatKindKey::ALL + .iter() + .map(|k| (k.clone().into_ident(), NbtCompound::new())) + .collect(), + }, + )] + .into_iter() + .collect(), + ..Default::default() + }); + impl ClientboundPlayerChat { /// Returns the content of the message. /// @@ -100,8 +132,22 @@ impl ClientboundPlayerChat { } /// Get the full message, including the sender part. + /// + /// Note that the returned message may be incorrect on servers that + /// customize the chat type registry. Consider using + /// [`Self::message_using_registries`] if you'd like to avoid that + /// problem. #[must_use] pub fn message(&self) -> FormattedText { + self.message_using_registries(&GUESSED_DEFAULT_REGISTRIES_FOR_CHAT) + } + + /// Get the full message, including the sender part, while ensuring that the + /// message chat type is correct based on the server's registries. + /// + /// Also see [`Self::message`]. + #[must_use] + pub fn message_using_registries(&self, registries: &RegistryHolder) -> FormattedText { let sender = self.chat_type.name.clone(); let content = self.content(); let target = self.chat_type.target_name.clone(); @@ -114,7 +160,8 @@ impl ClientboundPlayerChat { args.push(PrimitiveOrComponent::FormattedText(target)); } - let translation_key = self.chat_type.translation_key(); + // TODO: implement chat type registry and apply the styles from it here + let translation_key = self.chat_type.translation_key(registries); let component = TranslatableComponent::new(translation_key.to_string(), args); FormattedText::Translatable(component) @@ -122,9 +169,12 @@ impl ClientboundPlayerChat { } impl ChatTypeBound { - pub fn translation_key(&self) -> &str { + pub fn translation_key(&self, registries: &RegistryHolder) -> &str { match &self.chat_type { - Holder::Reference(r) => r.chat_translation_key(), + Holder::Reference(r) => r + .key(registries) + .map(|r| r.chat_translation_key()) + .unwrap_or("chat.type.text"), Holder::Direct(d) => d.chat.translation_key.as_str(), } } |
