chunkedge_protocol/packets/play/
stop_sound_s2c.rs1use std::borrow::Cow;
2use std::io::Write;
3
4use chunkedge_binary::{Decode, Encode};
5use chunkedge_ident::Ident;
6
7use crate::sound::SoundCategory;
8use crate::Packet;
9
10#[derive(Clone, PartialEq, Debug, Packet)]
11pub struct StopSoundS2c<'a> {
12 pub source: Option<SoundCategory>,
13 pub sound: Option<Ident<Cow<'a, str>>>,
14}
15
16impl Encode for StopSoundS2c<'_> {
17 fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
18 match (self.source, self.sound.as_ref()) {
19 (Some(source), Some(sound)) => {
20 3_i8.encode(&mut w)?;
21 source.encode(&mut w)?;
22 sound.encode(&mut w)?;
23 }
24 (None, Some(sound)) => {
25 2_i8.encode(&mut w)?;
26 sound.encode(&mut w)?;
27 }
28 (Some(source), None) => {
29 1_i8.encode(&mut w)?;
30 source.encode(&mut w)?;
31 }
32 _ => 0_i8.encode(&mut w)?,
33 }
34
35 Ok(())
36 }
37}
38
39impl<'a> Decode<'a> for StopSoundS2c<'a> {
40 fn decode(r: &mut &'a [u8]) -> anyhow::Result<Self> {
41 let (source, sound) = match i8::decode(r)? {
42 3 => (
43 Some(SoundCategory::decode(r)?),
44 Some(<Ident<Cow<'a, str>>>::decode(r)?),
45 ),
46 2 => (None, Some(<Ident<Cow<'a, str>>>::decode(r)?)),
47 1 => (Some(SoundCategory::decode(r)?), None),
48 _ => (None, None),
49 };
50
51 Ok(Self { source, sound })
52 }
53}