Skip to main content

steel_registry/
sound_event.rs

1use rustc_hash::FxHashMap;
2use steel_utils::Identifier;
3
4/// Built-in sound event registry entry used by sound packets and data-driven audio refs.
5#[derive(Debug, PartialEq)]
6pub struct SoundEvent {
7    pub key: Identifier,
8    pub sound_id: Identifier,
9    pub fixed_range: Option<f32>,
10}
11
12impl SoundEvent {
13    /// Vanilla `SoundEvent.getRange`.
14    #[must_use]
15    pub fn range(&self, volume: f32) -> f32 {
16        self.fixed_range
17            .unwrap_or(if volume > 1.0 { 16.0 * volume } else { 16.0 })
18    }
19
20    /// Returns the VarInt payload used by vanilla holder-based sound packets.
21    #[must_use]
22    pub fn packet_holder_id(&self) -> i32 {
23        let id = crate::RegistryEntry::id(self);
24        assert!(
25            id < i32::MAX as usize,
26            "sound event registry id exceeds protocol VarInt range"
27        );
28        id as i32 + 1
29    }
30}
31
32pub type SoundEventRef = &'static SoundEvent;
33
34pub struct SoundEventRegistry {
35    sound_events_by_id: Vec<SoundEventRef>,
36    sound_events_by_key: FxHashMap<Identifier, usize>,
37    allows_registering: bool,
38}
39
40impl SoundEventRegistry {
41    #[must_use]
42    pub fn new() -> Self {
43        Self {
44            sound_events_by_id: Vec::new(),
45            sound_events_by_key: FxHashMap::default(),
46            allows_registering: true,
47        }
48    }
49}
50
51crate::impl_standard_methods!(
52    SoundEventRegistry,
53    SoundEventRef,
54    sound_events_by_id,
55    sound_events_by_key,
56    allows_registering
57);
58
59crate::impl_registry!(
60    SoundEventRegistry,
61    SoundEvent,
62    sound_events_by_id,
63    sound_events_by_key,
64    sound_events
65);