parse Sounds into enum at compile time

This commit is contained in:
Alexander Medvedev 2025-01-10 14:47:34 +01:00
parent c711d1511c
commit 45a591b27b
6 changed files with 17 additions and 41 deletions

View File

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

View File

@ -67,12 +67,6 @@ pub fn pumpkin_block(input: TokenStream, item: TokenStream) -> TokenStream {
gen.into()
}
mod sound;
#[proc_macro]
pub fn sound(item: TokenStream) -> TokenStream {
sound::sound_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 SOUNDS: LazyLock<HashMap<String, u16>> = LazyLock::new(|| {
serde_json::from_str::<Vec<String>>(include_str!("../../assets/sounds.json"))
.expect("Could not parse sounds.json registry.")
.into_iter()
.enumerate()
.map(|(i, s)| (s, i as u16))
.collect()
});
pub(crate) fn sound_impl(item: TokenStream) -> TokenStream {
let input_string = item.to_string();
let sound_name = input_string.trim_matches('"');
let id = *SOUNDS.get(sound_name).expect("Invalid sound");
quote! { #id }.into()
}

View File

@ -1,7 +1,7 @@
use async_trait::async_trait;
use pumpkin_data::screen::WindowType;
use pumpkin_data::{screen::WindowType, sound::Sound};
use pumpkin_inventory::{Chest, OpenContainer};
use pumpkin_macros::{pumpkin_block, sound};
use pumpkin_macros::pumpkin_block;
use pumpkin_protocol::{client::play::CBlockAction, codec::var_int::VarInt};
use pumpkin_util::math::position::WorldPosition;
use pumpkin_world::{
@ -114,12 +114,12 @@ impl ChestBlock {
if state == ChestState::IsClosed && num_players == 0 {
player
.world()
.play_block_sound(sound!("block.chest.close"), location)
.play_block_sound(Sound::BlockChestClose as u16, location)
.await;
} else if state == ChestState::IsOpened && num_players == 1 {
player
.world()
.play_block_sound(sound!("block.chest.open"), location)
.play_block_sound(Sound::BlockChestOpen as u16, location)
.await;
}

View File

@ -9,9 +9,9 @@ use std::{
use crossbeam::atomic::AtomicCell;
use pumpkin_config::{ADVANCED_CONFIG, BASIC_CONFIG};
use pumpkin_data::sound::Sound;
use pumpkin_entity::{entity_type::EntityType, EntityId};
use pumpkin_inventory::player::PlayerInventory;
use pumpkin_macros::sound;
use pumpkin_protocol::server::play::{
SCloseContainer, SCookieResponse as SPCookieResponse, SPlayPingRequest, SPlayerLoaded,
};
@ -317,7 +317,7 @@ impl Player {
{
world
.play_sound(
sound!("entity.player.attack.nodamage"),
Sound::EntityPlayerAttackNodamage as u16,
SoundCategory::Players,
&pos,
)
@ -326,7 +326,7 @@ impl Player {
}
world
.play_sound(sound!("entity.player.hurt"), SoundCategory::Players, &pos)
.play_sound(Sound::EntityPlayerHurt as u16, SoundCategory::Players, &pos)
.await;
let attack_type = AttackType::new(self, attack_cooldown_progress as f32).await;

View File

@ -1,7 +1,6 @@
use std::f32::consts::PI;
use pumpkin_data::particle::Particle;
use pumpkin_macros::sound;
use pumpkin_data::{particle::Particle, sound::Sound};
use pumpkin_protocol::{
client::play::{CEntityVelocity, CParticle},
codec::var_int::VarInt,
@ -122,7 +121,7 @@ pub async fn player_attack_sound(pos: &Vector3<f64>, world: &World, attack_type:
AttackType::Knockback => {
world
.play_sound(
sound!("entity.player.attack.knockback"),
Sound::EntityPlayerAttackKnockback as u16,
SoundCategory::Players,
pos,
)
@ -131,7 +130,7 @@ pub async fn player_attack_sound(pos: &Vector3<f64>, world: &World, attack_type:
AttackType::Critical => {
world
.play_sound(
sound!("entity.player.attack.crit"),
Sound::EntityPlayerAttackCrit as u16,
SoundCategory::Players,
pos,
)
@ -140,7 +139,7 @@ pub async fn player_attack_sound(pos: &Vector3<f64>, world: &World, attack_type:
AttackType::Sweeping => {
world
.play_sound(
sound!("entity.player.attack.sweep"),
Sound::EntityPlayerAttackSweep as u16,
SoundCategory::Players,
pos,
)
@ -149,7 +148,7 @@ pub async fn player_attack_sound(pos: &Vector3<f64>, world: &World, attack_type:
AttackType::Strong => {
world
.play_sound(
sound!("entity.player.attack.strong"),
Sound::EntityPlayerAttackStrong as u16,
SoundCategory::Players,
pos,
)
@ -158,7 +157,7 @@ pub async fn player_attack_sound(pos: &Vector3<f64>, world: &World, attack_type:
AttackType::Weak => {
world
.play_sound(
sound!("entity.player.attack.weak"),
Sound::EntityPlayerAttackWeak as u16,
SoundCategory::Players,
pos,
)