diff --git a/pumpkin-config/src/chunk_optimization.rs b/pumpkin-config/src/chunk_optimization.rs index 5a8d70d3..b548cff6 100644 --- a/pumpkin-config/src/chunk_optimization.rs +++ b/pumpkin-config/src/chunk_optimization.rs @@ -6,17 +6,11 @@ use serde::{Deserialize, Serialize}; #[derive(Deserialize, Serialize)] #[serde(default)] +#[derive(Default)] pub struct ChunkOptimizationConfig { pub rle_compression: Option, } -impl Default for ChunkOptimizationConfig { - fn default() -> Self { - Self { - rle_compression: Some(Default::default()), - } - } -} #[derive(Deserialize, Serialize)] #[serde(default)] diff --git a/pumpkin-world/src/chunk/mod.rs b/pumpkin-world/src/chunk/mod.rs index dd302956..61c83b95 100644 --- a/pumpkin-world/src/chunk/mod.rs +++ b/pumpkin-world/src/chunk/mod.rs @@ -236,38 +236,35 @@ impl Subchunk { Self::Rle(blocks) => { blocks.set(convert_index(position), new_block); - if blocks.iter().all(|b| *b == new_block) { - *self = Self::Single(new_block) - } else if !compressed { - *self = Self::Multi(blocks.to_vec().try_into().unwrap()) + if compressed { + self.compress(); } } Self::Multi(blocks) => { blocks[convert_index(position)] = new_block; - if blocks.iter().all(|b| *b == new_block) { - *self = Self::Single(new_block) - } else if compressed && ADVANCED_CONFIG.chunk_optimization.rle_compression.is_some() - { - *self = Self::Rle(RleVec::from_iter(blocks.into_iter())) + if compressed { + self.compress(); } } } } #[allow(clippy::useless_conversion)] - pub fn optimize(&mut self) { + pub fn compress(&mut self) { match self { Self::Multi(blocks) => { if blocks.iter().all(|b| b == blocks.first().unwrap()) { *self = Self::Single(*blocks.first().unwrap()) } else if ADVANCED_CONFIG.chunk_optimization.rle_compression.is_some() { + log::info!("converting to rle"); *self = Self::Rle(RleVec::from_iter(blocks.into_iter())) } } Self::Rle(blocks) => { let mut runs = blocks.runs(); let first_run = runs.next().unwrap(); + log::info!("trying convert rle to single"); if runs.all(|r| r == first_run) { *self = Self::Single(*first_run.value) @@ -344,10 +341,11 @@ impl Subchunks { } } - pub fn optimize(&mut self) { + pub fn compress(&mut self) { if let Self::Multi(subchunks) = self { + log::info!("optimize started"); for subchunk in subchunks.iter_mut() { - subchunk.optimize(); + subchunk.compress(); } } } @@ -397,8 +395,8 @@ impl ChunkData { .set_block_no_heightmap_update(position, block, compressed); } - pub fn optimize(&mut self) { - self.subchunks.optimize(); + pub fn compress(&mut self) { + self.subchunks.compress(); } #[expect(dead_code)] diff --git a/pumpkin/src/world/mod.rs b/pumpkin/src/world/mod.rs index d5338e1b..1a0757f5 100644 --- a/pumpkin/src/world/mod.rs +++ b/pumpkin/src/world/mod.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, sync::Arc}; +use std::{collections::HashMap, sync::Arc, time::Duration}; pub mod level_time; pub mod player_chunker; @@ -597,7 +597,14 @@ impl World { } if ADVANCED_CONFIG.chunk_optimization.rle_compression.is_some() { - chunk_data_ref.write().await.optimize(); + let chunk_ref = chunk_data_ref.clone(); + + tokio::spawn(async move { + tokio::time::sleep(Duration::from_millis(100)).await; + + log::info!("trying optimize and not get deadlock"); + chunk_ref.write().await.compress(); + }); } }