From da64a57ad380f98d7e8cc4d7b92e666e9f3117ad Mon Sep 17 00:00:00 2001 From: Andrew Brower Date: Mon, 11 Nov 2024 18:11:57 -0500 Subject: [PATCH] fix: properly transcode world palettes (goodbye modern art :() --- src/net/io.rs | 10 +--------- src/net/mod.rs | 3 +++ src/protocol/packets/play/world.rs | 20 ++++++++++++-------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/net/io.rs b/src/net/io.rs index 4016aef..6099737 100644 --- a/src/net/io.rs +++ b/src/net/io.rs @@ -119,15 +119,7 @@ impl NetIo { trace!("Sending packet {:?}", packet); self.encoder.append_packet(packet)?; let bytes = self.encoder.take(); - trace!( - "sending raw bytes: {:?}", - bytes - .to_vec() - .iter() - .map(|b| b.to_string()) - .collect::>() - .join(" ") - ); + trace!("raw packet is {} bytes", bytes.len()); Ok(self.stream.write_all(&bytes).await?) } diff --git a/src/net/mod.rs b/src/net/mod.rs index 5ef1b54..d149028 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -28,6 +28,9 @@ use crate::CrawlState; #[cfg(feature = "lan")] pub async fn spawn_lan_broadcast(state: CrawlState) -> Result<()> { + use std::time::Duration; + use tokio::{net::UdpSocket, time}; + let port = state.port; let sock = UdpSocket::bind("0.0.0.0:9753").await?; diff --git a/src/protocol/packets/play/world.rs b/src/protocol/packets/play/world.rs index 07ff64b..363125f 100644 --- a/src/protocol/packets/play/world.rs +++ b/src/protocol/packets/play/world.rs @@ -192,18 +192,22 @@ impl Encode for ChunkDataUpdateLightC<'_> { impl ChunkSection { fn anvil_to_sec(value: &world::Section) -> Self { let mut blocks: [i16; 16 * 16 * 16] = [0; 16 * 16 * 16]; - let bit_length = 64 - (value.block_states.palette.len() as u64).leading_zeros(); - let bit_length = bit_length.max(4); + let bit_length = (64 + - (value.block_states.palette.len() as u64) + .leading_zeros()) + .max(4); + let blocks_per_long = 64 / bit_length; + + let bit_mask = (1 << bit_length) - 1; match value.block_states.data { None => blocks.fill(0), Some(ref data) => { let mut i = 0; for long in data.iter() { - let mut long = *long as u64; - while long != 0 { - blocks[i] = (long & ((1 << bit_length) - 1)) as i16; - long >>= bit_length; + let long = *long as u64; + for b in 0..blocks_per_long { + blocks[i] = ((long >> (bit_length * b)) & bit_mask) as i16; i += 1; } } @@ -233,13 +237,13 @@ impl ChunkSection { let mut blocks_so_far = 0; let mut long_index = 0; - for (index, block) in blocks.iter().enumerate() { + for block in blocks { if blocks_so_far == blocks_per_long { blocks_so_far = 0; long_index += 1; } - let block = *block as i64; + let block = block as i64; data[long_index] |= block << (blocks_so_far * bit_length); blocks_so_far += 1; -- 2.51.2