aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/packets/game/serverbound_seen_advancements_packet.rs
blob: 5a9b0e4883cf49e768881d728ddeaafa0a57e922 (plain)
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
use crate::packets::BufReadError;
use azalea_buf::{McBuf, McBufReadable, McBufWritable};
use azalea_core::ResourceLocation;
use azalea_protocol_macros::ServerboundGamePacket;
use std::io::Cursor;

#[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(())
    }
}