valence_protocol/
sound.rs
1use std::borrow::Cow;
2use std::io::Write;
3
4pub use valence_generated::sound::Sound;
5use valence_ident::Ident;
6
7use crate::var_int::VarInt;
8use crate::{Decode, Encode};
9
10#[derive(Clone, PartialEq, Debug)]
11pub enum SoundId<'a> {
12 Direct {
13 id: Ident<Cow<'a, str>>,
14 range: Option<f32>,
15 },
16 Reference {
17 id: VarInt,
18 },
19}
20
21#[derive(Copy, Clone, PartialEq, Eq, Debug, Encode, Decode)]
22pub enum SoundCategory {
23 Master,
24 Music,
25 Record,
26 Weather,
27 Block,
28 Hostile,
29 Neutral,
30 Player,
31 Ambient,
32 Voice,
33}
34
35impl Encode for SoundId<'_> {
36 fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
37 match self {
38 SoundId::Direct { id, range } => {
39 VarInt(0).encode(&mut w)?;
40 id.encode(&mut w)?;
41 range.encode(&mut w)?;
42 }
43 SoundId::Reference { id } => VarInt(id.0 + 1).encode(&mut w)?,
44 }
45
46 Ok(())
47 }
48}
49
50impl<'a> Decode<'a> for SoundId<'a> {
51 fn decode(r: &mut &'a [u8]) -> anyhow::Result<Self> {
52 let i = VarInt::decode(r)?.0;
53
54 if i == 0 {
55 Ok(SoundId::Direct {
56 id: Ident::decode(r)?,
57 range: <Option<f32>>::decode(r)?,
58 })
59 } else {
60 Ok(SoundId::Reference { id: VarInt(i - 1) })
61 }
62 }
63}