parse scoreboard display slot at compile time

This commit is contained in:
Alexander Medvedev 2025-01-11 16:52:09 +01:00
parent 621bc31f48
commit 03cb80b216
12 changed files with 47 additions and 37 deletions

View File

@ -0,0 +1 @@
["LIST","SIDEBAR","BELOW_NAME","TEAM_BLACK","TEAM_DARK_BLUE","TEAM_DARK_GREEN","TEAM_DARK_AQUA","TEAM_DARK_RED","TEAM_DARK_PURPLE","TEAM_GOLD","TEAM_GRAY","TEAM_DARK_GRAY","TEAM_BLUE","TEAM_GREEN","TEAM_AQUA","TEAM_RED","TEAM_LIGHT_PURPLE","TEAM_YELLOW","TEAM_WHITE"]

View File

@ -10,6 +10,7 @@ mod entity_pose;
mod game_event;
mod packet;
mod particle;
mod scoreboard_slot;
mod screen;
mod sound;
mod sound_category;
@ -23,6 +24,7 @@ pub fn main() {
write_generated_file(game_event::build(), "game_event.rs");
write_generated_file(sound_category::build(), "sound_category.rs");
write_generated_file(entity_pose::build(), "entity_pose.rs");
write_generated_file(scoreboard_slot::build(), "scoreboard_slot.rs");
}
pub fn array_to_tokenstream(array: Vec<String>) -> TokenStream {

View File

@ -0,0 +1,20 @@
use proc_macro2::TokenStream;
use quote::quote;
use crate::array_to_tokenstream;
pub(crate) fn build() -> TokenStream {
println!("cargo:rerun-if-changed=assets/scoreboard_display_slot.json");
let sound_categories: Vec<String> =
serde_json::from_str(include_str!("../../assets/scoreboard_display_slot.json"))
.expect("Failed to parse scoreboard_display_slot.json");
let variants = array_to_tokenstream(sound_categories);
quote! {
#[repr(u8)]
pub enum ScoreboardDisplaySlot {
#variants
}
}
}

View File

@ -12,6 +12,7 @@ pub mod particle {
pub mod sound {
include!(concat!(env!("OUT_DIR"), "/sound.rs"));
include!(concat!(env!("OUT_DIR"), "/sound_category.rs"));
}
pub mod chunk_status {
@ -22,10 +23,10 @@ pub mod game_event {
include!(concat!(env!("OUT_DIR"), "/game_event.rs"));
}
pub mod sound_category {
include!(concat!(env!("OUT_DIR"), "/sound_category.rs"));
}
pub mod entity_pose {
pub mod entity {
include!(concat!(env!("OUT_DIR"), "/entity_pose.rs"));
}
pub mod scoreboard {
include!(concat!(env!("OUT_DIR"), "/scoreboard_slot.rs"));
}

View File

@ -1,4 +1,6 @@
use pumpkin_data::packet::clientbound::PLAY_SET_DISPLAY_OBJECTIVE;
use pumpkin_data::{
packet::clientbound::PLAY_SET_DISPLAY_OBJECTIVE, scoreboard::ScoreboardDisplaySlot,
};
use pumpkin_macros::client_packet;
use serde::Serialize;
@ -12,33 +14,10 @@ pub struct CDisplayObjective<'a> {
}
impl<'a> CDisplayObjective<'a> {
pub fn new(position: DisplaySlot, score_name: &'a str) -> Self {
pub fn new(position: ScoreboardDisplaySlot, score_name: &'a str) -> Self {
Self {
position: VarInt(position as i32),
score_name,
}
}
}
#[repr(i32)]
pub enum DisplaySlot {
List,
Sidebar,
BelowName,
TeamBlack,
TeamDarkBlue,
TeamDarkGreen,
TeanDarkAqua,
TeamDarkRed,
TeamDarkPurple,
TeamGold,
TeamGray,
TeamDarkGray,
TeamBlue,
TeamGreen,
TeamAqua,
TeamRed,
TeamLightPurple,
TeamYellow,
TeamWhite,
}

View File

@ -1,5 +1,5 @@
use bytes::BufMut;
use pumpkin_data::{packet::clientbound::PLAY_SOUND_ENTITY, sound_category::SoundCategory};
use pumpkin_data::{packet::clientbound::PLAY_SOUND_ENTITY, sound::SoundCategory};
use pumpkin_macros::client_packet;
use crate::{bytebuf::ByteBufMut, ClientPacket, IDOrSoundEvent, SoundEvent, VarInt};

View File

@ -1,5 +1,5 @@
use bytes::BufMut;
use pumpkin_data::{packet::clientbound::PLAY_SOUND, sound_category::SoundCategory};
use pumpkin_data::{packet::clientbound::PLAY_SOUND, sound::SoundCategory};
use pumpkin_macros::client_packet;
use crate::{bytebuf::ByteBufMut, ClientPacket, IDOrSoundEvent, SoundEvent, VarInt};

View File

@ -2,7 +2,7 @@ use core::f32;
use std::sync::{atomic::AtomicBool, Arc};
use crossbeam::atomic::AtomicCell;
use pumpkin_data::entity_pose::EntityPose;
use pumpkin_data::entity::EntityPose;
use pumpkin_entity::{entity_type::EntityType, EntityId};
use pumpkin_protocol::{
client::play::{CHeadRot, CSetEntityMetadata, CTeleportEntity, CUpdateEntityRot, Metadata},

View File

@ -9,7 +9,7 @@ use std::{
use crossbeam::atomic::AtomicCell;
use pumpkin_config::{ADVANCED_CONFIG, BASIC_CONFIG};
use pumpkin_data::{sound::Sound, sound_category::SoundCategory};
use pumpkin_data::sound::{Sound, SoundCategory};
use pumpkin_entity::{entity_type::EntityType, EntityId};
use pumpkin_inventory::player::PlayerInventory;
use pumpkin_protocol::server::play::{

View File

@ -1,6 +1,9 @@
use std::f32::consts::PI;
use pumpkin_data::{particle::Particle, sound::Sound, sound_category::SoundCategory};
use pumpkin_data::{
particle::Particle,
sound::{Sound, SoundCategory},
};
use pumpkin_protocol::{
client::play::{CEntityVelocity, CParticle},
codec::var_int::VarInt,

View File

@ -11,7 +11,10 @@ use crate::{
};
use level_time::LevelTime;
use pumpkin_config::BasicConfiguration;
use pumpkin_data::{entity_pose::EntityPose, sound::Sound, sound_category::SoundCategory};
use pumpkin_data::{
entity::EntityPose,
sound::{Sound, SoundCategory},
};
use pumpkin_entity::{entity_type::EntityType, EntityId};
use pumpkin_protocol::client::play::{CBlockUpdate, CRespawn, CSoundEffect, CWorldEvent};
use pumpkin_protocol::{

View File

@ -1,5 +1,6 @@
use std::collections::HashMap;
use pumpkin_data::scoreboard::ScoreboardDisplaySlot;
use pumpkin_protocol::{
client::play::{CDisplayObjective, CUpdateObjectives, CUpdateScore, RenderType},
codec::var_int::VarInt,
@ -43,7 +44,7 @@ impl Scoreboard {
.await;
world
.broadcast_packet_all(&CDisplayObjective::new(
pumpkin_protocol::client::play::DisplaySlot::Sidebar,
ScoreboardDisplaySlot::Sidebar,
objective.name,
))
.await;