forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.rs
More file actions
57 lines (52 loc) · 1.88 KB
/
Copy pathinventory.rs
File metadata and controls
57 lines (52 loc) · 1.88 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
use feather_core::inventory::{
SlotIndex, SLOT_ARMOR_CHEST, SLOT_ARMOR_FEET, SLOT_ARMOR_HEAD, SLOT_ARMOR_LEGS,
SLOT_HOTBAR_OFFSET, SLOT_OFFHAND,
};
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(SLOT_HOTBAR_OFFSET + held_item).copied()
}
}
/// 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) -> Option<Self> {
match index {
SLOT_OFFHAND => Some(Equipment::OffHand),
SLOT_ARMOR_FEET => Some(Equipment::Boots),
SLOT_ARMOR_LEGS => Some(Equipment::Leggings),
SLOT_ARMOR_CHEST => Some(Equipment::Chestplate),
SLOT_ARMOR_HEAD => Some(Equipment::Helmet),
_ => None,
}
}
pub fn slot_index(self, held_item: SlotIndex) -> SlotIndex {
match self {
Equipment::MainHand => held_item + SLOT_HOTBAR_OFFSET,
Equipment::OffHand => SLOT_OFFHAND,
Equipment::Boots => SLOT_ARMOR_FEET,
Equipment::Leggings => SLOT_ARMOR_LEGS,
Equipment::Chestplate => SLOT_ARMOR_CHEST,
Equipment::Helmet => SLOT_ARMOR_HEAD,
}
}
}