From 448041fe2e27b1c8b1c51f41dbeec90c607adcd7 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 14 Mar 2025 22:09:32 +0700 Subject: [PATCH] More specialized versions of iterators in IEnhancedContainer --- .../mc/otm/container/IEnhancedContainer.kt | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/IEnhancedContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/IEnhancedContainer.kt index 2eb1774fe..9e1d5bb60 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/IEnhancedContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/IEnhancedContainer.kt @@ -76,10 +76,22 @@ interface IEnhancedContainer : Container, RecipeInput, I } fun nonEmptySlotIterator(): Iterator { - return slotIterator().filter { it.isNotEmpty } + return nonEmptySlotIndexIterator().map { containerSlot(it) } } - private fun slotIterator(allowedSlots: IntCollection, predicate: Predicate): IntIterator { + fun emptySlotIterator(): Iterator { + return emptySlotIndexIterator().map { containerSlot(it) } + } + + fun nonEmptySlotIterator(allowedSlots: IntCollection): Iterator { + return nonEmptySlotIndexIterator(allowedSlots).map { containerSlot(it) } + } + + fun emptySlotIterator(allowedSlots: IntCollection): Iterator { + return emptySlotIndexIterator(allowedSlots).map { containerSlot(it) } + } + + private fun slotIndexWalker(allowedSlots: IntCollection, predicate: Predicate): IntIterator { return object : IntIterator() { private val parent = allowedSlots.intIterator() private var foundNext = false @@ -120,6 +132,14 @@ interface IEnhancedContainer : Container, RecipeInput, I } } + fun emptySlotIndexIterator(allowedSlots: IntCollection): IntIterator { + return slotIndexWalker(allowedSlots) { it.isEmpty } + } + + fun nonEmptySlotIndexIterator(allowedSlots: IntCollection): IntIterator { + return slotIndexWalker(allowedSlots) { it.isNotEmpty } + } + fun emptySlotIndexIterator(): IntIterator { return emptySlotIndexIterator(slotRange) } @@ -128,20 +148,30 @@ interface IEnhancedContainer : Container, RecipeInput, I return nonEmptySlotIndexIterator(slotRange) } - fun emptySlotIndexIterator(allowedSlots: IntCollection): IntIterator { - return slotIterator(allowedSlots) { it.isEmpty } + fun slotIndexWithItemIterator(item: Item): IntIterator { + return slotIndexWithItemIterator(item, slotRange) } - fun nonEmptySlotIndexIterator(allowedSlots: IntCollection): IntIterator { - return slotIterator(allowedSlots) { it.isNotEmpty } + fun slotIndexWithItemIterator(item: Item, allowedSlots: IntCollection): IntIterator { + val parent = nonEmptySlotIndexIterator(allowedSlots).filter { this[it].isNotEmpty && this[it].item === item } + + return object : IntIterator() { + override fun nextInt(): Int { + return parent.next() + } + + override fun hasNext(): Boolean { + return parent.hasNext() + } + } } - fun slotWithItemIterator(item: Item): IntIterator { + fun slotWithItemIterator(item: Item): Iterator { return slotWithItemIterator(item, slotRange) } - fun slotWithItemIterator(item: Item, allowedSlots: IntCollection): IntIterator { - return slotIterator(allowedSlots) { it.isNotEmpty && it.item === item } + fun slotWithItemIterator(item: Item, allowedSlots: IntCollection): Iterator { + return slotIndexWithItemIterator(item, allowedSlots).map { containerSlot(it) } } fun nextEmptySlot(startIndex: Int): Int { @@ -257,9 +287,7 @@ interface IEnhancedContainer : Container, RecipeInput, I return stack // двигаем в одинаковые слоты - for (i in slotWithItemIterator(stack.item, slots)) { - val slot = containerSlot(i) - + for (slot in slotWithItemIterator(stack.item, slots)) { val condition: Boolean if (slot is IFilteredContainerSlot) { @@ -295,9 +323,7 @@ interface IEnhancedContainer : Container, RecipeInput, I } if (!onlyIntoExisting) { - for (i in emptySlotIndexIterator(slots)) { - val slot = containerSlot(i) - + for (slot in emptySlotIterator(slots)) { val condition: Boolean if (slot is IFilteredContainerSlot) {