azalea_registry/holder/
mod.rs1pub mod block_predicate;
9pub mod block_state_provider;
10pub mod components;
11pub mod dimension_type;
12pub mod enchantment;
13pub mod entity_effect;
14pub mod float_provider;
15pub mod particle;
16pub mod value;
17
18use std::{any::Any, collections::HashMap, sync::LazyLock};
19
20use azalea_codec::Codec;
21use azalea_core::identifier::Identifier;
22use indexmap::IndexMap;
23use simdnbt::owned::{NbtCompound, NbtTag};
24use tracing::error;
25
26#[derive(Clone, Debug, Default)]
35pub struct Registries {
36 #[rustfmt::skip] pub dimension_type: RegistryType<dimension_type::DimensionKindElement>,
43 pub enchantment: RegistryType<enchantment::EnchantmentData>,
44 pub world_clock: RegistryType<()>,
45 pub biome: RegistryType<()>,
46
47 pub extra: Box<HashMap<Identifier, RegistryType<NbtCompound>>>,
52}
53registry_holder_macro::registry_holder!(dimension_type, enchantment, world_clock, biome);
54
55mod registry_holder_macro {
57 #[macro_export]
58 macro_rules! registry_holder {
59 ($($registry:ident),* $(,)?) => {
60 impl Registries {
61 pub fn append(
62 &mut self,
63 id: Identifier,
64 entries: Vec<(Identifier, Option<NbtCompound>)>,
65 ) {
66 if id.namespace() == "minecraft" {
67 match id.path() {
68 $(
69 stringify!($registry) => {
70 return self.$registry.append_nbt(id, entries);
71 }
72 )*
73 _ => {}
74 }
75 }
76
77 self.extra
78 .entry(id.clone())
79 .or_default()
80 .append_nbt(id, entries);
81 }
82
83 pub fn extend(&mut self, other: Registries) {
84 $(
85 self.$registry = other.$registry;
86 )*
87 self.extra.extend(*other.extra);
88 }
89
90 pub fn protocol_id_to_identifier(
94 &self,
95 registry: Identifier,
96 protocol_id: u32,
97 ) -> Option<&Identifier> {
98 let index = protocol_id as usize;
99
100 if registry.namespace() == "minecraft" {
101 match registry.path() {
102 $(
103 stringify!($registry) => {
104 return self.$registry.map.get_index(index).map(|(k, _)| k);
105 }
106 )*
107 _ => {}
108 }
109 }
110
111 self.extra
112 .get(®istry)
113 .and_then(|r| r.map.get_index(index))
114 .map(|(k, _)| k)
115 }
116
117 pub fn identifier_to_protocol_id(
118 &self,
119 registry: &'static str,
120 ident: Identifier,
121 ) -> Option<u32> {
122 let registry = Identifier::from(registry);
123 if registry.namespace() == "minecraft" {
124 match registry.path() {
125 $(
126 stringify!($registry) => {
127 return self
128 .$registry
129 .map
130 .get_index_of(&ident)
131 .map(|i| i as u32);
132 }
133 )*
134 _ => {}
135 }
136 }
137
138 self.extra
139 .get(®istry)
140 .and_then(|r| r.map.get_index_of(&ident))
141 .map(|i| i as u32)
142 }
143 }
144 };
145 }
146
147 pub use registry_holder;
148}
149
150impl Default for &'static Registries {
151 fn default() -> Self {
152 static DEFAULT: LazyLock<Registries> = LazyLock::new(Registries::default);
153 &DEFAULT
154 }
155}
156impl azalea_buf::ResolverTrait for Registries {}
157
158#[derive(Clone, Debug, Codec)]
160pub struct RegistryType<T: Codec<NbtTag>> {
161 pub map: Box<IndexMap<Identifier, T>>,
162}
163
164impl<T: Codec<NbtTag>> Default for RegistryType<T> {
165 fn default() -> Self {
166 Self {
167 map: Box::new(IndexMap::new()),
168 }
169 }
170}
171
172impl<T: Codec<NbtTag>> RegistryType<T> {
173 fn append_nbt(&mut self, id: Identifier, entries: Vec<(Identifier, Option<NbtCompound>)>) {
174 let map = &mut self.map;
175 for (key, value) in entries {
176 if let Some(value) = value {
177 let parsed = T::codec_read(&NbtTag::from(value.clone()));
178 match parsed {
179 Ok(value) => {
180 map.insert(key, value);
181 }
182 Err(err) => {
183 error!("Error deserializing {id} entry {key}: {err:?}\n{value:?}");
184 }
185 }
186 } else {
187 map.shift_remove(&key);
188 }
189 }
190 }
191}
192
193pub trait RegistryDeserializesTo {
194 fn get_for_registry<'a>(
195 registries: &'a Registries,
196 registry_name: &'static str,
197 protocol_id: u32,
198 ) -> Option<&'a Self>;
199}
200
201impl RegistryDeserializesTo for NbtCompound {
202 fn get_for_registry<'a>(
203 registries: &'a Registries,
204 registry_name: &'static str,
205 protocol_id: u32,
206 ) -> Option<&'a Self> {
207 registries
208 .extra
209 .get(&Identifier::new(registry_name))?
210 .map
211 .get_index(protocol_id as usize)
212 .map(|i| i.1)
213 }
214}
215impl RegistryDeserializesTo for dimension_type::DimensionKindElement {
216 fn get_for_registry<'a>(
217 registries: &'a Registries,
218 registry_name: &'static str,
219 protocol_id: u32,
220 ) -> Option<&'a Self> {
221 if registry_name != "dimension_type" {
222 error!(
223 "called RegistryDeserializesTo::get_for_registry with the wrong registry: {registry_name}"
224 );
225 }
226 registries
227 .dimension_type
228 .map
229 .get_index(protocol_id as usize)
230 .map(|i| i.1)
231 }
232}
233impl RegistryDeserializesTo for enchantment::EnchantmentData {
234 fn get_for_registry<'a>(
235 registries: &'a Registries,
236 registry_name: &'static str,
237 protocol_id: u32,
238 ) -> Option<&'a Self> {
239 if registry_name != "enchantment" {
240 error!(
241 "called RegistryDeserializesTo::get_for_registry with the wrong registry: {registry_name}"
242 );
243 }
244 registries
245 .enchantment
246 .map
247 .get_index(protocol_id as usize)
248 .map(|i| i.1)
249 }
250}
251
252impl<'a> From<azalea_buf::Ctx<'a>> for &'a Registries {
253 fn from(ctx: azalea_buf::Ctx<'a>) -> Self {
254 (ctx.resolver as &dyn Any)
255 .downcast_ref::<Registries>()
256 .expect("ctx was expected to have `Registries`")
257 }
258}
259
260impl<'a> From<&'a Registries> for azalea_buf::Ctx<'a> {
261 fn from(registries: &'a Registries) -> Self {
262 azalea_buf::Ctx::new(registries)
263 }
264}