Extract moveItemStackToSlots static context

This commit is contained in:
DBotThePony 2025-03-15 17:43:41 +07:00
parent 427b5f9179
commit cf32fd9d2a
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -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<Slot>,
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<Slot>, 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<Slot>,
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<Slot>, 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
}
}
}