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

add as_property_list method for Block #147

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
66 changes: 64 additions & 2 deletions azalea-block/azalea-block-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod utils;
use proc_macro::TokenStream;
use proc_macro2::TokenTree;
use quote::quote;
use std::collections::HashMap;
use std::fmt::Write;
use std::{collections::HashMap, str::FromStr};
use syn::{
braced,
ext::IdentExt,
Expand Down Expand Up @@ -273,6 +273,19 @@ struct PropertyVariantData {
pub is_enum: bool,
}

fn pascal_to_snake_case(input: &str) -> String {
let mut snake_case = String::new();

for (i, c) in input.chars().enumerate() {
if c.is_ascii_uppercase() && i > 0 {
snake_case.push('_');
}
snake_case.push(c.to_ascii_lowercase());
}

snake_case
}

#[proc_macro]
pub fn make_block_states(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as MakeBlockStates);
Expand All @@ -296,6 +309,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
} => {
let mut property_enum_variants = quote! {};
let mut property_from_number_variants = quote! {};
let mut property_to_string = quote! {};

property_value_name = enum_name.clone();
property_struct_name = enum_name.clone();
Expand Down Expand Up @@ -323,6 +337,15 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
#i_lit => #property_struct_name::#variant,
});

let struct_name = property_struct_name.to_string();

let variant_snake =
pascal_to_snake_case(struct_name.strip_prefix('_').unwrap_or(&struct_name));

property_to_string.extend(quote! {
#property_struct_name::#variant => #variant_snake,
});

property_variant_types.push(variant.to_string());
}

Expand All @@ -340,6 +363,14 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
}
}
}

impl ToString for #property_struct_name{
fn to_string(&self) -> String{
match *self{
#property_to_string
}.to_string()
}
}
});
}
PropertyType::Boolean { struct_name } => {
Expand All @@ -360,6 +391,13 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
}
}
}

impl ToString for #property_struct_name{
fn to_string(&self) -> String{
self.0.to_string()
}
}

});
}
}
Expand Down Expand Up @@ -450,6 +488,9 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
// pub has_bottle_1: HasBottle,
// pub has_bottle_2: HasBottle,
let mut block_struct_fields = quote! {};
let mut get_property_match = quote! {};
let mut build_properties_map = quote! {};

for PropertyWithNameAndDefault {
property_value_type,
name,
Expand All @@ -463,6 +504,13 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
} else {
quote! { pub #name_ident: #property_value_type, }
});

get_property_match.extend(quote! {
#name => Some(self.#name_ident.to_string()),
});

build_properties_map
.extend(quote! { map.insert(#name.to_string(), self.#name_ident.to_string()); })
}

let block_name_pascal_case = Ident::new(
Expand Down Expand Up @@ -668,6 +716,19 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
fn as_registry_block(&self) -> azalea_registry::Block {
azalea_registry::Block::#block_name_pascal_case
}
fn get_property(&self, name: &str) -> Option<String>{
match name{
#get_property_match
_ => None
}
}
fn as_property_map(&self) -> std::collections::HashMap<String, String>{
let mut map = std::collections::HashMap::new();

#build_properties_map
map
}

}

impl From<#block_struct_name> for BlockState {
Expand Down Expand Up @@ -819,5 +880,6 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
}
});

generated.into()
let out = generated.into();
out
}
5 changes: 5 additions & 0 deletions azalea-block/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use core::fmt::Debug;
pub use range::BlockStates;
use std::{
any::Any,
collections::HashMap,
io::{Cursor, Write},
};

Expand All @@ -27,6 +28,10 @@ pub trait Block: Debug + Any {
/// Convert the block to an [`azalea_registry::Block`]. This is lossy, as
/// `azalea_registry::Block` doesn't contain any state data.
fn as_registry_block(&self) -> azalea_registry::Block;

fn as_property_map(&self) -> HashMap<String, String>;

fn get_property(&self, name: &str) -> Option<String>;
}
impl dyn Block {
pub fn downcast_ref<T: Block>(&self) -> Option<&T> {
Expand Down
2 changes: 1 addition & 1 deletion azalea-buf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = "0.10.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
simdnbt = { version = "0.5", git = "https://github.com/azalea-rs/simdnbt" }
simdnbt = {git = "https://github.com/azalea-rs/simdnbt" }
azalea-buf-macros = { path = "./azalea-buf-macros", version = "0.10.0" }
byteorder = "^1.5.0"
tracing = "0.1.40"
Expand Down
5 changes: 1 addition & 4 deletions azalea-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,10 +880,7 @@ impl PluginGroup for DefaultPlugins {
.add(ChunkPlugin)
.add(ConfigurationPlugin)
.add(TickBroadcastPlugin);
#[cfg(feature = "log")]
{
group = group.add(bevy_log::LogPlugin::default());
}

group
}
}