Skip to content

Commit

Permalink
Fix: Serverbound Chat Message
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 20, 2024
1 parent 5445975 commit a6fbc3d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 29 deletions.
30 changes: 10 additions & 20 deletions pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/packet_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: ClientPacket>(&mut self, packet: &P) -> Result<(), PacketError> {
let start_len = self.buf.len();

Expand Down
5 changes: 3 additions & 2 deletions pumpkin-protocol/src/server/play/s_chat_message.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bytes::Bytes;
use pumpkin_macros::packet;

use crate::{
Expand All @@ -11,7 +12,7 @@ pub struct SChatMessage {
pub message: String,
pub timestamp: i64,
pub salt: i64,
pub signature: Option<Vec<u8>>,
pub signature: Option<Bytes>,
pub messagee_count: VarInt,
// acknowledged: BitSet,
}
Expand All @@ -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(),
})
}
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions pumpkin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ use crate::{
pub const CURRENT_MC_VERSION: &str = "1.21.1";

pub struct Server {
pub compression_threshold: Option<u8>,

pub public_key: RsaPublicKey,
pub private_key: RsaPrivateKey,
pub public_key_der: Box<[u8]>,
Expand Down Expand Up @@ -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(
Expand All @@ -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(),
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a6fbc3d

Please sign in to comment.