parse Particle into enum at compile time

This commit is contained in:
Alexander Medvedev 2025-01-10 14:39:25 +01:00
parent 1208e67f36
commit c711d1511c
4 changed files with 7 additions and 29 deletions

View File

@ -5,3 +5,7 @@ pub mod packet {
pub mod screen {
include!(concat!(env!("OUT_DIR"), "/screen.rs"));
}
pub mod particle {
include!(concat!(env!("OUT_DIR"), "/particle.rs"));
}

View File

@ -73,12 +73,6 @@ pub fn sound(item: TokenStream) -> TokenStream {
sound::sound_impl(item)
}
mod particle;
#[proc_macro]
pub fn particle(item: TokenStream) -> TokenStream {
particle::particle_impl(item)
}
mod block_state;
#[proc_macro]
pub fn block_state(item: TokenStream) -> TokenStream {

View File

@ -1,21 +0,0 @@
use std::{collections::HashMap, sync::LazyLock};
use proc_macro::TokenStream;
use quote::quote;
static PARTICLES: LazyLock<HashMap<String, u16>> = LazyLock::new(|| {
serde_json::from_str::<Vec<String>>(include_str!("../../assets/particles.json"))
.expect("Could not parse particles.json registry")
.into_iter()
.enumerate()
.map(|(i, s)| (s, i as u16))
.collect()
});
pub(crate) fn particle_impl(item: TokenStream) -> TokenStream {
let input_string = item.to_string();
let particle_name = input_string.trim_matches('"');
let id = *PARTICLES.get(particle_name).expect("Invalid Particle");
quote! { #id }.into()
}

View File

@ -1,6 +1,7 @@
use std::f32::consts::PI;
use pumpkin_macros::{particle, sound};
use pumpkin_data::particle::Particle;
use pumpkin_macros::sound;
use pumpkin_protocol::{
client::play::{CEntityVelocity, CParticle},
codec::var_int::VarInt,
@ -110,7 +111,7 @@ pub async fn spawn_sweep_particle(attacker_entity: &Entity, world: &World, pos:
Vector3::new(0.0, 0.0, 0.0),
0.0,
0,
VarInt(i32::from(particle!("sweep_attack"))), // sweep
VarInt(Particle::SweepAttack as i32), // sweep
&[],
))
.await;