Simply abstraction of property trait

This commit is contained in:
Edvin Bryntesson 2024-08-24 18:03:32 +02:00
parent ef21b3ae40
commit 52943f66c8
No known key found for this signature in database
3 changed files with 19 additions and 67 deletions

View File

@ -1,10 +1,10 @@
use num_derive::ToPrimitive; use num_derive::{FromPrimitive, ToPrimitive};
pub mod player; pub mod player;
pub mod window_property; pub mod window_property;
/// https://wiki.vg/Inventory /// https://wiki.vg/Inventory
#[derive(Debug, ToPrimitive, Clone)] #[derive(Debug, ToPrimitive, FromPrimitive, Clone)]
pub enum WindowType { pub enum WindowType {
// not used // not used
Generic9x1, Generic9x1,
@ -52,37 +52,3 @@ impl WindowType {
"WINDOW TITLE" "WINDOW TITLE"
} }
} }
impl TryFrom<u8> for WindowType {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
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(()),
}
}
}

View File

@ -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<T: ToPrimitive> WindowPropertyTrait for T {
fn to_id(self) -> i16 { fn to_id(self) -> i16 {
0 self.to_i16().unwrap()
} }
} }
@ -21,7 +28,7 @@ impl<T: WindowPropertyTrait> WindowProperty<T> {
(self.window_property.to_id(), self.value) (self.window_property.to_id(), self.value)
} }
} }
#[derive(ToPrimitive)]
pub enum Furnace { pub enum Furnace {
FireIcon, FireIcon,
MaximumFuelBurnTime, MaximumFuelBurnTime,
@ -29,12 +36,6 @@ pub enum Furnace {
MaximumProgress, MaximumProgress,
} }
impl WindowPropertyTrait for Furnace {
fn to_id(self) -> i16 {
self as i16
}
}
pub enum EnchantmentTable { pub enum EnchantmentTable {
LevelRequirement { slot: u8 }, LevelRequirement { slot: u8 },
EnchantmentSeed, EnchantmentSeed,
@ -54,50 +55,35 @@ impl WindowPropertyTrait for EnchantmentTable {
}) as i16 }) as i16
} }
} }
#[derive(ToPrimitive)]
pub enum Beacon { pub enum Beacon {
PowerLevel, PowerLevel,
FirstPotionEffect, FirstPotionEffect,
SecondPotionEffect, SecondPotionEffect,
} }
impl WindowPropertyTrait for Beacon { #[derive(ToPrimitive)]
fn to_id(self) -> i16 {
self as i16
}
}
pub enum Anvil { pub enum Anvil {
RepairCost, RepairCost,
} }
impl WindowPropertyTrait for Anvil {} #[derive(ToPrimitive)]
pub enum BrewingStand { pub enum BrewingStand {
BrewTime, BrewTime,
FuelTime, FuelTime,
} }
impl WindowPropertyTrait for BrewingStand { #[derive(ToPrimitive)]
fn to_id(self) -> i16 {
self as i16
}
}
pub enum Stonecutter { pub enum Stonecutter {
SelectedRecipe, SelectedRecipe,
} }
impl WindowPropertyTrait for Stonecutter {} #[derive(ToPrimitive)]
pub enum Loom { pub enum Loom {
SelectedPattern, SelectedPattern,
} }
impl WindowPropertyTrait for Loom {} #[derive(ToPrimitive)]
pub enum Lectern { pub enum Lectern {
PageNumber, PageNumber,
} }
impl WindowPropertyTrait for Lectern {}

View File

@ -422,7 +422,7 @@ impl Client {
// But this is not possible yet // But this is not possible yet
pub fn handle_close_container(&mut self, _server: &mut Server, packet: SCloseContainer) { pub fn handle_close_container(&mut self, _server: &mut Server, packet: SCloseContainer) {
// window_id 0 represents both 9x1 Generic AND inventory here // 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"); self.kick("Invalid window ID");
return; return;
}; };