1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00
azalea/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs
mat 7857a014b9
1.20.3 (#110)
* 23w40a

* 23w41a

* 23w42a

* 23w43a

* 23w44a

* serialize FormattedText as nbt in network

* use azalea-nbt/serde in azalea-chat

* 23w45a

* fix 23w45a to compile

* handle Object in codegen

* 1.20.3-pre2

* remove unused clientbound_resource_pack_packet.rs

* merge main and make azalea-chat use simdnbt

* 1.20.3-rc1

* fix tests

* use simdnbt 0.3

* fix ServerboundSetJigsawBlockPacket

* 1.20.3
2023-12-05 10:59:05 -06:00

171 lines
5.1 KiB
Rust

use azalea_buf::{
BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable,
};
use azalea_chat::{
translatable_component::{StringOrComponent, TranslatableComponent},
FormattedText,
};
use azalea_core::bitset::BitSet;
use azalea_crypto::MessageSignature;
use azalea_protocol_macros::ClientboundGamePacket;
use std::io::{Cursor, Write};
use uuid::Uuid;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket, PartialEq)]
pub struct ClientboundPlayerChatPacket {
pub sender: Uuid,
#[var]
pub index: u32,
pub signature: Option<MessageSignature>,
pub body: PackedSignedMessageBody,
pub unsigned_content: Option<FormattedText>,
pub filter_mask: FilterMask,
pub chat_type: ChatTypeBound,
}
#[derive(Clone, Debug, PartialEq, McBuf)]
pub struct PackedSignedMessageBody {
// the error is here, for some reason it skipped a byte earlier and here
// it's reading `0` when it should be `11`
pub content: String,
pub timestamp: u64,
pub salt: u64,
pub last_seen: PackedLastSeenMessages,
}
#[derive(Clone, Debug, PartialEq, McBuf)]
pub struct PackedLastSeenMessages {
pub entries: Vec<PackedMessageSignature>,
}
/// Messages can be deleted by either their signature or message id.
#[derive(Clone, Debug, PartialEq)]
pub enum PackedMessageSignature {
Signature(Box<MessageSignature>),
Id(u32),
}
#[derive(Clone, Debug, PartialEq, McBuf)]
pub enum FilterMask {
PassThrough,
FullyFiltered,
PartiallyFiltered(BitSet),
}
#[derive(Copy, Clone, Debug, McBuf, PartialEq, Eq)]
pub enum ChatType {
Chat = 0,
SayCommand = 1,
MsgCommandIncoming = 2,
MsgCommandOutgoing = 3,
TeamMsgCommandIncoming = 4,
TeamMsgCommandOutgoing = 5,
EmoteCommand = 6,
}
#[derive(Clone, Debug, McBuf, PartialEq)]
pub struct ChatTypeBound {
pub chat_type: ChatType,
pub name: FormattedText,
pub target_name: Option<FormattedText>,
}
// must be in Client
#[derive(Clone, Debug, PartialEq)]
pub struct MessageSignatureCache {
pub entries: Vec<Option<MessageSignature>>,
}
// impl MessageSignatureCache {
// pub fn unpacker(&self) -> impl Fn(u32) -> Option<SignedMessageBody> {
// }
// }
// impl PackedSignedMessageBody {
// pub fn unpack(&self, unpacker: impl Fn(u32) -> Option<SignedMessageBody>)
// {} }
impl ClientboundPlayerChatPacket {
/// Returns the content of the message. If you want to get the FormattedText
/// for the whole message including the sender part, use
/// [`ClientboundPlayerChatPacket::message`].
#[must_use]
pub fn content(&self) -> FormattedText {
self.unsigned_content
.clone()
.unwrap_or_else(|| FormattedText::from(self.body.content.clone()))
}
/// Get the full message, including the sender part.
#[must_use]
pub fn message(&self) -> FormattedText {
let sender = self.chat_type.name.clone();
let content = self.content();
let target = self.chat_type.target_name.clone();
let translation_key = self.chat_type.chat_type.chat_translation_key();
let mut args = vec![
StringOrComponent::FormattedText(sender),
StringOrComponent::FormattedText(content),
];
if let Some(target) = target {
args.push(StringOrComponent::FormattedText(target));
}
let component = TranslatableComponent::new(translation_key.to_string(), args);
FormattedText::Translatable(component)
}
}
impl ChatType {
#[must_use]
pub fn chat_translation_key(&self) -> &'static str {
match self {
ChatType::Chat => "chat.type.text",
ChatType::SayCommand => "chat.type.announcement",
ChatType::MsgCommandIncoming => "commands.message.display.incoming",
ChatType::MsgCommandOutgoing => "commands.message.display.outgoing",
ChatType::TeamMsgCommandIncoming => "chat.type.team.text",
ChatType::TeamMsgCommandOutgoing => "chat.type.team.sent",
ChatType::EmoteCommand => "chat.type.emote",
}
}
#[must_use]
pub fn narrator_translation_key(&self) -> &'static str {
match self {
ChatType::EmoteCommand => "chat.type.emote",
_ => "chat.type.text.narrate",
}
}
}
impl McBufReadable for PackedMessageSignature {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let id = u32::var_read_from(buf)?;
if id == 0 {
let full_signature = MessageSignature::read_from(buf)?;
Ok(PackedMessageSignature::Signature(Box::new(full_signature)))
} else {
Ok(PackedMessageSignature::Id(id - 1))
}
}
}
impl McBufWritable for PackedMessageSignature {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
PackedMessageSignature::Signature(full_signature) => {
0u32.var_write_into(buf)?;
full_signature.write_into(buf)?;
}
PackedMessageSignature::Id(id) => {
(id + 1).var_write_into(buf)?;
}
}
Ok(())
}
}