refactor(inventory): replace per-slot Arc<Mutex<ItemStack>> with container-level synchronization
This commit is contained in:
1 parent
ffcf09ece4
commit
63d97e2c82
121 files changed
+1674
-1931
No files matched your search
@@ -53,10 +53,7 @@ impl AnvilScreenHandler {
|
||||
}
|
||||
|
||||
pub async fn update_result_slot(&mut self) {
|
||||
let input_a = {
|
||||
let lock = self.inventory.get_stack(0).await;
|
||||
lock.lock().await.clone()
|
||||
};
|
||||
let input_a = self.inventory.get_stack(0).await;
|
||||
|
||||
if input_a.is_empty() {
|
||||
self.inventory.set_stack(2, ItemStack::EMPTY.clone()).await;
|
||||
@@ -130,7 +127,7 @@ impl ScreenHandler for AnvilScreenHandler {
|
||||
|
||||
fn quick_move<'a>(
|
||||
&'a mut self,
|
||||
_player: &'a dyn InventoryPlayer,
|
||||
player: &'a dyn InventoryPlayer,
|
||||
slot_index: i32,
|
||||
) -> ItemStackFuture<'a> {
|
||||
Box::pin(async move {
|
||||
@@ -138,40 +135,31 @@ impl ScreenHandler for AnvilScreenHandler {
|
||||
let slot = self.get_behaviour().slots[slot_index as usize].clone();
|
||||
|
||||
if slot.has_stack().await {
|
||||
let slot_stack_lock = slot.get_stack().await;
|
||||
let slot_stack_guard = slot_stack_lock.lock().await;
|
||||
stack_left = slot_stack_guard.clone();
|
||||
drop(slot_stack_guard);
|
||||
|
||||
let mut slot_stack_mut = slot_stack_lock.lock().await;
|
||||
let mut slot_stack = slot.get_stack().await;
|
||||
stack_left = slot_stack.clone();
|
||||
|
||||
if slot_index < 3 {
|
||||
// From anvil to player
|
||||
if !self
|
||||
.insert_item(
|
||||
&mut slot_stack_mut,
|
||||
3,
|
||||
self.get_behaviour().slots.len() as i32,
|
||||
true,
|
||||
)
|
||||
.await
|
||||
{
|
||||
if !self.insert_item(&mut slot_stack, 3, 39, true).await {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
slot.on_quick_move_crafted(slot_stack.clone(), stack_left.clone())
|
||||
.await;
|
||||
} else {
|
||||
// From player to anvil input 0 and 1
|
||||
if !self.insert_item(&mut slot_stack_mut, 0, 2, false).await {
|
||||
// From player to anvil
|
||||
if !self.insert_item(&mut slot_stack, 0, 2, false).await {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
}
|
||||
|
||||
if slot_stack_mut.is_empty() {
|
||||
drop(slot_stack_mut);
|
||||
slot.set_stack(ItemStack::EMPTY.clone()).await;
|
||||
} else {
|
||||
drop(slot_stack_mut);
|
||||
slot.mark_dirty().await;
|
||||
if slot_stack.item_count == stack_left.item_count {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
|
||||
slot.set_stack_prev(slot_stack.clone(), stack_left.clone())
|
||||
.await;
|
||||
slot.on_take_item(player, &slot_stack).await;
|
||||
slot.mark_dirty().await;
|
||||
}
|
||||
|
||||
stack_left
|
||||
|
||||
@@ -94,18 +94,14 @@ impl ScreenHandler for BeaconScreenHandler {
|
||||
let slot = self.get_behaviour().slots[slot_index as usize].clone();
|
||||
|
||||
if slot.has_stack().await {
|
||||
let slot_stack_lock = slot.get_stack().await;
|
||||
let slot_stack_guard = slot_stack_lock.lock().await;
|
||||
stack_left = slot_stack_guard.clone();
|
||||
drop(slot_stack_guard);
|
||||
|
||||
let mut slot_stack_mut = slot_stack_lock.lock().await;
|
||||
let mut slot_stack = slot.get_stack().await;
|
||||
stack_left = slot_stack.clone();
|
||||
|
||||
if slot_index == 0 {
|
||||
// Move from the single beacon slot to the player inventory (slots 1 to end)
|
||||
if !self
|
||||
.insert_item(
|
||||
&mut slot_stack_mut,
|
||||
&mut slot_stack,
|
||||
1,
|
||||
self.get_behaviour().slots.len() as i32,
|
||||
true,
|
||||
@@ -116,17 +112,15 @@ impl ScreenHandler for BeaconScreenHandler {
|
||||
}
|
||||
} else {
|
||||
// Move from player inventory into the beacon payment slot (slot 0)
|
||||
if !self.insert_item(&mut slot_stack_mut, 0, 1, false).await {
|
||||
if !self.insert_item(&mut slot_stack, 0, 1, false).await {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
}
|
||||
|
||||
if slot_stack_mut.is_empty() {
|
||||
drop(slot_stack_mut);
|
||||
if slot_stack.is_empty() {
|
||||
slot.set_stack(ItemStack::EMPTY.clone()).await;
|
||||
} else {
|
||||
drop(slot_stack_mut);
|
||||
slot.mark_dirty().await;
|
||||
slot.set_stack(slot_stack).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -142,8 +142,7 @@ impl ScreenHandler for BrewingScreenHandler {
|
||||
return stack_left;
|
||||
}
|
||||
|
||||
let slot_stack_lock = slot.get_stack().await;
|
||||
let mut stack = slot_stack_lock.lock().await;
|
||||
let mut stack = slot.get_stack().await;
|
||||
stack_left = stack.clone();
|
||||
|
||||
let success = if slot_index < 5 {
|
||||
@@ -179,10 +178,9 @@ impl ScreenHandler for BrewingScreenHandler {
|
||||
}
|
||||
|
||||
if stack.is_empty() {
|
||||
drop(stack);
|
||||
slot.set_stack(ItemStack::EMPTY.clone()).await;
|
||||
} else {
|
||||
slot.mark_dirty().await;
|
||||
slot.set_stack(stack).await;
|
||||
}
|
||||
|
||||
stack_left
|
||||
|
||||
@@ -9,12 +9,10 @@
|
||||
//! Unlike regular inventories, crafting grids are typically cleared when
|
||||
//! the container closes, and their contents are used up when crafting.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::{any::Any, pin::Pin};
|
||||
|
||||
use pumpkin_data::item_stack::ItemStack;
|
||||
use pumpkin_world::inventory::split_stack;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use pumpkin_world::inventory::{Clearable, Inventory, InventoryFuture};
|
||||
|
||||
@@ -29,14 +27,14 @@ use super::recipes::RecipeInputInventory;
|
||||
///
|
||||
/// When a player places items in the crafting grid, they are stored here.
|
||||
/// When the crafting result is taken, the ingredients are consumed from this inventory.
|
||||
#[derive(Clone)]
|
||||
#[derive(Default)]
|
||||
pub struct CraftingInventory {
|
||||
/// Width of the crafting grid (typically 2 or 3).
|
||||
pub width: u8,
|
||||
/// Height of the crafting grid (typically 2 or 3).
|
||||
pub height: u8,
|
||||
/// Items in the crafting grid, stored row by row.
|
||||
pub items: Vec<Arc<Mutex<ItemStack>>>,
|
||||
pub items: RwLock<Vec<ItemStack>>,
|
||||
}
|
||||
|
||||
impl CraftingInventory {
|
||||
@@ -60,54 +58,64 @@ impl CraftingInventory {
|
||||
Self {
|
||||
width,
|
||||
height,
|
||||
items: {
|
||||
// Creates a Vec with different Mutexes for each slot
|
||||
let mut v = Vec::with_capacity(width as usize * height as usize);
|
||||
(0..width as usize * height as usize)
|
||||
.for_each(|_| v.push(Arc::new(Mutex::new(ItemStack::EMPTY.clone()))));
|
||||
v
|
||||
},
|
||||
items: RwLock::new(vec![
|
||||
ItemStack::EMPTY.clone();
|
||||
width as usize * height as usize
|
||||
]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Inventory for CraftingInventory {
|
||||
fn size(&self) -> usize {
|
||||
self.items.len()
|
||||
(self.width as usize) * (self.height as usize)
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> InventoryFuture<'_, bool> {
|
||||
Box::pin(async move {
|
||||
for slot in &self.items {
|
||||
if !slot.lock().await.is_empty() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
let items = self.items.read().await;
|
||||
items.iter().all(ItemStack::is_empty)
|
||||
})
|
||||
}
|
||||
|
||||
fn get_stack(&self, slot: usize) -> InventoryFuture<'_, Arc<Mutex<ItemStack>>> {
|
||||
Box::pin(async move { self.items[slot].clone() })
|
||||
fn get_stack(&self, slot: usize) -> InventoryFuture<'_, ItemStack> {
|
||||
Box::pin(async move {
|
||||
let items = self.items.read().await;
|
||||
items
|
||||
.get(slot)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| ItemStack::EMPTY.clone())
|
||||
})
|
||||
}
|
||||
|
||||
fn remove_stack(&self, slot: usize) -> InventoryFuture<'_, ItemStack> {
|
||||
Box::pin(async move {
|
||||
let mut removed = ItemStack::EMPTY.clone();
|
||||
let mut guard = self.items[slot].lock().await;
|
||||
std::mem::swap(&mut removed, &mut *guard);
|
||||
removed
|
||||
let mut items = self.items.write().await;
|
||||
if slot < items.len() {
|
||||
std::mem::replace(&mut items[slot], ItemStack::EMPTY.clone())
|
||||
} else {
|
||||
ItemStack::EMPTY.clone()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn remove_stack_specific(&self, slot: usize, amount: u8) -> InventoryFuture<'_, ItemStack> {
|
||||
Box::pin(async move { split_stack(&self.items, slot, amount).await })
|
||||
Box::pin(async move {
|
||||
let mut items = self.items.write().await;
|
||||
if slot < items.len() && !items[slot].is_empty() && amount > 0 {
|
||||
items[slot].split(amount)
|
||||
} else {
|
||||
ItemStack::EMPTY.clone()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn set_stack(&self, slot: usize, stack: ItemStack) -> InventoryFuture<'_, ()> {
|
||||
Box::pin(async move {
|
||||
*self.items[slot].lock().await = stack;
|
||||
let mut items = self.items.write().await;
|
||||
if slot < items.len() {
|
||||
items[slot] = stack;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -129,9 +137,8 @@ impl RecipeInputInventory for CraftingInventory {
|
||||
impl Clearable for CraftingInventory {
|
||||
fn clear(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
|
||||
Box::pin(async move {
|
||||
for item in &self.items {
|
||||
*item.lock().await = ItemStack::EMPTY.clone();
|
||||
}
|
||||
let mut items = self.items.write().await;
|
||||
items.fill_with(|| ItemStack::EMPTY.clone());
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -113,7 +113,7 @@ async fn recipe_matches(
|
||||
.get_stack((y + y_offset) * inventory.get_width() + (x + x_offset))
|
||||
.await;
|
||||
if current_key == ' ' {
|
||||
if !slot.lock().await.is_empty() {
|
||||
if !slot.is_empty() {
|
||||
matched = false;
|
||||
break 'outer;
|
||||
}
|
||||
@@ -125,8 +125,6 @@ async fn recipe_matches(
|
||||
.find_map(|(k, v)| (*k == current_key).then_some(v))
|
||||
.expect("Crafting recipe used invalid key");
|
||||
|
||||
let slot = slot.lock().await;
|
||||
|
||||
if !ingredient.match_item(slot.item) {
|
||||
matched = false;
|
||||
break 'outer;
|
||||
@@ -146,7 +144,7 @@ async fn recipe_matches(
|
||||
)
|
||||
.await;
|
||||
if current_key == ' ' {
|
||||
if !slot.lock().await.is_empty() {
|
||||
if !slot.is_empty() {
|
||||
matched = false;
|
||||
break 'outer;
|
||||
}
|
||||
@@ -156,7 +154,6 @@ async fn recipe_matches(
|
||||
.iter()
|
||||
.find_map(|(k, v)| (*k == current_key).then_some(v))
|
||||
.expect("Crafting recipe used invalid key");
|
||||
let slot = slot.lock().await;
|
||||
if !ingredient.match_item(slot.item) {
|
||||
matched = false;
|
||||
break 'outer;
|
||||
@@ -181,7 +178,6 @@ async fn recipe_matches(
|
||||
let mut ingredient_used = vec![false; ingredients.len()];
|
||||
'next_slot: for i in 0..inventory.size() {
|
||||
let slot = inventory.get_stack(i).await;
|
||||
let slot = slot.lock().await;
|
||||
if slot.is_empty() {
|
||||
continue 'next_slot;
|
||||
}
|
||||
@@ -209,7 +205,6 @@ async fn recipe_matches(
|
||||
}
|
||||
'item_stack: for i in 0..inventory.size() {
|
||||
let slot = inventory.get_stack(i).await;
|
||||
let slot = slot.lock().await;
|
||||
if slot.is_empty() {
|
||||
continue 'item_stack;
|
||||
}
|
||||
@@ -228,7 +223,6 @@ async fn recipe_matches(
|
||||
}
|
||||
for position in (1..=7).step_by(2) {
|
||||
let slot = inventory.get_stack(position).await;
|
||||
let slot = slot.lock().await;
|
||||
if slot.is_empty()
|
||||
|| !slot
|
||||
.item
|
||||
@@ -268,7 +262,7 @@ async fn recipe_matches(
|
||||
.get_stack((y + y_offset) * inventory.get_width() + (x + x_offset))
|
||||
.await;
|
||||
if current_key == ' ' {
|
||||
if !slot.lock().await.is_empty() {
|
||||
if !slot.is_empty() {
|
||||
matched = false;
|
||||
break 'outer;
|
||||
}
|
||||
@@ -279,7 +273,6 @@ async fn recipe_matches(
|
||||
.find(|(k, _)| *k == current_key)
|
||||
.map(|(_, v)| v)
|
||||
.expect("Crafting recipe used invalid key");
|
||||
let slot = slot.lock().await;
|
||||
if !ingredient.match_item(slot.item) {
|
||||
matched = false;
|
||||
break 'outer;
|
||||
@@ -302,7 +295,6 @@ async fn recipe_matches(
|
||||
let mut ingredient_used = vec![false; ingredients.len()];
|
||||
'next_slot: for i in 0..inventory.size() {
|
||||
let slot = inventory.get_stack(i).await;
|
||||
let slot = slot.lock().await;
|
||||
if slot.is_empty() {
|
||||
continue 'next_slot;
|
||||
}
|
||||
@@ -347,7 +339,6 @@ impl ResultSlot {
|
||||
let x = i % inventory_width;
|
||||
let y = i / inventory_width;
|
||||
let slot = self.inventory.get_stack(i).await;
|
||||
let slot = slot.lock().await;
|
||||
if !slot.is_empty() {
|
||||
top_x = top_x.min(x);
|
||||
top_y = top_y.min(y);
|
||||
@@ -450,11 +441,7 @@ impl Slot for ResultSlot {
|
||||
)
|
||||
.await;
|
||||
for i in 0..self.inventory.size() {
|
||||
let slot = self.inventory.get_stack(i).await;
|
||||
let mut stack = slot.lock().await;
|
||||
if !stack.is_empty() {
|
||||
stack.item_count -= 1;
|
||||
}
|
||||
self.inventory.remove_stack_specific(i, 1).await;
|
||||
}
|
||||
self.mark_dirty().await;
|
||||
})
|
||||
@@ -462,8 +449,8 @@ impl Slot for ResultSlot {
|
||||
fn can_insert(&self, _stack: &ItemStack) -> BoxFuture<'_, bool> {
|
||||
Box::pin(async move { false })
|
||||
}
|
||||
fn get_stack(&self) -> BoxFuture<'_, Arc<Mutex<ItemStack>>> {
|
||||
Box::pin(async move { self.result.clone() })
|
||||
fn get_stack(&self) -> BoxFuture<'_, ItemStack> {
|
||||
Box::pin(async move { self.result.lock().await.clone() })
|
||||
}
|
||||
fn get_cloned_stack(&self) -> BoxFuture<'_, ItemStack> {
|
||||
Box::pin(async move { self.result.lock().await.clone() })
|
||||
@@ -491,7 +478,6 @@ impl Slot for ResultSlot {
|
||||
let mut count = u8::MAX;
|
||||
for i in 0..self.inventory.size() {
|
||||
let slot = self.inventory.get_stack(i).await;
|
||||
let slot = slot.lock().await;
|
||||
if !slot.is_empty() {
|
||||
count = count.min(slot.item_count);
|
||||
}
|
||||
@@ -613,8 +599,7 @@ impl ScreenHandler for CraftingTableScreenHandler {
|
||||
Box::pin(async move {
|
||||
let slot = self.get_behaviour().slots[slot_index as usize].clone();
|
||||
if slot.has_stack().await {
|
||||
let slot_stack = slot.get_stack().await;
|
||||
let mut slot_stack = slot_stack.lock().await;
|
||||
let mut slot_stack = slot.get_stack().await;
|
||||
let stack_prev = slot_stack.clone();
|
||||
if slot_index == 0 {
|
||||
if !self.insert_item(&mut slot_stack, 10, 46, true).await {
|
||||
|
||||
@@ -12,7 +12,6 @@ use std::{any::Any, pin::Pin, sync::Arc};
|
||||
|
||||
use pumpkin_data::item_stack::ItemStack;
|
||||
use pumpkin_world::inventory::{Clearable, Inventory, InventoryFuture};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
/// A composite inventory combining two inventories.
|
||||
///
|
||||
@@ -49,7 +48,7 @@ impl Inventory for DoubleInventory {
|
||||
Box::pin(async move { self.first.is_empty().await && self.second.is_empty().await })
|
||||
}
|
||||
|
||||
fn get_stack(&self, slot: usize) -> InventoryFuture<'_, Arc<Mutex<ItemStack>>> {
|
||||
fn get_stack(&self, slot: usize) -> InventoryFuture<'_, ItemStack> {
|
||||
Box::pin(async move {
|
||||
if slot >= self.first.size() {
|
||||
self.second.get_stack(slot - self.first.size()).await
|
||||
|
||||
@@ -95,7 +95,6 @@ impl EnchantingTableScreenHandler {
|
||||
|
||||
pub async fn update_enchantments(&mut self, _player: &dyn InventoryPlayer) {
|
||||
let item = self.inventory.get_stack(0).await;
|
||||
let item = item.lock().await;
|
||||
|
||||
if item.is_empty() || item.has_enchantments() {
|
||||
for i in 0..3 {
|
||||
@@ -349,8 +348,7 @@ impl ScreenHandler for EnchantingTableScreenHandler {
|
||||
return false;
|
||||
}
|
||||
|
||||
let lapis_slot = self.inventory.get_stack(1).await;
|
||||
let mut lapis_stack = lapis_slot.lock().await;
|
||||
let mut lapis_stack = self.inventory.get_stack(1).await;
|
||||
let lapis_cost = (id + 1) as u8;
|
||||
|
||||
if !player.is_creative()
|
||||
@@ -362,8 +360,7 @@ impl ScreenHandler for EnchantingTableScreenHandler {
|
||||
}
|
||||
|
||||
// Perform enchantment
|
||||
let item_slot = self.inventory.get_stack(0).await;
|
||||
let mut item_stack = item_slot.lock().await;
|
||||
let mut item_stack = self.inventory.get_stack(0).await;
|
||||
|
||||
if item_stack.is_empty() || item_stack.has_enchantments() {
|
||||
return false;
|
||||
@@ -380,16 +377,13 @@ impl ScreenHandler for EnchantingTableScreenHandler {
|
||||
if !player.is_creative() {
|
||||
player.add_experience_levels(-(id + 1)).await;
|
||||
lapis_stack.decrement(lapis_cost);
|
||||
self.inventory.set_stack(1, lapis_stack).await;
|
||||
}
|
||||
|
||||
for (enchant, level) in enchantments {
|
||||
item_stack.add_enchantment(enchant, level as u16);
|
||||
}
|
||||
|
||||
// CRITICAL FIX: Drop locks *before* calling `update_enchantments`
|
||||
// Otherwise, update_enchantments will try to lock slot 0 again and deadlock!
|
||||
drop(item_stack);
|
||||
drop(lapis_stack);
|
||||
self.inventory.set_stack(0, item_stack).await;
|
||||
|
||||
// Update seed
|
||||
player.set_enchantment_seed(rand::random()).await;
|
||||
@@ -420,18 +414,14 @@ impl ScreenHandler for EnchantingTableScreenHandler {
|
||||
let slot = self.get_behaviour().slots[slot_index as usize].clone();
|
||||
|
||||
if slot.has_stack().await {
|
||||
let slot_stack_lock = slot.get_stack().await;
|
||||
let slot_stack_guard = slot_stack_lock.lock().await;
|
||||
stack_left = slot_stack_guard.clone();
|
||||
drop(slot_stack_guard);
|
||||
|
||||
let mut slot_stack_mut = slot_stack_lock.lock().await;
|
||||
let mut slot_stack = slot.get_stack().await;
|
||||
stack_left = slot_stack.clone();
|
||||
|
||||
if slot_index < 2 {
|
||||
// From enchanting to player
|
||||
if !self
|
||||
.insert_item(
|
||||
&mut slot_stack_mut,
|
||||
&mut slot_stack,
|
||||
2,
|
||||
self.get_behaviour().slots.len() as i32,
|
||||
true,
|
||||
@@ -443,21 +433,19 @@ impl ScreenHandler for EnchantingTableScreenHandler {
|
||||
} else {
|
||||
// From player to enchanting
|
||||
// Lapis check
|
||||
if slot_stack_mut.item == &Item::LAPIS_LAZULI {
|
||||
if !self.insert_item(&mut slot_stack_mut, 1, 2, false).await {
|
||||
if slot_stack.item == &Item::LAPIS_LAZULI {
|
||||
if !self.insert_item(&mut slot_stack, 1, 2, false).await {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
} else if !self.insert_item(&mut slot_stack_mut, 0, 1, false).await {
|
||||
} else if !self.insert_item(&mut slot_stack, 0, 1, false).await {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
}
|
||||
|
||||
if slot_stack_mut.is_empty() {
|
||||
drop(slot_stack_mut);
|
||||
if slot_stack.is_empty() {
|
||||
slot.set_stack(ItemStack::EMPTY.clone()).await;
|
||||
} else {
|
||||
drop(slot_stack_mut);
|
||||
slot.mark_dirty().await;
|
||||
slot.set_stack(slot_stack).await;
|
||||
}
|
||||
|
||||
// CRITICAL FIX: Ensure the client is notified when shift-clicking items into the slots
|
||||
|
||||
@@ -7,11 +7,10 @@
|
||||
//! the entity model (armor is rendered on the player, held items are visible
|
||||
//! in hands).
|
||||
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use pumpkin_data::data_component_impl::EquipmentSlot;
|
||||
use pumpkin_data::item_stack::ItemStack;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
/// Equipment storage for an entity.
|
||||
///
|
||||
@@ -21,19 +20,12 @@ use tokio::sync::Mutex;
|
||||
///
|
||||
/// See also: [`EquipmentSlot`](EquipmentSlot)
|
||||
// EntityEquipment.java
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Default)]
|
||||
pub struct EntityEquipment {
|
||||
/// Map of equipment slots to their equipped items.
|
||||
///
|
||||
/// Keys are equipment slot types (head, chest, legs, feet, off-hand).
|
||||
/// Values are mutex-protected item stacks for thread-safe access.
|
||||
pub equipment: HashMap<EquipmentSlot, Arc<Mutex<ItemStack>>>,
|
||||
}
|
||||
|
||||
impl Default for EntityEquipment {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
pub equipment: HashMap<EquipmentSlot, ItemStack>,
|
||||
}
|
||||
|
||||
impl EntityEquipment {
|
||||
@@ -53,27 +45,10 @@ impl EntityEquipment {
|
||||
///
|
||||
/// # Returns
|
||||
/// The previously equipped item, or an empty stack if the slot was empty.
|
||||
pub async fn put(&mut self, slot: &EquipmentSlot, stack: ItemStack) -> ItemStack {
|
||||
pub fn put(&mut self, slot: &EquipmentSlot, stack: ItemStack) -> ItemStack {
|
||||
self.equipment
|
||||
.insert(slot.clone(), Arc::new(Mutex::new(stack)))
|
||||
.unwrap_or(Arc::new(Mutex::new(ItemStack::EMPTY.clone())))
|
||||
.lock()
|
||||
.await
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// Gets or inserts an empty stack for a slot.
|
||||
///
|
||||
/// If the slot doesn't exist, creates it with an empty stack.
|
||||
///
|
||||
/// # Returns
|
||||
/// A mutex-protected item stack for this slot.
|
||||
#[must_use]
|
||||
pub fn get_or_insert(&mut self, slot: &EquipmentSlot) -> Arc<Mutex<ItemStack>> {
|
||||
self.equipment
|
||||
.entry(slot.clone())
|
||||
.or_insert_with(|| Arc::new(Mutex::new(ItemStack::EMPTY.clone())))
|
||||
.clone()
|
||||
.insert(slot.clone(), stack)
|
||||
.unwrap_or_else(|| ItemStack::EMPTY.clone())
|
||||
}
|
||||
|
||||
/// Gets the item in a slot.
|
||||
@@ -81,22 +56,16 @@ impl EntityEquipment {
|
||||
/// # Returns
|
||||
/// The equipped item, or an empty stack if nothing is equipped.
|
||||
#[must_use]
|
||||
pub fn get(&self, slot: &EquipmentSlot) -> Arc<Mutex<ItemStack>> {
|
||||
pub fn get(&self, slot: &EquipmentSlot) -> ItemStack {
|
||||
self.equipment
|
||||
.get(slot)
|
||||
.cloned()
|
||||
.unwrap_or(Arc::new(Mutex::new(ItemStack::EMPTY.clone())))
|
||||
.unwrap_or_else(|| ItemStack::EMPTY.clone())
|
||||
}
|
||||
|
||||
/// Checks if all equipment slots are empty.
|
||||
pub async fn is_empty(&self) -> bool {
|
||||
for stack in self.equipment.values() {
|
||||
if !stack.lock().await.is_empty() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.equipment.values().all(ItemStack::is_empty)
|
||||
}
|
||||
|
||||
/// Clears all equipped items.
|
||||
|
||||
@@ -179,10 +179,7 @@ impl ScreenHandler for FurnaceLikeScreenHandler {
|
||||
return stack_left;
|
||||
}
|
||||
|
||||
let slot_stack_lock = slot.get_stack().await;
|
||||
|
||||
// Acquire the lock to read/clone the stack
|
||||
let mut stack = slot_stack_lock.lock().await;
|
||||
let mut stack = slot.get_stack().await;
|
||||
stack_left = stack.clone();
|
||||
|
||||
let success = if slot_index < 3 {
|
||||
@@ -205,10 +202,9 @@ impl ScreenHandler for FurnaceLikeScreenHandler {
|
||||
}
|
||||
|
||||
if stack.is_empty() {
|
||||
drop(stack); // Release lock before awaiting
|
||||
slot.set_stack(ItemStack::EMPTY.clone()).await;
|
||||
} else {
|
||||
slot.mark_dirty().await;
|
||||
slot.set_stack(stack).await;
|
||||
}
|
||||
|
||||
// Award XP when taking from output slot (slot 2)
|
||||
|
||||
@@ -218,20 +218,14 @@ impl ScreenHandler for GenericContainerScreenHandler {
|
||||
let slot = self.get_behaviour().slots[slot_index as usize].clone();
|
||||
|
||||
if slot.has_stack().await {
|
||||
let slot_stack_lock = slot.get_stack().await;
|
||||
let slot_stack_guard = slot_stack_lock.lock().await;
|
||||
stack_left = slot_stack_guard.clone();
|
||||
// Release the guard before calling insert_item which needs its own lock
|
||||
drop(slot_stack_guard);
|
||||
|
||||
// Re-acquire lock for insert_item (which expects &mut ItemStack)
|
||||
let mut slot_stack_mut = slot_stack_lock.lock().await;
|
||||
let mut slot_stack = slot.get_stack().await;
|
||||
stack_left = slot_stack.clone();
|
||||
|
||||
if slot_index < i32::from(self.rows * 9) {
|
||||
// Move from inventory to player area (end)
|
||||
if !self
|
||||
.insert_item(
|
||||
&mut slot_stack_mut,
|
||||
&mut slot_stack,
|
||||
(self.rows * 9).into(),
|
||||
self.get_behaviour().slots.len() as i32,
|
||||
true,
|
||||
@@ -241,7 +235,7 @@ impl ScreenHandler for GenericContainerScreenHandler {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
} else if !self
|
||||
.insert_item(&mut slot_stack_mut, 0, (self.rows * 9).into(), false)
|
||||
.insert_item(&mut slot_stack, 0, (self.rows * 9).into(), false)
|
||||
.await
|
||||
{
|
||||
// Move from player area to inventory (start)
|
||||
@@ -249,12 +243,10 @@ impl ScreenHandler for GenericContainerScreenHandler {
|
||||
}
|
||||
|
||||
// Check the resulting state of the slot stack after insert_item
|
||||
if slot_stack_mut.is_empty() {
|
||||
drop(slot_stack_mut); // Release lock
|
||||
if slot_stack.is_empty() {
|
||||
slot.set_stack(ItemStack::EMPTY.clone()).await;
|
||||
} else {
|
||||
drop(slot_stack_mut); // Release lock
|
||||
slot.mark_dirty().await;
|
||||
slot.set_stack(slot_stack).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -140,12 +140,8 @@ impl ScreenHandler for GUIScreenHandler {
|
||||
let slot = self.get_behaviour().slots[slot_index as usize].clone();
|
||||
|
||||
if slot.has_stack().await {
|
||||
let slot_stack_lock = slot.get_stack().await;
|
||||
let slot_stack_guard = slot_stack_lock.lock().await;
|
||||
stack_left = slot_stack_guard.clone();
|
||||
drop(slot_stack_guard);
|
||||
|
||||
let mut slot_stack_mut = slot_stack_lock.lock().await;
|
||||
let mut slot_stack = slot.get_stack().await;
|
||||
stack_left = slot_stack.clone();
|
||||
let container_slots = i32::from(self.rows * self.columns);
|
||||
|
||||
if slot_index < container_slots {
|
||||
@@ -155,7 +151,7 @@ impl ScreenHandler for GUIScreenHandler {
|
||||
}
|
||||
if !self
|
||||
.insert_item(
|
||||
&mut slot_stack_mut,
|
||||
&mut slot_stack,
|
||||
container_slots,
|
||||
self.get_behaviour().slots.len() as i32,
|
||||
true,
|
||||
@@ -170,7 +166,7 @@ impl ScreenHandler for GUIScreenHandler {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
if !self
|
||||
.insert_item(&mut slot_stack_mut, 0, container_slots, false)
|
||||
.insert_item(&mut slot_stack, 0, container_slots, false)
|
||||
.await
|
||||
{
|
||||
// Move from player area to inventory (start)
|
||||
@@ -178,12 +174,10 @@ impl ScreenHandler for GUIScreenHandler {
|
||||
}
|
||||
}
|
||||
|
||||
if slot_stack_mut.is_empty() {
|
||||
drop(slot_stack_mut);
|
||||
if slot_stack.is_empty() {
|
||||
slot.set_stack(ItemStack::EMPTY.clone()).await;
|
||||
} else {
|
||||
drop(slot_stack_mut);
|
||||
slot.mark_dirty().await;
|
||||
slot.set_stack(slot_stack).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -63,9 +63,7 @@ impl MerchantScreenHandler {
|
||||
|
||||
let offer = &self.offers[self.selected_offer];
|
||||
let input_a = self.inventory.get_stack(0).await;
|
||||
let input_a = input_a.lock().await;
|
||||
let input_b = self.inventory.get_stack(1).await;
|
||||
let input_b = input_b.lock().await;
|
||||
|
||||
let match_a = input_a.are_items_and_components_equal(&offer.base_cost_a.0)
|
||||
&& input_a.item_count >= offer.base_cost_a.0.item_count;
|
||||
@@ -130,18 +128,14 @@ impl ScreenHandler for MerchantScreenHandler {
|
||||
let slot = self.get_behaviour().slots[slot_index as usize].clone();
|
||||
|
||||
if slot.has_stack().await {
|
||||
let slot_stack_lock = slot.get_stack().await;
|
||||
let slot_stack_guard = slot_stack_lock.lock().await;
|
||||
stack_left = slot_stack_guard.clone();
|
||||
drop(slot_stack_guard);
|
||||
|
||||
let mut slot_stack_mut = slot_stack_lock.lock().await;
|
||||
let mut slot_stack = slot.get_stack().await;
|
||||
stack_left = slot_stack.clone();
|
||||
|
||||
if slot_index < 3 {
|
||||
// From merchant slots to player inventory
|
||||
if !self
|
||||
.insert_item(
|
||||
&mut slot_stack_mut,
|
||||
&mut slot_stack,
|
||||
3,
|
||||
self.get_behaviour().slots.len() as i32,
|
||||
true,
|
||||
@@ -151,18 +145,16 @@ impl ScreenHandler for MerchantScreenHandler {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
} else {
|
||||
// From player inventory to merchant inputs (0 and 1)
|
||||
if !self.insert_item(&mut slot_stack_mut, 0, 2, false).await {
|
||||
// From player inventory to merchant
|
||||
if !self.insert_item(&mut slot_stack, 0, 2, false).await {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
}
|
||||
|
||||
if slot_stack_mut.is_empty() {
|
||||
drop(slot_stack_mut);
|
||||
if slot_stack.is_empty() {
|
||||
slot.set_stack(ItemStack::EMPTY.clone()).await;
|
||||
} else {
|
||||
drop(slot_stack_mut);
|
||||
slot.mark_dirty().await;
|
||||
slot.set_stack(slot_stack).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,24 +184,20 @@ impl ScreenHandler for MerchantScreenHandler {
|
||||
(offer.base_cost_a.0.item_count, count_b, offer.xp)
|
||||
};
|
||||
|
||||
let input_a = self.inventory.get_stack(0).await;
|
||||
let mut input_a = input_a.lock().await;
|
||||
let mut input_a = self.inventory.get_stack(0).await;
|
||||
input_a.decrement(count_a);
|
||||
if input_a.is_empty() {
|
||||
*input_a = ItemStack::EMPTY.clone();
|
||||
input_a = ItemStack::EMPTY.clone();
|
||||
}
|
||||
drop(input_a);
|
||||
self.get_behaviour().slots[0].mark_dirty().await;
|
||||
self.inventory.set_stack(0, input_a).await;
|
||||
|
||||
if let Some(count_b) = count_b {
|
||||
let input_b = self.inventory.get_stack(1).await;
|
||||
let mut input_b = input_b.lock().await;
|
||||
let mut input_b = self.inventory.get_stack(1).await;
|
||||
input_b.decrement(count_b);
|
||||
if input_b.is_empty() {
|
||||
*input_b = ItemStack::EMPTY.clone();
|
||||
input_b = ItemStack::EMPTY.clone();
|
||||
}
|
||||
drop(input_b);
|
||||
self.get_behaviour().slots[1].mark_dirty().await;
|
||||
self.inventory.set_stack(1, input_b).await;
|
||||
}
|
||||
|
||||
// Award XP
|
||||
|
||||
@@ -10,14 +10,14 @@
|
||||
//! Ender chests track when players open and close them to properly
|
||||
//! manage the viewer count for animation purposes.
|
||||
|
||||
use std::{any::Any, array::from_fn, pin::Pin, sync::Arc};
|
||||
use std::{any::Any, pin::Pin, sync::Arc};
|
||||
|
||||
use pumpkin_data::item_stack::ItemStack;
|
||||
use pumpkin_world::{
|
||||
block::viewer::ViewerCountTracker,
|
||||
inventory::{Clearable, Inventory, InventoryFuture, split_stack},
|
||||
inventory::{Clearable, Inventory, InventoryFuture},
|
||||
};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::{Mutex, RwLock};
|
||||
|
||||
/// A player's ender chest inventory.
|
||||
///
|
||||
@@ -26,7 +26,7 @@ use tokio::sync::Mutex;
|
||||
/// ender chest block.
|
||||
pub struct EnderChestInventory {
|
||||
/// The 27 item slots in the ender chest.
|
||||
pub items: [Arc<Mutex<ItemStack>>; Self::INVENTORY_SIZE],
|
||||
pub items: RwLock<[ItemStack; Self::INVENTORY_SIZE]>,
|
||||
/// Viewer count tracker for lid animation.
|
||||
///
|
||||
/// Tracks how many players have the ender chest open to animate the lid.
|
||||
@@ -47,7 +47,7 @@ impl EnderChestInventory {
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
items: from_fn(|_| Arc::new(Mutex::new(ItemStack::EMPTY.clone()))),
|
||||
items: RwLock::new(std::array::from_fn(|_| ItemStack::EMPTY.clone())),
|
||||
tracker: Mutex::new(None),
|
||||
}
|
||||
}
|
||||
@@ -75,41 +75,45 @@ impl EnderChestInventory {
|
||||
|
||||
impl Inventory for EnderChestInventory {
|
||||
fn size(&self) -> usize {
|
||||
self.items.len()
|
||||
Self::INVENTORY_SIZE
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> InventoryFuture<'_, bool> {
|
||||
Box::pin(async move {
|
||||
for slot in &self.items {
|
||||
if !slot.lock().await.is_empty() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
let items = self.items.read().await;
|
||||
items.iter().all(ItemStack::is_empty)
|
||||
})
|
||||
}
|
||||
|
||||
fn get_stack(&self, slot: usize) -> InventoryFuture<'_, Arc<Mutex<ItemStack>>> {
|
||||
Box::pin(async move { self.items[slot].clone() })
|
||||
fn get_stack(&self, slot: usize) -> InventoryFuture<'_, ItemStack> {
|
||||
Box::pin(async move {
|
||||
let items = self.items.read().await;
|
||||
items[slot].clone()
|
||||
})
|
||||
}
|
||||
|
||||
fn remove_stack(&self, slot: usize) -> InventoryFuture<'_, ItemStack> {
|
||||
Box::pin(async move {
|
||||
let mut removed = ItemStack::EMPTY.clone();
|
||||
let mut guard = self.items[slot].lock().await;
|
||||
std::mem::swap(&mut removed, &mut *guard);
|
||||
removed
|
||||
let mut items = self.items.write().await;
|
||||
std::mem::replace(&mut items[slot], ItemStack::EMPTY.clone())
|
||||
})
|
||||
}
|
||||
|
||||
fn remove_stack_specific(&self, slot: usize, amount: u8) -> InventoryFuture<'_, ItemStack> {
|
||||
Box::pin(async move { split_stack(&self.items, slot, amount).await })
|
||||
Box::pin(async move {
|
||||
let mut items = self.items.write().await;
|
||||
if !items[slot].is_empty() && amount > 0 {
|
||||
items[slot].split(amount)
|
||||
} else {
|
||||
ItemStack::EMPTY.clone()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn set_stack(&self, slot: usize, stack: ItemStack) -> InventoryFuture<'_, ()> {
|
||||
Box::pin(async move {
|
||||
*self.items[slot].lock().await = stack;
|
||||
let mut items = self.items.write().await;
|
||||
items[slot] = stack;
|
||||
})
|
||||
}
|
||||
|
||||
@@ -139,9 +143,8 @@ impl Inventory for EnderChestInventory {
|
||||
impl Clearable for EnderChestInventory {
|
||||
fn clear(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
|
||||
Box::pin(async move {
|
||||
for item in &self.items {
|
||||
*item.lock().await = ItemStack::EMPTY.clone();
|
||||
}
|
||||
let mut items = self.items.write().await;
|
||||
items.fill_with(|| ItemStack::EMPTY.clone());
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -11,18 +11,17 @@ use crate::entity_equipment::EntityEquipment;
|
||||
use crate::screen_handler::InventoryPlayer;
|
||||
|
||||
use pumpkin_data::data_component_impl::EquipmentSlot;
|
||||
use pumpkin_data::item::Item;
|
||||
use pumpkin_data::item_stack::ItemStack;
|
||||
use pumpkin_protocol::java::client::play::CSetPlayerInventory;
|
||||
use pumpkin_util::Hand;
|
||||
use pumpkin_world::inventory::split_stack;
|
||||
use pumpkin_world::inventory::{Clearable, Inventory, InventoryFuture};
|
||||
use std::any::Any;
|
||||
use std::array::from_fn;
|
||||
use std::collections::HashMap;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicU8, Ordering};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::{Mutex, RwLock};
|
||||
use tracing::warn;
|
||||
|
||||
/// The player's inventory.
|
||||
@@ -33,13 +32,13 @@ pub struct PlayerInventory {
|
||||
/// The 36 main inventory slots (slots 0-35).
|
||||
///
|
||||
/// The first 9 slots (0-8) are the hotbar, the remaining 27 (9-35) are the main storage.
|
||||
pub main_inventory: [Arc<Mutex<ItemStack>>; Self::MAIN_SIZE],
|
||||
pub main_inventory: RwLock<[ItemStack; Self::MAIN_SIZE]>,
|
||||
/// Mapping of slot indices to equipment slot types.
|
||||
///
|
||||
/// Used to identify which slots correspond to armor and off-hand equipment.
|
||||
pub equipment_slots: Arc<HashMap<usize, EquipmentSlot>>,
|
||||
/// The currently selected hotbar slot index (0-8).
|
||||
selected_slot: AtomicU8,
|
||||
pub selected_slot: AtomicU8,
|
||||
/// The entity equipment storage for armor and off-hand items.
|
||||
///
|
||||
/// This is separate from the main inventory and is rendered on the player model.
|
||||
@@ -65,58 +64,79 @@ impl PlayerInventory {
|
||||
equipment_slots: Arc<HashMap<usize, EquipmentSlot>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
// Normal syntax can't be used here because Arc doesn't implement Copy
|
||||
main_inventory: from_fn(|_| Arc::new(Mutex::new(ItemStack::EMPTY.clone()))),
|
||||
main_inventory: RwLock::new(std::array::from_fn(|_| ItemStack::EMPTY.clone())),
|
||||
equipment_slots,
|
||||
selected_slot: AtomicU8::new(0),
|
||||
entity_equipment,
|
||||
}
|
||||
}
|
||||
|
||||
/// Fast non-blocking count of an item across all main inventory slots.
|
||||
pub fn count_item(&self, item: &'static Item) -> u32 {
|
||||
let mut total = 0u32;
|
||||
if let Ok(inv) = self.main_inventory.try_read() {
|
||||
for stack in inv.iter() {
|
||||
if stack.get_item().id == item.id {
|
||||
total += u32::from(stack.item_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
total
|
||||
}
|
||||
|
||||
/// Fast non-blocking check if the main inventory contains a given item.
|
||||
pub fn contains_item(&self, item: &'static Item) -> bool {
|
||||
if let Ok(inv) = self.main_inventory.try_read() {
|
||||
for stack in inv.iter() {
|
||||
if !stack.is_empty() && stack.get_item().id == item.id {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Gets the item in the currently selected hotbar slot.
|
||||
///
|
||||
/// This is the item the player is currently holding in their main hand.
|
||||
///
|
||||
/// Mojang name: `getSelectedStack`
|
||||
pub fn held_item(&self) -> Arc<Mutex<ItemStack>> {
|
||||
self.main_inventory
|
||||
.get(self.get_selected_slot() as usize)
|
||||
.unwrap()
|
||||
.clone()
|
||||
pub async fn held_item(&self) -> ItemStack {
|
||||
let inv = self.main_inventory.read().await;
|
||||
inv[self.get_selected_slot() as usize].clone()
|
||||
}
|
||||
|
||||
/// Sets the item in the currently selected hotbar slot.
|
||||
pub async fn set_held_item(&self, stack: ItemStack) {
|
||||
let selected = self.get_selected_slot() as usize;
|
||||
let mut inv = self.main_inventory.write().await;
|
||||
inv[selected] = stack;
|
||||
}
|
||||
|
||||
/// Sets the item in the specified hand.
|
||||
pub async fn set_stack_in_hand(&self, hand: Hand, stack: ItemStack) {
|
||||
match hand {
|
||||
Hand::Right => self.set_held_item(stack).await,
|
||||
Hand::Left => {
|
||||
let slot = self.equipment_slots.get(&Self::OFF_HAND_SLOT).unwrap();
|
||||
self.entity_equipment.lock().await.put(slot, stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the item in the specified hand.
|
||||
///
|
||||
/// # Arguments
|
||||
/// - `hand` - Which hand to get the item from
|
||||
pub async fn get_stack_in_hand(&self, hand: Hand) -> Arc<Mutex<ItemStack>> {
|
||||
pub async fn get_stack_in_hand(&self, hand: Hand) -> ItemStack {
|
||||
match hand {
|
||||
Hand::Left => self.off_hand_item().await,
|
||||
Hand::Right => self.held_item(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if the given item Arc already points to the target equipment slot's stack.
|
||||
///
|
||||
/// Use before attempting to re-equip an item to prevent self-deadlock:
|
||||
/// Tokio's `Mutex` is not reentrant, so locking an `Arc<Mutex<T>>` that
|
||||
/// you already hold will deadlock forever.
|
||||
pub async fn is_already_equipped(
|
||||
&self,
|
||||
item_arc: &Arc<Mutex<ItemStack>>,
|
||||
slot: &EquipmentSlot,
|
||||
) -> bool {
|
||||
match slot {
|
||||
EquipmentSlot::OffHand(_) => Arc::ptr_eq(item_arc, &self.off_hand_item().await),
|
||||
EquipmentSlot::MainHand(_) => Arc::ptr_eq(item_arc, &self.held_item()),
|
||||
_ => false,
|
||||
Hand::Right => self.held_item().await,
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the item in the off-hand.
|
||||
///
|
||||
/// Mojang name: `getOffHandStack`
|
||||
pub async fn off_hand_item(&self) -> Arc<Mutex<ItemStack>> {
|
||||
pub async fn off_hand_item(&self) -> ItemStack {
|
||||
let slot = self.equipment_slots.get(&Self::OFF_HAND_SLOT).unwrap();
|
||||
self.entity_equipment.lock().await.get(slot)
|
||||
}
|
||||
@@ -128,11 +148,12 @@ impl PlayerInventory {
|
||||
pub async fn swap_item(&self) -> (ItemStack, ItemStack) {
|
||||
let slot = self.equipment_slots.get(&Self::OFF_HAND_SLOT).unwrap();
|
||||
let mut equipment = self.entity_equipment.lock().await;
|
||||
let binding = self.held_item();
|
||||
let mut main_hand_item = binding.lock().await;
|
||||
let off_hand_item = main_hand_item.clone();
|
||||
*main_hand_item = equipment.put(slot, off_hand_item.clone()).await;
|
||||
(main_hand_item.clone(), off_hand_item)
|
||||
let selected = self.get_selected_slot() as usize;
|
||||
let mut main_inv = self.main_inventory.write().await;
|
||||
let main_hand_item = main_inv[selected].clone();
|
||||
let new_main = equipment.put(slot, main_hand_item.clone());
|
||||
main_inv[selected] = new_main.clone();
|
||||
(new_main, main_hand_item)
|
||||
}
|
||||
|
||||
/// Checks if a slot index is a valid hotbar slot.
|
||||
@@ -160,13 +181,24 @@ impl PlayerInventory {
|
||||
///
|
||||
/// Returns the number of items that couldn't fit.
|
||||
async fn add_stack_to_slot(&self, slot: usize, stack: ItemStack) -> usize {
|
||||
if slot >= Self::MAIN_SIZE {
|
||||
if let Some(slot_type) = self.equipment_slots.get(&slot) {
|
||||
let mut equipment = self.entity_equipment.lock().await;
|
||||
let current = equipment.get(slot_type);
|
||||
if current.is_empty() {
|
||||
equipment.put(slot_type, stack);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return stack.item_count as usize;
|
||||
}
|
||||
|
||||
let mut inv = self.main_inventory.write().await;
|
||||
let mut stack_count = stack.item_count;
|
||||
let binding = self.get_stack(slot).await;
|
||||
let mut self_stack = binding.lock().await;
|
||||
let self_stack = &mut inv[slot];
|
||||
|
||||
if self_stack.is_empty() {
|
||||
*self_stack = stack.copy_with_count(0);
|
||||
//self.set_stack(slot, self_stack).await;
|
||||
}
|
||||
|
||||
let count_left = self_stack.get_max_stack_size() - self_stack.item_count;
|
||||
@@ -184,12 +216,12 @@ impl PlayerInventory {
|
||||
/// # Returns
|
||||
/// The slot index or -1 if inventory is full.
|
||||
async fn get_empty_slot(&self) -> i16 {
|
||||
for i in 0..Self::MAIN_SIZE {
|
||||
if self.main_inventory[i].lock().await.is_empty() {
|
||||
let inv = self.main_inventory.read().await;
|
||||
for (i, stack) in inv.iter().enumerate() {
|
||||
if stack.is_empty() {
|
||||
return i as i16;
|
||||
}
|
||||
}
|
||||
|
||||
-1
|
||||
}
|
||||
|
||||
@@ -205,29 +237,24 @@ impl PlayerInventory {
|
||||
///
|
||||
/// Checks selected slot, off-hand, then other slots.
|
||||
async fn get_occupied_slot_with_room_for_stack(&self, stack: &ItemStack) -> i16 {
|
||||
if Self::can_stack_add_more(
|
||||
&*self
|
||||
.get_stack(self.get_selected_slot() as usize)
|
||||
.await
|
||||
.lock()
|
||||
.await,
|
||||
stack,
|
||||
) {
|
||||
i16::from(self.get_selected_slot())
|
||||
} else if Self::can_stack_add_more(
|
||||
&*self.get_stack(Self::OFF_HAND_SLOT).await.lock().await,
|
||||
stack,
|
||||
) {
|
||||
Self::OFF_HAND_SLOT as i16
|
||||
} else {
|
||||
for i in 0..Self::MAIN_SIZE {
|
||||
if Self::can_stack_add_more(&*self.main_inventory[i].lock().await, stack) {
|
||||
return i as i16;
|
||||
}
|
||||
}
|
||||
|
||||
-1
|
||||
let selected = self.get_selected_slot() as usize;
|
||||
let inv = self.main_inventory.read().await;
|
||||
if Self::can_stack_add_more(&inv[selected], stack) {
|
||||
return selected as i16;
|
||||
}
|
||||
|
||||
let off_hand = self.off_hand_item().await;
|
||||
if Self::can_stack_add_more(&off_hand, stack) {
|
||||
return Self::OFF_HAND_SLOT as i16;
|
||||
}
|
||||
|
||||
for (i, item) in inv.iter().enumerate() {
|
||||
if Self::can_stack_add_more(item, stack) {
|
||||
return i as i16;
|
||||
}
|
||||
}
|
||||
|
||||
-1
|
||||
}
|
||||
|
||||
/// Inserts a stack into any available slot.
|
||||
@@ -254,8 +281,6 @@ impl PlayerInventory {
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: if (stack.isDamaged()) {
|
||||
|
||||
let mut i;
|
||||
|
||||
loop {
|
||||
@@ -271,8 +296,6 @@ impl PlayerInventory {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Creative mode check
|
||||
|
||||
stack.item_count < i
|
||||
}
|
||||
|
||||
@@ -281,17 +304,12 @@ impl PlayerInventory {
|
||||
/// # Returns
|
||||
/// The slot index or -1 if not found.
|
||||
pub async fn get_slot_with_stack(&self, stack: &ItemStack) -> i16 {
|
||||
for i in 0..Self::MAIN_SIZE {
|
||||
if !self.main_inventory[i].lock().await.is_empty()
|
||||
&& self.main_inventory[i]
|
||||
.lock()
|
||||
.await
|
||||
.are_items_and_components_equal(stack)
|
||||
{
|
||||
let inv = self.main_inventory.read().await;
|
||||
for (i, item) in inv.iter().enumerate() {
|
||||
if !item.is_empty() && item.are_items_and_components_equal(stack) {
|
||||
return i as i16;
|
||||
}
|
||||
}
|
||||
|
||||
-1
|
||||
}
|
||||
|
||||
@@ -299,63 +317,43 @@ impl PlayerInventory {
|
||||
///
|
||||
/// First looks for empty slots, then slots without enchantments.
|
||||
async fn get_swappable_hotbar_slot(&self) -> usize {
|
||||
let inv = self.main_inventory.read().await;
|
||||
let selected_slot = self.get_selected_slot() as usize;
|
||||
for i in 0..Self::HOTBAR_SIZE {
|
||||
let check_index = (i + selected_slot) % 9;
|
||||
if self.main_inventory[check_index].lock().await.is_empty() {
|
||||
if inv[check_index].is_empty() {
|
||||
return check_index;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(i) = (0..Self::HOTBAR_SIZE).next() {
|
||||
let check_index = (i + selected_slot) % 9;
|
||||
return check_index;
|
||||
}
|
||||
|
||||
self.get_selected_slot() as usize
|
||||
selected_slot
|
||||
}
|
||||
|
||||
/// Swaps an item stack with an item on the hotbar.
|
||||
///
|
||||
/// Finds an empty hotbar slot and places the stack there.
|
||||
pub async fn swap_stack_with_hotbar(&self, stack: ItemStack) {
|
||||
self.set_selected_slot(self.get_swappable_hotbar_slot().await as u8);
|
||||
let swappable = self.get_swappable_hotbar_slot().await;
|
||||
self.set_selected_slot(swappable as u8);
|
||||
let selected = self.get_selected_slot() as usize;
|
||||
let mut inv = self.main_inventory.write().await;
|
||||
|
||||
if !self.main_inventory[self.get_selected_slot() as usize]
|
||||
.lock()
|
||||
.await
|
||||
.is_empty()
|
||||
if let Some(empty_slot) = inv.iter().position(ItemStack::is_empty)
|
||||
&& !inv[selected].is_empty()
|
||||
{
|
||||
let empty_slot = self.get_empty_slot().await;
|
||||
if empty_slot != -1 {
|
||||
self.set_stack(
|
||||
empty_slot as usize,
|
||||
self.main_inventory[self.get_selected_slot() as usize]
|
||||
.lock()
|
||||
.await
|
||||
.clone(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
inv[empty_slot] = inv[selected].clone();
|
||||
}
|
||||
|
||||
self.set_stack(self.get_selected_slot() as usize, stack)
|
||||
.await;
|
||||
inv[selected] = stack;
|
||||
}
|
||||
|
||||
/// Swaps the items at two slot indices.
|
||||
pub async fn swap_slot_with_hotbar(&self, slot: usize) {
|
||||
self.set_selected_slot(self.get_swappable_hotbar_slot().await as u8);
|
||||
let stack = self.main_inventory[self.get_selected_slot() as usize]
|
||||
.lock()
|
||||
.await
|
||||
.clone();
|
||||
self.set_stack(
|
||||
self.get_selected_slot() as usize,
|
||||
self.main_inventory[slot].lock().await.clone(),
|
||||
)
|
||||
.await;
|
||||
self.set_stack(slot, stack).await;
|
||||
let swappable = self.get_swappable_hotbar_slot().await;
|
||||
self.set_selected_slot(swappable as u8);
|
||||
let selected = self.get_selected_slot() as usize;
|
||||
let mut inv = self.main_inventory.write().await;
|
||||
inv.swap(selected, slot);
|
||||
}
|
||||
|
||||
/// Gives a stack to the player or drops it if inventory is full.
|
||||
@@ -383,12 +381,7 @@ impl PlayerInventory {
|
||||
}
|
||||
|
||||
let items_fit = stack.get_max_stack_size()
|
||||
- self
|
||||
.get_stack(room_for_stack as usize)
|
||||
.await
|
||||
.lock()
|
||||
.await
|
||||
.item_count;
|
||||
- self.get_stack(room_for_stack as usize).await.item_count;
|
||||
if self
|
||||
.insert_stack(room_for_stack, &mut stack.split(items_fit))
|
||||
.await
|
||||
@@ -397,13 +390,7 @@ impl PlayerInventory {
|
||||
player
|
||||
.enqueue_slot_set_packet(&CSetPlayerInventory::new(
|
||||
i32::from(room_for_stack).into(),
|
||||
&self
|
||||
.get_stack(room_for_stack as usize)
|
||||
.await
|
||||
.lock()
|
||||
.await
|
||||
.clone()
|
||||
.into(),
|
||||
&self.get_stack(room_for_stack as usize).await.into(),
|
||||
))
|
||||
.await;
|
||||
}
|
||||
@@ -414,10 +401,8 @@ impl PlayerInventory {
|
||||
impl Clearable for PlayerInventory {
|
||||
fn clear(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
|
||||
Box::pin(async move {
|
||||
for item in &self.main_inventory {
|
||||
*item.lock().await = ItemStack::EMPTY.clone();
|
||||
}
|
||||
|
||||
let mut inv = self.main_inventory.write().await;
|
||||
inv.fill_with(|| ItemStack::EMPTY.clone());
|
||||
self.entity_equipment.lock().await.clear();
|
||||
})
|
||||
}
|
||||
@@ -425,27 +410,19 @@ impl Clearable for PlayerInventory {
|
||||
|
||||
impl Inventory for PlayerInventory {
|
||||
fn size(&self) -> usize {
|
||||
self.main_inventory.len() + self.equipment_slots.len()
|
||||
Self::MAIN_SIZE + self.equipment_slots.len()
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> InventoryFuture<'_, bool> {
|
||||
Box::pin(async move {
|
||||
for item in &self.main_inventory {
|
||||
if !item.lock().await.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let inv = self.main_inventory.read().await;
|
||||
if inv.iter().any(|s| !s.is_empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for slot in self.equipment_slots.values() {
|
||||
if !self
|
||||
.entity_equipment
|
||||
.lock()
|
||||
.await
|
||||
.get(slot)
|
||||
.lock()
|
||||
.await
|
||||
.is_empty()
|
||||
{
|
||||
let eq_item = self.entity_equipment.lock().await.get(slot);
|
||||
if !eq_item.is_empty() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -454,49 +431,56 @@ impl Inventory for PlayerInventory {
|
||||
})
|
||||
}
|
||||
|
||||
fn get_stack(&self, slot: usize) -> InventoryFuture<'_, Arc<Mutex<ItemStack>>> {
|
||||
fn get_stack(&self, slot: usize) -> InventoryFuture<'_, ItemStack> {
|
||||
Box::pin(async move {
|
||||
if slot < self.main_inventory.len() {
|
||||
self.main_inventory[slot].clone()
|
||||
} else {
|
||||
let slot = self.equipment_slots.get(&slot).unwrap();
|
||||
if slot < Self::MAIN_SIZE {
|
||||
let inv = self.main_inventory.read().await;
|
||||
inv[slot].clone()
|
||||
} else if let Some(slot) = self.equipment_slots.get(&slot) {
|
||||
self.entity_equipment.lock().await.get(slot)
|
||||
} else {
|
||||
ItemStack::EMPTY.clone()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn remove_stack(&self, slot: usize) -> InventoryFuture<'_, ItemStack> {
|
||||
Box::pin(async move {
|
||||
if slot < self.main_inventory.len() {
|
||||
let mut removed = ItemStack::EMPTY.clone();
|
||||
let mut guard = self.main_inventory[slot].lock().await;
|
||||
std::mem::swap(&mut removed, &mut *guard);
|
||||
removed
|
||||
} else {
|
||||
let slot = self.equipment_slots.get(&slot).unwrap();
|
||||
if slot < Self::MAIN_SIZE {
|
||||
let mut inv = self.main_inventory.write().await;
|
||||
std::mem::replace(&mut inv[slot], ItemStack::EMPTY.clone())
|
||||
} else if let Some(slot) = self.equipment_slots.get(&slot) {
|
||||
self.entity_equipment
|
||||
.lock()
|
||||
.await
|
||||
.put(slot, ItemStack::EMPTY.clone())
|
||||
.await
|
||||
} else {
|
||||
ItemStack::EMPTY.clone()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn remove_stack_specific(&self, slot: usize, amount: u8) -> InventoryFuture<'_, ItemStack> {
|
||||
Box::pin(async move {
|
||||
if slot < self.main_inventory.len() {
|
||||
split_stack(&self.main_inventory, slot, amount).await
|
||||
} else {
|
||||
let slot = self.equipment_slots.get(&slot).unwrap();
|
||||
|
||||
let equipment = self.entity_equipment.lock().await.get(slot);
|
||||
let mut stack = equipment.lock().await;
|
||||
|
||||
if !stack.is_empty() {
|
||||
return stack.split(amount);
|
||||
if slot < Self::MAIN_SIZE {
|
||||
let mut inv = self.main_inventory.write().await;
|
||||
if !inv[slot].is_empty() && amount > 0 {
|
||||
inv[slot].split(amount)
|
||||
} else {
|
||||
ItemStack::EMPTY.clone()
|
||||
}
|
||||
} else if let Some(slot) = self.equipment_slots.get(&slot) {
|
||||
let mut equipment = self.entity_equipment.lock().await;
|
||||
let mut stack = equipment.get(slot);
|
||||
|
||||
if !stack.is_empty() && amount > 0 {
|
||||
let split = stack.split(amount);
|
||||
equipment.put(slot, stack);
|
||||
split
|
||||
} else {
|
||||
ItemStack::EMPTY.clone()
|
||||
}
|
||||
} else {
|
||||
ItemStack::EMPTY.clone()
|
||||
}
|
||||
})
|
||||
@@ -504,10 +488,11 @@ impl Inventory for PlayerInventory {
|
||||
|
||||
fn set_stack(&self, slot: usize, stack: ItemStack) -> InventoryFuture<'_, ()> {
|
||||
Box::pin(async move {
|
||||
if slot < self.main_inventory.len() {
|
||||
*self.main_inventory[slot].lock().await = stack;
|
||||
if slot < Self::MAIN_SIZE {
|
||||
let mut inv = self.main_inventory.write().await;
|
||||
inv[slot] = stack;
|
||||
} else if let Some(slot) = self.equipment_slots.get(&slot) {
|
||||
self.entity_equipment.lock().await.put(slot, stack).await;
|
||||
self.entity_equipment.lock().await.put(slot, stack);
|
||||
} else {
|
||||
warn!("Failed to get Equipment Slot at {slot}");
|
||||
}
|
||||
|
||||
@@ -164,8 +164,7 @@ impl ScreenHandler for PlayerScreenHandler {
|
||||
// TODO: Equippable component
|
||||
|
||||
if slot.has_stack().await {
|
||||
let slot_stack_lock = slot.get_stack().await;
|
||||
let mut slot_stack = slot_stack_lock.lock().await;
|
||||
let mut slot_stack = slot.get_stack().await;
|
||||
let stack_prev = slot_stack.clone();
|
||||
|
||||
let equipment_slot = slot_stack
|
||||
@@ -232,13 +231,12 @@ impl ScreenHandler for PlayerScreenHandler {
|
||||
}
|
||||
|
||||
let stack = slot_stack.clone();
|
||||
drop(slot_stack); // release the lock before calling other methods
|
||||
|
||||
if stack.is_empty() {
|
||||
slot.set_stack_prev(ItemStack::EMPTY.clone(), stack_prev.clone())
|
||||
.await;
|
||||
} else {
|
||||
slot.mark_dirty().await;
|
||||
slot.set_stack(stack.clone()).await;
|
||||
}
|
||||
|
||||
if stack.item_count == stack_prev.item_count {
|
||||
|
||||
@@ -725,8 +725,7 @@ pub trait ScreenHandler: Send + Sync {
|
||||
})
|
||||
{
|
||||
let slot = self.get_behaviour().slots[current_index as usize].clone();
|
||||
let slot_stack_lock = slot.get_stack().await;
|
||||
let mut slot_stack = slot_stack_lock.lock().await;
|
||||
let mut slot_stack = slot.get_stack().await;
|
||||
|
||||
if !slot_stack.is_empty() && slot_stack.are_items_and_components_equal(stack) {
|
||||
let combined_count = slot_stack.item_count + stack.item_count;
|
||||
@@ -734,14 +733,12 @@ pub trait ScreenHandler: Send + Sync {
|
||||
if combined_count <= max_slot_count {
|
||||
stack.set_count(0);
|
||||
slot_stack.set_count(combined_count);
|
||||
drop(slot_stack);
|
||||
slot.mark_dirty().await;
|
||||
slot.set_stack(slot_stack).await;
|
||||
success = true;
|
||||
} else if slot_stack.item_count < max_slot_count {
|
||||
stack.decrement(max_slot_count - slot_stack.item_count);
|
||||
slot_stack.set_count(max_slot_count);
|
||||
drop(slot_stack);
|
||||
slot.mark_dirty().await;
|
||||
slot.set_stack(slot_stack).await;
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
@@ -768,11 +765,9 @@ pub trait ScreenHandler: Send + Sync {
|
||||
} {
|
||||
let slot = self.get_behaviour().slots[current_index as usize].clone();
|
||||
let slot_stack = slot.get_stack().await;
|
||||
let slot_stack = slot_stack.lock().await;
|
||||
|
||||
if slot_stack.is_empty() && slot.can_insert(stack).await {
|
||||
let max_count = slot.get_max_item_count_for_stack(stack).await;
|
||||
drop(slot_stack);
|
||||
slot.set_stack(stack.split(max_count.min(stack.item_count)))
|
||||
.await;
|
||||
slot.mark_dirty().await;
|
||||
@@ -885,8 +880,7 @@ pub trait ScreenHandler: Send + Sync {
|
||||
let cursor_stack = behaviour.cursor_stack.lock().await;
|
||||
|
||||
let slot = &behaviour.slots[slot_index as usize];
|
||||
let stack_lock = slot.get_stack().await;
|
||||
let stack = stack_lock.lock().await;
|
||||
let stack = slot.get_stack().await;
|
||||
if !cursor_stack.is_empty()
|
||||
&& slot.can_insert(&cursor_stack).await
|
||||
&& (stack.are_items_and_components_equal(&cursor_stack) || stack.is_empty())
|
||||
@@ -918,8 +912,7 @@ pub trait ScreenHandler: Send + Sync {
|
||||
let initial_count = cursor_stack.item_count;
|
||||
for slot_index in &behaviour.drag_slots {
|
||||
let slot = behaviour.slots[*slot_index as usize].clone();
|
||||
let stack_lock = slot.get_stack().await;
|
||||
let stack = stack_lock.lock().await;
|
||||
let stack = slot.get_stack().await;
|
||||
|
||||
if (stack.are_items_and_components_equal(&cursor_stack) || stack.is_empty())
|
||||
&& slot.can_insert(&cursor_stack).await
|
||||
@@ -942,13 +935,12 @@ pub trait ScreenHandler: Send + Sync {
|
||||
))
|
||||
.min(cursor_stack.item_count);
|
||||
if inserting_count > 0 {
|
||||
let mut stack_clone = stack.clone();
|
||||
drop(stack);
|
||||
if stack_clone.is_empty() {
|
||||
stack_clone = cursor_stack.copy_with_count(0);
|
||||
let mut new_stack = stack.clone();
|
||||
if new_stack.is_empty() {
|
||||
new_stack = cursor_stack.copy_with_count(0);
|
||||
}
|
||||
stack_clone.increment(inserting_count);
|
||||
slot.set_stack(stack_clone).await;
|
||||
new_stack.increment(inserting_count);
|
||||
slot.set_stack(new_stack).await;
|
||||
if drag_button != 2 {
|
||||
cursor_stack.decrement(inserting_count);
|
||||
}
|
||||
@@ -999,8 +991,7 @@ pub trait ScreenHandler: Send + Sync {
|
||||
return;
|
||||
}
|
||||
let slot = behaviour.slots[slot_index as usize].clone();
|
||||
let stack_lock = slot.get_stack().await;
|
||||
let stack = stack_lock.lock().await;
|
||||
let stack = slot.get_stack().await;
|
||||
*cursor_stack = stack.copy_with_count(stack.get_max_stack_size());
|
||||
}
|
||||
} else if (action_type == SlotActionType::Pickup
|
||||
@@ -1064,32 +1055,32 @@ pub trait ScreenHandler: Send + Sync {
|
||||
let mut intercepted = false;
|
||||
|
||||
if !cursor_stack.is_empty() {
|
||||
let stack_guard = slot.get_stack().await;
|
||||
let mut inner_slot_stack = stack_guard.lock().await;
|
||||
let mut inner_slot_stack = slot.get_stack().await;
|
||||
if let Some(bundle) = inner_slot_stack.get_data_component_mut::<pumpkin_data::data_component_impl::BundleContentsImpl>()
|
||||
&& bundle.try_insert(&mut cursor_stack) {
|
||||
slot.set_stack(inner_slot_stack).await;
|
||||
intercepted = true;
|
||||
}
|
||||
}
|
||||
|
||||
if !intercepted && !slot_stack.is_empty()
|
||||
&& let Some(bundle) = cursor_stack.get_data_component_mut::<pumpkin_data::data_component_impl::BundleContentsImpl>() {
|
||||
let stack_guard = slot.get_stack().await;
|
||||
let mut inner_slot_stack = stack_guard.lock().await;
|
||||
let mut inner_slot_stack = slot.get_stack().await;
|
||||
if bundle.try_insert(&mut inner_slot_stack) {
|
||||
if inner_slot_stack.item_count == 0 {
|
||||
*inner_slot_stack = ItemStack::EMPTY.clone();
|
||||
inner_slot_stack = ItemStack::EMPTY.clone();
|
||||
}
|
||||
slot.set_stack(inner_slot_stack).await;
|
||||
intercepted = true;
|
||||
}
|
||||
}
|
||||
|
||||
if !intercepted && cursor_stack.is_empty() {
|
||||
let stack_guard = slot.get_stack().await;
|
||||
let mut inner_slot_stack = stack_guard.lock().await;
|
||||
let mut inner_slot_stack = slot.get_stack().await;
|
||||
if let Some(bundle) = inner_slot_stack.get_data_component_mut::<pumpkin_data::data_component_impl::BundleContentsImpl>()
|
||||
&& let Some(extracted) = bundle.try_extract() {
|
||||
*cursor_stack = extracted;
|
||||
slot.set_stack(inner_slot_stack).await;
|
||||
intercepted = true;
|
||||
}
|
||||
}
|
||||
@@ -1226,13 +1217,7 @@ pub trait ScreenHandler: Send + Sync {
|
||||
if slot_index < 0 {
|
||||
return;
|
||||
}
|
||||
let mut button_stack = player
|
||||
.get_inventory()
|
||||
.get_stack(button as usize)
|
||||
.await
|
||||
.lock()
|
||||
.await
|
||||
.clone();
|
||||
let mut button_stack = player.get_inventory().get_stack(button as usize).await;
|
||||
let source_slot = self.get_behaviour().slots[slot_index as usize].clone();
|
||||
let source_stack = source_slot.get_cloned_stack().await;
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ use std::{
|
||||
Arc,
|
||||
atomic::{AtomicU8, Ordering},
|
||||
},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use crate::screen_handler::InventoryPlayer;
|
||||
@@ -33,7 +32,6 @@ use pumpkin_data::data_component_impl::EquipmentSlot;
|
||||
use pumpkin_data::item::Item;
|
||||
use pumpkin_data::item_stack::ItemStack;
|
||||
use pumpkin_world::inventory::Inventory;
|
||||
use tokio::{sync::Mutex, time::timeout};
|
||||
|
||||
/// Type alias for async slot operations.
|
||||
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
|
||||
@@ -101,37 +99,21 @@ pub trait Slot: Send + Sync {
|
||||
}
|
||||
|
||||
/// Gets the stack in this slot.
|
||||
fn get_stack(&self) -> BoxFuture<'_, Arc<Mutex<ItemStack>>> {
|
||||
fn get_stack(&self) -> BoxFuture<'_, ItemStack> {
|
||||
// Default implementation logic:
|
||||
Box::pin(async move { self.get_inventory().get_stack(self.get_index()).await })
|
||||
}
|
||||
|
||||
/// Gets a copy of the stack in this slot.
|
||||
///
|
||||
/// Acquires a lock and returns a clone of the stack.
|
||||
fn get_cloned_stack(&self) -> BoxFuture<'_, ItemStack> {
|
||||
// Default implementation logic:
|
||||
Box::pin(async move {
|
||||
let stack = self.get_stack().await;
|
||||
let lock = timeout(Duration::from_secs(5), stack.lock())
|
||||
.await
|
||||
.expect("Timed out while trying to acquire lock");
|
||||
|
||||
lock.clone()
|
||||
})
|
||||
Box::pin(async move { self.get_stack().await })
|
||||
}
|
||||
|
||||
/// Checks if this slot has a non-empty stack.
|
||||
fn has_stack(&self) -> BoxFuture<'_, bool> {
|
||||
// Default implementation logic:
|
||||
Box::pin(async move {
|
||||
let inv = self.get_inventory();
|
||||
!inv.get_stack(self.get_index())
|
||||
.await
|
||||
.lock()
|
||||
.await
|
||||
.is_empty()
|
||||
})
|
||||
Box::pin(async move { !self.get_stack().await.is_empty() })
|
||||
}
|
||||
|
||||
/// Sets the stack in this slot.
|
||||
@@ -295,22 +277,18 @@ pub trait Slot: Send + Sync {
|
||||
// Default implementation logic:
|
||||
Box::pin(async move {
|
||||
if !stack.is_empty() && self.can_insert(&stack).await {
|
||||
let stack_mutex = self.get_stack().await;
|
||||
let mut stack_self = stack_mutex.lock().await;
|
||||
let mut stack_self = self.get_stack().await;
|
||||
let min_count = count
|
||||
.min(stack.item_count)
|
||||
.min(self.get_max_item_count_for_stack(&stack).await - stack_self.item_count);
|
||||
|
||||
if min_count != 0 {
|
||||
if stack_self.is_empty() {
|
||||
drop(stack_self);
|
||||
self.set_stack(stack.split(min_count)).await;
|
||||
} else if stack.are_items_and_components_equal(&stack_self) {
|
||||
stack.decrement(min_count);
|
||||
stack_self.increment(min_count);
|
||||
let cloned_stack = stack_self.clone();
|
||||
drop(stack_self);
|
||||
self.set_stack(cloned_stack).await;
|
||||
self.set_stack(stack_self).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use std::any::Any;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicU8, Ordering};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::player::player_inventory::PlayerInventory;
|
||||
use crate::screen_handler::{
|
||||
@@ -56,8 +55,7 @@ impl StonecutterScreenHandler {
|
||||
}
|
||||
|
||||
async fn update_output(&self) {
|
||||
let input_stack = self.input_inventory.get_stack(0).await;
|
||||
let input_lock = input_stack.lock().await;
|
||||
let input_lock = self.input_inventory.get_stack(0).await;
|
||||
|
||||
if input_lock.is_empty() {
|
||||
self.output_inventory
|
||||
@@ -213,14 +211,7 @@ impl Slot for StonecutterOutputSlot {
|
||||
stack.item_count as i32,
|
||||
)
|
||||
.await;
|
||||
let input_stack = self.input_inventory.get_stack(0).await;
|
||||
let mut input_lock = input_stack.lock().await;
|
||||
if !input_lock.is_empty() {
|
||||
input_lock.item_count -= 1;
|
||||
if input_lock.item_count == 0 {
|
||||
*input_lock = ItemStack::EMPTY.clone();
|
||||
}
|
||||
}
|
||||
self.input_inventory.remove_stack_specific(0, 1).await;
|
||||
self.mark_dirty().await;
|
||||
})
|
||||
}
|
||||
@@ -229,22 +220,16 @@ impl Slot for StonecutterOutputSlot {
|
||||
Box::pin(async move { false })
|
||||
}
|
||||
|
||||
fn get_stack(&self) -> BoxFuture<'_, Arc<Mutex<ItemStack>>> {
|
||||
fn get_stack(&self) -> BoxFuture<'_, ItemStack> {
|
||||
Box::pin(async move { self.inventory.get_stack(self.index).await })
|
||||
}
|
||||
|
||||
fn get_cloned_stack(&self) -> BoxFuture<'_, ItemStack> {
|
||||
Box::pin(async move {
|
||||
let stack = self.inventory.get_stack(self.index).await;
|
||||
stack.lock().await.clone()
|
||||
})
|
||||
Box::pin(async move { self.inventory.get_stack(self.index).await })
|
||||
}
|
||||
|
||||
fn has_stack(&self) -> BoxFuture<'_, bool> {
|
||||
Box::pin(async move {
|
||||
let stack = self.inventory.get_stack(self.index).await;
|
||||
!stack.lock().await.is_empty()
|
||||
})
|
||||
Box::pin(async move { !self.inventory.get_stack(self.index).await.is_empty() })
|
||||
}
|
||||
|
||||
fn set_stack(&self, stack: ItemStack) -> BoxFuture<'_, ()> {
|
||||
|
||||
Reference in new issue
Block a user