More streamlined iterators
This commit is contained in:
parent
e6b8b9b409
commit
70dac3ba93
@ -3,8 +3,8 @@ 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
|
||||
import java.util.function.Consumer
|
||||
|
||||
class ContainerIterator(private val container: Container) : MutableIterator<ItemStack> {
|
||||
private var index = 0
|
||||
@ -30,10 +30,7 @@ class ContainerIterator(private val container: Container) : MutableIterator<Item
|
||||
}
|
||||
}
|
||||
|
||||
fun Container.iterator() = ContainerIterator(this)
|
||||
|
||||
class NonEmptyContainerIterator(container: Container) : MutableIterator<ItemStack> {
|
||||
private val parent = ContainerIterator(container)
|
||||
open class NonEmptyItemStackIterator(protected open val parent: Iterator<ItemStack>) : Iterator<ItemStack> {
|
||||
private var itemStack: ItemStack? = null
|
||||
private var searched = false
|
||||
|
||||
@ -70,30 +67,29 @@ class NonEmptyContainerIterator(container: Container) : MutableIterator<ItemStac
|
||||
this.searched = false
|
||||
return itemStack
|
||||
}
|
||||
}
|
||||
|
||||
class NonEmptyMutableItemStackIterator(override val parent: MutableIterator<ItemStack>) : NonEmptyItemStackIterator(parent), MutableIterator<ItemStack> {
|
||||
override fun remove() {
|
||||
parent.remove()
|
||||
}
|
||||
}
|
||||
|
||||
fun Container.nonEmptyIterator() = NonEmptyContainerIterator(this)
|
||||
|
||||
class ContainerIteratorCapability<T>(container: Container, private val cap: Capability<T>) : MutableIterator<Pair<ItemStack, T>> {
|
||||
private val parent = NonEmptyContainerIterator(container)
|
||||
private var itemStack: ItemStack? = null
|
||||
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 (itemStack != null) {
|
||||
if (provider != null) {
|
||||
return
|
||||
}
|
||||
|
||||
for (item in parent) {
|
||||
item.getCapability(cap).ifPresentK {
|
||||
itemStack = item
|
||||
for (provider in parent) {
|
||||
provider.getCapability(cap).ifPresentK {
|
||||
this.provider = provider
|
||||
capability = it
|
||||
return
|
||||
}
|
||||
@ -105,28 +101,38 @@ class ContainerIteratorCapability<T>(container: Container, private val cap: Capa
|
||||
search()
|
||||
}
|
||||
|
||||
return itemStack != null
|
||||
return provider != null
|
||||
}
|
||||
|
||||
override fun next(): Pair<ItemStack, T> {
|
||||
override fun next(): Pair<P, T> {
|
||||
if (!searched) {
|
||||
search()
|
||||
}
|
||||
|
||||
val itemStack = itemStack ?: throw IllegalStateException("No next element")
|
||||
val provider = provider ?: throw IllegalStateException("No next element")
|
||||
val capability = capability ?: throw IllegalStateException("No next element")
|
||||
this.itemStack = null
|
||||
this.provider = null
|
||||
this.capability = null
|
||||
this.searched = false
|
||||
return itemStack to capability
|
||||
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>) = ContainerIteratorCapability(this, cap)
|
||||
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)) {
|
||||
|
Loading…
Reference in New Issue
Block a user