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 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<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 {
0
self.to_i16().unwrap()
}
}
@ -21,7 +28,7 @@ impl<T: WindowPropertyTrait> WindowProperty<T> {
(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 {}

View File

@ -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;
};