azalea_protocol/packets/game/
c_sound.rs

1use azalea_buf::AzBuf;
2use azalea_core::resource_location::ResourceLocation;
3use azalea_protocol_macros::ClientboundGamePacket;
4use azalea_registry::SoundEvent;
5
6#[derive(Clone, Debug, AzBuf, ClientboundGamePacket)]
7pub struct ClientboundSound {
8    pub sound: azalea_registry::Holder<SoundEvent, CustomSound>,
9    pub source: SoundSource,
10    // this can't be a BlockPos because it serializes differently :(
11    pub x: i32,
12    pub y: i32,
13    pub z: i32,
14    pub volume: f32,
15    pub pitch: f32,
16    pub seed: u64,
17}
18
19#[derive(Clone, Debug, AzBuf)]
20pub struct CustomSound {
21    pub location: ResourceLocation,
22    pub fixed_range: Option<f32>,
23}
24
25#[derive(AzBuf, Clone, Copy, Debug)]
26pub enum SoundSource {
27    Master = 0,
28    Music = 1,
29    Records = 2,
30    Weather = 3,
31    Blocks = 4,
32    Hostile = 5,
33    Neutral = 6,
34    Players = 7,
35    Ambient = 8,
36    Voice = 9,
37}
38
39#[cfg(test)]
40mod tests {
41    use std::io::Cursor;
42
43    use azalea_buf::AzaleaRead;
44
45    use crate::packets::game::ClientboundSound;
46
47    #[test]
48    fn test_read_write_custom_sound() {
49        let contents = [
50            0, 21, 109, 105, 110, 101, 99, 114, 97, 102, 116, 58, 97, 115, 102, 97, 115, 100, 102,
51            115, 100, 102, 103, 0, 8, 0, 0, 0, 63, 255, 255, 254, 32, 0, 0, 0, 82, 66, 200, 0, 0,
52            63, 128, 0, 0, 71, 94, 219, 133, 200, 13, 150, 31,
53        ];
54        let mut buf = Cursor::new(contents.as_slice());
55        let packet = ClientboundSound::azalea_read(&mut buf).unwrap();
56        println!("{:?}", packet);
57
58        assert_eq!(buf.position(), contents.len() as u64);
59
60        let mut buf = Vec::new();
61        packet.write(&mut buf).unwrap();
62        assert_eq!(buf, contents);
63    }
64}