1use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite};
2
3use crate::Registry;
4
5pub 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 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
62data_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 Biome,
78 "worldgen/biome"
79}
80
81impl 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}