Fix: blocking

This commit is contained in:
Snowiiii 2024-10-19 13:53:41 +02:00
parent a9f645488d
commit 387da2c8b5
4 changed files with 32 additions and 25 deletions

View File

@ -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))
}

View File

@ -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;

View File

@ -240,7 +240,12 @@ impl World {
player_chunker::player_join(self, player.clone()).await;
}
async fn spawn_world_chunks(&self, client: &Client, chunks: Vec<Vector2<i32>>, distance: i32) {
async fn spawn_world_chunks(
&self,
client: Arc<Client>,
chunks: Vec<Vector2<i32>>,
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

View File

@ -55,7 +55,7 @@ pub async fn player_join(world: &World, player: Arc<Player>) {
);
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;
}
}