azalea_protocol/packets/game/
c_sound.rs

1use azalea_buf::AzBuf;
2use azalea_core::sound::CustomSound;
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(AzBuf, Clone, Copy, Debug)]
20pub enum SoundSource {
21    Master = 0,
22    Music = 1,
23    Records = 2,
24    Weather = 3,
25    Blocks = 4,
26    Hostile = 5,
27    Neutral = 6,
28    Players = 7,
29    Ambient = 8,
30    Voice = 9,
31}
32
33#[cfg(test)]
34mod tests {
35    use std::io::Cursor;
36
37    use azalea_buf::AzaleaRead;
38
39    use crate::packets::game::ClientboundSound;
40
41    #[test]
42    fn test_read_write_custom_sound() {
43        let contents = [
44            0, 21, 109, 105, 110, 101, 99, 114, 97, 102, 116, 58, 97, 115, 102, 97, 115, 100, 102,
45            115, 100, 102, 103, 0, 8, 0, 0, 0, 63, 255, 255, 254, 32, 0, 0, 0, 82, 66, 200, 0, 0,
46            63, 128, 0, 0, 71, 94, 219, 133, 200, 13, 150, 31,
47        ];
48        let mut buf = Cursor::new(contents.as_slice());
49        let packet = ClientboundSound::azalea_read(&mut buf).unwrap();
50        println!("{:?}", packet);
51
52        assert_eq!(buf.position(), contents.len() as u64);
53
54        let mut buf = Vec::new();
55        packet.write(&mut buf).unwrap();
56        assert_eq!(buf, contents);
57    }
58}