Skip to content

Commit

Permalink
Add World struct
Browse files Browse the repository at this point in the history
We also check now if world exits
  • Loading branch information
Snowiiii committed Aug 19, 2024
1 parent de7b3aa commit 222b40c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pumpkin-world/src/block/block_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
2 changes: 1 addition & 1 deletion pumpkin-world/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]>,
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-world/src/dimension.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion pumpkin-world/src/world.rs → pumpkin-world/src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 14 additions & 1 deletion pumpkin-world/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use level::Level;

pub mod chunk;
pub mod dimension;
pub const WORLD_HEIGHT: usize = 384;
Expand All @@ -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 }
}
}
34 changes: 22 additions & 12 deletions pumpkin/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{
collections::HashMap,
io::Cursor,
rc::Rc,
sync::atomic::{AtomicI32, Ordering},
sync::{
atomic::{AtomicI32, Ordering},
Arc,
},
time::Duration,
};

Expand All @@ -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,
Expand All @@ -47,7 +50,7 @@ pub struct Server {
pub private_key: RsaPrivateKey,
pub public_key_der: Box<[u8]>,

// pub world: World,
pub world: Arc<Mutex<World>>,
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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
});
Expand Down

0 comments on commit 222b40c

Please sign in to comment.