From 52943f66c876bcb7eb1f5422329d297c7d054656 Mon Sep 17 00:00:00 2001 From: Edvin Bryntesson Date: Sat, 24 Aug 2024 18:03:32 +0200 Subject: [PATCH] Simply abstraction of property trait --- pumpkin-inventory/src/lib.rs | 38 ++------------------ pumpkin-inventory/src/window_property.rs | 46 +++++++++--------------- pumpkin/src/client/player_packet.rs | 2 +- 3 files changed, 19 insertions(+), 67 deletions(-) diff --git a/pumpkin-inventory/src/lib.rs b/pumpkin-inventory/src/lib.rs index 3595bd6c2..dadb10cc6 100644 --- a/pumpkin-inventory/src/lib.rs +++ b/pumpkin-inventory/src/lib.rs @@ -1,10 +1,10 @@ -use num_derive::ToPrimitive; +use num_derive::{FromPrimitive, ToPrimitive}; pub mod player; pub mod window_property; /// https://wiki.vg/Inventory -#[derive(Debug, ToPrimitive, Clone)] +#[derive(Debug, ToPrimitive, FromPrimitive, Clone)] pub enum WindowType { // not used Generic9x1, @@ -52,37 +52,3 @@ impl WindowType { "WINDOW TITLE" } } -impl TryFrom for WindowType { - type Error = (); - - fn try_from(value: u8) -> Result { - match value { - 0 => Ok(WindowType::Generic9x1), - 1 => Ok(WindowType::Generic9x2), - 2 => Ok(WindowType::Generic9x3), - 3 => Ok(WindowType::Generic9x4), - 4 => Ok(WindowType::Generic9x5), - 5 => Ok(WindowType::Generic9x6), - 6 => Ok(WindowType::Generic3x3), - 7 => Ok(WindowType::Craft3x3), - 8 => Ok(WindowType::Anvil), - 9 => Ok(WindowType::Beacon), - 10 => Ok(WindowType::BlastFurnace), - 11 => Ok(WindowType::BrewingStand), - 12 => Ok(WindowType::CraftingTable), - 13 => Ok(WindowType::EnchantmentTable), - 14 => Ok(WindowType::Furnace), - 15 => Ok(WindowType::Grindstone), - 16 => Ok(WindowType::Hopper), - 17 => Ok(WindowType::Lectern), - 18 => Ok(WindowType::Loom), - 19 => Ok(WindowType::Merchant), - 20 => Ok(WindowType::ShulkerBox), - 21 => Ok(WindowType::SmithingTable), - 22 => Ok(WindowType::Smoker), - 23 => Ok(WindowType::CartographyTable), - 24 => Ok(WindowType::Stonecutter), - _ => Err(()), - } - } -} diff --git a/pumpkin-inventory/src/window_property.rs b/pumpkin-inventory/src/window_property.rs index 67deb646e..56f4b2942 100644 --- a/pumpkin-inventory/src/window_property.rs +++ b/pumpkin-inventory/src/window_property.rs @@ -1,6 +1,13 @@ -pub trait WindowPropertyTrait: Sized { +use num_derive::ToPrimitive; +use num_traits::ToPrimitive; + +pub trait WindowPropertyTrait { + fn to_id(self) -> i16; +} + +impl WindowPropertyTrait for T { fn to_id(self) -> i16 { - 0 + self.to_i16().unwrap() } } @@ -21,7 +28,7 @@ impl WindowProperty { (self.window_property.to_id(), self.value) } } - +#[derive(ToPrimitive)] pub enum Furnace { FireIcon, MaximumFuelBurnTime, @@ -29,12 +36,6 @@ pub enum Furnace { MaximumProgress, } -impl WindowPropertyTrait for Furnace { - fn to_id(self) -> i16 { - self as i16 - } -} - pub enum EnchantmentTable { LevelRequirement { slot: u8 }, EnchantmentSeed, @@ -54,50 +55,35 @@ impl WindowPropertyTrait for EnchantmentTable { }) as i16 } } - +#[derive(ToPrimitive)] pub enum Beacon { PowerLevel, FirstPotionEffect, SecondPotionEffect, } -impl WindowPropertyTrait for Beacon { - fn to_id(self) -> i16 { - self as i16 - } -} - +#[derive(ToPrimitive)] pub enum Anvil { RepairCost, } -impl WindowPropertyTrait for Anvil {} - +#[derive(ToPrimitive)] pub enum BrewingStand { BrewTime, FuelTime, } -impl WindowPropertyTrait for BrewingStand { - fn to_id(self) -> i16 { - self as i16 - } -} - +#[derive(ToPrimitive)] pub enum Stonecutter { SelectedRecipe, } -impl WindowPropertyTrait for Stonecutter {} - +#[derive(ToPrimitive)] pub enum Loom { SelectedPattern, } -impl WindowPropertyTrait for Loom {} - +#[derive(ToPrimitive)] pub enum Lectern { PageNumber, } - -impl WindowPropertyTrait for Lectern {} diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index 27fe5e94c..5f4c0c61c 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -422,7 +422,7 @@ impl Client { // But this is not possible yet pub fn handle_close_container(&mut self, _server: &mut Server, packet: SCloseContainer) { // window_id 0 represents both 9x1 Generic AND inventory here - let Ok(_window_type) = WindowType::try_from(packet.window_id) else { + let Some(_window_type) = WindowType::from_u8(packet.window_id) else { self.kick("Invalid window ID"); return; };