diff options
| author | mat <git@matdoes.dev> | 2025-06-04 01:53:24 -0330 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-06-04 01:53:24 -0330 |
| commit | f5f50b85e5d427aab6a0ef00570b4076b61babe8 (patch) | |
| tree | 721524e3e4b079adeb87caf5bba96eef34b4239c /azalea-inventory/src | |
| parent | f311ac27d47c43eb4c33d760f3e1d1f2b8008a4f (diff) | |
| download | azalea-drasl-f5f50b85e5d427aab6a0ef00570b4076b61babe8.tar.xz | |
re-enable click prediction and fix related issues
Diffstat (limited to 'azalea-inventory/src')
| -rw-r--r-- | azalea-inventory/src/operations.rs | 39 | ||||
| -rw-r--r-- | azalea-inventory/src/slot.rs | 44 |
2 files changed, 58 insertions, 25 deletions
diff --git a/azalea-inventory/src/operations.rs b/azalea-inventory/src/operations.rs index f410c2c5..e7668ab5 100644 --- a/azalea-inventory/src/operations.rs +++ b/azalea-inventory/src/operations.rs @@ -617,13 +617,26 @@ impl Menu { } /// Whether the item in the given slot could be clicked and picked up. + /// /// TODO: right now this always returns true pub fn may_pickup(&self, _source_slot_index: usize) -> bool { true } + /// Whether the item in the slot can be picked up and placed. + pub fn allow_modification(&self, target_slot_index: usize) -> bool { + if !self.may_pickup(target_slot_index) { + return false; + } + let item = self.slot(target_slot_index).unwrap(); + // the default here probably doesn't matter since we should only be calling this + // if we already checked that the slot isn't empty + item.as_present() + .is_some_and(|item| self.may_place(target_slot_index, item)) + } + /// Get the maximum number of items that can be placed in this slot. - pub fn max_stack_size(&self, _target_slot_index: usize) -> u32 { + pub fn max_stack_size(&self, _target_slot_index: usize) -> i32 { 64 } @@ -657,7 +670,10 @@ impl Menu { } } - item_slot.is_empty() + let is_source_slot_now_empty = item_slot.is_empty(); + + *self.slot_mut(item_slot_index).unwrap() = item_slot; + is_source_slot_now_empty } /// Merge this item slot into the target item slot, only if the target item @@ -677,7 +693,7 @@ impl Menu { && target_item.is_same_item_and_components(item) { let slot_item_limit = self.max_stack_size(target_slot_index); - let new_target_slot_data = item.split(u32::min(slot_item_limit, item.count as u32)); + let new_target_slot_data = item.split(i32::min(slot_item_limit, item.count) as u32); // get the target slot again but mut this time so we can update it let target_slot = self.slot_mut(target_slot_index).unwrap(); @@ -688,18 +704,23 @@ impl Menu { } } - fn move_item_to_slot_if_empty(&mut self, item_slot: &mut ItemStack, target_slot_index: usize) { - let ItemStack::Present(item) = item_slot else { + fn move_item_to_slot_if_empty( + &mut self, + source_item: &mut ItemStack, + target_slot_index: usize, + ) { + let ItemStack::Present(source_item_data) = source_item else { return; }; let target_slot = self.slot(target_slot_index).unwrap(); - if target_slot.is_empty() && self.may_place(target_slot_index, item) { + if target_slot.is_empty() && self.may_place(target_slot_index, source_item_data) { let slot_item_limit = self.max_stack_size(target_slot_index); - let new_target_slot_data = item.split(u32::min(slot_item_limit, item.count as u32)); + let new_target_slot_data = + source_item_data.split(i32::min(slot_item_limit, source_item_data.count) as u32); + source_item.update_empty(); let target_slot = self.slot_mut(target_slot_index).unwrap(); - *target_slot = ItemStack::Present(new_target_slot_data); - item_slot.update_empty(); + *target_slot = new_target_slot_data.into(); } } } diff --git a/azalea-inventory/src/slot.rs b/azalea-inventory/src/slot.rs index 66f8bf50..2d00236f 100644 --- a/azalea-inventory/src/slot.rs +++ b/azalea-inventory/src/slot.rs @@ -87,6 +87,13 @@ impl ItemStack { ItemStack::Present(i) => Some(i), } } + + pub fn as_present_mut(&mut self) -> Option<&mut ItemStackData> { + match self { + ItemStack::Empty => None, + ItemStack::Present(i) => Some(i), + } + } } /// An item in an inventory, with a count and NBT. Usually you want @@ -172,6 +179,16 @@ impl AzaleaWrite for ItemStack { } } +impl From<ItemStackData> for ItemStack { + fn from(item: ItemStackData) -> Self { + if item.is_empty() { + ItemStack::Empty + } else { + ItemStack::Present(item) + } + } +} + /// An update to an item's data components. /// /// Note that in vanilla items come with their own set of default components, @@ -311,24 +328,19 @@ impl PartialEq for DataComponentPatch { return false; } for (kind, component) in &self.components { - match other.components.get(kind) { - Some(other_component) => { - // we can't use PartialEq, but we can use our own eq method - if let Some(component) = component { - if let Some(other_component) = other_component { - if !component.eq((*other_component).clone()) { - return false; - } - } else { - return false; - } - } else if other_component.is_some() { - return false; - } - } - _ => { + let Some(other_component) = other.components.get(kind) else { + return false; + }; + // we can't use PartialEq, but we can use our own eq method + if let Some(component) = component { + let Some(other_component) = other_component else { + return false; + }; + if !component.eq((*other_component).clone()) { return false; } + } else if other_component.is_some() { + return false; } } true |
