aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/slot.rs
blob: d60c739015ba60317fd1c584edecac5414452ef5 (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 std::io::{Cursor, Write};

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

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

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

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

        Ok(())
    }
}