Skip to main content

steel_registry/
jukebox_song.rs

1use rustc_hash::FxHashMap;
2use simdnbt::ToNbtTag;
3use simdnbt::owned::NbtTag;
4use steel_utils::Identifier;
5use text_components::TextComponent;
6
7use crate::sound_event::SoundEventRef;
8
9/// Represents a jukebox song definition from a data pack JSON file.
10#[derive(Debug)]
11pub struct JukeboxSong {
12    pub key: Identifier,
13    pub sound_event: SoundEventRef,
14    pub description: TextComponent,
15    pub length_in_seconds: f32,
16    pub comparator_output: i32,
17}
18
19impl ToNbtTag for &JukeboxSong {
20    fn to_nbt_tag(self) -> NbtTag {
21        use simdnbt::owned::NbtCompound;
22        let mut compound = NbtCompound::new();
23        let sound_event = self.sound_event.key.to_string();
24        compound.insert("sound_event", sound_event.as_str());
25        compound.insert("description", (&self.description).to_nbt_tag());
26        compound.insert("length_in_seconds", self.length_in_seconds);
27        compound.insert("comparator_output", self.comparator_output);
28        NbtTag::Compound(compound)
29    }
30}
31
32pub type JukeboxSongRef = &'static JukeboxSong;
33
34pub struct JukeboxSongRegistry {
35    jukebox_songs_by_id: Vec<JukeboxSongRef>,
36    jukebox_songs_by_key: FxHashMap<Identifier, usize>,
37    allows_registering: bool,
38}
39
40impl JukeboxSongRegistry {
41    #[must_use]
42    pub fn new() -> Self {
43        Self {
44            jukebox_songs_by_id: Vec::new(),
45            jukebox_songs_by_key: FxHashMap::default(),
46            allows_registering: true,
47        }
48    }
49}
50
51crate::impl_standard_methods!(
52    JukeboxSongRegistry,
53    JukeboxSongRef,
54    jukebox_songs_by_id,
55    jukebox_songs_by_key,
56    allows_registering
57);
58
59crate::impl_registry!(
60    JukeboxSongRegistry,
61    JukeboxSong,
62    jukebox_songs_by_id,
63    jukebox_songs_by_key,
64    jukebox_songs
65);