use crate::VarInt; use pumpkin_world::item::Item; use serde::{ de::{self, SeqAccess, Visitor}, Deserialize, }; #[derive(Debug, Clone)] #[allow(dead_code)] pub struct Slot { item_count: VarInt, item_id: Option, num_components_to_add: Option, num_components_to_remove: Option, components_to_add: Option>, // The second type depends on the varint components_to_remove: Option>, } impl<'de> Deserialize<'de> for Slot { fn deserialize(deserializer: D) -> Result where D: de::Deserializer<'de>, { struct VarIntVisitor; impl<'de> Visitor<'de> for VarIntVisitor { type Value = Slot; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { formatter.write_str("a valid VarInt encoded in a byte sequence") } fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { let item_count = seq .next_element::()? .ok_or(de::Error::custom("Failed to decode VarInt"))?; if item_count.0 == 0 { return Ok(Slot { item_count: 0.into(), item_id: None, num_components_to_add: None, num_components_to_remove: None, components_to_add: None, components_to_remove: None, }); } let item_id = seq .next_element::()? .ok_or(de::Error::custom("Failed to decode VarInt"))?; let num_components_to_add = seq .next_element::()? .ok_or(de::Error::custom("Failed to decode VarInt"))?; let num_components_to_remove = seq .next_element::()? .ok_or(de::Error::custom("Failed to decode VarInt"))?; if num_components_to_add.0 != 0 || num_components_to_remove.0 != 0 { return Err(de::Error::custom( "Slot components are currently unsupported", )); } Ok(Slot { item_count, item_id: Some(item_id), num_components_to_add: Some(num_components_to_add), num_components_to_remove: Some(num_components_to_remove), components_to_add: None, components_to_remove: None, }) } } deserializer.deserialize_seq(VarIntVisitor) } } impl Slot { pub fn to_item(self) -> Option { let item_id = self.item_id?.0.try_into().unwrap(); Some(Item { item_id, item_count: self.item_count.0.try_into().unwrap(), }) } } impl From for Item { fn from(slot: Slot) -> Self { Item { item_count: slot.item_count.0.try_into().unwrap(), item_id: slot.item_id.unwrap().0.try_into().unwrap(), } } }