From a6fbc3d13a1add521216faeda16d19c6b5d5d299 Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Tue, 20 Aug 2024 13:19:50 +0200 Subject: [PATCH] Fix: Serverbound Chat Message --- pumpkin-protocol/src/lib.rs | 30 +++++++------------ pumpkin-protocol/src/packet_encoder.rs | 2 ++ .../src/server/play/s_chat_message.rs | 5 ++-- pumpkin/src/client/player_packet.rs | 2 +- pumpkin/src/server.rs | 9 ++---- 5 files changed, 19 insertions(+), 29 deletions(-) diff --git a/pumpkin-protocol/src/lib.rs b/pumpkin-protocol/src/lib.rs index fda17b544..96abdc31f 100644 --- a/pumpkin-protocol/src/lib.rs +++ b/pumpkin-protocol/src/lib.rs @@ -55,26 +55,16 @@ impl VarInt { } pub fn encode(&self, mut w: impl Write) -> Result<(), io::Error> { - let x = self.0 as u64; - let stage1 = (x & 0x000000000000007f) - | ((x & 0x0000000000003f80) << 1) - | ((x & 0x00000000001fc000) << 2) - | ((x & 0x000000000fe00000) << 3) - | ((x & 0x00000000f0000000) << 4); - - let leading = stage1.leading_zeros(); - - let unused_bytes = (leading - 1) >> 3; - let bytes_needed = 8 - unused_bytes; - - // set all but the last MSBs - let msbs = 0x8080808080808080; - let msbmask = 0xffffffffffffffff >> (((8 - bytes_needed + 1) << 3) - 1); - - let merged = stage1 | (msbs & msbmask); - let bytes = merged.to_le_bytes(); - - w.write_all(unsafe { bytes.get_unchecked(..bytes_needed as usize) })?; + let mut x = self.0 as u64; + loop { + let byte = (x & 0x7F) as u8; + x >>= 7; + if x == 0 { + w.write_all(&[byte])?; + break; + } + w.write_all(&[byte | 0x80])?; + } Ok(()) } diff --git a/pumpkin-protocol/src/packet_encoder.rs b/pumpkin-protocol/src/packet_encoder.rs index 1c545804b..511d095d2 100644 --- a/pumpkin-protocol/src/packet_encoder.rs +++ b/pumpkin-protocol/src/packet_encoder.rs @@ -22,6 +22,8 @@ pub struct PacketEncoder { } impl PacketEncoder { + // i think clippy is broken + #[allow(clippy::needless_borrows_for_generic_args)] pub fn append_packet(&mut self, packet: &P) -> Result<(), PacketError> { let start_len = self.buf.len(); diff --git a/pumpkin-protocol/src/server/play/s_chat_message.rs b/pumpkin-protocol/src/server/play/s_chat_message.rs index bcd7dd91a..c0a989648 100644 --- a/pumpkin-protocol/src/server/play/s_chat_message.rs +++ b/pumpkin-protocol/src/server/play/s_chat_message.rs @@ -1,3 +1,4 @@ +use bytes::Bytes; use pumpkin_macros::packet; use crate::{ @@ -11,7 +12,7 @@ pub struct SChatMessage { pub message: String, pub timestamp: i64, pub salt: i64, - pub signature: Option>, + pub signature: Option, pub messagee_count: VarInt, // acknowledged: BitSet, } @@ -23,7 +24,7 @@ impl ServerPacket for SChatMessage { message: bytebuf.get_string().unwrap(), timestamp: bytebuf.get_i64(), salt: bytebuf.get_i64(), - signature: bytebuf.get_option(|v| v.get_slice().to_vec()), + signature: bytebuf.get_option(|v| v.copy_to_bytes(255)), messagee_count: bytebuf.get_var_int(), }) } diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index 66482ee0b..05e0bccd1 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -218,7 +218,7 @@ impl Client { self, &CPlayerChatMessage::new( pumpkin_protocol::uuid::UUID(gameprofile.id), - chat_message.messagee_count, + 1.into(), chat_message.signature.as_deref(), &message, chat_message.timestamp, diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index 4731ce0bb..db1e45022 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -44,8 +44,6 @@ use crate::{ pub const CURRENT_MC_VERSION: &str = "1.21.1"; pub struct Server { - pub compression_threshold: Option, - pub public_key: RsaPublicKey, pub private_key: RsaPrivateKey, pub public_key_der: Box<[u8]>, @@ -81,7 +79,7 @@ impl Server { let cached_server_brand = Self::build_brand(); // TODO: only create when needed - dbg!("creating keys"); + log::debug!("Creating encryption keys..."); let (public_key, private_key) = Self::generate_keys(); let public_key_der = rsa_der::public_key_to_der( @@ -100,7 +98,7 @@ impl Server { None }; - log::debug!("Pumpkin does currently not have World or Chunk generation, Using ../world folder with vanilla pregenerated chunks"); + log::warn!("Pumpkin does currently not have World or Chunk generation, Using ../world folder with vanilla pregenerated chunks"); let world = World::load(Dimension::OverWorld.into_level( // TODO: load form config "./world".parse().unwrap(), @@ -111,7 +109,6 @@ impl Server { // 0 is invalid entity_id: 2.into(), world: Arc::new(Mutex::new(world)), - compression_threshold: None, // 256 public_key, cached_server_brand, private_key, @@ -249,7 +246,7 @@ impl Server { &CSpawnEntity::new( entity_id.into(), UUID(gameprofile.id), - EntityType::Player.to_i32().unwrap().into(), + (EntityType::Player as i32).into(), x, y, z,