mirror of
https://github.com/Pumpkin-MC/Pumpkin
synced 2025-08-12 13:53:00 +00:00
* improve tag v1 * improve tag v2 * fix typo and make BlockPredicate better * improve get state from state id * impl DataComponent * make tag pub * make component work * apply clippy * apply clippy * remove unused crate * apply to itemstack and no lifetime mark anymore * Update tag.rs * use Cow * remove unnecessary lazy_static and Cow * Update mod.rs * Update mod.rs * make DataComponent dyn * 1 * fmt * remove unused crate * fix error * fix * Update Cargo.toml * Update Cargo.lock * fix * Update composter.rs * update rust-version, remove unused crate, and merge * Update furnace.rs * Update lib.rs * Update living.rs * Update living.rs
113 lines
2.9 KiB
Rust
113 lines
2.9 KiB
Rust
use std::{collections::HashMap, fs};
|
|
|
|
use heck::ToShoutySnakeCase;
|
|
use proc_macro2::TokenStream;
|
|
use quote::{format_ident, quote};
|
|
use serde::Deserialize;
|
|
|
|
#[derive(Deserialize, Clone)]
|
|
struct Potion {
|
|
id: u8,
|
|
effects: Vec<Effect>,
|
|
}
|
|
|
|
#[derive(Deserialize, Clone)]
|
|
pub struct Effect {
|
|
effect_type: String,
|
|
duration: i32,
|
|
amplifier: u8,
|
|
ambient: bool,
|
|
show_particles: bool,
|
|
show_icon: bool,
|
|
}
|
|
|
|
impl Effect {
|
|
pub fn to_tokens(&self) -> TokenStream {
|
|
let effect_type = format_ident!(
|
|
"{}",
|
|
self.effect_type
|
|
.strip_prefix("minecraft:")
|
|
.unwrap()
|
|
.to_uppercase()
|
|
);
|
|
let duration = self.duration;
|
|
let amplifier = self.amplifier;
|
|
let ambient = self.ambient;
|
|
let show_particles = self.show_particles;
|
|
let show_icon = self.show_icon;
|
|
quote! {
|
|
Effect {
|
|
effect_type: &StatusEffect::#effect_type,
|
|
duration: #duration,
|
|
amplifier: #amplifier,
|
|
ambient: #ambient,
|
|
show_particles: #show_particles,
|
|
show_icon: #show_icon,
|
|
blend: false,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn build() -> TokenStream {
|
|
println!("cargo:rerun-if-changed=../assets/potion.json");
|
|
|
|
let potions: HashMap<String, Potion> =
|
|
serde_json::from_str(&fs::read_to_string("../assets/potion.json").unwrap())
|
|
.expect("Failed to parse potion.json");
|
|
|
|
let mut variants = TokenStream::new();
|
|
let mut name_to_type = TokenStream::new();
|
|
|
|
for (name, potion) in potions.iter() {
|
|
let format_name = format_ident!("{}", name.to_shouty_snake_case());
|
|
let id = potion.id;
|
|
let slots = potion.effects.clone();
|
|
let slots = slots.iter().map(|slot| slot.to_tokens());
|
|
|
|
variants.extend([quote! {
|
|
pub const #format_name: Self = Self {
|
|
name: #name,
|
|
id: #id,
|
|
effects: &[#(#slots),*],
|
|
};
|
|
}]);
|
|
|
|
name_to_type.extend(quote! { #name => Some(&Self::#format_name), });
|
|
}
|
|
|
|
quote! {
|
|
use std::hash::Hash;
|
|
use crate::effect::StatusEffect;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Potion {
|
|
pub id: u8,
|
|
pub name: &'static str,
|
|
pub effects: &'static [Effect],
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Effect {
|
|
pub effect_type: &'static StatusEffect,
|
|
pub duration: i32,
|
|
pub amplifier: u8,
|
|
pub ambient: bool,
|
|
pub show_particles: bool,
|
|
pub show_icon: bool,
|
|
pub blend: bool,
|
|
}
|
|
|
|
impl Potion {
|
|
#variants
|
|
|
|
pub fn from_name(name: &str) -> Option<&'static Self> {
|
|
match name {
|
|
#name_to_type
|
|
_ => None
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|