From 222b40c8bdcb9d760e387fd432347fdc5c319aa8 Mon Sep 17 00:00:00 2001 From: Alexander Medvedev <71594357+Snowiiii@users.noreply.github.com> Date: Mon, 19 Aug 2024 21:14:48 +0100 Subject: [PATCH] Add World struct We also check now if world exits --- pumpkin-world/src/block/block_registry.rs | 2 +- pumpkin-world/src/chunk.rs | 2 +- pumpkin-world/src/dimension.rs | 2 +- pumpkin-world/src/{world.rs => level.rs} | 8 +++++- pumpkin-world/src/lib.rs | 15 +++++++++- pumpkin/src/server.rs | 34 +++++++++++++++-------- 6 files changed, 46 insertions(+), 17 deletions(-) rename pumpkin-world/src/{world.rs => level.rs} (97%) diff --git a/pumpkin-world/src/block/block_registry.rs b/pumpkin-world/src/block/block_registry.rs index e44de5042..13a423dec 100644 --- a/pumpkin-world/src/block/block_registry.rs +++ b/pumpkin-world/src/block/block_registry.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use lazy_static::lazy_static; -use crate::world::WorldError; +use crate::level::WorldError; const BLOCKS_JSON: &str = include_str!("../../assets/blocks.json"); diff --git a/pumpkin-world/src/chunk.rs b/pumpkin-world/src/chunk.rs index 23dea8691..70869857e 100644 --- a/pumpkin-world/src/chunk.rs +++ b/pumpkin-world/src/chunk.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use fastnbt::LongArray; -use crate::{world::WorldError, WORLD_HEIGHT}; +use crate::{level::WorldError, WORLD_HEIGHT}; pub struct ChunkData { pub blocks: Box<[i32; 16 * 16 * WORLD_HEIGHT]>, diff --git a/pumpkin-world/src/dimension.rs b/pumpkin-world/src/dimension.rs index f89e20931..31fe399c9 100644 --- a/pumpkin-world/src/dimension.rs +++ b/pumpkin-world/src/dimension.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use crate::world::Level; +use crate::level::Level; #[derive(Clone, Copy, PartialEq, Eq, Hash)] pub enum Dimension { diff --git a/pumpkin-world/src/world.rs b/pumpkin-world/src/level.rs similarity index 97% rename from pumpkin-world/src/world.rs rename to pumpkin-world/src/level.rs index 2d98fc95b..96dbfb5ad 100644 --- a/pumpkin-world/src/world.rs +++ b/pumpkin-world/src/level.rs @@ -13,6 +13,7 @@ use tokio::sync::mpsc; use crate::chunk::ChunkData; #[allow(dead_code)] +/// The Level represents a pub struct Level { root_folder: PathBuf, region_folder: PathBuf, @@ -69,8 +70,13 @@ impl Compression { impl Level { pub fn from_root_folder(root_folder: PathBuf) -> Self { - // TODO: Check if exists + assert!(root_folder.exists(), "World root folder does not exist!"); let region_folder = root_folder.join("region"); + assert!( + region_folder.exists(), + "World region folder does not exist!" + ); + Level { root_folder, region_folder, diff --git a/pumpkin-world/src/lib.rs b/pumpkin-world/src/lib.rs index 209451eb2..df3a2b289 100644 --- a/pumpkin-world/src/lib.rs +++ b/pumpkin-world/src/lib.rs @@ -1,3 +1,5 @@ +use level::Level; + pub mod chunk; pub mod dimension; pub const WORLD_HEIGHT: usize = 384; @@ -6,6 +8,17 @@ pub const DIRECT_PALETTE_BITS: u32 = 15; pub mod block; mod global_registry; pub mod item; +mod level; pub mod radial_chunk_iterator; pub mod vector3; -mod world; + +pub struct World { + pub level: Level, + // entities, players... +} + +impl World { + pub fn load(level: Level) -> Self { + Self { level } + } +} diff --git a/pumpkin/src/server.rs b/pumpkin/src/server.rs index 30624900e..4731ce0bb 100644 --- a/pumpkin/src/server.rs +++ b/pumpkin/src/server.rs @@ -3,7 +3,10 @@ use std::{ collections::HashMap, io::Cursor, rc::Rc, - sync::atomic::{AtomicI32, Ordering}, + sync::{ + atomic::{AtomicI32, Ordering}, + Arc, + }, time::Duration, }; @@ -25,12 +28,12 @@ use pumpkin_protocol::{ uuid::UUID, ClientPacket, Players, Sample, StatusResponse, VarInt, Version, CURRENT_MC_PROTOCOL, }; -use pumpkin_world::{dimension::Dimension, radial_chunk_iterator::RadialIterator}; +use pumpkin_world::{dimension::Dimension, radial_chunk_iterator::RadialIterator, World}; use pumpkin_registry::Registry; use rsa::{traits::PublicKeyParts, RsaPrivateKey, RsaPublicKey}; use serde::{Deserialize, Serialize}; -use tokio::sync::mpsc; +use tokio::sync::{mpsc, Mutex}; use crate::{ client::Client, @@ -47,7 +50,7 @@ pub struct Server { pub private_key: RsaPrivateKey, pub public_key_der: Box<[u8]>, - // pub world: World, + pub world: Arc>, pub status_response: StatusResponse, // We cache the json response here so we don't parse it every time someone makes a Status request. // Keep in mind that we must parse this again, when the StatusResponse changes which usally happen when a player joins or leaves @@ -97,11 +100,17 @@ impl Server { None }; + log::debug!("Pumpkin does currently not have World or Chunk generation, Using ../world folder with vanilla pregenerated chunks"); + let world = World::load(Dimension::OverWorld.into_level( + // TODO: load form config + "./world".parse().unwrap(), + )); + Self { cached_registry: Registry::get_static(), // 0 is invalid entity_id: 2.into(), - // world: World::load(""), + world: Arc::new(Mutex::new(world)), compression_threshold: None, // 256 public_key, cached_server_brand, @@ -288,7 +297,8 @@ impl Server { ) } - Server::spawn_test_chunk(client, self.base_config.view_distance as u32).await; + self.spawn_test_chunk(client, self.base_config.view_distance as u32) + .await; } /// TODO: This definitly should be in world @@ -337,15 +347,15 @@ impl Server { } // TODO: do this in a world - async fn spawn_test_chunk(client: &mut Client, distance: u32) { + async fn spawn_test_chunk(&self, client: &mut Client, distance: u32) { let inst = std::time::Instant::now(); let (sender, mut chunk_receiver) = mpsc::channel(distance as usize); + let world = self.world.clone(); tokio::spawn(async move { - let level = Dimension::OverWorld.into_level( - // TODO: load form config - "./world".parse().unwrap(), - ); - level + world + .lock() + .await + .level .read_chunks(RadialIterator::new(distance).collect(), sender) .await; });