azalea_protocol/packets/game/
serverbound_seen_advancements_packet.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::io::Cursor;

use azalea_buf::{McBuf, McBufReadable, McBufWritable};
use azalea_core::resource_location::ResourceLocation;
use azalea_protocol_macros::ServerboundGamePacket;

use crate::packets::BufReadError;

#[derive(Clone, Debug, ServerboundGamePacket)]
pub struct ServerboundSeenAdvancementsPacket {
    pub action: Action,
    pub tab: Option<ResourceLocation>,
}

#[derive(McBuf, Clone, Copy, Debug, Eq, PartialEq)]
pub enum Action {
    OpenedTab = 0,
    ClosedScreen = 1,
}

impl McBufReadable for ServerboundSeenAdvancementsPacket {
    fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
        let action = Action::read_from(buf)?;
        let tab = if action == Action::OpenedTab {
            Some(ResourceLocation::read_from(buf)?)
        } else {
            None
        };
        Ok(Self { action, tab })
    }
}

impl McBufWritable for ServerboundSeenAdvancementsPacket {
    fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
        self.action.write_into(buf)?;
        if let Some(tab) = &self.tab {
            tab.write_into(buf)?;
        }
        Ok(())
    }
}