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}
45
46data_registry! {Enchantment, "enchantment"}
47data_registry! {DimensionType, "dimension_type"}
48data_registry! {DamageKind, "damage_kind"}
49data_registry! {Dialog, "dialog"}
50
51// entity variants
52data_registry! {WolfSoundVariant, "wolf_sound_variant"}
53data_registry! {CowVariant, "cow_variant"}
54data_registry! {ChickenVariant, "chicken_variant"}
55data_registry! {FrogVariant, "frog_variant"}
56data_registry! {CatVariant, "cat_variant"}
57data_registry! {PigVariant, "pig_variant"}
58data_registry! {PaintingVariant, "painting_variant"}
59data_registry! {WolfVariant, "wolf_variant"}
60
61data_registry! {
62    /// An opaque biome identifier.
63    ///
64    /// You'll probably want to resolve this into its name before using it, by
65    /// using `Client::with_resolved_registry` or a similar function.
66    Biome,
67    "worldgen/biome"
68}
69
70// these extra traits are required for Biome to be allowed to be palletable
71impl Default for Biome {
72    fn default() -> Self {
73        Self::new_raw(0)
74    }
75}
76impl From<u32> for Biome {
77    fn from(id: u32) -> Self {
78        Self::new_raw(id)
79    }
80}
81impl From<Biome> for u32 {
82    fn from(biome: Biome) -> Self {
83        biome.protocol_id()
84    }
85}