Skip to content

Commit

Permalink
add some nice helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryntet committed Oct 23, 2024
1 parent 28eae10 commit c4cbb7f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pumpkin-core/src/math/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>);

Expand Down
9 changes: 9 additions & 0 deletions pumpkin-core/src/math/vector2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ pub struct Vector2<T> {
pub z: T,
}

impl<T: Math + Default> Default for Vector2<T> {
fn default() -> Self {
Self {
x: T::default(),
z: T::default(),
}
}
}

impl<T: Math + Copy> Vector2<T> {
pub fn new(x: T, z: T) -> Self {
Vector2 { x, z }
Expand Down
10 changes: 10 additions & 0 deletions pumpkin-core/src/math/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ pub struct Vector3<T> {
pub z: T,
}

impl<T: Math + Copy + Default> Default for Vector3<T> {
fn default() -> Self {
Vector3 {
x: T::default(),
y: T::default(),
z: T::default(),
}
}
}

impl<T: Math + Copy> Vector3<T> {
pub const fn new(x: T, y: T, z: T) -> Self {
Vector3 { x, y, z }
Expand Down
13 changes: 11 additions & 2 deletions pumpkin-world/src/block/block_registry.rs
Original file line number Diff line number Diff line change
@@ -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<HashMap<String, RegistryBlockType>> = LazyLock::new(|| {
serde_json::from_str(include_str!("../../../assets/blocks.json"))
Expand Down Expand Up @@ -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<BlockState> for BlockId {
Expand Down
1 change: 0 additions & 1 deletion pumpkin-world/src/item/item_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit c4cbb7f

Please sign in to comment.