aboutsummaryrefslogtreecommitdiff
path: root/azalea-registry/src/data.rs
blob: c1c1efe5baa373b49d93e0e610a3ce20ab10436e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite};

use crate::Registry;

/// A registry which has its values decided by the server in the
/// `ClientboundRegistryData` packet.
///
/// These can be resolved into their actual values with
/// `ResolvableDataRegistry` from azalea-core.
pub trait DataRegistry: AzaleaRead + AzaleaWrite {
    const NAME: &'static str;

    fn protocol_id(&self) -> u32;
    fn new_raw(id: u32) -> Self;
}
impl<T: DataRegistry> Registry for T {
    fn from_u32(value: u32) -> Option<Self> {
        Some(Self::new_raw(value))
    }

    fn to_u32(&self) -> u32 {
        self.protocol_id()
    }
}

macro_rules! data_registry {
    ($(#[$doc:meta])* $name:ident, $registry_name:expr) => {
        $(#[$doc])*
        #[derive(Debug, Clone, Copy, AzBuf, PartialEq, Eq, Hash)]
        pub struct $name {
            #[var]
            id: u32,
        }
        impl DataRegistry for $name {
            const NAME: &'static str = $registry_name;
            fn protocol_id(&self) -> u32 {
                self.id
            }
            fn new_raw(id: u32) -> Self {
                Self { id }
            }
        }

        #[cfg(feature = "serde")]
        impl serde::Serialize for $name {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                // see ChecksumSerializer::serialize_newtype_variant
                serializer.serialize_newtype_variant(concat!("minecraft:", $registry_name), self.id, "", &())
            }
        }
    };
}

data_registry! {Enchantment, "enchantment"}
data_registry! {DimensionType, "dimension_type"}
data_registry! {DamageKind, "damage_kind"}
data_registry! {Dialog, "dialog"}

// entity variants
data_registry! {WolfSoundVariant, "wolf_sound_variant"}
data_registry! {CowVariant, "cow_variant"}
data_registry! {ChickenVariant, "chicken_variant"}
data_registry! {FrogVariant, "frog_variant"}
data_registry! {CatVariant, "cat_variant"}
data_registry! {PigVariant, "pig_variant"}
data_registry! {PaintingVariant, "painting_variant"}
data_registry! {WolfVariant, "wolf_variant"}

data_registry! {
    /// An opaque biome identifier.
    ///
    /// You'll probably want to resolve this into its name before using it, by
    /// using `Client::with_resolved_registry` or a similar function.
    Biome,
    "worldgen/biome"
}

// these extra traits are required for Biome to be allowed to be palletable
impl Default for Biome {
    fn default() -> Self {
        Self::new_raw(0)
    }
}
impl From<u32> for Biome {
    fn from(id: u32) -> Self {
        Self::new_raw(id)
    }
}
impl From<Biome> for u32 {
    fn from(biome: Biome) -> Self {
        biome.protocol_id()
    }
}