parse game event at compile time

This commit is contained in:
Alexander Medvedev 2025-01-11 15:54:26 +01:00
parent 624fa750e7
commit 82386a57c4
9 changed files with 52 additions and 28 deletions

1
assets/game_event.json Normal file
View File

@ -0,0 +1 @@
["block_activate","block_attach","block_change","block_close","block_deactivate","block_destroy","block_detach","block_open","block_place","container_close","container_open","drink","eat","elytra_glide","entity_damage","entity_die","entity_dismount","entity_interact","entity_mount","entity_place","entity_action","equip","explode","flap","fluid_pickup","fluid_place","hit_ground","instrument_play","item_interact_finish","item_interact_start","jukebox_play","jukebox_stop_play","lightning_strike","note_block_play","prime_fuse","projectile_land","projectile_shoot","sculk_sensor_tendrils_clicking","shear","shriek","splash","step","swim","teleport","unequip","resonate_1","resonate_2","resonate_3","resonate_4","resonate_5","resonate_6","resonate_7","resonate_8","resonate_9","resonate_10","resonate_11","resonate_12","resonate_13","resonate_14","resonate_15"]

1
assets/message_type.json Normal file
View File

@ -0,0 +1 @@
{"chat":{"data":{"chat":{"translation_key":"chat.type.text","parameters":["sender","content"]},"narration":{"translation_key":"chat.type.text.narrate","parameters":["sender","content"]}}},"emote_command":{"data":{"chat":{"translation_key":"chat.type.emote","parameters":["sender","content"]},"narration":{"translation_key":"chat.type.emote","parameters":["sender","content"]}}},"msg_command_incoming":{"data":{"chat":{"translation_key":"commands.message.display.incoming","parameters":["sender","content"],"style":{"italic":true,"color":"gray"}},"narration":{"translation_key":"chat.type.text.narrate","parameters":["sender","content"]}}},"msg_command_outgoing":{"data":{"chat":{"translation_key":"commands.message.display.outgoing","parameters":["target","content"],"style":{"italic":true,"color":"gray"}},"narration":{"translation_key":"chat.type.text.narrate","parameters":["sender","content"]}}},"say_command":{"data":{"chat":{"translation_key":"chat.type.announcement","parameters":["sender","content"]},"narration":{"translation_key":"chat.type.text.narrate","parameters":["sender","content"]}}},"team_msg_command_incoming":{"data":{"chat":{"translation_key":"chat.type.team.text","parameters":["target","sender","content"]},"narration":{"translation_key":"chat.type.text.narrate","parameters":["sender","content"]}}},"team_msg_command_outgoing":{"data":{"chat":{"translation_key":"chat.type.team.sent","parameters":["target","sender","content"]},"narration":{"translation_key":"chat.type.text.narrate","parameters":["sender","content"]}}}}

View File

