change Slot enum into alias of Option<ItemStack> (#107)

This commit is contained in:
Carson McManus 2022-10-09 15:38:00 -04:00 committed by GitHub
parent 82323eaa12
commit 0652fa13a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 40 deletions

View file

@ -302,7 +302,7 @@ impl<C: Config> Client<C> {
.with_created_this_tick(true), .with_created_this_tick(true),
player_data: Player::new(), player_data: Player::new(),
entity_events: Vec::new(), entity_events: Vec::new(),
cursor_held_item: Slot::Empty, cursor_held_item: None,
} }
} }
@ -785,12 +785,10 @@ impl<C: Config> Client<C> {
C2sPlayPacket::ClickContainer(p) => { C2sPlayPacket::ClickContainer(p) => {
if p.slot_idx == -999 { if p.slot_idx == -999 {
// client is trying to drop the currently held stack // 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 { match held {
Slot::Empty => {} None => {}
Slot::Present(stack) => { Some(stack) => self.events.push_back(ClientEvent::DropItemStack { stack }),
self.events.push_back(ClientEvent::DropItemStack { stack })
}
} }
} else { } else {
self.cursor_held_item = p.carried_item.clone(); self.cursor_held_item = p.carried_item.clone();
@ -971,12 +969,10 @@ impl<C: Config> Client<C> {
if e.slot == -1 { if e.slot == -1 {
// The client is trying to drop a stack of items // The client is trying to drop a stack of items
match e.clicked_item { match e.clicked_item {
Slot::Empty => log::warn!( None => log::warn!(
"Invalid packet, creative client tried to drop a stack of nothing." "Invalid packet, creative client tried to drop a stack of nothing."
), ),
Slot::Present(stack) => { Some(stack) => self.events.push_back(ClientEvent::DropItemStack { stack }),
self.events.push_back(ClientEvent::DropItemStack { stack })
}
} }
} else { } else {
self.events.push_back(ClientEvent::SetSlotCreative { self.events.push_back(ClientEvent::SetSlotCreative {

View file

@ -9,18 +9,13 @@ use crate::protocol::{Decode, Encode};
pub type SlotId = i16; pub type SlotId = i16;
/// Represents a slot in an inventory. /// Represents a slot in an inventory.
#[derive(Clone, Default, Debug)] pub type Slot = Option<ItemStack>;
pub enum Slot {
#[default]
Empty,
Present(ItemStack),
}
impl Encode for Slot { impl Encode for Slot {
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> { fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
match self { match self {
Slot::Empty => false.encode(w), None => false.encode(w),
Slot::Present(s) => { Some(s) => {
true.encode(w)?; true.encode(w)?;
s.item.encode(w)?; s.item.encode(w)?;
s.item_count.encode(w)?; s.item_count.encode(w)?;
@ -37,9 +32,9 @@ impl Decode for Slot {
fn decode(r: &mut &[u8]) -> anyhow::Result<Self> { fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
let present = bool::decode(r)?; let present = bool::decode(r)?;
if !present { if !present {
return Ok(Slot::Empty); return Ok(None);
} }
Ok(Slot::Present(ItemStack { Ok(Some(ItemStack {
item: ItemKind::decode(r)?, item: ItemKind::decode(r)?,
item_count: u8::decode(r)?, item_count: u8::decode(r)?,
nbt: if r.first() == Some(&0) { nbt: if r.first() == Some(&0) {
@ -51,23 +46,3 @@ impl Decode for Slot {
})) }))
} }
} }
impl From<Option<ItemStack>> for Slot {
fn from(s: Option<ItemStack>) -> Self {
if let Some(s) = s {
Slot::Present(s)
} else {
Slot::Empty
}
}
}
impl From<Slot> for Option<ItemStack> {
fn from(s: Slot) -> Self {
if let Slot::Present(s) = s {
Some(s)
} else {
None
}
}
}