steel_registry/
cow_sound_variant.rs1use rustc_hash::FxHashMap;
2use simdnbt::ToNbtTag;
3use simdnbt::owned::NbtTag;
4use steel_utils::Identifier;
5use steel_utils::random::Random;
6
7use crate::RegistryExt;
8use crate::sound_event::SoundEventRef;
9
10#[derive(Debug)]
12pub struct CowSoundVariant {
13 pub key: Identifier,
14 pub ambient_sound: SoundEventRef,
15 pub death_sound: SoundEventRef,
16 pub hurt_sound: SoundEventRef,
17 pub step_sound: SoundEventRef,
18}
19
20impl ToNbtTag for &CowSoundVariant {
21 fn to_nbt_tag(self) -> NbtTag {
22 use simdnbt::owned::{NbtCompound, NbtTag};
23 let mut compound = NbtCompound::new();
24 let s = self.ambient_sound.key.to_string();
25 compound.insert("ambient_sound", s.as_str());
26 let s = self.death_sound.key.to_string();
27 compound.insert("death_sound", s.as_str());
28 let s = self.hurt_sound.key.to_string();
29 compound.insert("hurt_sound", s.as_str());
30 let s = self.step_sound.key.to_string();
31 compound.insert("step_sound", s.as_str());
32 NbtTag::Compound(compound)
33 }
34}
35
36pub type CowSoundVariantRef = &'static CowSoundVariant;
37
38pub struct CowSoundVariantRegistry {
39 cow_sound_variants_by_id: Vec<CowSoundVariantRef>,
40 cow_sound_variants_by_key: FxHashMap<Identifier, usize>,
41 allows_registering: bool,
42}
43
44impl CowSoundVariantRegistry {
45 #[must_use]
46 pub fn new() -> Self {
47 Self {
48 cow_sound_variants_by_id: Vec::new(),
49 cow_sound_variants_by_key: FxHashMap::default(),
50 allows_registering: true,
51 }
52 }
53
54 #[must_use]
55 pub fn pick_random(&self, random: &mut impl Random) -> Option<CowSoundVariantRef> {
56 let bound = i32::try_from(self.len()).ok()?;
58 if bound == 0 {
59 return None;
60 }
61
62 self.by_id(random.next_i32_bounded(bound) as usize)
63 }
64}
65
66crate::impl_standard_methods!(
67 CowSoundVariantRegistry,
68 CowSoundVariantRef,
69 cow_sound_variants_by_id,
70 cow_sound_variants_by_key,
71 allows_registering
72);
73
74crate::impl_registry!(
75 CowSoundVariantRegistry,
76 CowSoundVariant,
77 cow_sound_variants_by_id,
78 cow_sound_variants_by_key,
79 cow_sound_variants
80);