Skip to main content

azalea_protocol/packets/game/
s_custom_click_action.rs

1use std::io::{self, Cursor, Write};
2
3use azalea_buf::{AzBuf, AzBufLimited, BufReadError};
4use azalea_core::identifier::Identifier;
5use azalea_protocol_macros::ServerboundGamePacket;
6use simdnbt::owned::{NbtCompound, NbtTag};
7
8#[derive(Clone, Debug, PartialEq, ServerboundGamePacket)]
9pub struct ServerboundCustomClickAction {
10    pub id: Identifier,
11    pub payload: Option<NbtCompound>,
12}
13
14impl AzBuf for ServerboundCustomClickAction {
15    fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
16        self.id.azalea_write(ctx, buf)?;
17
18        let mut payload_bytes = Vec::new();
19        match &self.payload {
20            Some(c) => {
21                NbtTag::Compound(c.clone()).write(&mut payload_bytes);
22            }
23            None => payload_bytes.push(0),
24        }
25        payload_bytes.azalea_write(ctx, buf)?;
26
27        Ok(())
28    }
29    fn azalea_read(ctx: azalea_buf::Ctx, buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
30        let id = Identifier::azalea_read(ctx, buf)?;
31
32        let payload_bytes = Box::<[u8]>::azalea_read_limited(ctx, buf, 65536)?;
33        let payload = simdnbt::owned::read_optional_tag(&mut Cursor::new(&payload_bytes))
34            .map_err(simdnbt::Error::from)?;
35        let payload = match payload {
36            Some(NbtTag::Compound(c)) => Some(c),
37            Some(tag) => return Err(simdnbt::Error::InvalidRootType(tag.id()).into()),
38            None => None,
39        };
40
41        Ok(Self { id, payload })
42    }
43}