From 0652fa13a42db0cde84721a6ca89a85437624b6a Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sun, 9 Oct 2022 15:38:00 -0400 Subject: [PATCH] change `Slot` enum into alias of `Option` (#107) --- src/client.rs | 16 ++++++---------- src/protocol/slot.rs | 35 +++++------------------------------ 2 files changed, 11 insertions(+), 40 deletions(-) diff --git a/src/client.rs b/src/client.rs index c64b17e..727b846 100644 --- a/src/client.rs +++ b/src/client.rs @@ -302,7 +302,7 @@ impl Client { .with_created_this_tick(true), player_data: Player::new(), entity_events: Vec::new(), - cursor_held_item: Slot::Empty, + cursor_held_item: None, } } @@ -785,12 +785,10 @@ impl Client { C2sPlayPacket::ClickContainer(p) => { if p.slot_idx == -999 { // client is trying to drop the currently held stack - let held = std::mem::replace(&mut self.cursor_held_item, Slot::Empty); + let held = std::mem::replace(&mut self.cursor_held_item, None); match held { - Slot::Empty => {} - Slot::Present(stack) => { - self.events.push_back(ClientEvent::DropItemStack { stack }) - } + None => {} + Some(stack) => self.events.push_back(ClientEvent::DropItemStack { stack }), } } else { self.cursor_held_item = p.carried_item.clone(); @@ -971,12 +969,10 @@ impl Client { if e.slot == -1 { // The client is trying to drop a stack of items match e.clicked_item { - Slot::Empty => log::warn!( + None => log::warn!( "Invalid packet, creative client tried to drop a stack of nothing." ), - Slot::Present(stack) => { - self.events.push_back(ClientEvent::DropItemStack { stack }) - } + Some(stack) => self.events.push_back(ClientEvent::DropItemStack { stack }), } } else { self.events.push_back(ClientEvent::SetSlotCreative { diff --git a/src/protocol/slot.rs b/src/protocol/slot.rs index 02cae5f..47098bf 100644 --- a/src/protocol/slot.rs +++ b/src/protocol/slot.rs @@ -9,18 +9,13 @@ use crate::protocol::{Decode, Encode}; pub type SlotId = i16; /// Represents a slot in an inventory. -#[derive(Clone, Default, Debug)] -pub enum Slot { - #[default] - Empty, - Present(ItemStack), -} +pub type Slot = Option; impl Encode for Slot { fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> { match self { - Slot::Empty => false.encode(w), - Slot::Present(s) => { + None => false.encode(w), + Some(s) => { true.encode(w)?; s.item.encode(w)?; s.item_count.encode(w)?; @@ -37,9 +32,9 @@ impl Decode for Slot { fn decode(r: &mut &[u8]) -> anyhow::Result { let present = bool::decode(r)?; if !present { - return Ok(Slot::Empty); + return Ok(None); } - Ok(Slot::Present(ItemStack { + Ok(Some(ItemStack { item: ItemKind::decode(r)?, item_count: u8::decode(r)?, nbt: if r.first() == Some(&0) { @@ -51,23 +46,3 @@ impl Decode for Slot { })) } } - -impl From> for Slot { - fn from(s: Option) -> Self { - if let Some(s) = s { - Slot::Present(s) - } else { - Slot::Empty - } - } -} - -impl From for Option { - fn from(s: Slot) -> Self { - if let Slot::Present(s) = s { - Some(s) - } else { - None - } - } -}