From 124a1b3db646feaf8b2bd47bbcdb3d5f059449a3 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 27 Feb 2025 21:55:03 +0700 Subject: [PATCH] Get rid of "slot" and "container" in IContainerSlot since they were bad design choices --- .../dbotthepony/mc/otm/container/ContainerHelpers.kt | 11 +++++++---- .../ru/dbotthepony/mc/otm/container/ContainerSlot.kt | 6 +++--- .../ru/dbotthepony/mc/otm/container/IContainerSlot.kt | 8 +------- .../mc/otm/container/ISlottedContainerSlot.kt | 2 -- 4 files changed, 11 insertions(+), 16 deletions(-) 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 74cd6669f..4a3f69ce2 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerHelpers.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerHelpers.kt @@ -266,21 +266,24 @@ fun Container.computeSortedIndices(comparator: Comparator = ItemStack if (isEmpty) return IntList.of() - val slots = slotIterator().filter { (it !is IFilteredContainerSlot || !it.isForbiddenForAutomation) && it.maxStackSize(it.item) >= it.item.count }.toList() + val slots = slotIterator() + .withIndex() + .filter { (_, it) -> (it !is IFilteredContainerSlot || !it.isForbiddenForAutomation) && it.maxStackSize(it.item) >= it.item.count } + .toList() if (slots.isEmpty()) return IntList.of() val items = Object2ObjectOpenCustomHashMap>(ItemStackHashStrategy) - slots.forEach { + slots.forEach { (index, it) -> val get = items[it.item] if (get == null) { - items[it.item] = it.item.copy() to IntArrayList().also { s -> s.add(it.slot) } + items[it.item] = it.item.copy() to IntArrayList().also { s -> s.add(index) } } else { get.first.count += it.item.count - get.second.add(it.slot) + get.second.add(index) } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerSlot.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerSlot.kt index 027939b62..2106682a5 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerSlot.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerSlot.kt @@ -15,8 +15,8 @@ import ru.dbotthepony.mc.otm.core.registryName import ru.dbotthepony.mc.otm.data.getOrNull open class ContainerSlot( - final override val container: SlottedContainer, - final override val slot: Int + protected val container: SlottedContainer, + protected val slot: Int ) : ISlottedContainerSlot, INBTSerializable { private var _item: ItemStack = ItemStack.EMPTY @@ -110,7 +110,7 @@ open class ContainerSlot( open class Simple( protected val listener: (new: ItemStack, old: ItemStack) -> Unit, protected val maxStackSize: Int = Item.DEFAULT_MAX_STACK_SIZE, - ) : SlottedContainerBuilder.SlotProvider { + ) : SlottedContainer.SlotProvider { protected open inner class Instance(container: SlottedContainer, slot: Int) : ContainerSlot(container, slot) { override val maxStackSize: Int get() = this@Simple.maxStackSize diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/IContainerSlot.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/IContainerSlot.kt index e8860b407..de28c6fa4 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/IContainerSlot.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/IContainerSlot.kt @@ -10,15 +10,9 @@ import ru.dbotthepony.mc.otm.core.isNotEmpty * for Player interaction. */ interface IContainerSlot : Delegate { - val slot: Int - val container: Container - fun setChanged() var item: ItemStack - operator fun component1() = slot - operator fun component2() = item - /** * Max stack size regardless of item * @@ -53,7 +47,7 @@ interface IContainerSlot : Delegate { val isNotEmpty: Boolean get() = item.isNotEmpty - class Simple(override val slot: Int, override val container: Container) : IContainerSlot { + class Simple(private val slot: Int, private val container: Container) : IContainerSlot { override fun setChanged() { container.setChanged() } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/ISlottedContainerSlot.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ISlottedContainerSlot.kt index 23db5590a..9da3d32da 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/ISlottedContainerSlot.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ISlottedContainerSlot.kt @@ -7,8 +7,6 @@ import net.neoforged.neoforge.items.IItemHandler * Slot of [ISlottedContainer], with additional methods to implement interaction behavior for both for players and mechanisms */ interface ISlottedContainerSlot : IContainerSlot { - override val container: ISlottedContainer - fun canAutomationPlaceItem(itemStack: ItemStack): Boolean { return true }