diff --git a/pumpkin-world/src/level.rs b/pumpkin-world/src/level.rs index c210232ce..866fd9759 100644 --- a/pumpkin-world/src/level.rs +++ b/pumpkin-world/src/level.rs @@ -6,7 +6,7 @@ use rayon::prelude::*; use tokio::sync::mpsc; use crate::{ - chunk::{anvil::AnvilChunkReader, ChunkData, ChunkReader, ChunkReadingError}, + chunk::{anvil::AnvilChunkReader, ChunkData, ChunkParsingError, ChunkReader, ChunkReadingError}, world_gen::{get_world_gen, Seed, WorldGenerator}, }; @@ -96,7 +96,7 @@ impl Level { let data = match &self.save_file { Some(save_file) => { match self.chunk_reader.read_chunk(save_file, at) { - Err(ChunkReadingError::ChunkNotExist) => { + Err(ChunkReadingError::ParsingError(ChunkParsingError::ChunkNotGenerated) | ChunkReadingError::ChunkNotExist) => { // This chunk was not generated yet. Ok(self.world_gen.generate_chunk(at)) } diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index 19de17dbb..f2034c664 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -83,7 +83,6 @@ const fn convert_logger_filter(level: pumpkin_config::logging::LevelFilter) -> L async fn main() -> io::Result<()> { use std::sync::Arc; - use entity::player::Player; use pumpkin_config::{ADVANCED_CONFIG, BASIC_CONFIG}; use pumpkin_core::text::{color::NamedColor, TextComponent}; use rcon::RCONServer; diff --git a/pumpkin/src/world/mod.rs b/pumpkin/src/world/mod.rs index 5bf7e7062..fbec77e6a 100644 --- a/pumpkin/src/world/mod.rs +++ b/pumpkin/src/world/mod.rs @@ -240,7 +240,12 @@ impl World { player_chunker::player_join(self, player.clone()).await; } - async fn spawn_world_chunks(&self, client: &Client, chunks: Vec>, distance: i32) { + async fn spawn_world_chunks( + &self, + client: Arc, + chunks: Vec>, + distance: i32, + ) { let inst = std::time::Instant::now(); let (sender, mut chunk_receiver) = mpsc::channel(distance as usize); @@ -252,26 +257,29 @@ impl World { level.fetch_chunks(&chunks, sender, closed) }); - while let Some(chunk_data) = chunk_receiver.recv().await { - // dbg!(chunk_pos); - #[cfg(debug_assertions)] - if chunk_data.position == (0, 0).into() { - use pumpkin_protocol::bytebuf::ByteBuffer; - let mut test = ByteBuffer::empty(); - CChunkData(&chunk_data).write(&mut test); - let len = test.buf().len(); - log::debug!( - "Chunk packet size: {}B {}KB {}MB", - len, - len / 1024, - len / (1024 * 1024) - ); + let client = client.clone(); + tokio::spawn(async move { + while let Some(chunk_data) = chunk_receiver.recv().await { + // dbg!(chunk_pos); + #[cfg(debug_assertions)] + if chunk_data.position == (0, 0).into() { + use pumpkin_protocol::bytebuf::ByteBuffer; + let mut test = ByteBuffer::empty(); + CChunkData(&chunk_data).write(&mut test); + let len = test.buf().len(); + log::debug!( + "Chunk packet size: {}B {}KB {}MB", + len, + len / 1024, + len / (1024 * 1024) + ); + } + if !client.closed.load(std::sync::atomic::Ordering::Relaxed) { + client.send_packet(&CChunkData(&chunk_data)).await; + } } - if !client.closed.load(std::sync::atomic::Ordering::Relaxed) { - client.send_packet(&CChunkData(&chunk_data)).await; - } - } - dbg!("DONE CHUNKS", inst.elapsed()); + dbg!("DONE CHUNKS", inst.elapsed()); + }); } /// Gets a Player by entity id diff --git a/pumpkin/src/world/player_chunker.rs b/pumpkin/src/world/player_chunker.rs index 7d9fe1f4a..fc61a70c8 100644 --- a/pumpkin/src/world/player_chunker.rs +++ b/pumpkin/src/world/player_chunker.rs @@ -55,7 +55,7 @@ pub async fn player_join(world: &World, player: Arc) { ); if !loading_chunks.is_empty() { world - .spawn_world_chunks(&player.client, loading_chunks, view_distance) + .spawn_world_chunks(player.client.clone(), loading_chunks, view_distance) .await; } } @@ -98,7 +98,7 @@ pub async fn update_position(entity: &Entity, player: &Player) { if !loading_chunks.is_empty() { entity .world - .spawn_world_chunks(&player.client, loading_chunks, view_distance) + .spawn_world_chunks(player.client.clone(), loading_chunks, view_distance) .await; } }