azalea_protocol/packets/game/
clientbound_stop_sound_packet.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::io::{Cursor, Write};

use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
use azalea_core::{bitset::FixedBitSet, resource_location::ResourceLocation};
use azalea_protocol_macros::ClientboundGamePacket;

use super::clientbound_sound_packet::SoundSource;

#[derive(Clone, Debug, ClientboundGamePacket)]
pub struct ClientboundStopSoundPacket {
    pub source: Option<SoundSource>,
    pub name: Option<ResourceLocation>,
}

impl McBufReadable for ClientboundStopSoundPacket {
    fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
        let set = FixedBitSet::<2>::read_from(buf)?;
        let source = if set.index(0) {
            Some(SoundSource::read_from(buf)?)
        } else {
            None
        };
        let name = if set.index(1) {
            Some(ResourceLocation::read_from(buf)?)
        } else {
            None
        };

        Ok(Self { source, name })
    }
}

impl McBufWritable for ClientboundStopSoundPacket {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        let mut set = FixedBitSet::<2>::new();
        if self.source.is_some() {
            set.set(0);
        }
        if self.name.is_some() {
            set.set(1);
        }
        set.write_into(buf)?;
        if let Some(source) = &self.source {
            source.write_into(buf)?;
        }
        if let Some(name) = &self.name {
            name.write_into(buf)?;
        }
        Ok(())
    }
}