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 1da0493a7..94a2d51e5 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt @@ -1,7 +1,5 @@ package ru.dbotthepony.mc.otm.container -import net.minecraft.MethodsReturnNonnullByDefault -import javax.annotation.ParametersAreNonnullByDefault import net.minecraft.world.item.ItemStack import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.ListTag @@ -130,14 +128,26 @@ open class MatteryContainer(val watcher: Runnable, private val size: Int) : Cont protected var handler: MatteryContainerHandler? = null fun handler( - insert_validator: (Int, ItemStack) -> Boolean, - extract_validator: (Int, Int, ItemStack) -> Boolean + insert_validator: (slot: Int, stack: ItemStack) -> Boolean, + extract_validator: (slot: Int, amount: Int, stack: ItemStack) -> Boolean ): MatteryContainerHandler { - return handler ?: MatteryContainerHandler(this, insert_validator, extract_validator).also { handler = it } + return handler ?: MatteryContainerHandler(this, object : MatteryContainerFilter { + override fun canInsert(slot: Int, stack: ItemStack) = insert_validator(slot, stack) + override fun canExtract(slot: Int, amount: Int, stack: ItemStack) = extract_validator(slot, amount, stack) + }).also { handler = it } + } + + fun handler( + filter: MatteryContainerFilter + ): MatteryContainerHandler { + return handler ?: MatteryContainerHandler(this, filter).also { handler = it } } fun handler(insert_validator: (Int, ItemStack) -> Boolean): MatteryContainerHandler { - return handler ?: MatteryContainerHandler(this, insert_validator).also { handler = it } + return handler ?: MatteryContainerHandler(this, object : MatteryContainerFilter { + override fun canInsert(slot: Int, stack: ItemStack) = insert_validator(slot, stack) + override fun canExtract(slot: Int, amount: Int, stack: ItemStack) = false + }).also { handler = it } } fun handler(): MatteryContainerHandler { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainerHandler.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainerHandler.kt index 4d65ecbc8..69b68d68f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainerHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainerHandler.kt @@ -4,10 +4,18 @@ import net.minecraft.world.item.ItemStack import net.minecraftforge.common.util.LazyOptional import net.minecraftforge.items.IItemHandler +interface MatteryContainerFilter { + fun canInsert(slot: Int, stack: ItemStack): Boolean + fun canExtract(slot: Int, amount: Int, stack: ItemStack): Boolean +} + class MatteryContainerHandler @JvmOverloads internal constructor( private val container: MatteryContainer, - private val insert_validator: (Int, ItemStack) -> Boolean = { _: Int, _: ItemStack -> true }, - private val extract_validator: (Int, Int, ItemStack) -> Boolean = { _: Int, _: Int, _: ItemStack -> true } + + private val filter: MatteryContainerFilter = object : MatteryContainerFilter { + override fun canInsert(slot: Int, stack: ItemStack) = true + override fun canExtract(slot: Int, amount: Int, stack: ItemStack) = true + }, ) : IItemHandler { private var handler = LazyOptional.of { this } @@ -27,7 +35,7 @@ class MatteryContainerHandler @JvmOverloads internal constructor( override fun getStackInSlot(slot: Int) = container[slot] override fun insertItem(slot: Int, stack: ItemStack, simulate: Boolean): ItemStack { - if (!insert_validator(slot, stack)) + if (!filter.canInsert(slot, stack)) return stack val localStack = container[slot] @@ -68,7 +76,7 @@ class MatteryContainerHandler @JvmOverloads internal constructor( val localStack = container.getItem(slot) if (localStack.isEmpty) return ItemStack.EMPTY - if (!extract_validator(slot, amount, localStack)) return ItemStack.EMPTY + if (!filter.canExtract(slot, amount, localStack)) return ItemStack.EMPTY val minimal = Math.min(amount, localStack.count) val copy = localStack.copy() @@ -88,6 +96,6 @@ class MatteryContainerHandler @JvmOverloads internal constructor( } override fun isItemValid(slot: Int, stack: ItemStack): Boolean { - return insert_validator(slot, stack) + return filter.canInsert(slot, stack) } }