Skip to content

Commit

Permalink
Simply abstraction of property trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryntet committed Aug 24, 2024
1 parent ef21b3a commit 52943f6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 67 deletions.
38 changes: 2 additions & 36 deletions pumpkin-inventory/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(()),
}
}
}
46 changes: 16 additions & 30 deletions pumpkin-inventory/src/window_property.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}

Expand All @@ -21,20 +28,14 @@ impl<T: WindowPropertyTrait> WindowProperty<T> {
(self.window_property.to_id(), self.value)
}
}

#[derive(ToPrimitive)]
pub enum Furnace {
FireIcon,
MaximumFuelBurnTime,
ProgressArrow,
MaximumProgress,
}

impl WindowPropertyTrait for Furnace {
fn to_id(self) -> i16 {
self as i16
}
}

pub enum EnchantmentTable {
LevelRequirement { slot: u8 },
EnchantmentSeed,
Expand All @@ -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 {}
2 changes: 1 addition & 1 deletion pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit 52943f6

Please sign in to comment.