aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/packets/game/c_stop_sound.rs
blob: 0afba2c76fbf7b88040b4def1adfdde56107ca58 (plain)
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
use std::io::{self, Cursor, Write};

use azalea_buf::{AzBuf, BufReadError};
use azalea_core::bitset::FixedBitSet;
use azalea_protocol_macros::ClientboundGamePacket;
use azalea_registry::identifier::Identifier;

use super::c_sound::SoundSource;

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

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

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