valence_protocol/packets/play/
stop_sound_s2c.rs

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