-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathinventory.rs
More file actions
63 lines (58 loc) · 2.02 KB
/
Copy pathinventory.rs
File metadata and controls
63 lines (58 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use feather_core::inventory::{slot, Area, SlotIndex};
use feather_core::items::ItemStack;
use feather_server_types::{HeldItem, Inventory};
use fecs::{Entity, World};
use num_derive::{FromPrimitive, ToPrimitive};
pub trait InventoryExt {
/// Returns the item in the main hand of this entity.
fn item_in_main_hand(&self, entity: Entity, world: &World) -> Option<ItemStack>;
}
impl InventoryExt for Inventory {
fn item_in_main_hand(&self, entity: Entity, world: &World) -> Option<ItemStack> {
let held_item = world.get::<HeldItem>(entity).0;
self.item_at(Area::Hotbar, held_item).unwrap()
}
}
/// An equipment slot, with variants
/// listed in the order of the Entity Equipment
/// IDs to allow for easy conversion using `ToPrimitive`/`FromPrimitive`.
#[derive(Debug, Clone, Copy, ToPrimitive, FromPrimitive, PartialEq, Eq, Hash)]
pub enum Equipment {
MainHand,
OffHand,
Boots,
Leggings,
Chestplate,
Helmet,
}
impl Equipment {
pub fn from_slot_index(index: SlotIndex, held_item: usize) -> Option<Self> {
use feather_core::inventory::Area::*;
match index.area {
Offhand => Some(Equipment::OffHand),
Feet => Some(Equipment::Boots),
Legs => Some(Equipment::Leggings),
Torso => Some(Equipment::Chestplate),
Head => Some(Equipment::Helmet),
Hotbar => {
if index.slot == held_item {
Some(Equipment::MainHand)
} else {
None
}
}
_ => None,
}
}
pub fn slot_index(self, held_item: usize) -> SlotIndex {
use feather_core::inventory::Area::*;
match self {
Equipment::MainHand => slot(Hotbar, held_item),
Equipment::OffHand => slot(Offhand, 0),
Equipment::Boots => slot(Feet, 0),
Equipment::Leggings => slot(Legs, 0),
Equipment::Chestplate => slot(Torso, 0),
Equipment::Helmet => slot(Head, 0),
}
}
}