Skip to main content

steel_registry/
wolf_variant.rs

1use crate::shared_structs::{SpawnConditionEntry, insert_spawn_conditions};
2use rustc_hash::FxHashMap;
3use simdnbt::ToNbtTag;
4use simdnbt::owned::NbtTag;
5use steel_utils::Identifier;
6
7/// Represents a full wolf variant definition from a data pack JSON file.
8#[derive(Debug)]
9pub struct WolfVariant {
10    pub key: Identifier,
11    pub assets: WolfAssetInfo,
12    pub baby_assets: WolfAssetInfo,
13    pub spawn_conditions: &'static [SpawnConditionEntry],
14}
15
16/// Contains the texture resource locations for a wolf variant.
17#[derive(Debug)]
18pub struct WolfAssetInfo {
19    pub wild: Identifier,
20    pub tame: Identifier,
21    pub angry: Identifier,
22}
23
24impl ToNbtTag for &WolfVariant {
25    fn to_nbt_tag(self) -> NbtTag {
26        use simdnbt::owned::NbtCompound;
27        let mut compound = NbtCompound::new();
28        let mut assets = NbtCompound::new();
29        let wild = self.assets.wild.to_string();
30        assets.insert("wild", wild.as_str());
31        let tame = self.assets.tame.to_string();
32        assets.insert("tame", tame.as_str());
33        let angry = self.assets.angry.to_string();
34        assets.insert("angry", angry.as_str());
35        compound.insert("assets", NbtTag::Compound(assets));
36        let mut baby_assets = NbtCompound::new();
37        let wild = self.baby_assets.wild.to_string();
38        baby_assets.insert("wild", wild.as_str());
39        let tame = self.baby_assets.tame.to_string();
40        baby_assets.insert("tame", tame.as_str());
41        let angry = self.baby_assets.angry.to_string();
42        baby_assets.insert("angry", angry.as_str());
43        compound.insert("baby_assets", NbtTag::Compound(baby_assets));
44        insert_spawn_conditions(&mut compound, self.spawn_conditions);
45        NbtTag::Compound(compound)
46    }
47}
48
49pub type WolfVariantRef = &'static WolfVariant;
50
51pub struct WolfVariantRegistry {
52    wolf_variants_by_id: Vec<WolfVariantRef>,
53    wolf_variants_by_key: FxHashMap<Identifier, usize>,
54    allows_registering: bool,
55}
56
57impl WolfVariantRegistry {
58    #[must_use]
59    pub fn new() -> Self {
60        Self {
61            wolf_variants_by_id: Vec::new(),
62            wolf_variants_by_key: FxHashMap::default(),
63            allows_registering: true,
64        }
65    }
66}
67
68crate::impl_standard_methods!(
69    WolfVariantRegistry,
70    WolfVariantRef,
71    wolf_variants_by_id,
72    wolf_variants_by_key,
73    allows_registering
74);
75
76crate::impl_registry!(
77    WolfVariantRegistry,
78    WolfVariant,
79    wolf_variants_by_id,
80    wolf_variants_by_key,
81    wolf_variants
82);