@ -4,6 +4,7 @@ use proc_macro2::{Span, TokenStream};
use syn::Ident; use syn::Ident;
mod chunk_status; mod chunk_status;
mod game_event;
mod packet; mod packet;
mod particle; mod particle;
mod screen; mod screen;
@ -15,6 +16,7 @@ pub fn main() {
write_generated_file(particle::build(), "particle.rs"); write_generated_file(particle::build(), "particle.rs");
write_generated_file(sound::build(), "sound.rs"); write_generated_file(sound::build(), "sound.rs");
write_generated_file(chunk_status::build(), "chunk_status.rs"); write_generated_file(chunk_status::build(), "chunk_status.rs");
write_generated_file(game_event::build(), "game_event.rs");
} }
pub fn write_generated_file(content: TokenStream, out_file: &str) { pub fn write_generated_file(content: TokenStream, out_file: &str) {

View File

@ -0,0 +1,27 @@
use heck::ToPascalCase;
use proc_macro2::TokenStream;
use quote::quote;
use crate::ident;
pub(crate) fn build() -> TokenStream {
println!("cargo:rerun-if-changed=assets/game_event.json");
let game_event: Vec<String> =
serde_json::from_str(include_str!("../../assets/game_event.json"))
.expect("Failed to parse game_event.json");
let mut variants = TokenStream::new();
for event in game_event.iter() {
let name = ident(event.to_pascal_case());
variants.extend([quote! {
#name,
}]);
}
quote! {
#[repr(u8)]
pub enum GameEvent {
#variants
}
}
}

View File

@ -17,3 +17,7 @@ pub mod sound {
pub mod chunk_status { pub mod chunk_status {
include!(concat!(env!("OUT_DIR"), "/chunk_status.rs")); include!(concat!(env!("OUT_DIR"), "/chunk_status.rs"));
} }
pub mod game_event {
include!(concat!(env!("OUT_DIR"), "/game_event.rs"));
}

View File

@ -114,12 +114,12 @@ impl ChestBlock {
if state == ChestState::IsClosed && num_players == 0 { if state == ChestState::IsClosed && num_players == 0 {
player player
.world() .world()
.play_block_sound(Sound::BlockChestClose as u16, location) .play_block_sound(Sound::BlockChestClose, location)
.await; .await;
} else if state == ChestState::IsOpened && num_players == 1 { } else if state == ChestState::IsOpened && num_players == 1 {
player player
.world() .world()
.play_block_sound(Sound::BlockChestOpen as u16, location) .play_block_sound(Sound::BlockChestOpen, location)
.await; .await;
} }

View File

@ -317,7 +317,7 @@ impl Player {
{ {
world world
.play_sound( .play_sound(
Sound::EntityPlayerAttackNodamage as u16, Sound::EntityPlayerAttackNodamage,
SoundCategory::Players, SoundCategory::Players,
&pos, &pos,
) )
@ -326,7 +326,7 @@ impl Player {
} }
world world
.play_sound(Sound::EntityPlayerHurt as u16, SoundCategory::Players, &pos) .play_sound(Sound::EntityPlayerHurt, SoundCategory::Players, &pos)
.await; .await;
let attack_type = AttackType::new(self, attack_cooldown_progress as f32).await; let attack_type = AttackType::new(self, attack_cooldown_progress as f32).await;

View File

@ -121,7 +121,7 @@ pub async fn player_attack_sound(pos: &Vector3<f64>, world: &World, attack_type:
AttackType::Knockback => { AttackType::Knockback => {
world world
.play_sound( .play_sound(
Sound::EntityPlayerAttackKnockback as u16, Sound::EntityPlayerAttackKnockback,
SoundCategory::Players, SoundCategory::Players,
pos, pos,
) )
@ -129,38 +129,22 @@ pub async fn player_attack_sound(pos: &Vector3<f64>, world: &World, attack_type:
} }
AttackType::Critical => { AttackType::Critical => {
world world
.play_sound( .play_sound(Sound::EntityPlayerAttackCrit, SoundCategory::Players, pos)
Sound::EntityPlayerAttackCrit as u16,
SoundCategory::Players,
pos,
)
.await; .await;
} }
AttackType::Sweeping => { AttackType::Sweeping => {
world world
.play_sound( .play_sound(Sound::EntityPlayerAttackSweep, SoundCategory::Players, pos)
Sound::EntityPlayerAttackSweep as u16,
SoundCategory::Players,
pos,
)
.await; .await;
} }
AttackType::Strong => { AttackType::Strong => {
world world
.play_sound( .play_sound(Sound::EntityPlayerAttackStrong, SoundCategory::Players, pos)
Sound::EntityPlayerAttackStrong as u16,
SoundCategory::Players,
pos,
)
.await; .await;
} }
AttackType::Weak => { AttackType::Weak => {
world world
.play_sound( .play_sound(Sound::EntityPlayerAttackWeak, SoundCategory::Players, pos)
Sound::EntityPlayerAttackWeak as u16,
SoundCategory::Players,
pos,
)
.await; .await;
} }
}; };

View File

@ -11,6 +11,7 @@ use crate::{
}; };
use level_time::LevelTime; use level_time::LevelTime;
use pumpkin_config::BasicConfiguration; use pumpkin_config::BasicConfiguration;
use pumpkin_data::sound::Sound;
use pumpkin_entity::{entity_type::EntityType, pose::EntityPose, EntityId}; use pumpkin_entity::{entity_type::EntityType, pose::EntityPose, EntityId};
use pumpkin_protocol::{ use pumpkin_protocol::{
client::play::CLevelEvent, client::play::CLevelEvent,
@ -155,7 +156,11 @@ impl World {
} }
} }
pub async fn play_sound( pub async fn play_sound(&self, sound: Sound, category: SoundCategory, position: &Vector3<f64>) {
self.play_sound_raw(sound as u16, category, position).await;
}
pub async fn play_sound_raw(
&self, &self,
sound_id: u16, sound_id: u16,
category: SoundCategory, category: SoundCategory,
@ -176,13 +181,13 @@ impl World {
.await; .await;
} }
pub async fn play_block_sound(&self, sound_id: u16, position: WorldPosition) { pub async fn play_block_sound(&self, sound: Sound, position: WorldPosition) {
let new_vec = Vector3::new( let new_vec = Vector3::new(
f64::from(position.0.x) + 0.5, f64::from(position.0.x) + 0.5,
f64::from(position.0.y) + 0.5, f64::from(position.0.y) + 0.5,
f64::from(position.0.z) + 0.5, f64::from(position.0.z) + 0.5,
); );
self.play_sound(sound_id, SoundCategory::Blocks, &new_vec) self.play_sound(sound, SoundCategory::Blocks, &new_vec)
.await; .await;
} }