Skip to content

Commit 615ded0

Browse files
authored
Merge pull request #221 from feather-rs/inventory-refactor
Implement a new inventory API The old inventory design had a key problem: It could not correctly handle slot indexing in different contexts. As per this page, indices in the protocol take different forms depending on the current window. The old API used raw indices based on those used for the main inventory context, but this cannot be extended to work with other windows, such as crafting tables, chests, etc. The new design hopes to solve the above. Resolves #79.
2 parents 9477a19 + f4db3e7 commit 615ded0

File tree

13 files changed

+718
-529
lines changed

13 files changed

+718
-529
lines changed

Cargo.lock

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/anvil/src/player.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::entity::BaseEntityData;
2-
use feather_inventory::{
3-
SlotIndex, HOTBAR_SIZE, INVENTORY_SIZE, SLOT_ARMOR_MAX, SLOT_ARMOR_MIN, SLOT_HOTBAR_OFFSET,
2+
use feather_inventory::player_constants::{
3+
HOTBAR_SIZE, INVENTORY_SIZE, SLOT_ARMOR_MAX, SLOT_ARMOR_MIN, SLOT_HOTBAR_OFFSET,
44
SLOT_INVENTORY_OFFSET, SLOT_OFFHAND,
55
};
66
use feather_items::{Item, ItemStack};
@@ -45,7 +45,7 @@ impl InventorySlot {
4545

4646
/// Converts a network protocol index, item, and count
4747
/// to an `InventorySlot`.
48-
pub fn from_network_index(network: SlotIndex, stack: ItemStack) -> Self {
48+
pub fn from_network_index(network: usize, stack: ItemStack) -> Option<Self> {
4949
let slot = if SLOT_HOTBAR_OFFSET <= network && network < SLOT_HOTBAR_OFFSET + HOTBAR_SIZE {
5050
// Hotbar
5151
(network - SLOT_HOTBAR_OFFSET) as i8
@@ -58,19 +58,19 @@ impl InventorySlot {
5858
{
5959
network as i8
6060
} else {
61-
panic!("Invalid slot index {} on server", network);
61+
return None;
6262
};
6363

64-
Self {
64+
Some(Self {
6565
count: stack.amount as i8,
6666
slot,
6767
item: stack.ty.identifier().to_string(),
68-
}
68+
})
6969
}
7070

7171
/// Converts an NBT inventory index to a network protocol index.
7272
/// Returns None if the index is invalid.
73-
pub fn convert_index(&self) -> Option<SlotIndex> {
73+
pub fn convert_index(&self) -> Option<usize> {
7474
if 0 <= self.slot && self.slot <= 8 {
7575
// Hotbar
7676
Some(SLOT_HOTBAR_OFFSET + (self.slot as usize))
@@ -131,6 +131,9 @@ fn file_path(world_dir: &Path, uuid: Uuid) -> PathBuf {
131131
#[cfg(test)]
132132
mod tests {
133133
use super::*;
134+
use feather_inventory::player_constants::{
135+
SLOT_ARMOR_CHEST, SLOT_ARMOR_FEET, SLOT_ARMOR_HEAD, SLOT_ARMOR_LEGS,
136+
};
134137
use feather_util::Gamemode;
135138
use std::collections::HashMap;
136139
use std::io::Cursor;
@@ -173,15 +176,15 @@ mod tests {
173176
let mut map: HashMap<i8, usize> = HashMap::new();
174177

175178
// Equipment
176-
map.insert(103, feather_inventory::SLOT_ARMOR_HEAD);
177-
map.insert(102, feather_inventory::SLOT_ARMOR_CHEST);
178-
map.insert(101, feather_inventory::SLOT_ARMOR_LEGS);
179-
map.insert(100, feather_inventory::SLOT_ARMOR_FEET);
180-
map.insert(-106, feather_inventory::SLOT_OFFHAND);
179+
map.insert(103, SLOT_ARMOR_HEAD);
180+
map.insert(102, SLOT_ARMOR_CHEST);
181+
map.insert(101, SLOT_ARMOR_LEGS);
182+
map.insert(100, SLOT_ARMOR_FEET);
183+
map.insert(-106, SLOT_OFFHAND);
181184

182185
// Hotbar
183186
for x in 0..9 {
184-
map.insert(x, feather_inventory::SLOT_HOTBAR_OFFSET + (x as usize));
187+
map.insert(x, SLOT_HOTBAR_OFFSET + (x as usize));
185188
}
186189

187190
// Rest of inventory
@@ -199,7 +202,7 @@ mod tests {
199202
assert_eq!(slot.convert_index().unwrap(), expected);
200203
assert_eq!(
201204
InventorySlot::from_network_index(expected, ItemStack::new(Item::Stone, 1)),
202-
slot
205+
Some(slot),
203206
);
204207
}
205208

core/inventory/Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ edition = "2018"
66

77
[dependencies]
88
feather-items = { path = "../items" }
9-
smallvec = "1.3"
10-
once_cell = "1.3"
9+
10+
fecs = { git = "https://github.com/feather-rs/fecs", rev = "fed8bcb516941b12cb980e354e77b699be075a89" }
11+
legion = { git = "https://github.com/TomGillen/legion", rev = "bd441f4811e7a9e877a0f479a674bbdbf4e4cda3" }
1112
thiserror = "1.0"
13+
parking_lot = "0.10"
14+
maplit = "1.0"
15+
smallvec = "1.4"
16+
once_cell = "1.3"

0 commit comments

Comments
 (0)