aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/slot.rs
blob: 22961437645d0e1c8512686ac1c7469d09363c7a (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
40
// TODO: have an azalea-inventory or azalea-container crate and put this there

use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable};
use azalea_nbt::Nbt;
use std::io::{Cursor, Write};

#[derive(Debug, Clone, Default)]
pub enum Slot {
    #[default]
    Empty,
    Present(SlotData),
}

#[derive(Debug, Clone, McBuf)]
pub struct SlotData {
    #[var]
    pub id: u32,
    pub count: u8,
    pub nbt: Nbt,
}

impl McBufReadable for Slot {
    fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
        let slot = Option::<SlotData>::read_from(buf)?;
        Ok(slot.map_or(Slot::Empty, Slot::Present))
    }
}

impl McBufWritable for Slot {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        match self {
            Slot::Empty => false.write_into(buf)?,
            Slot::Present(i) => {
                true.write_into(buf)?;
                i.write_into(buf)?;
            }
        };
        Ok(())
    }
}