azalea_protocol/packets/game/
c_disguised_chat.rs

1use azalea_buf::AzBuf;
2use azalea_chat::{
3    FormattedText,
4    translatable_component::{PrimitiveOrComponent, TranslatableComponent},
5};
6use azalea_core::registry_holder::RegistryHolder;
7use azalea_protocol_macros::ClientboundGamePacket;
8
9use super::c_player_chat::ChatTypeBound;
10use crate::packets::game::c_player_chat::GUESSED_DEFAULT_REGISTRIES_FOR_CHAT;
11
12/// Similar to a [`ClientboundPlayerChat`](super::ClientboundPlayerChat), but
13/// without chat signing.
14///
15/// Vanilla servers use this packet when messages are sent from the console.
16#[derive(Clone, Debug, AzBuf, PartialEq, ClientboundGamePacket)]
17pub struct ClientboundDisguisedChat {
18    pub message: FormattedText,
19    pub chat_type: ChatTypeBound,
20}
21
22impl ClientboundDisguisedChat {
23    /// Get the full message, including the sender part.
24    ///
25    /// Note that the returned message may be incorrect on servers that
26    /// customize the chat type registry. Consider using
27    /// [`Self::message_using_registries`] if you'd like to avoid that
28    /// problem.
29    #[must_use]
30    pub fn message(&self) -> FormattedText {
31        self.message_using_registries(&GUESSED_DEFAULT_REGISTRIES_FOR_CHAT)
32    }
33
34    /// Get the full message, including the sender part, while ensuring that the
35    /// message chat type is correct based on the server's registries.
36    ///
37    /// Also see [`Self::message`].
38    #[must_use]
39    pub fn message_using_registries(&self, registries: &RegistryHolder) -> FormattedText {
40        let sender = self.chat_type.name.clone();
41        let content = self.message.clone();
42        let target = self.chat_type.target_name.clone();
43
44        let mut args = vec![
45            PrimitiveOrComponent::FormattedText(sender),
46            PrimitiveOrComponent::FormattedText(content),
47        ];
48        if let Some(target) = target {
49            args.push(PrimitiveOrComponent::FormattedText(target));
50        }
51
52        let translation_key = self.chat_type.translation_key(registries);
53        let component = TranslatableComponent::new(translation_key.to_owned(), args);
54
55        FormattedText::Translatable(component)
56    }
57}