diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/Ext.kt index 94024d62d..6b279ce3c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/Ext.kt @@ -119,8 +119,8 @@ inline fun Container.forEachNonEmpty(lambda: (ItemStack) -> Unit) { } } -fun Container.balance(slots: IntSet) { - if (slots.isEmpty() || !slots.any { getItem(it).isNotEmpty }) return +fun Container.balance(slots: IntSet, checkForEmpty: Boolean = true) { + if (slots.isEmpty() || checkForEmpty && !slots.any { getItem(it).isNotEmpty }) return val empty = IntArrayList() val itemTypes = Object2ObjectOpenCustomHashMap(ItemStackHashStrategy) @@ -250,5 +250,15 @@ fun Container.balance(slots: IntRange) { fun Container.balance(startSlot: Int = 0, endSlot: Int = containerSize - 1) { require(startSlot <= endSlot) { "Invalid slot range: $startSlot .. $endSlot" } - balance(IntArrayList(endSlot - startSlot + 1).also { for (i in startSlot .. endSlot) it.add(i) }) + var any = false + + for (i in startSlot .. endSlot) { + if (getItem(i).isNotEmpty) { + any = true + break + } + } + + if (!any) return + balance(IntArraySet(endSlot - startSlot + 1).also { for (i in startSlot .. endSlot) it.add(i) }, false) }