From cf32fd9d2a03c67f211b57230764f4358f9d77ea Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 15 Mar 2025 17:43:41 +0700 Subject: [PATCH] Extract moveItemStackToSlots static context --- .../ru/dbotthepony/mc/otm/menu/MatteryMenu.kt | 184 +++++++++--------- 1 file changed, 93 insertions(+), 91 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt index eb29aaa04..576fba7bb 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt @@ -469,7 +469,7 @@ abstract class MatteryMenu( if (target.any { it.any { it.containerSlotOrNull() is IFilteredContainerSlot } }) { for (collection in target) { - if (moveItemStackTo(slot, collection, onlyFiltered = true)) { + if (moveItemStackTo(ply, slot, collection, onlyFiltered = true)) { any = true if (!slot.hasItem()) { @@ -480,7 +480,7 @@ abstract class MatteryMenu( } for (collection in target) { - if (moveItemStackTo(slot, collection)) { + if (moveItemStackTo(ply, slot, collection)) { any = true if (!slot.hasItem()) { @@ -527,95 +527,6 @@ abstract class MatteryMenu( return true } - fun moveItemStackTo( - source: Slot, - slots: Collection, - onlyFiltered: Boolean = false - ): Boolean { - val remainder = moveItemStackToSlots(source.item, slots, onlyFiltered = onlyFiltered) - - if (remainder.count == source.item.count) { - return false - } - - val copy = source.item.copy() - - if (remainder.isEmpty) { - source.setByPlayer(ItemStack.EMPTY) - source.onTake(player, copy) - } else { - copy.count = source.item.count - remainder.count - source.item.count = remainder.count - source.onTake(player, copy) - } - - return true - } - - fun moveItemStackToSlots(item: ItemStack, slots: Collection, simulate: Boolean = false, onlyFiltered: Boolean = false): ItemStack { - if (item.isEmpty) { - return ItemStack.EMPTY - } - - val copy = item.copy() - - // first pass - stack with existing slots - if (copy.isStackable) { - for (slot in slots) { - if (onlyFiltered && slot.containerSlotOrNull().let { it !is IFilteredContainerSlot || it.filter == null || !it.testSlotFilter(item) }) { - continue - } else if (!onlyFiltered && slot.containerSlotOrNull().let { it is IFilteredContainerSlot && it.filter != null }) { - continue - } - - val limit = slot.getMaxStackSize(copy) - - if (limit > slot.item.count && slot.mayPlace(item) && ItemStack.isSameItemSameComponents(slot.item, copy)) { - val newCount = (slot.item.count + copy.count).coerceAtMost(limit) - val diff = newCount - slot.item.count - copy.count -= diff - - if (!simulate) { - slot.item.count += diff - slot.setChanged() - } - - if (copy.isEmpty) { - return copy - } - } - } - } - - // second pass - drop stack into first free slot - for (slot in slots) { - if (onlyFiltered && slot.containerSlotOrNull().let { it !is IFilteredContainerSlot || it.filter == null || !it.testSlotFilter(item) }) { - continue - } else if (!onlyFiltered && slot.containerSlotOrNull().let { it is IFilteredContainerSlot && it.filter != null }) { - continue - } - - val limit = slot.getMaxStackSize(copy) - - if (!slot.hasItem() && slot.mayPlace(item)) { - val newCount = copy.count.coerceAtMost(limit) - - if (!simulate) { - slot.setByPlayer(copy.copy().also { it.count = newCount }) - slot.setChanged() - } - - copy.count -= newCount - - if (copy.isEmpty) { - return copy - } - } - } - - return copy - } - fun moveItemStackToSlots(item: ItemStack, initialSlot: Int, finalSlot: Int, inverse: Boolean = false, simulate: Boolean = false): ItemStack { if (initialSlot > finalSlot) { return item @@ -717,5 +628,96 @@ abstract class MatteryMenu( InventoryMenu.EMPTY_ARMOR_SLOT_LEGGINGS, InventoryMenu.EMPTY_ARMOR_SLOT_CHESTPLATE, InventoryMenu.EMPTY_ARMOR_SLOT_HELMET) + + fun moveItemStackTo( + player: Player, + source: Slot, + slots: Collection, + onlyFiltered: Boolean = false + ): Boolean { + val remainder = moveItemStackToSlots(source.item, slots, onlyFiltered = onlyFiltered) + + if (remainder.count == source.item.count) { + return false + } + + val copy = source.item.copy() + + if (remainder.isEmpty) { + source.setByPlayer(ItemStack.EMPTY) + source.onTake(player, copy) + } else { + copy.count = source.item.count - remainder.count + source.item.count = remainder.count + source.onTake(player, copy) + } + + return true + } + + + fun moveItemStackToSlots(item: ItemStack, slots: Collection, simulate: Boolean = false, onlyFiltered: Boolean = false): ItemStack { + if (item.isEmpty) { + return ItemStack.EMPTY + } + + val copy = item.copy() + + // first pass - stack with existing slots + if (copy.isStackable) { + for (slot in slots) { + if (onlyFiltered && slot.containerSlotOrNull().let { it !is IFilteredContainerSlot || it.filter == null || !it.testSlotFilter(item) }) { + continue + } else if (!onlyFiltered && slot.containerSlotOrNull().let { it is IFilteredContainerSlot && it.filter != null }) { + continue + } + + val limit = slot.getMaxStackSize(copy) + + if (limit > slot.item.count && slot.mayPlace(item) && ItemStack.isSameItemSameComponents(slot.item, copy)) { + val newCount = (slot.item.count + copy.count).coerceAtMost(limit) + val diff = newCount - slot.item.count + copy.count -= diff + + if (!simulate) { + slot.item.count += diff + slot.setChanged() + } + + if (copy.isEmpty) { + return copy + } + } + } + } + + // second pass - drop stack into first free slot + for (slot in slots) { + if (onlyFiltered && slot.containerSlotOrNull().let { it !is IFilteredContainerSlot || it.filter == null || !it.testSlotFilter(item) }) { + continue + } else if (!onlyFiltered && slot.containerSlotOrNull().let { it is IFilteredContainerSlot && it.filter != null }) { + continue + } + + val limit = slot.getMaxStackSize(copy) + + if (!slot.hasItem() && slot.mayPlace(item)) { + val newCount = copy.count.coerceAtMost(limit) + + if (!simulate) { + slot.setByPlayer(copy.copy().also { it.count = newCount }) + slot.setChanged() + } + + copy.count -= newCount + + if (copy.isEmpty) { + return copy + } + } + } + + return copy + } } }