Better block air checking

This commit is contained in:
Snowiiii 2024-11-09 11:33:38 +01:00
parent 65cb0b52b4
commit 63979710b2
3 changed files with 37 additions and 26 deletions

View File

@ -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<Shape>,
block_entity_types: Vec<BlockEntityKind>,
}
#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
pub struct Block {
pub id: u16,
pub item_id: u16,
wall_variant_id: Option<u16>,
translation_key: String,
pub wall_variant_id: Option<u16>,
pub translation_key: String,
pub name: String,
properties: Vec<Property>,
pub properties: Vec<Property>,
pub default_state_id: u16,
states: Vec<State>,
pub states: Vec<State>,
}
#[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<String>,
}
#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]
struct State {
id: u16,
air: bool,
luminance: u8,
burnable: bool,
opacity: Option<u32>,
replaceable: bool,
collision_shapes: Vec<u16>,
block_entity_type: Option<u32>,
pub struct State {
pub id: u16,
pub air: bool,
pub luminance: u8,
pub burnable: bool,
pub opacity: Option<u32>,
pub replaceable: bool,
pub collision_shapes: Vec<u16>,
pub block_entity_type: Option<u32>,
}
#[expect(dead_code)]
#[derive(Deserialize, Clone, Debug)]

View File

@ -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;

View File

@ -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)
}
}