diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/CombinedContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/CombinedContainer.kt index 946136c0c..76be00a2b 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/CombinedContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/CombinedContainer.kt @@ -10,20 +10,16 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectFunction import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap import net.minecraft.world.Container import net.minecraft.world.entity.player.Player -import net.minecraft.world.inventory.Slot import net.minecraft.world.item.Item import net.minecraft.world.item.ItemStack -import ru.dbotthepony.mc.otm.container.util.IContainerSlot import ru.dbotthepony.mc.otm.container.util.containerSlot import ru.dbotthepony.mc.otm.container.util.iterator -import ru.dbotthepony.mc.otm.container.util.slotIterator import ru.dbotthepony.mc.otm.core.collect.concatIterators import ru.dbotthepony.mc.otm.core.collect.filter import ru.dbotthepony.mc.otm.core.collect.flatMap import ru.dbotthepony.mc.otm.core.collect.map import ru.dbotthepony.mc.otm.core.isNotEmpty import ru.dbotthepony.mc.otm.core.stream -import java.util.LinkedList import java.util.stream.Stream class CombinedContainer(containers: Stream>>) : IMatteryContainer { @@ -33,18 +29,6 @@ class CombinedContainer(containers: Stream>>) : IM private inner class Slot(override val slot: Int, val outer: IContainerSlot) : IContainerSlot by outer { override val container: Container get() = this@CombinedContainer - - override fun setChanged() { - outer.setChanged() - } - - override fun component1(): Int { - return super.component1() - } - - override fun component2(): ItemStack { - return super.component2() - } } private val slots: ImmutableList 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 5634a13d1..42f781e8f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerHelpers.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ContainerHelpers.kt @@ -18,7 +18,6 @@ import net.minecraft.world.item.ItemStack import net.minecraft.world.item.enchantment.EnchantmentEffectComponents import net.minecraft.world.item.enchantment.EnchantmentHelper import net.neoforged.neoforge.fluids.capability.IFluidHandler -import ru.dbotthepony.mc.otm.container.util.IContainerSlot import ru.dbotthepony.mc.otm.container.util.ItemStackHashStrategy import ru.dbotthepony.mc.otm.container.util.containerSlot import ru.dbotthepony.mc.otm.container.util.slotIterator diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/IContainerSlot.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/IContainerSlot.kt new file mode 100644 index 000000000..ee1ccfd22 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/IContainerSlot.kt @@ -0,0 +1,68 @@ +package ru.dbotthepony.mc.otm.container + +import net.minecraft.world.Container +import net.minecraft.world.item.Item +import net.minecraft.world.item.ItemStack +import net.minecraft.world.item.Items +import ru.dbotthepony.kommons.util.Delegate +import ru.dbotthepony.mc.otm.core.isNotEmpty + +/** + * While this somewhat similar to [net.minecraft.world.inventory.Slot], this slot is not meant + * for Player interaction. + */ +interface IContainerSlot : Delegate { + val slot: Int + val container: Container + + operator fun component1() = slot + operator fun component2() = item + + fun getMaxStackSize(item: ItemStack = this.item): Int { + return container.maxStackSize + } + + val isForbiddenForAutomation: Boolean get() { + return getFilter() === Items.AIR + } + + fun getFilter(): Item? + + /** + * @return whenever the filter was set. Returns false only if container can't be filtered. + */ + fun setFilter(filter: Item? = null): Boolean + + val hasFilter: Boolean + get() = getFilter() != null + + fun remove() { + container[slot] = ItemStack.EMPTY + } + + fun remove(count: Int): ItemStack { + return container.removeItem(slot, count) + } + + override fun get(): ItemStack { + return container[slot] + } + + override fun accept(t: ItemStack) { + container[slot] = t + } + + fun setChanged() { + container.setChanged() + } + + var item: ItemStack + get() = container[slot] + set(value) { container[slot] = value } + + val isEmpty: Boolean + get() = container[slot].isEmpty + + val isNotEmpty: Boolean + get() = container[slot].isNotEmpty +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/IMatteryContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/IMatteryContainer.kt index d212c0be6..cb2dfce45 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/IMatteryContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/IMatteryContainer.kt @@ -5,7 +5,6 @@ import net.minecraft.world.item.Item import net.minecraft.world.item.ItemStack import net.minecraft.world.item.Items import net.minecraft.world.item.crafting.RecipeInput -import ru.dbotthepony.mc.otm.container.util.IContainerSlot import ru.dbotthepony.mc.otm.core.collect.filter import ru.dbotthepony.mc.otm.core.collect.map import ru.dbotthepony.mc.otm.core.isNotEmpty 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 75c8a329b..20d103927 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt @@ -22,7 +22,6 @@ import net.minecraft.world.item.Items import net.neoforged.neoforge.common.util.INBTSerializable import org.apache.logging.log4j.LogManager import ru.dbotthepony.kommons.util.Delegate -import ru.dbotthepony.mc.otm.container.util.IContainerSlot import ru.dbotthepony.mc.otm.core.addSorted import ru.dbotthepony.mc.otm.core.collect.any import ru.dbotthepony.mc.otm.core.collect.count diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/util/Iterators.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/util/Iterators.kt index 95c58087e..1dd6bad9c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/util/Iterators.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/util/Iterators.kt @@ -3,76 +3,14 @@ package ru.dbotthepony.mc.otm.container.util import net.minecraft.world.Container import net.minecraft.world.item.Item import net.minecraft.world.item.ItemStack -import net.minecraft.world.item.Items -import ru.dbotthepony.kommons.util.Delegate +import ru.dbotthepony.mc.otm.container.IContainerSlot import ru.dbotthepony.mc.otm.container.IMatteryContainer import ru.dbotthepony.mc.otm.container.get -import ru.dbotthepony.mc.otm.container.set import ru.dbotthepony.mc.otm.core.collect.filter import ru.dbotthepony.mc.otm.core.collect.map import ru.dbotthepony.mc.otm.core.isNotEmpty -/** - * While this somewhat similar to [net.minecraft.world.inventory.Slot], this slot is not meant - * for Player interaction. - */ -interface IContainerSlot : Delegate { - val slot: Int - val container: Container - - operator fun component1() = slot - operator fun component2() = item - - fun getMaxStackSize(item: ItemStack = this.item): Int { - return container.maxStackSize - } - - val isForbiddenForAutomation: Boolean get() { - return getFilter() === Items.AIR - } - - fun getFilter(): Item? - - /** - * @return whenever the filter was set. Returns false only if container can't be filtered. - */ - fun setFilter(filter: Item? = null): Boolean - - val hasFilter: Boolean - get() = getFilter() != null - - fun remove() { - container[slot] = ItemStack.EMPTY - } - - fun remove(count: Int): ItemStack { - return container.removeItem(slot, count) - } - - override fun get(): ItemStack { - return container[slot] - } - - override fun accept(t: ItemStack) { - container[slot] = t - } - - fun setChanged() { - container.setChanged() - } - - var item: ItemStack - get() = container[slot] - set(value) { container[slot] = value } - - val isEmpty: Boolean - get() = container[slot].isEmpty - - val isNotEmpty: Boolean - get() = container[slot].isNotEmpty -} - -class ContainerSlot(override val slot: Int, override val container: Container) : IContainerSlot { +class SimpleContainerSlot(override val slot: Int, override val container: Container) : IContainerSlot { init { require(slot in 0 until container.containerSize) { "Slot out of bounds: $slot (container: $container with size ${container.containerSize})" } } @@ -90,7 +28,7 @@ fun Container.containerSlot(slot: Int): IContainerSlot { if (this is IMatteryContainer) { return containerSlot(slot) } else { - return ContainerSlot(slot, this) + return SimpleContainerSlot(slot, this) } } @@ -110,8 +48,8 @@ fun Container.slotIterator(nonEmpty: Boolean = true): Iterator { if (this is IMatteryContainer) { return slotIterator(nonEmpty) } else if (nonEmpty) { - return (0 until containerSize).iterator().filter { this[it].isNotEmpty }.map { ContainerSlot(it, this) } + return (0 until containerSize).iterator().filter { this[it].isNotEmpty }.map { SimpleContainerSlot(it, this) } } else { - return (0 until containerSize).iterator().map { ContainerSlot(it, this) } + return (0 until containerSize).iterator().map { SimpleContainerSlot(it, this) } } }