Refractored read_chunk

Added new error types to indicate a chunk should be generated.
Renamed to `fetch_chunks` to indicate that it no longer just reads chunks (might write to the worldfile in the future)
Take a reference to a slice instead of an owned vector because the data is Copy anyways.

Remove the ChunkCoordinates that are sent over the mpsc Sender.
- The coordinates are stored in ChunkData anyways.
- If there is an Error the coordinates should not matter because the error can't be recovered from.

Moved the reading from file logic to `read_chunk`.
- So that we don't always have to repeat `channel.blocking_send(...); return;`
- To make it clearly distinct from the main program logic.

Replaced some verbose match statements with `Result.map_err()`.
This commit is contained in:
DaniD3v committed 2024-08-28 12:52:26 +02:00
1 parent cdb0e6b761
commit 17b285081a
2 files changed
+114 -128

No files matched your search

+6 -8
View File
@@ -353,13 +353,10 @@ impl Server {
let inst = std::time::Instant::now();
let (sender, mut chunk_receiver) = mpsc::channel(distance as usize);
let world = self.world.clone();
let chunks: Vec<_> = RadialIterator::new(distance).collect();
tokio::spawn(async move {
world
.lock()
.await
.level
.read_chunks(RadialIterator::new(distance).collect(), sender)
.await;
world.lock().await.level.fetch_chunks(&chunks, sender).await;
});
player.client.send_packet(&CCenterChunk {
@@ -367,14 +364,15 @@ impl Server {
chunk_z: 0.into(),
});
while let Some((_chunk_pos, chunk_data)) = chunk_receiver.recv().await {
while let Some(chunk_data) = chunk_receiver.recv().await {
// dbg!(chunk_pos);
let chunk_data = match chunk_data {
Ok(d) => d,
Err(_) => continue,
};
#[cfg(debug_assertions)]
if _chunk_pos == (pumpkin_world::coordinates::ChunkCoordinates { x: 0, z: 0 }) {
if chunk_data.position == (pumpkin_world::coordinates::ChunkCoordinates { x: 0, z: 0 })
{
use pumpkin_protocol::bytebuf::ByteBuffer;
let mut test = ByteBuffer::empty();
CChunkData(&chunk_data).write(&mut test);