Skip to main content

steel_registry/
pig_sound_variant.rs

1use rustc_hash::FxHashMap;
2use simdnbt::ToNbtTag;
3use simdnbt::owned::{NbtCompound, NbtTag};
4use steel_utils::Identifier;
5
6use crate::sound_event::SoundEventRef;
7
8/// Represents a set of sounds for a pig variant from a data pack JSON file.
9#[derive(Debug)]
10pub struct PigSoundVariant {
11    pub key: Identifier,
12    pub adult_sounds: PigAge,
13    pub baby_sounds: PigAge,
14}
15#[derive(Debug)]
16pub struct PigAge {
17    pub ambient_sound: SoundEventRef,
18    pub death_sound: SoundEventRef,
19    pub hurt_sound: SoundEventRef,
20    pub eat_sound: SoundEventRef,
21    pub step_sound: SoundEventRef,
22}
23impl ToNbtTag for &PigAge {
24    fn to_nbt_tag(self) -> NbtTag {
25        let mut component = NbtCompound::new();
26        let s = self.ambient_sound.key.to_string();
27        component.insert("ambient_sound", s.as_str());
28        let s = self.death_sound.key.to_string();
29        component.insert("death_sound", s.as_str());
30        let s = self.hurt_sound.key.to_string();
31        component.insert("hurt_sound", s.as_str());
32        let s = self.step_sound.key.to_string();
33        component.insert("step_sound", s.as_str());
34        let s = self.eat_sound.key.to_string();
35        component.insert("eat_sound", s.as_str());
36        NbtTag::Compound(component)
37    }
38}
39
40impl ToNbtTag for &PigSoundVariant {
41    fn to_nbt_tag(self) -> NbtTag {
42        use simdnbt::owned::{NbtCompound, NbtTag};
43        let mut compound = NbtCompound::new();
44        compound.insert("adult_sounds", self.adult_sounds.to_nbt_tag());
45        compound.insert("baby_sounds", self.baby_sounds.to_nbt_tag());
46        NbtTag::Compound(compound)
47    }
48}
49
50pub type PigSoundVariantRef = &'static PigSoundVariant;
51
52pub struct PigSoundVariantRegistry {
53    pig_sound_variants_by_id: Vec<PigSoundVariantRef>,
54    pig_sound_variants_by_key: FxHashMap<Identifier, usize>,
55    allows_registering: bool,
56}
57
58impl PigSoundVariantRegistry {
59    #[must_use]
60    pub fn new() -> Self {
61        Self {
62            pig_sound_variants_by_id: Vec::new(),
63            pig_sound_variants_by_key: FxHashMap::default(),
64            allows_registering: true,
65        }
66    }
67}
68
69crate::impl_standard_methods!(
70    PigSoundVariantRegistry,
71    PigSoundVariantRef,
72    pig_sound_variants_by_id,
73    pig_sound_variants_by_key,
74    allows_registering
75);
76
77crate::impl_registry!(
78    PigSoundVariantRegistry,
79    PigSoundVariant,
80    pig_sound_variants_by_id,
81    pig_sound_variants_by_key,
82    pig_sound_variants
83);