azalea_protocol/packets/game/
c_stop_sound.rs1use std::io::{self, Cursor, Write};
2
3use azalea_buf::{AzaleaRead, AzaleaWrite, BufReadError};
4use azalea_core::bitset::FixedBitSet;
5use azalea_protocol_macros::ClientboundGamePacket;
6use azalea_registry::identifier::Identifier;
7
8use super::c_sound::SoundSource;
9
10#[derive(Clone, Debug, PartialEq, ClientboundGamePacket)]
11pub struct ClientboundStopSound {
12 pub source: Option<SoundSource>,
13 pub name: Option<Identifier>,
14}
15
16impl AzaleaRead for ClientboundStopSound {
17 fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
18 let set = FixedBitSet::<2>::azalea_read(buf)?;
19 let source = if set.index(0) {
20 Some(SoundSource::azalea_read(buf)?)
21 } else {
22 None
23 };
24 let name = if set.index(1) {
25 Some(Identifier::azalea_read(buf)?)
26 } else {
27 None
28 };
29
30 Ok(Self { source, name })
31 }
32}
33
34impl AzaleaWrite for ClientboundStopSound {
35 fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
36 let mut set = FixedBitSet::<2>::new();
37 if self.source.is_some() {
38 set.set(0);
39 }
40 if self.name.is_some() {
41 set.set(1);
42 }
43 set.azalea_write(buf)?;
44 if let Some(source) = &self.source {
45 source.azalea_write(buf)?;
46 }
47 if let Some(name) = &self.name {
48 name.azalea_write(buf)?;
49 }
50 Ok(())
51 }
52}