From 3db63aa5890a416fa1fb831a6e775628f5b82840 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 1 Jan 2024 11:45:06 +0700 Subject: [PATCH] Add MatteryContainer.ContainerListener --- .../mc/otm/container/MatteryContainer.kt | 15 ++++++++++++--- .../mc/otm/container/MatteryCraftingContainer.kt | 6 ++++-- .../mc/otm/container/UpgradeContainer.kt | 4 ++-- 3 files changed, 18 insertions(+), 7 deletions(-) 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 b6904f670..65831e744 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryCraftingContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryCraftingContainer.kt @@ -3,8 +3,10 @@ package ru.dbotthepony.mc.otm.container import net.minecraft.world.inventory.CraftingContainer import net.minecraft.world.item.ItemStack -open class MatteryCraftingContainer(watcher: Runnable, private val width: Int, private val height: Int) : MatteryContainer(watcher, width * height), CraftingContainer { - constructor(width: Int, height: Int) : this({}, width, height) +open class MatteryCraftingContainer(listener: ContainerListener, private val width: Int, private val height: Int) : MatteryContainer(listener, width * height), CraftingContainer { + constructor(listener: () -> Unit, width: Int, height: Int) : this({ _, _, _ -> listener.invoke() }, width, height) + constructor(width: Int, height: Int) : this(EmptyListener, width, height) + final override fun getWidth(): Int { return width } 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()