Move iterators to more appropriate places
This commit is contained in:
parent
a9d78fc31a
commit
a355767ea6
@ -8,7 +8,8 @@ import net.minecraft.resources.ResourceLocation
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import ru.dbotthepony.mc.otm.core.TranslatableComponent
|
||||
import ru.dbotthepony.mc.otm.client.render.SkinElement
|
||||
import ru.dbotthepony.mc.otm.container.nonEmptyIterator
|
||||
import ru.dbotthepony.mc.otm.container.iterator
|
||||
import ru.dbotthepony.mc.otm.core.nonEmpty
|
||||
import ru.dbotthepony.mc.otm.registry.MRegistry
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
@ -260,7 +261,7 @@ class AndroidResearchBuilder(
|
||||
|
||||
for (item in items) {
|
||||
var required = item.count
|
||||
val iterator = capability.ply.inventory.nonEmptyIterator()
|
||||
val iterator = capability.ply.inventory.iterator().nonEmpty()
|
||||
|
||||
for (invItem in iterator) {
|
||||
if (ItemStack.isSameItemSameTags(invItem, item)) {
|
||||
|
@ -9,7 +9,6 @@ import ru.dbotthepony.mc.otm.capability.MatteryCapability
|
||||
import ru.dbotthepony.mc.otm.block.matter.PatternStorageBlock
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraftforge.common.util.LazyOptional
|
||||
import net.minecraftforge.items.CapabilityItemHandler
|
||||
import net.minecraft.world.entity.player.Inventory
|
||||
import net.minecraft.world.entity.player.Player
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu
|
||||
@ -25,13 +24,12 @@ import net.minecraftforge.common.capabilities.ForgeCapabilities
|
||||
import ru.dbotthepony.mc.otm.core.TranslatableComponent
|
||||
import ru.dbotthepony.mc.otm.capability.matter.*
|
||||
import ru.dbotthepony.mc.otm.container.forEachCapability
|
||||
import ru.dbotthepony.mc.otm.container.iterator
|
||||
import ru.dbotthepony.mc.otm.core.iterator
|
||||
import ru.dbotthepony.mc.otm.graph.Graph6Node
|
||||
import ru.dbotthepony.mc.otm.graph.matter.IMatterGraphNode
|
||||
import ru.dbotthepony.mc.otm.graph.matter.MatterNetworkGraph
|
||||
import ru.dbotthepony.mc.otm.core.ifHas
|
||||
import ru.dbotthepony.mc.otm.registry.MBlockEntities
|
||||
import ru.dbotthepony.mc.otm.container.set
|
||||
import ru.dbotthepony.mc.otm.core.set
|
||||
import java.util.ArrayList
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
package ru.dbotthepony.mc.otm.capability
|
||||
|
||||
import net.minecraft.core.Direction
|
||||
import net.minecraft.world.Container
|
||||
import net.minecraft.world.entity.LivingEntity
|
||||
import net.minecraft.world.entity.player.Player
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraftforge.common.capabilities.ForgeCapabilities
|
||||
@ -12,7 +10,7 @@ import net.minecraftforge.energy.IEnergyStorage
|
||||
import net.minecraftforge.fml.ModList
|
||||
import ru.dbotthepony.mc.otm.compat.mekanism.getMekanismEnergySided
|
||||
import ru.dbotthepony.mc.otm.compat.mekanism.mekanismEnergy
|
||||
import ru.dbotthepony.mc.otm.container.iterator
|
||||
import ru.dbotthepony.mc.otm.core.iterator
|
||||
import ru.dbotthepony.mc.otm.core.ImpreciseFraction
|
||||
import ru.dbotthepony.mc.otm.core.orNull
|
||||
|
||||
|
@ -25,4 +25,6 @@ class ContainerIterator(private val container: Container) : MutableIterator<Item
|
||||
|
||||
container[index - 1] = ItemStack.EMPTY
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Container.iterator() = ContainerIterator(this)
|
||||
|
@ -2,6 +2,9 @@ package ru.dbotthepony.mc.otm.container
|
||||
|
||||
import net.minecraft.world.Container
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraftforge.common.capabilities.Capability
|
||||
import ru.dbotthepony.mc.otm.core.iterator
|
||||
import ru.dbotthepony.mc.otm.core.nonEmpty
|
||||
|
||||
operator fun Container.set(index: Int, value: ItemStack) = setItem(index, value)
|
||||
operator fun Container.get(index: Int): ItemStack = getItem(index)
|
||||
@ -67,3 +70,39 @@ fun Container.addItem(stack: ItemStack, range: IntRange, simulate: Boolean = fal
|
||||
}
|
||||
|
||||
fun Container.addItem(stack: ItemStack, simulate: Boolean): ItemStack = addItem(stack, 0 until containerSize, simulate)
|
||||
|
||||
inline fun <T> Container.forEach(cap: Capability<T>, consumer: (Pair<ItemStack, T>) -> Unit) {
|
||||
for (value in iterator(cap)) {
|
||||
consumer(value)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> Container.forEach(cap: Capability<T>, consumer: (ItemStack, T) -> Unit) {
|
||||
for ((a, b) in iterator(cap)) {
|
||||
consumer(a, b)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> Container.forEachItem(cap: Capability<T>, consumer: (ItemStack) -> Unit) {
|
||||
for (pair in iterator(cap)) {
|
||||
consumer(pair.first)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> Container.forEachCapability(cap: Capability<T>, consumer: (T) -> Unit) {
|
||||
for (pair in iterator(cap)) {
|
||||
consumer(pair.second)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Container.forEach(lambda: (ItemStack) -> Unit) {
|
||||
for (value in iterator()) {
|
||||
lambda(value)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Container.forEachNonEmpty(lambda: (ItemStack) -> Unit) {
|
||||
for (value in iterator().nonEmpty()) {
|
||||
lambda(value)
|
||||
}
|
||||
}
|
||||
|
@ -1,147 +0,0 @@
|
||||
package ru.dbotthepony.mc.otm.container
|
||||
|
||||
import net.minecraft.world.Container
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraftforge.common.capabilities.Capability
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider
|
||||
import ru.dbotthepony.mc.otm.core.ifPresentK
|
||||
|
||||
open class NonEmptyItemStackIterator(protected open val parent: Iterator<ItemStack>) : Iterator<ItemStack> {
|
||||
private var itemStack: ItemStack? = null
|
||||
private var searched = false
|
||||
|
||||
private fun search() {
|
||||
searched = true
|
||||
|
||||
if (itemStack != null) {
|
||||
return
|
||||
}
|
||||
|
||||
for (stack in parent) {
|
||||
if (!stack.isEmpty) {
|
||||
itemStack = stack
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
if (!searched) {
|
||||
search()
|
||||
}
|
||||
|
||||
return itemStack != null
|
||||
}
|
||||
|
||||
override fun next(): ItemStack {
|
||||
if (!searched) {
|
||||
search()
|
||||
}
|
||||
|
||||
val itemStack = itemStack ?: throw IllegalStateException("No next element")
|
||||
this.itemStack = null
|
||||
this.searched = false
|
||||
return itemStack
|
||||
}
|
||||
}
|
||||
|
||||
class NonEmptyMutableItemStackIterator(override val parent: MutableIterator<ItemStack>) : NonEmptyItemStackIterator(parent), MutableIterator<ItemStack> {
|
||||
override fun remove() {
|
||||
parent.remove()
|
||||
}
|
||||
}
|
||||
|
||||
open class CapabilityIterator<P : ICapabilityProvider, T>(protected open val parent: Iterator<P>, private val cap: Capability<T>) : Iterator<Pair<P, T>> {
|
||||
private var provider: P? = null
|
||||
private var capability: T? = null
|
||||
private var searched = false
|
||||
|
||||
private fun search() {
|
||||
searched = true
|
||||
|
||||
if (provider != null) {
|
||||
return
|
||||
}
|
||||
|
||||
for (provider in parent) {
|
||||
provider.getCapability(cap).ifPresentK {
|
||||
this.provider = provider
|
||||
capability = it
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
if (!searched) {
|
||||
search()
|
||||
}
|
||||
|
||||
return provider != null
|
||||
}
|
||||
|
||||
override fun next(): Pair<P, T> {
|
||||
if (!searched) {
|
||||
search()
|
||||
}
|
||||
|
||||
val provider = provider ?: throw IllegalStateException("No next element")
|
||||
val capability = capability ?: throw IllegalStateException("No next element")
|
||||
this.provider = null
|
||||
this.capability = null
|
||||
this.searched = false
|
||||
return provider to capability
|
||||
}
|
||||
}
|
||||
|
||||
class MutableCapabilityIterator<P : ICapabilityProvider, T>(override val parent: MutableIterator<P>, cap: Capability<T>) : CapabilityIterator<P, T>(parent, cap), MutableIterator<Pair<P, T>> {
|
||||
override fun remove() {
|
||||
parent.remove()
|
||||
}
|
||||
}
|
||||
|
||||
fun Container.iterator() = ContainerIterator(this)
|
||||
fun Iterator<ItemStack>.nonEmpty() = NonEmptyItemStackIterator(this)
|
||||
fun MutableIterator<ItemStack>.nonEmpty() = NonEmptyMutableItemStackIterator(this)
|
||||
fun Container.nonEmptyIterator() = iterator().nonEmpty()
|
||||
|
||||
fun <T> Container.iterator(cap: Capability<T>) = CapabilityIterator(nonEmptyIterator(), cap)
|
||||
|
||||
fun <P : ICapabilityProvider, T> Iterator<P>.filtered(cap: Capability<T>) = CapabilityIterator(this, cap)
|
||||
fun <P : ICapabilityProvider, T> MutableIterator<P>.filtered(cap: Capability<T>) = MutableCapabilityIterator(this, cap)
|
||||
|
||||
inline fun <T> Container.forEach(cap: Capability<T>, consumer: (Pair<ItemStack, T>) -> Unit) {
|
||||
for (value in iterator(cap)) {
|
||||
consumer(value)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> Container.forEach(cap: Capability<T>, consumer: (ItemStack, T) -> Unit) {
|
||||
for ((a, b) in iterator(cap)) {
|
||||
consumer(a, b)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> Container.forEachItem(cap: Capability<T>, consumer: (ItemStack) -> Unit) {
|
||||
for (pair in iterator(cap)) {
|
||||
consumer(pair.first)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> Container.forEachCapability(cap: Capability<T>, consumer: (T) -> Unit) {
|
||||
for (pair in iterator(cap)) {
|
||||
consumer(pair.second)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Container.forEach(lambda: (ItemStack) -> Unit) {
|
||||
for (value in iterator()) {
|
||||
lambda(value)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Container.forEachNonEmpty(lambda: (ItemStack) -> Unit) {
|
||||
for (value in nonEmptyIterator()) {
|
||||
lambda(value)
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package ru.dbotthepony.mc.otm.core
|
||||
|
||||
import net.minecraft.world.Container
|
||||
import net.minecraftforge.common.capabilities.Capability
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider
|
||||
import ru.dbotthepony.mc.otm.container.iterator
|
||||
|
||||
open class CapabilityIterator<P : ICapabilityProvider, T>(protected open val parent: Iterator<P>, private val cap: Capability<T>) : Iterator<Pair<P, T>> {
|
||||
private var provider: P? = null
|
||||
private var capability: T? = null
|
||||
private var searched = false
|
||||
|
||||
private fun search() {
|
||||
searched = true
|
||||
|
||||
if (provider != null) {
|
||||
return
|
||||
}
|
||||
|
||||
for (provider in parent) {
|
||||
provider.getCapability(cap).ifPresentK {
|
||||
this.provider = provider
|
||||
capability = it
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
if (!searched) {
|
||||
search()
|
||||
}
|
||||
|
||||
return provider != null
|
||||
}
|
||||
|
||||
override fun next(): Pair<P, T> {
|
||||
if (!searched) {
|
||||
search()
|
||||
}
|
||||
|
||||
val provider = provider ?: throw IllegalStateException("No next element")
|
||||
val capability = capability ?: throw IllegalStateException("No next element")
|
||||
this.provider = null
|
||||
this.capability = null
|
||||
this.searched = false
|
||||
return provider to capability
|
||||
}
|
||||
}
|
||||
|
||||
class MutableCapabilityIterator<P : ICapabilityProvider, T>(override val parent: MutableIterator<P>, cap: Capability<T>) : CapabilityIterator<P, T>(parent, cap), MutableIterator<Pair<P, T>> {
|
||||
override fun remove() {
|
||||
parent.remove()
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> Container.iterator(cap: Capability<T>) = CapabilityIterator(iterator().nonEmpty(), cap)
|
||||
fun <P : ICapabilityProvider, T> Iterator<P>.filtered(cap: Capability<T>) = CapabilityIterator(this, cap)
|
||||
fun <P : ICapabilityProvider, T> MutableIterator<P>.filtered(cap: Capability<T>) = MutableCapabilityIterator(this, cap)
|
@ -0,0 +1,51 @@
|
||||
package ru.dbotthepony.mc.otm.core
|
||||
|
||||
import net.minecraft.world.item.ItemStack
|
||||
|
||||
open class NonEmptyItemStackIterator(protected open val parent: Iterator<ItemStack>) : Iterator<ItemStack> {
|
||||
private var itemStack: ItemStack? = null
|
||||
private var searched = false
|
||||
|
||||
private fun search() {
|
||||
searched = true
|
||||
|
||||
if (itemStack != null) {
|
||||
return
|
||||
}
|
||||
|
||||
for (stack in parent) {
|
||||
if (!stack.isEmpty) {
|
||||
itemStack = stack
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
if (!searched) {
|
||||
search()
|
||||
}
|
||||
|
||||
return itemStack != null
|
||||
}
|
||||
|
||||
override fun next(): ItemStack {
|
||||
if (!searched) {
|
||||
search()
|
||||
}
|
||||
|
||||
val itemStack = itemStack ?: throw IllegalStateException("No next element")
|
||||
this.itemStack = null
|
||||
this.searched = false
|
||||
return itemStack
|
||||
}
|
||||
}
|
||||
|
||||
class NonEmptyMutableItemStackIterator(override val parent: MutableIterator<ItemStack>) : NonEmptyItemStackIterator(parent), MutableIterator<ItemStack> {
|
||||
override fun remove() {
|
||||
parent.remove()
|
||||
}
|
||||
}
|
||||
|
||||
fun Iterator<ItemStack>.nonEmpty() = NonEmptyItemStackIterator(this)
|
||||
fun MutableIterator<ItemStack>.nonEmpty() = NonEmptyMutableItemStackIterator(this)
|
@ -22,7 +22,7 @@ import ru.dbotthepony.mc.otm.*
|
||||
import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.MatteryCapability
|
||||
import ru.dbotthepony.mc.otm.core.itemStackIterator
|
||||
import ru.dbotthepony.mc.otm.container.nonEmpty
|
||||
import ru.dbotthepony.mc.otm.core.nonEmpty
|
||||
import ru.dbotthepony.mc.otm.core.*
|
||||
import ru.dbotthepony.mc.otm.network.GenericNetworkChannel
|
||||
import ru.dbotthepony.mc.otm.network.MatteryPacket
|
||||
|
@ -11,7 +11,7 @@ import net.minecraftforge.network.PacketDistributor
|
||||
import ru.dbotthepony.mc.otm.block.entity.storage.ItemMonitorBlockEntity
|
||||
import ru.dbotthepony.mc.otm.block.entity.storage.ItemMonitorPlayerSettings
|
||||
import ru.dbotthepony.mc.otm.container.get
|
||||
import ru.dbotthepony.mc.otm.container.nonEmptyIterator
|
||||
import ru.dbotthepony.mc.otm.core.nonEmpty
|
||||
import ru.dbotthepony.mc.otm.menu.data.INetworkedItemViewProvider
|
||||
import ru.dbotthepony.mc.otm.menu.data.NetworkedItemView
|
||||
import ru.dbotthepony.mc.otm.network.MenuNetworkChannel
|
||||
@ -198,7 +198,7 @@ class ItemMonitorMenu @JvmOverloads constructor(
|
||||
var maxStack = 64
|
||||
|
||||
if (settings.craftingAmount == ItemMonitorPlayerSettings.Amount.FULL) {
|
||||
for (gridItem in tile.craftingGrid.nonEmptyIterator()) {
|
||||
for (gridItem in tile.craftingGrid.iterator().nonEmpty()) {
|
||||
if (!gridItem.isStackable) {
|
||||
hasUnstackables = true
|
||||
break
|
||||
@ -207,7 +207,7 @@ class ItemMonitorMenu @JvmOverloads constructor(
|
||||
} else {
|
||||
maxStack = 0
|
||||
|
||||
for (gridItem in tile.craftingGrid.nonEmptyIterator()) {
|
||||
for (gridItem in tile.craftingGrid.iterator().nonEmpty()) {
|
||||
maxStack = maxStack.coerceAtLeast(gridItem.maxStackSize)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user