diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerHelpers.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerHelpers.kt index 5750aa75d..5634a13d1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerHelpers.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerHelpers.kt @@ -295,31 +295,7 @@ operator fun CraftingContainer.get(column: Int, row: Int, flop: Boolean): ItemSt } fun Container.sort(comparator: Comparator = ItemStackSorter.DEFAULT) { - if (isEmpty) - return - - val slots = slotIterator().filter { !it.isForbiddenForAutomation && it.getMaxStackSize() >= it.item.count }.toList() - - if (slots.isEmpty()) - return - - val items = Object2ObjectOpenCustomHashMap(ItemStackHashStrategy) - - slots.forEach { - val get = items[it.item] - - if (get == null) { - items[it.item] = it.item.copy() - } else { - get.count += it.item.count - } - } - - val sortedItems = ObjectArrayList(items.values) - sortedItems.sortWith(comparator) - - slots.forEach { it.remove() } - sortedItems.forEach { addItem(it, false) } + sortWithIndices(computeSortedIndices(comparator)) } fun Container.sortWithIndices(sortedSlots: IntCollection) { @@ -337,7 +313,12 @@ fun Container.sortWithIndices(sortedSlots: IntCollection) { if (value in 0 until containerSize && seen.add(value)) { val slot = containerSlot(value) - if (slot.isNotEmpty && !slot.isForbiddenForAutomation && slot.item.count <= slot.getMaxStackSize()) { + if ( + slot.isNotEmpty && + !slot.isForbiddenForAutomation && + slot.item.count <= slot.getMaxStackSize() && + (!slot.hasFilter || slot.getFilter() != slot.item.item || slot.getMaxStackSize() > 1) + ) { valid.add(slot) } }