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}
45
46data_registry! {Enchantment, "enchantment"}
47data_registry! {DimensionType, "dimension_type"}
48data_registry! {DamageKind, "damage_kind"}
49data_registry! {WolfSoundVariant, "wolf_sound_variant"}
50data_registry! {CowVariant, "cow_variant"}
51data_registry! {ChickenVariant, "chicken_variant"}
52data_registry! {FrogVariant, "frog_variant"}
53data_registry! {CatVariant, "cat_variant"}
54data_registry! {PigVariant, "pig_variant"}
55data_registry! {PaintingVariant, "painting_variant"}
56data_registry! {WolfVariant, "wolf_variant"}
57
58data_registry! {
59 Biome,
64 "worldgen/biome"
65}
66
67impl Default for Biome {
69 fn default() -> Self {
70 Self::new_raw(0)
71 }
72}
73impl From<u32> for Biome {
74 fn from(id: u32) -> Self {
75 Self::new_raw(id)
76 }
77}
78impl From<Biome> for u32 {
79 fn from(biome: Biome) -> Self {
80 biome.protocol_id()
81 }
82}