From d0a8790d7bd706d4bc31efed6d1e7a8fd6d1e325 Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Wed, 7 Aug 2024 10:47:29 +0200 Subject: [PATCH] Add Clientbound Player Chat Message --- .../src/client/play/c_player_chat_message.rs | 70 +++++++++++++++++++ pumpkin-protocol/src/client/play/mod.rs | 2 + pumpkin/src/main.rs | 18 ++--- 3 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 pumpkin-protocol/src/client/play/c_player_chat_message.rs diff --git a/pumpkin-protocol/src/client/play/c_player_chat_message.rs b/pumpkin-protocol/src/client/play/c_player_chat_message.rs new file mode 100644 index 000000000..cfbdb63ca --- /dev/null +++ b/pumpkin-protocol/src/client/play/c_player_chat_message.rs @@ -0,0 +1,70 @@ +use num_derive::FromPrimitive; +use serde::Serialize; + +use crate::{text::TextComponent, VarInt}; + +#[derive(Serialize)] +pub struct PlayerChatMessage<'a> { + sender: uuid::Uuid, + index: VarInt, + message_signature: Option<&'a [u8]>, + message: String, + timestamp: i64, + salt: i64, + previous_messages: &'a [PreviousMessage<'a>], // max 20 + unsigned_content: Option, + /// See `FilterType` + filter_type: VarInt, + chat_type: VarInt, + sender_name: TextComponent, + target_name: Option, +} + +impl<'a> PlayerChatMessage<'a> { + #[allow(clippy::too_many_arguments)] + pub fn new( + sender: uuid::Uuid, + index: VarInt, + message_signature: Option<&'a [u8]>, + message: String, + timestamp: i64, + salt: i64, + previous_messages: &'a [PreviousMessage<'a>], + unsigned_content: Option, + filter_type: VarInt, + chat_type: VarInt, + sender_name: TextComponent, + target_name: Option, + ) -> Self { + Self { + sender, + index, + message_signature, + message, + timestamp, + salt, + previous_messages, + unsigned_content, + filter_type, + chat_type, + sender_name, + target_name, + } + } +} + +#[derive(Serialize)] +pub struct PreviousMessage<'a> { + message_id: VarInt, + signature: Option<&'a [u8]>, +} + +#[derive(FromPrimitive)] +pub enum FilterType { + /// Message is not filtered at all + PassThrough, + /// Message is fully filtered + FullyFiltered, + /// Only some characters in the message are filtered + PartiallyFiltered, +} diff --git a/pumpkin-protocol/src/client/play/mod.rs b/pumpkin-protocol/src/client/play/mod.rs index e807663cd..0399b656a 100644 --- a/pumpkin-protocol/src/client/play/mod.rs +++ b/pumpkin-protocol/src/client/play/mod.rs @@ -5,6 +5,7 @@ mod c_head_rot; mod c_login; mod c_play_disconnect; mod c_player_abilities; +mod c_player_chat_message; mod c_player_info_update; mod c_set_held_item; mod c_spawn_player; @@ -22,6 +23,7 @@ pub use c_head_rot::*; pub use c_login::*; pub use c_play_disconnect::*; pub use c_player_abilities::*; +pub use c_player_chat_message::*; pub use c_player_info_update::*; pub use c_set_held_item::*; pub use c_spawn_player::*; diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index 92fe79bb8..13c25ce8f 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -6,11 +6,7 @@ use client::Client; use commands::handle_command; use configuration::AdvancedConfiguration; -use std::{ - collections::HashMap, - rc::Rc, - thread, -}; +use std::{collections::HashMap, rc::Rc, thread}; use client::interrupted; use configuration::BasicConfiguration; @@ -64,10 +60,7 @@ fn main() -> io::Result<()> { let mut connections: HashMap>> = HashMap::new(); - let mut server = Server::new(( - basic_config, - advanced_configuration, - )); + let mut server = Server::new((basic_config, advanced_configuration)); log::info!("Started Server took {}ms", time.elapsed().as_millis()); log::info!("You now can connect to the server"); @@ -112,8 +105,8 @@ fn main() -> io::Result<()> { } }; if let Err(e) = connection.set_nodelay(true) { - log::warn!("failed to set TCP_NODELAY {e}"); - } + log::warn!("failed to set TCP_NODELAY {e}"); + } log::info!("Accepted connection from: {}", address); @@ -129,8 +122,7 @@ fn main() -> io::Result<()> { connection, addr, ))); - server - .add_client(rc_token, Rc::clone(&client)); + server.add_client(rc_token, Rc::clone(&client)); connections.insert(token, client); },