azalea_registry/
data.rs

1use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite};
2
3use crate::Registry;
4
5/// A registry which has its values decided by the server in the
6/// `ClientboundRegistryData` packet.
7///
8/// These can be resolved into their actual values with
9/// `ResolvableDataRegistry` from azalea-core.
10pub trait DataRegistry: AzaleaRead + AzaleaWrite {
11    const NAME: &'static str;
12
13    fn protocol_id(&self) -> u32;
14    fn new_raw(id: u32) -> Self;
15}
16impl<T: DataRegistry> Registry for T {
17    fn from_u32(value: u32) -> Option<Self> {
18        Some(Self::new_raw(value))
19    }
20
21    fn to_u32(&self) -> u32 {
22        self.protocol_id()
23    }
24}
25
26macro_rules! data_registry {
27    ($(#[$doc:meta])* $name:ident, $registry_name:expr) => {
28        $(#[$doc])*
29        #[derive(Debug, Clone, Copy, AzBuf, PartialEq, Eq, Hash)]
30        pub struct $name {
31            #[var]
32            id: u32,
33        }
34        impl DataRegistry for $name {
35            const NAME: &'static str = $registry_name;
36            fn protocol_id(&self) -> u32 {
37                self.id
38            }
39            fn new_raw(id: u32) -> Self {
40                Self { id }
41            }
42        }
43
44        #[cfg(feature = "serde")]
45        impl serde::Serialize for $name {
46            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
47            where
48                S: serde::Serializer,
49            {
50                // see ChecksumSerializer::serialize_newtype_variant
51                serializer.serialize_newtype_variant(concat!("minecraft:", $registry_name), self.id, "", &())
52            }
53        }
54    };
55}
56
57data_registry! {Enchantment, "enchantment"}
58data_registry! {DimensionType, "dimension_type"}
59data_registry! {DamageKind, "damage_kind"}
60data_registry! {Dialog, "dialog"}
61
62// entity variants
63data_registry! {WolfSoundVariant, "wolf_sound_variant"}
64data_registry! {CowVariant, "cow_variant"}
65data_registry! {ChickenVariant, "chicken_variant"}
66data_registry! {FrogVariant, "frog_variant"}
67data_registry! {CatVariant, "cat_variant"}
68data_registry! {PigVariant, "pig_variant"}
69data_registry! {PaintingVariant, "painting_variant"}
70data_registry! {WolfVariant, "wolf_variant"}
71
72data_registry! {
73    /// An opaque biome identifier.
74    ///
75    /// You'll probably want to resolve this into its name before using it, by
76    /// using `Client::with_resolved_registry` or a similar function.
77    Biome,
78    "worldgen/biome"
79}
80
81// these extra traits are required for Biome to be allowed to be palletable
82impl Default for Biome {
83    fn default() -> Self {
84        Self::new_raw(0)
85    }
86}
87impl From<u32> for Biome {
88    fn from(id: u32) -> Self {
89        Self::new_raw(id)
90    }
91}
92impl From<Biome> for u32 {
93    fn from(biome: Biome) -> Self {
94        biome.protocol_id()
95    }
96}