From c4cbb7f87e69149f53d18f3a53c6e830a3eb5339 Mon Sep 17 00:00:00 2001 From: Edvin Bryntesson Date: Tue, 22 Oct 2024 10:51:36 +0200 Subject: [PATCH] add some nice helper functions --- pumpkin-core/src/math/position.rs | 2 +- pumpkin-core/src/math/vector2.rs | 9 +++++++++ pumpkin-core/src/math/vector3.rs | 10 ++++++++++ pumpkin-world/src/block/block_registry.rs | 13 +++++++++++-- pumpkin-world/src/item/item_registry.rs | 1 - 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/pumpkin-core/src/math/position.rs b/pumpkin-core/src/math/position.rs index 7c51dff45..c0ac44793 100644 --- a/pumpkin-core/src/math/position.rs +++ b/pumpkin-core/src/math/position.rs @@ -5,7 +5,7 @@ use crate::math::vector2::Vector2; use num_traits::Euclid; use serde::{Deserialize, Serialize}; -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Default)] /// Aka Block Position pub struct WorldPosition(pub Vector3); diff --git a/pumpkin-core/src/math/vector2.rs b/pumpkin-core/src/math/vector2.rs index 3c3728ba5..200ba28f4 100644 --- a/pumpkin-core/src/math/vector2.rs +++ b/pumpkin-core/src/math/vector2.rs @@ -8,6 +8,15 @@ pub struct Vector2 { pub z: T, } +impl Default for Vector2 { + fn default() -> Self { + Self { + x: T::default(), + z: T::default(), + } + } +} + impl Vector2 { pub fn new(x: T, z: T) -> Self { Vector2 { x, z } diff --git a/pumpkin-core/src/math/vector3.rs b/pumpkin-core/src/math/vector3.rs index 5b1475261..79e189970 100644 --- a/pumpkin-core/src/math/vector3.rs +++ b/pumpkin-core/src/math/vector3.rs @@ -9,6 +9,16 @@ pub struct Vector3 { pub z: T, } +impl Default for Vector3 { + fn default() -> Self { + Vector3 { + x: T::default(), + y: T::default(), + z: T::default(), + } + } +} + impl Vector3 { pub const fn new(x: T, y: T, z: T) -> Self { Vector3 { x, y, z } diff --git a/pumpkin-world/src/block/block_registry.rs b/pumpkin-world/src/block/block_registry.rs index 896efd8b2..98df1a375 100644 --- a/pumpkin-world/src/block/block_registry.rs +++ b/pumpkin-world/src/block/block_registry.rs @@ -1,8 +1,8 @@ use std::{collections::HashMap, sync::LazyLock}; -use serde::Deserialize; - use super::BlockState; +use crate::item::get_item_protocol_id; +use serde::Deserialize; pub static BLOCKS: LazyLock> = LazyLock::new(|| { serde_json::from_str(include_str!("../../../assets/blocks.json")) @@ -70,6 +70,15 @@ impl BlockId { pub fn get_id(&self) -> u16 { self.data } + + pub fn get_as_item_id(&self) -> u32 { + let id = BLOCKS + .iter() + .find(|(_, val)| val.states.iter().any(|state| state.id == *self)) + .map(|(key, _)| key.as_str()) + .unwrap(); + get_item_protocol_id(id) + } } impl From for BlockId { diff --git a/pumpkin-world/src/item/item_registry.rs b/pumpkin-world/src/item/item_registry.rs index 756c30b4e..eae3275ba 100644 --- a/pumpkin-world/src/item/item_registry.rs +++ b/pumpkin-world/src/item/item_registry.rs @@ -33,7 +33,6 @@ pub fn get_item_element(item_id: &str) -> &ItemComponents { &ITEMS.get(item_id).expect("Item not found").components } -#[expect(dead_code)] pub fn get_item_protocol_id(item_id: &str) -> u32 { global_registry::get_protocol_id(ITEM_REGISTRY, item_id) }