diff options
Diffstat (limited to 'azalea-core/src/slot.rs')
| -rw-r--r-- | azalea-core/src/slot.rs | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/azalea-core/src/slot.rs b/azalea-core/src/slot.rs index 5e42f558..6e622872 100644 --- a/azalea-core/src/slot.rs +++ b/azalea-core/src/slot.rs @@ -1,14 +1,40 @@ // TODO: have an azalea-inventory or azalea-container crate and put this there +use azalea_buf::{McBuf, McBufReadable, McBufWritable}; +use std::io::{Read, Write}; + #[derive(Debug, Clone)] pub enum Slot { - Present(SlotData), Empty, + Present(SlotData), } -#[derive(Debug, Clone)] +#[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_into(buf: &mut impl Read) -> Result<Self, String> { + let present = bool::read_into(buf)?; + if !present { + return Ok(Slot::Empty); + } + let slot = SlotData::read_into(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(()) + } +} |
