Skip to main content

azalea_inventory/
operations.rs

1use std::ops::RangeInclusive;
2
3use azalea_buf::AzBuf;
4use azalea_registry::item::{ItemStack, ItemStackData, MaxStackSizeExt};
5
6use crate::{
7    AnvilMenuLocation, BeaconMenuLocation, BlastFurnaceMenuLocation, BrewingStandMenuLocation,
8    CartographyTableMenuLocation, Crafter3x3MenuLocation, CraftingMenuLocation,
9    EnchantmentMenuLocation, FurnaceMenuLocation, Generic3x3MenuLocation, Generic9x1MenuLocation,
10    Generic9x2MenuLocation, Generic9x3MenuLocation, Generic9x4MenuLocation, Generic9x5MenuLocation,
11    Generic9x6MenuLocation, GrindstoneMenuLocation, HopperMenuLocation, LecternMenuLocation,
12    LoomMenuLocation, Menu, MenuLocation, MerchantMenuLocation, Player, PlayerMenuLocation,
13    ShulkerBoxMenuLocation, SmithingMenuLocation, SmokerMenuLocation, StonecutterMenuLocation,
14};
15
16/// A type of click in a Minecraft inventory.
17#[derive(Clone, Debug)]
18pub enum ClickOperation {
19    Pickup(PickupClick),
20    QuickMove(QuickMoveClick),
21    Swap(SwapClick),
22    Clone(CloneClick),
23    Throw(ThrowClick),
24    QuickCraft(QuickCraftClick),
25    PickupAll(PickupAllClick),
26}
27
28#[derive(Clone, Debug)]
29pub enum PickupClick {
30    /// Left mouse click.
31    ///
32    /// Note that in the protocol, None is represented as -999.
33    Left { slot: Option<u16> },
34    /// Right mouse click.
35    ///
36    /// Note that in the protocol, None is represented as -999.
37    Right { slot: Option<u16> },
38    /// Drop cursor stack.
39    LeftOutside,
40    /// Drop cursor single item.
41    RightOutside,
42}
43impl From<PickupClick> for ClickOperation {
44    fn from(click: PickupClick) -> Self {
45        ClickOperation::Pickup(click)
46    }
47}
48
49/// Shift click
50#[derive(Clone, Debug)]
51pub enum QuickMoveClick {
52    /// Shift + left mouse click
53    Left { slot: u16 },
54    /// Shift + right mouse click (identical behavior)
55    Right { slot: u16 },
56}
57impl From<QuickMoveClick> for ClickOperation {
58    fn from(click: QuickMoveClick) -> Self {
59        ClickOperation::QuickMove(click)
60    }
61}
62
63/// Used when you press number keys or F in an inventory.
64#[derive(Clone, Debug)]
65pub struct SwapClick {
66    pub source_slot: u16,
67    /// 0-8 for hotbar slots, 40 for offhand, everything else is treated as a
68    /// slot index.
69    pub target_slot: u8,
70}
71
72impl From<SwapClick> for ClickOperation {
73    fn from(click: SwapClick) -> Self {
74        ClickOperation::Swap(click)
75    }
76}
77/// Middle click, only defined for creative players in non-player
78/// inventories.
79#[derive(Clone, Debug)]
80pub struct CloneClick {
81    pub slot: u16,
82}
83impl From<CloneClick> for ClickOperation {
84    fn from(click: CloneClick) -> Self {
85        ClickOperation::Clone(click)
86    }
87}
88#[derive(Clone, Debug)]
89pub enum ThrowClick {
90    /// Drop key (Q)
91    Single { slot: u16 },
92    /// Ctrl + drop key (Q)
93    All { slot: u16 },
94}
95impl From<ThrowClick> for ClickOperation {
96    fn from(click: ThrowClick) -> Self {
97        ClickOperation::Throw(click)
98    }
99}
100#[derive(Clone, Debug, Eq, PartialEq)]
101pub struct QuickCraftClick {
102    pub kind: QuickCraftKind,
103    pub status: QuickCraftStatus,
104}
105#[derive(Clone, Debug, Eq, PartialEq)]
106pub enum QuickCraftKind {
107    Left,
108    Right,
109    Middle,
110}
111#[derive(Clone, Debug, Eq, PartialEq)]
112pub enum QuickCraftStatusKind {
113    /// Starting drag
114    Start,
115    /// Add slot
116    Add,
117    /// Ending drag
118    End,
119}
120#[derive(Clone, Debug, Eq, PartialEq)]
121pub enum QuickCraftStatus {
122    /// Starting drag
123    Start,
124    /// Add a slot.
125    Add { slot: u16 },
126    /// Ending drag
127    End,
128}
129impl From<QuickCraftStatus> for QuickCraftStatusKind {
130    fn from(status: QuickCraftStatus) -> Self {
131        match status {
132            QuickCraftStatus::Start => QuickCraftStatusKind::Start,
133            QuickCraftStatus::Add { .. } => QuickCraftStatusKind::Add,
134            QuickCraftStatus::End => QuickCraftStatusKind::End,
135        }
136    }
137}
138
139/// Double click.
140#[derive(Clone, Debug)]
141pub struct PickupAllClick {
142    /// The slot that we're double clicking on.
143    ///
144    /// It should be empty or at least not pickup-able (since the carried item
145    /// is used as the filter).
146    pub slot: u16,
147    /// Impossible in vanilla clients.
148    pub reversed: bool,
149}
150impl From<PickupAllClick> for ClickOperation {
151    fn from(click: PickupAllClick) -> Self {
152        ClickOperation::PickupAll(click)
153    }
154}
155
156impl ClickOperation {
157    /// Return the slot number that this operation is acting on, if any.
158    ///
159    /// Note that in the protocol, "None" is represented as -999.
160    pub fn slot_num(&self) -> Option<u16> {
161        match self {
162            ClickOperation::Pickup(pickup) => match pickup {
163                PickupClick::Left { slot } => *slot,
164                PickupClick::Right { slot } => *slot,
165                PickupClick::LeftOutside => None,
166                PickupClick::RightOutside => None,
167            },
168            ClickOperation::QuickMove(quick_move) => match quick_move {
169                QuickMoveClick::Left { slot } => Some(*slot),
170                QuickMoveClick::Right { slot } => Some(*slot),
171            },
172            ClickOperation::Swap(swap) => Some(swap.source_slot),
173            ClickOperation::Clone(clone) => Some(clone.slot),
174            ClickOperation::Throw(throw) => match throw {
175                ThrowClick::Single { slot } => Some(*slot),
176                ThrowClick::All { slot } => Some(*slot),
177            },
178            ClickOperation::QuickCraft(quick_craft) => match quick_craft.status {
179                QuickCraftStatus::Start => None,
180                QuickCraftStatus::Add { slot } => Some(slot),
181                QuickCraftStatus::End => None,
182            },
183            ClickOperation::PickupAll(pickup_all) => Some(pickup_all.slot),
184        }
185    }
186
187    pub fn button_num(&self) -> u8 {
188        match self {
189            ClickOperation::Pickup(pickup) => match pickup {
190                PickupClick::Left { .. } => 0,
191                PickupClick::Right { .. } => 1,
192                PickupClick::LeftOutside => 0,
193                PickupClick::RightOutside => 1,
194            },
195            ClickOperation::QuickMove(quick_move) => match quick_move {
196                QuickMoveClick::Left { .. } => 0,
197                QuickMoveClick::Right { .. } => 1,
198            },
199            ClickOperation::Swap(swap) => swap.target_slot,
200            ClickOperation::Clone(_) => 2,
201            ClickOperation::Throw(throw) => match throw {
202                ThrowClick::Single { .. } => 0,
203                ThrowClick::All { .. } => 1,
204            },
205            ClickOperation::QuickCraft(quick_craft) => match quick_craft {
206                QuickCraftClick {
207                    kind: QuickCraftKind::Left,
208                    status: QuickCraftStatus::Start,
209                } => 0,
210                QuickCraftClick {
211                    kind: QuickCraftKind::Right,
212                    status: QuickCraftStatus::Start,
213                } => 4,
214                QuickCraftClick {
215                    kind: QuickCraftKind::Middle,
216                    status: QuickCraftStatus::Start,
217                } => 8,
218                QuickCraftClick {
219                    kind: QuickCraftKind::Left,
220                    status: QuickCraftStatus::Add { .. },
221                } => 1,
222                QuickCraftClick {
223                    kind: QuickCraftKind::Right,
224                    status: QuickCraftStatus::Add { .. },
225                } => 5,
226                QuickCraftClick {
227                    kind: QuickCraftKind::Middle,
228                    status: QuickCraftStatus::Add { .. },
229                } => 9,
230                QuickCraftClick {
231                    kind: QuickCraftKind::Left,
232                    status: QuickCraftStatus::End,
233                } => 2,
234                QuickCraftClick {
235                    kind: QuickCraftKind::Right,
236                    status: QuickCraftStatus::End,
237                } => 6,
238                QuickCraftClick {
239                    kind: QuickCraftKind::Middle,
240                    status: QuickCraftStatus::End,
241                } => 10,
242            },
243            ClickOperation::PickupAll(_) => 0,
244        }
245    }
246
247    pub fn click_type(&self) -> ClickType {
248        match self {
249            ClickOperation::Pickup(_) => ClickType::Pickup,
250            ClickOperation::QuickMove(_) => ClickType::QuickMove,
251            ClickOperation::Swap(_) => ClickType::Swap,
252            ClickOperation::Clone(_) => ClickType::Clone,
253            ClickOperation::Throw(_) => ClickType::Throw,
254            ClickOperation::QuickCraft(_) => ClickType::QuickCraft,
255            ClickOperation::PickupAll(_) => ClickType::PickupAll,
256        }
257    }
258}
259
260#[derive(AzBuf, Clone, Copy, Debug, PartialEq)]
261pub enum ClickType {
262    Pickup = 0,
263    QuickMove = 1,
264    Swap = 2,
265    Clone = 3,
266    Throw = 4,
267    QuickCraft = 5,
268    PickupAll = 6,
269}
270
271impl Menu {
272    /// Shift-click a slot in this menu.
273    ///
274    /// Keep in mind that this doesn't send any packets to the server, it just
275    /// mutates this specific `Menu`.
276    pub fn quick_move_stack(&mut self, slot_index: usize) -> ItemStack {
277        let slot = self.slot(slot_index);
278        if slot.is_none() {
279            return ItemStack::Empty;
280        };
281
282        let slot_location = self
283            .location_for_slot(slot_index)
284            .expect("we just checked to make sure the slot is Some above, so this shouldn't be able to error");
285        match slot_location {
286            MenuLocation::Player(l) => match l {
287                PlayerMenuLocation::CraftResult => {
288                    self.try_move_item_to_slots(slot_index, Player::INVENTORY_SLOTS);
289                }
290                PlayerMenuLocation::Craft => {
291                    self.try_move_item_to_slots(slot_index, Player::INVENTORY_SLOTS);
292                }
293                PlayerMenuLocation::Armor => {
294                    self.try_move_item_to_slots(slot_index, Player::INVENTORY_SLOTS);
295                }
296                _ => {
297                    // TODO: armor handling (see quickMoveStack in
298                    // InventoryMenu.java)
299
300                    // if slot.kind().is_armor() &&
301
302                    // also offhand handling
303
304                    if l == PlayerMenuLocation::Inventory {
305                        // shift-clicking in hotbar moves to inventory, and vice
306                        // versa
307                        if Player::is_hotbar_slot(slot_index) {
308                            self.try_move_item_to_slots(
309                                slot_index,
310                                Player::INVENTORY_WITHOUT_HOTBAR_SLOTS,
311                            );
312                        } else {
313                            self.try_move_item_to_slots(slot_index, Player::HOTBAR_SLOTS);
314                        }
315                    } else {
316                        self.try_move_item_to_slots(slot_index, self.player_slots_range());
317                    }
318                }
319            },
320            MenuLocation::Generic9x1(l) => match l {
321                Generic9x1MenuLocation::Contents => {
322                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
323                }
324                Generic9x1MenuLocation::Player => {
325                    self.try_move_item_to_slots_or_toggle_hotbar(
326                        slot_index,
327                        Menu::GENERIC9X1_CONTENTS_SLOTS,
328                    );
329                }
330            },
331            MenuLocation::Generic9x2(l) => match l {
332                Generic9x2MenuLocation::Contents => {
333                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
334                }
335                Generic9x2MenuLocation::Player => {
336                    self.try_move_item_to_slots_or_toggle_hotbar(
337                        slot_index,
338                        Menu::GENERIC9X2_CONTENTS_SLOTS,
339                    );
340                }
341            },
342            MenuLocation::Generic9x3(l) => match l {
343                Generic9x3MenuLocation::Contents => {
344                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
345                }
346                Generic9x3MenuLocation::Player => {
347                    self.try_move_item_to_slots_or_toggle_hotbar(
348                        slot_index,
349                        Menu::GENERIC9X3_CONTENTS_SLOTS,
350                    );
351                }
352            },
353            MenuLocation::Generic9x4(l) => match l {
354                Generic9x4MenuLocation::Contents => {
355                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
356                }
357                Generic9x4MenuLocation::Player => {
358                    self.try_move_item_to_slots_or_toggle_hotbar(
359                        slot_index,
360                        Menu::GENERIC9X4_CONTENTS_SLOTS,
361                    );
362                }
363            },
364            MenuLocation::Generic9x5(l) => match l {
365                Generic9x5MenuLocation::Contents => {
366                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
367                }
368                Generic9x5MenuLocation::Player => {
369                    self.try_move_item_to_slots_or_toggle_hotbar(
370                        slot_index,
371                        Menu::GENERIC9X5_CONTENTS_SLOTS,
372                    );
373                }
374            },
375            MenuLocation::Generic9x6(l) => match l {
376                Generic9x6MenuLocation::Contents => {
377                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
378                }
379                Generic9x6MenuLocation::Player => {
380                    self.try_move_item_to_slots_or_toggle_hotbar(
381                        slot_index,
382                        Menu::GENERIC9X6_CONTENTS_SLOTS,
383                    );
384                }
385            },
386            MenuLocation::Generic3x3(l) => match l {
387                Generic3x3MenuLocation::Contents => {
388                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
389                }
390                Generic3x3MenuLocation::Player => {
391                    self.try_move_item_to_slots_or_toggle_hotbar(
392                        slot_index,
393                        Menu::GENERIC3X3_CONTENTS_SLOTS,
394                    );
395                }
396            },
397            MenuLocation::Crafter3x3(l) => match l {
398                Crafter3x3MenuLocation::Contents => {
399                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
400                }
401                Crafter3x3MenuLocation::Player => {
402                    self.try_move_item_to_slots_or_toggle_hotbar(
403                        slot_index,
404                        Menu::GENERIC3X3_CONTENTS_SLOTS,
405                    );
406                }
407            },
408            MenuLocation::Anvil(l) => match l {
409                AnvilMenuLocation::Player => {
410                    self.try_move_item_to_slots_or_toggle_hotbar(
411                        slot_index,
412                        Menu::ANVIL_FIRST_SLOT..=Menu::ANVIL_SECOND_SLOT,
413                    );
414                }
415                _ => {
416                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
417                }
418            },
419            MenuLocation::Beacon(l) => match l {
420                BeaconMenuLocation::Payment => {
421                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
422                }
423                BeaconMenuLocation::Player => {
424                    self.try_move_item_to_slots(
425                        slot_index,
426                        Menu::BEACON_PAYMENT_SLOT..=Menu::BEACON_PAYMENT_SLOT,
427                    );
428                }
429            },
430            MenuLocation::BlastFurnace(l) => match l {
431                BlastFurnaceMenuLocation::Player => {
432                    self.try_move_item_to_slots(
433                        slot_index,
434                        Menu::BLAST_FURNACE_INGREDIENT_SLOT..=Menu::BLAST_FURNACE_FUEL_SLOT,
435                    );
436                }
437                _ => {
438                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
439                }
440            },
441            MenuLocation::BrewingStand(l) => match l {
442                BrewingStandMenuLocation::Player => {
443                    self.try_move_item_to_slots(
444                        slot_index,
445                        *Menu::BREWING_STAND_BOTTLES_SLOTS.start()
446                            ..=Menu::BREWING_STAND_INGREDIENT_SLOT,
447                    );
448                }
449                _ => {
450                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
451                }
452            },
453            MenuLocation::Crafting(l) => match l {
454                CraftingMenuLocation::Player => {
455                    self.try_move_item_to_slots_or_toggle_hotbar(
456                        slot_index,
457                        Menu::CRAFTING_GRID_SLOTS,
458                    );
459                }
460                _ => {
461                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
462                }
463            },
464            MenuLocation::Enchantment(l) => match l {
465                EnchantmentMenuLocation::Player => {
466                    self.try_move_item_to_slots_or_toggle_hotbar(
467                        slot_index,
468                        Menu::ENCHANTMENT_ITEM_SLOT..=Menu::ENCHANTMENT_LAPIS_SLOT,
469                    );
470                }
471                _ => {
472                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
473                }
474            },
475            MenuLocation::Furnace(l) => match l {
476                FurnaceMenuLocation::Player => {
477                    self.try_move_item_to_slots(
478                        slot_index,
479                        Menu::FURNACE_INGREDIENT_SLOT..=Menu::FURNACE_FUEL_SLOT,
480                    );
481                }
482                _ => {
483                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
484                }
485            },
486            MenuLocation::Grindstone(l) => match l {
487                GrindstoneMenuLocation::Player => {
488                    self.try_move_item_to_slots_or_toggle_hotbar(
489                        slot_index,
490                        Menu::GRINDSTONE_INPUT_SLOT..=Menu::GRINDSTONE_ADDITIONAL_SLOT,
491                    );
492                }
493                _ => {
494                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
495                }
496            },
497            MenuLocation::Hopper(l) => match l {
498                HopperMenuLocation::Player => {
499                    self.try_move_item_to_slots_or_toggle_hotbar(
500                        slot_index,
501                        Menu::HOPPER_CONTENTS_SLOTS,
502                    );
503                }
504                _ => {
505                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
506                }
507            },
508            MenuLocation::Lectern(l) => match l {
509                LecternMenuLocation::Player => {
510                    self.try_move_item_to_slots_or_toggle_hotbar(
511                        slot_index,
512                        Menu::LECTERN_BOOK_SLOT..=Menu::LECTERN_BOOK_SLOT,
513                    );
514                }
515                _ => {
516                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
517                }
518            },
519            MenuLocation::Loom(l) => match l {
520                LoomMenuLocation::Player => {
521                    self.try_move_item_to_slots_or_toggle_hotbar(
522                        slot_index,
523                        Menu::LOOM_BANNER_SLOT..=Menu::LOOM_PATTERN_SLOT,
524                    );
525                }
526                _ => {
527                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
528                }
529            },
530            MenuLocation::Merchant(l) => match l {
531                MerchantMenuLocation::Player => {
532                    self.try_move_item_to_slots_or_toggle_hotbar(
533                        slot_index,
534                        Menu::MERCHANT_PAYMENTS_SLOTS,
535                    );
536                }
537                _ => {
538                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
539                }
540            },
541            MenuLocation::ShulkerBox(l) => match l {
542                ShulkerBoxMenuLocation::Player => {
543                    self.try_move_item_to_slots_or_toggle_hotbar(
544                        slot_index,
545                        Menu::SHULKER_BOX_CONTENTS_SLOTS,
546                    );
547                }
548                _ => {
549                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
550                }
551            },
552            MenuLocation::Smithing(l) => match l {
553                SmithingMenuLocation::Player => {
554                    self.try_move_item_to_slots_or_toggle_hotbar(
555                        slot_index,
556                        Menu::SMITHING_TEMPLATE_SLOT..=Menu::SMITHING_ADDITIONAL_SLOT,
557                    );
558                }
559                _ => {
560                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
561                }
562            },
563            MenuLocation::Smoker(l) => match l {
564                SmokerMenuLocation::Player => {
565                    self.try_move_item_to_slots(
566                        slot_index,
567                        Menu::SMOKER_INGREDIENT_SLOT..=Menu::SMOKER_FUEL_SLOT,
568                    );
569                }
570                _ => {
571                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
572                }
573            },
574            MenuLocation::CartographyTable(l) => match l {
575                CartographyTableMenuLocation::Player => {
576                    self.try_move_item_to_slots_or_toggle_hotbar(
577                        slot_index,
578                        Menu::CARTOGRAPHY_TABLE_MAP_SLOT..=Menu::CARTOGRAPHY_TABLE_ADDITIONAL_SLOT,
579                    );
580                }
581                _ => {
582                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
583                }
584            },
585            MenuLocation::Stonecutter(l) => match l {
586                StonecutterMenuLocation::Player => {
587                    self.try_move_item_to_slots_or_toggle_hotbar(
588                        slot_index,
589                        Menu::STONECUTTER_INPUT_SLOT..=Menu::STONECUTTER_INPUT_SLOT,
590                    );
591                }
592                _ => {
593                    self.try_move_item_to_slots(slot_index, self.player_slots_range());
594                }
595            },
596        }
597
598        ItemStack::Empty
599    }
600
601    fn try_move_item_to_slots_or_toggle_hotbar(
602        &mut self,
603        slot_index: usize,
604        target_slot_indexes: RangeInclusive<usize>,
605    ) {
606        if !self.try_move_item_to_slots(slot_index, target_slot_indexes) {
607            self.try_move_item_to_slots(
608                slot_index,
609                if self.is_hotbar_slot(slot_index) {
610                    self.player_slots_without_hotbar_range()
611                } else {
612                    self.hotbar_slots_range()
613                },
614            );
615        }
616    }
617
618    /// Whether the given item could be placed in this menu.
619    ///
620    /// TODO: right now this always returns true
621    pub fn may_place(&self, _target_slot_index: usize, _item: &ItemStackData) -> bool {
622        true
623    }
624
625    /// Whether the item in the given slot could be clicked and picked up.
626    ///
627    /// TODO: right now this always returns true
628    pub fn may_pickup(&self, _source_slot_index: usize) -> bool {
629        true
630    }
631
632    /// Whether the item in the slot can be picked up and placed.
633    pub fn allow_modification(&self, target_slot_index: usize) -> bool {
634        if !self.may_pickup(target_slot_index) {
635            return false;
636        }
637        let item = self.slot(target_slot_index).unwrap();
638        // the default here probably doesn't matter since we should only be
639        // calling this if we already checked that the slot isn't empty
640        item.as_present()
641            .is_some_and(|item| self.may_place(target_slot_index, item))
642    }
643
644    /// Get the maximum number of items that can be placed in this slot.
645    pub fn max_stack_size(&self, _target_slot_index: usize) -> i32 {
646        64
647    }
648
649    /// Try moving an item to a set of slots in this menu.
650    ///
651    /// Returns the updated item slot.
652    fn try_move_item_to_slots(
653        &mut self,
654        item_slot_index: usize,
655        target_slot_indexes: RangeInclusive<usize>,
656    ) -> bool {
657        let mut item_slot = self.slot(item_slot_index).unwrap().clone();
658
659        // first see if we can stack it with another item
660        if item_slot.kind().stackable() {
661            for target_slot_index in target_slot_indexes.clone() {
662                self.move_item_to_slot_if_stackable(&mut item_slot, target_slot_index);
663                if item_slot.is_empty() {
664                    break;
665                }
666            }
667        }
668
669        // and if not then just try putting it in an empty slot
670        if item_slot.is_present() {
671            for target_slot_index in target_slot_indexes {
672                self.move_item_to_slot_if_empty(&mut item_slot, target_slot_index);
673                if item_slot.is_empty() {
674                    break;
675                }
676            }
677        }
678
679        let is_source_slot_now_empty = item_slot.is_empty();
680
681        *self.slot_mut(item_slot_index).unwrap() = item_slot;
682        is_source_slot_now_empty
683    }
684
685    /// Merge this item slot into the target item slot, only if the target item
686    /// slot is present and the same item.
687    fn move_item_to_slot_if_stackable(
688        &mut self,
689        item_slot: &mut ItemStack,
690        target_slot_index: usize,
691    ) {
692        let ItemStack::Present(item) = item_slot else {
693            return;
694        };
695        let target_slot = self.slot(target_slot_index).unwrap();
696        if let ItemStack::Present(target_item) = target_slot {
697            // the target slot is empty, so we can just move the item there
698            if self.may_place(target_slot_index, item)
699                && target_item.is_same_item_and_components(item)
700            {
701                let slot_item_limit = self.max_stack_size(target_slot_index);
702                let new_target_slot_data = item.split(i32::min(slot_item_limit, item.count) as u32);
703
704                // get the target slot again but mut this time so we can update
705                // it
706                let target_slot = self.slot_mut(target_slot_index).unwrap();
707                *target_slot = ItemStack::from(new_target_slot_data);
708
709                item_slot.update_empty();
710            }
711        }
712    }
713
714    fn move_item_to_slot_if_empty(
715        &mut self,
716        source_item: &mut ItemStack,
717        target_slot_index: usize,
718    ) {
719        let ItemStack::Present(source_item_data) = source_item else {
720            return;
721        };
722        let target_slot = self.slot(target_slot_index).unwrap();
723        if target_slot.is_empty() && self.may_place(target_slot_index, source_item_data) {
724            let slot_item_limit = self.max_stack_size(target_slot_index);
725            let new_target_slot_data =
726                source_item_data.split(i32::min(slot_item_limit, source_item_data.count) as u32);
727            source_item.update_empty();
728
729            let target_slot = self.slot_mut(target_slot_index).unwrap();
730            *target_slot = new_target_slot_data.into();
731        }
732    }
733}