diff --git a/pumpkin-world/src/block/block_registry.rs b/pumpkin-world/src/block/block_registry.rs index 12c6c7b7c..f885c11e4 100644 --- a/pumpkin-world/src/block/block_registry.rs +++ b/pumpkin-world/src/block/block_registry.rs @@ -14,6 +14,10 @@ pub fn get_block(registry_id: &str) -> Option<&Block> { .find(|&block| block.name == registry_id) } +pub fn get_block_by_id<'a>(id: u16) -> Option<&'a Block> { + BLOCKS.blocks.iter().find(|&block| block.id == id) +} + pub fn get_block_by_item<'a>(item_id: u16) -> Option<&'a Block> { BLOCKS.blocks.iter().find(|&block| block.item_id == item_id) } @@ -24,17 +28,16 @@ pub struct TopLevel { shapes: Vec, block_entity_types: Vec, } -#[expect(dead_code)] #[derive(Deserialize, Clone, Debug)] pub struct Block { pub id: u16, pub item_id: u16, - wall_variant_id: Option, - translation_key: String, + pub wall_variant_id: Option, + pub translation_key: String, pub name: String, - properties: Vec, + pub properties: Vec, pub default_state_id: u16, - states: Vec, + pub states: Vec, } #[expect(dead_code)] #[derive(Deserialize, Clone, Debug)] @@ -45,21 +48,20 @@ struct BlockEntityKind { } #[expect(dead_code)] #[derive(Deserialize, Clone, Debug)] -struct Property { +pub struct Property { name: String, values: Vec, } -#[expect(dead_code)] #[derive(Deserialize, Clone, Debug)] -struct State { - id: u16, - air: bool, - luminance: u8, - burnable: bool, - opacity: Option, - replaceable: bool, - collision_shapes: Vec, - block_entity_type: Option, +pub struct State { + pub id: u16, + pub air: bool, + pub luminance: u8, + pub burnable: bool, + pub opacity: Option, + pub replaceable: bool, + pub collision_shapes: Vec, + pub block_entity_type: Option, } #[expect(dead_code)] #[derive(Deserialize, Clone, Debug)] diff --git a/pumpkin/src/entity/player.rs b/pumpkin/src/entity/player.rs index 04af647a2..b61081fcf 100644 --- a/pumpkin/src/entity/player.rs +++ b/pumpkin/src/entity/player.rs @@ -518,15 +518,13 @@ impl Player { let world = &self.living_entity.entity.world; // teleport - let mut position = Vector3::new(10.0, 120.0, 10.0); + let x = 10.0; + let z = 10.0; + let top = world.get_top_block(Vector2::new(x as i32, z as i32)).await; + let position = Vector3::new(x, f64::from(top), z); let yaw = 10.0; let pitch = 10.0; - let top = world - .get_top_block(Vector2::new(position.x as i32, position.z as i32)) - .await; - position.y = f64::from(top + 1); - log::debug!("Sending player teleport to {}", self.gameprofile.name); self.teleport(position, yaw, pitch).await; diff --git a/pumpkin/src/world/mod.rs b/pumpkin/src/world/mod.rs index 4c8a8790a..0daa03cd4 100644 --- a/pumpkin/src/world/mod.rs +++ b/pumpkin/src/world/mod.rs @@ -25,9 +25,9 @@ use pumpkin_protocol::{ }, ClientPacket, VarInt, }; -use pumpkin_world::chunk::ChunkData; use pumpkin_world::coordinates::ChunkRelativeBlockCoordinates; use pumpkin_world::level::Level; +use pumpkin_world::{block::block_registry::get_block_by_id, chunk::ChunkData}; use rand::{thread_rng, Rng}; use scoreboard::Scoreboard; use tokio::sync::{mpsc::Receiver, Mutex}; @@ -139,8 +139,10 @@ impl World { for y in (-64..=319).rev() { let pos = WorldPosition(Vector3::new(position.x, y, position.z)); let block = self.get_block(pos).await; - if block == 0 || block == 750 || block == 751 { - continue; + if let Some(block) = block { + if block.states[0].air { + continue; + } } return y; } @@ -627,11 +629,20 @@ impl World { .await; } - pub async fn get_block(&self, position: WorldPosition) -> u16 { + pub async fn get_block_id(&self, position: WorldPosition) -> u16 { let (chunk, relative) = position.chunk_and_chunk_relative_position(); let relative = ChunkRelativeBlockCoordinates::from(relative); let chunk = self.receive_chunk(chunk).await; let chunk = chunk.read().await; chunk.blocks.get_block(relative) } + + /// Gets the Block from the Block Registry, Returns None if the Block has not been found + pub async fn get_block( + &self, + position: WorldPosition, + ) -> Option<&pumpkin_world::block::block_registry::Block> { + let block_id = self.get_block_id(position).await; + get_block_by_id(block_id) + } }