Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworking block properties #529

Merged
merged 8 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pumpkin-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pumpkin-data = { path = "../pumpkin-data" }
serde.workspace = true
serde_json.workspace = true

heck = "0.5"
proc-macro2 = "1.0"
quote = "1.0"
syn = "2.0"
69 changes: 69 additions & 0 deletions pumpkin-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use heck::ToSnakeCase;
use proc_macro::TokenStream;
use pumpkin_data::item::Item;
use quote::quote;
Expand Down Expand Up @@ -238,6 +239,74 @@ pub fn pumpkin_item(input: TokenStream, item: TokenStream) -> TokenStream {
gen.into()
}

#[proc_macro_attribute]
pub fn block_property(input: TokenStream, item: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(item.clone()).unwrap();
let name = &ast.ident;
let (impl_generics, ty_generics, _) = ast.generics.split_for_impl();

let input_string = input.to_string();
let input_parts: Vec<&str> = input_string.split("[").collect();
let property_name = input_parts[0].trim_ascii().trim_matches(&['"', ','][..]);
let mut property_values: Vec<&str> = Vec::new();
if input_parts.len() > 1 {
property_values = input_parts[1]
.trim_matches(']')
.split(", ")
.map(|p| p.trim_ascii().trim_matches(&['"', ','][..]))
.collect::<Vec<&str>>();
}

let item: proc_macro2::TokenStream = item.into();

let variants = match ast.data {
syn::Data::Enum(enum_item) => enum_item.variants.into_iter().map(|v| v.ident),
_ => panic!("Block properties can only be enums"),
};

let values = variants.clone().enumerate().map(|(i, v)| {
let mut value = v.to_string().to_snake_case();
if !property_values.is_empty() && i < property_values.len() {
value = property_values[i].to_string();
}
quote! {
Self::#v => #value.to_string(),
}
});

let from_values = variants.clone().enumerate().map(|(i, v)| {
let mut value = v.to_string().to_lowercase();
if !property_values.is_empty() && i < property_values.len() {
value = property_values[i].to_string();
}
quote! {
#value => Self::#v,
}
});

let gen = quote! {
#item
impl #impl_generics crate::block::properties::BlockPropertyMetadata for #name #ty_generics {
fn name(&self) -> &'static str {
#property_name
}
fn value(&self) -> String {
match self {
#(#values)*
}
}
fn from_value(value: String) -> Self {
match value.as_str() {
#(#from_values)*
_ => panic!("Invalid value for block property"),
}
}
}
};

gen.into()
}

mod block_state;
#[proc_macro]
pub fn block_state(item: TokenStream) -> TokenStream {
Expand Down
16 changes: 16 additions & 0 deletions pumpkin-util/src/math/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ where
}
}

impl<T: Math + Copy> Vector3<T>
where
T: Into<f64>,
{
pub fn to_i32(&self) -> Vector3<i32> {
let x: f64 = self.x.into();
let y: f64 = self.y.into();
let z: f64 = self.z.into();
Vector3 {
x: x.round() as i32,
y: y.round() as i32,
z: z.round() as i32,
}
}
}

pub trait Math:
Mul<Output = Self>
//+ Neg<Output = Self>
Expand Down
60 changes: 60 additions & 0 deletions pumpkin/src/block/blocks/lever.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::entity::player::Player;
use async_trait::async_trait;
use pumpkin_data::item::Item;
use pumpkin_macros::pumpkin_block;
use pumpkin_protocol::server::play::SUseItemOn;
use pumpkin_util::math::position::BlockPos;
use pumpkin_world::block::{registry::Block, BlockDirection};

use crate::{
block::{properties::Direction, pumpkin_block::PumpkinBlock, registry::BlockActionResult},
server::Server,
world::World,
};

#[pumpkin_block("minecraft:lever")]
pub struct LeverBlock;

#[async_trait]
impl PumpkinBlock for LeverBlock {
async fn on_place(
&self,
server: &Server,
world: &World,
block: &Block,
face: &BlockDirection,
block_pos: &BlockPos,
use_item_on: &SUseItemOn,
player_direction: &Direction,
other: bool,
) -> u16 {
let face = match face {
BlockDirection::Bottom | BlockDirection::Top => *face,
_ => face.opposite(),
};

server
.block_properties_manager
.on_place_state(
world,
block,
&face,
block_pos,
use_item_on,
player_direction,
other,
)
.await
}

async fn use_with_item(
&self,
_block: &Block,
_player: &Player,
_location: BlockPos,
_item: &Item,
_server: &Server,
) -> BlockActionResult {
BlockActionResult::Consume
}
}
1 change: 1 addition & 0 deletions pumpkin/src/block/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) mod chest;
pub(crate) mod crafting_table;
pub(crate) mod furnace;
pub(crate) mod jukebox;
pub(crate) mod lever;

/// The standard destroy with container removes the player forcibly from the container,
/// drops items to the floor, and back to the player's inventory if the item stack is in movement.
Expand Down
43 changes: 40 additions & 3 deletions pumpkin/src/block/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
use blocks::chest::ChestBlock;
use blocks::furnace::FurnaceBlock;
use properties::BlockPropertiesManager;
use blocks::{chest::ChestBlock, furnace::FurnaceBlock, lever::LeverBlock};
use properties::{
age::Age,
attachment::Attachment,
axis::Axis,
cardinal::{Down, East, North, South, Up, West},
face::Face,
facing::Facing,
half::Half,
layers::Layers,
open::Open,
powered::Powered,
signal_fire::SignalFire,
slab_type::SlabType,
stair_shape::StairShape,
waterlog::Waterlogged,
BlockPropertiesManager,
};
use pumpkin_data::entity::EntityType;
use pumpkin_data::item::Item;
use pumpkin_util::math::position::BlockPos;
Expand Down Expand Up @@ -30,6 +45,7 @@ pub fn default_registry() -> Arc<BlockRegistry> {
manager.register(CraftingTableBlock);
manager.register(FurnaceBlock);
manager.register(ChestBlock);
manager.register(LeverBlock);

Arc::new(manager)
}
Expand All @@ -56,6 +72,27 @@ pub async fn drop_loot(server: &Server, world: &Arc<World>, block: &Block, pos:
pub fn default_block_properties_manager() -> Arc<BlockPropertiesManager> {
let mut manager = BlockPropertiesManager::default();

// This is the default state of the blocks
manager.register(Age::Age0);
manager.register(Attachment::Floor);
manager.register(Axis::Y);
manager.register(Down::False);
manager.register(East::False);
manager.register(Face::Floor);
manager.register(Facing::North);
manager.register(Half::Bottom);
manager.register(Layers::Lay1);
manager.register(North::False);
manager.register(Open::False);
manager.register(Powered::False);
manager.register(SignalFire::False);
manager.register(SlabType::Bottom);
manager.register(South::False);
manager.register(StairShape::Straight);
manager.register(Up::False);
manager.register(Waterlogged::False);
manager.register(West::False);

manager.build_properties_registry();

Arc::new(manager)
Expand Down
Loading