diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt index 9061de489..688d803cd 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt @@ -44,8 +44,17 @@ import java.util.stream.StreamSupport import kotlin.collections.ArrayList @Suppress("UNUSED") -open class MatteryContainer(protected val watcher: Runnable, private val size: Int) : IMatteryContainer, INBTSerializable, StackedContentsCompatible { - constructor(size: Int) : this({}, size) +open class MatteryContainer(var listener: ContainerListener, private val size: Int) : IMatteryContainer, INBTSerializable, StackedContentsCompatible { + constructor(watcher: () -> Unit, size: Int) : this({ _, _, _ -> watcher.invoke() }, size) + constructor(size: Int) : this(EmptyListener, size) + + fun interface ContainerListener { + fun setChanged(slot: Int, new: ItemStack, old: ItemStack) + } + + object EmptyListener : ContainerListener { + override fun setChanged(slot: Int, new: ItemStack, old: ItemStack) {} + } init { require(size >= 0) { "Invalid container size $size" } @@ -254,7 +263,7 @@ open class MatteryContainer(protected val watcher: Runnable, private val size: I } protected open fun setChanged(slot: Int, new: ItemStack, old: ItemStack) { - watcher.run() + listener.setChanged(slot, new, old) } override fun serializeNBT(): CompoundTag { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryCraftingContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryCraftingContainer.kt index 3da9b65a8..27f03529e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryCraftingContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryCraftingContainer.kt @@ -23,9 +23,18 @@ object DummyMenu : AbstractContainerMenu(null, 0) { } // урод -open class MatteryCraftingContainer private constructor(watcher: Runnable, width: Int, height: Int, private val parent: MatteryContainer) : CraftingContainer(DummyMenu, width, height), IMatteryContainer by parent, INBTSerializable { - constructor(width: Int, height: Int) : this({}, width, height) - constructor(watcher: Runnable, width: Int, height: Int) : this(watcher, width, height, MatteryContainer(watcher, width * height)) +open class MatteryCraftingContainer private constructor(private val listener: MatteryContainer.ContainerListener, width: Int, height: Int, private val parent: MatteryContainer) : CraftingContainer(DummyMenu, width, height), IMatteryContainer by parent, INBTSerializable { + constructor(width: Int, height: Int) : this(MatteryContainer.EmptyListener, width, height) + constructor(listener: () -> Unit, width: Int, height: Int) : this({ _, _, _ -> listener.invoke() }, width, height, MatteryContainer(width * height)) + constructor(listener: MatteryContainer.ContainerListener, width: Int, height: Int) : this(listener, width, height, MatteryContainer(width * height)) + + init { + parent.listener = MatteryContainer.ContainerListener { slot, new, old -> this@MatteryCraftingContainer.setChanged(slot, new, old) } + } + + protected open fun setChanged(slot: Int, new: ItemStack, old: ItemStack) { + listener.setChanged(slot, new, old) + } override fun serializeNBT(): CompoundTag { return parent.serializeNBT() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt index a8fa5095e..35abc5839 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt @@ -10,8 +10,8 @@ import ru.dbotthepony.mc.otm.core.collect.reduce import ru.dbotthepony.mc.otm.core.math.Decimal import kotlin.math.pow -open class UpgradeContainer(slotCount: Int, open val allowedUpgrades: Set = UpgradeType.ALL, watcher: Runnable = Runnable {}) : MatteryContainer(watcher, slotCount), IMatteryUpgrade { - constructor(watcher: Runnable, slotCount: Int, allowedUpgrades: Set) : this(slotCount, allowedUpgrades, watcher) +open class UpgradeContainer(slotCount: Int, open val allowedUpgrades: Set = UpgradeType.ALL, listener: ContainerListener = EmptyListener) : MatteryContainer(listener, slotCount), IMatteryUpgrade { + constructor(listener: () -> Unit, slotCount: Int, allowedUpgrades: Set) : this(slotCount, allowedUpgrades, { _, _, _ -> listener.invoke() }) final override val upgradeTypes: Set get() = setOf()