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 tokio::sync::mpsc;
use crate::{ use crate::{
chunk::{anvil::AnvilChunkReader, ChunkData, ChunkReader, ChunkReadingError}, chunk::{anvil::AnvilChunkReader, ChunkData, ChunkParsingError, ChunkReader, ChunkReadingError},
world_gen::{get_world_gen, Seed, WorldGenerator}, world_gen::{get_world_gen, Seed, WorldGenerator},
}; };
@ -96,7 +96,7 @@ impl Level {
let data = match &self.save_file { let data = match &self.save_file {
Some(save_file) => { Some(save_file) => {
match self.chunk_reader.read_chunk(save_file, at) { 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. // This chunk was not generated yet.
Ok(self.world_gen.generate_chunk(at)) 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<()> { async fn main() -> io::Result<()> {
use std::sync::Arc; use std::sync::Arc;
use entity::player::Player;
use pumpkin_config::{ADVANCED_CONFIG, BASIC_CONFIG}; use pumpkin_config::{ADVANCED_CONFIG, BASIC_CONFIG};
use pumpkin_core::text::{color::NamedColor, TextComponent}; use pumpkin_core::text::{color::NamedColor, TextComponent};
use rcon::RCONServer; use rcon::RCONServer;

View File

@ -240,7 +240,12 @@ impl World {
player_chunker::player_join(self, player.clone()).await; 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 inst = std::time::Instant::now();
let (sender, mut chunk_receiver) = mpsc::channel(distance as usize); let (sender, mut chunk_receiver) = mpsc::channel(distance as usize);
@ -252,26 +257,29 @@ impl World {
level.fetch_chunks(&chunks, sender, closed) level.fetch_chunks(&chunks, sender, closed)
}); });
while let Some(chunk_data) = chunk_receiver.recv().await { let client = client.clone();
// dbg!(chunk_pos); tokio::spawn(async move {
#[cfg(debug_assertions)] while let Some(chunk_data) = chunk_receiver.recv().await {
if chunk_data.position == (0, 0).into() { // dbg!(chunk_pos);
use pumpkin_protocol::bytebuf::ByteBuffer; #[cfg(debug_assertions)]
let mut test = ByteBuffer::empty(); if chunk_data.position == (0, 0).into() {
CChunkData(&chunk_data).write(&mut test); use pumpkin_protocol::bytebuf::ByteBuffer;
let len = test.buf().len(); let mut test = ByteBuffer::empty();
log::debug!( CChunkData(&chunk_data).write(&mut test);
"Chunk packet size: {}B {}KB {}MB", let len = test.buf().len();
len, log::debug!(
len / 1024, "Chunk packet size: {}B {}KB {}MB",
len / (1024 * 1024) 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) { dbg!("DONE CHUNKS", inst.elapsed());
client.send_packet(&CChunkData(&chunk_data)).await; });
}
}
dbg!("DONE CHUNKS", inst.elapsed());
} }
/// Gets a Player by entity id /// 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() { if !loading_chunks.is_empty() {
world world
.spawn_world_chunks(&player.client, loading_chunks, view_distance) .spawn_world_chunks(player.client.clone(), loading_chunks, view_distance)
.await; .await;
} }
} }
@ -98,7 +98,7 @@ pub async fn update_position(entity: &Entity, player: &Player) {
if !loading_chunks.is_empty() { if !loading_chunks.is_empty() {
entity entity
.world .world
.spawn_world_chunks(&player.client, loading_chunks, view_distance) .spawn_world_chunks(player.client.clone(), loading_chunks, view_distance)
.await; .await;
} }
} }