Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 17 additions & 14 deletions core/anvil/src/player.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::entity::BaseEntityData;
use feather_inventory::{
SlotIndex, HOTBAR_SIZE, INVENTORY_SIZE, SLOT_ARMOR_MAX, SLOT_ARMOR_MIN, SLOT_HOTBAR_OFFSET,
use feather_inventory::player_constants::{
HOTBAR_SIZE, INVENTORY_SIZE, SLOT_ARMOR_MAX, SLOT_ARMOR_MIN, SLOT_HOTBAR_OFFSET,
SLOT_INVENTORY_OFFSET, SLOT_OFFHAND,
};
use feather_items::{Item, ItemStack};
Expand Down Expand Up @@ -45,7 +45,7 @@ impl InventorySlot {

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

Self {
Some(Self {
count: stack.amount as i8,
slot,
item: stack.ty.identifier().to_string(),
}
})
}

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

// Equipment
map.insert(103, feather_inventory::SLOT_ARMOR_HEAD);
map.insert(102, feather_inventory::SLOT_ARMOR_CHEST);
map.insert(101, feather_inventory::SLOT_ARMOR_LEGS);
map.insert(100, feather_inventory::SLOT_ARMOR_FEET);
map.insert(-106, feather_inventory::SLOT_OFFHAND);
map.insert(103, SLOT_ARMOR_HEAD);
map.insert(102, SLOT_ARMOR_CHEST);
map.insert(101, SLOT_ARMOR_LEGS);
map.insert(100, SLOT_ARMOR_FEET);
map.insert(-106, SLOT_OFFHAND);

// Hotbar
for x in 0..9 {
map.insert(x, feather_inventory::SLOT_HOTBAR_OFFSET + (x as usize));
map.insert(x, SLOT_HOTBAR_OFFSET + (x as usize));
}

// Rest of inventory
Expand All @@ -199,7 +202,7 @@ mod tests {
assert_eq!(slot.convert_index().unwrap(), expected);
assert_eq!(
InventorySlot::from_network_index(expected, ItemStack::new(Item::Stone, 1)),
slot
Some(slot),
);
}

Expand Down
9 changes: 7 additions & 2 deletions core/inventory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ edition = "2018"

[dependencies]
feather-items = { path = "../items" }
smallvec = "1.3"
once_cell = "1.3"

fecs = { git = "https://github.com/feather-rs/fecs", rev = "fed8bcb516941b12cb980e354e77b699be075a89" }
legion = { git = "https://github.com/TomGillen/legion", rev = "bd441f4811e7a9e877a0f479a674bbdbf4e4cda3" }
thiserror = "1.0"
parking_lot = "0.10"
maplit = "1.0"
smallvec = "1.4"
once_cell = "1.3"
Loading