Remove capability iterator
This commit is contained in:
parent
06e6168a73
commit
5e7c8c08f5
@ -18,14 +18,15 @@ import net.minecraft.world.level.block.Block
|
|||||||
import ru.dbotthepony.mc.otm.block.entity.MatteryDeviceBlockEntity
|
import ru.dbotthepony.mc.otm.block.entity.MatteryDeviceBlockEntity
|
||||||
import ru.dbotthepony.mc.otm.capability.matter.*
|
import ru.dbotthepony.mc.otm.capability.matter.*
|
||||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||||
|
import ru.dbotthepony.mc.otm.container.iterator
|
||||||
import ru.dbotthepony.mc.otm.container.stream
|
import ru.dbotthepony.mc.otm.container.stream
|
||||||
import ru.dbotthepony.mc.otm.core.collect.iterator
|
import ru.dbotthepony.mc.otm.core.collect.filterNotNull
|
||||||
|
import ru.dbotthepony.mc.otm.core.collect.map
|
||||||
import ru.dbotthepony.mc.otm.core.filterNotNull
|
import ru.dbotthepony.mc.otm.core.filterNotNull
|
||||||
import ru.dbotthepony.mc.otm.core.isNotEmpty
|
import ru.dbotthepony.mc.otm.core.isNotEmpty
|
||||||
import ru.dbotthepony.mc.otm.core.orNull
|
import ru.dbotthepony.mc.otm.core.orNull
|
||||||
import ru.dbotthepony.mc.otm.graph.matter.SimpleMatterNode
|
import ru.dbotthepony.mc.otm.graph.matter.SimpleMatterNode
|
||||||
import ru.dbotthepony.mc.otm.registry.MBlockEntities
|
import ru.dbotthepony.mc.otm.registry.MBlockEntities
|
||||||
import java.util.ArrayList
|
|
||||||
import java.util.stream.Stream
|
import java.util.stream.Stream
|
||||||
|
|
||||||
@MethodsReturnNonnullByDefault
|
@MethodsReturnNonnullByDefault
|
||||||
@ -110,7 +111,7 @@ class PatternStorageBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
|
|||||||
override val patternCapacity: Int get() {
|
override val patternCapacity: Int get() {
|
||||||
var stored = 0L
|
var stored = 0L
|
||||||
|
|
||||||
for ((_, pattern) in this.container.iterator(MatteryCapability.PATTERN))
|
for (pattern in this.container.iterator().map { it.getCapability(MatteryCapability.PATTERN).orNull() }.filterNotNull())
|
||||||
stored += pattern.patternCapacity.toLong()
|
stored += pattern.patternCapacity.toLong()
|
||||||
|
|
||||||
return if (stored > Int.MAX_VALUE) Int.MAX_VALUE else stored.toInt()
|
return if (stored > Int.MAX_VALUE) Int.MAX_VALUE else stored.toInt()
|
||||||
@ -119,7 +120,7 @@ class PatternStorageBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
|
|||||||
override val storedPatterns: Int get() {
|
override val storedPatterns: Int get() {
|
||||||
var stored = 0L
|
var stored = 0L
|
||||||
|
|
||||||
for ((_, pattern) in this.container.iterator(MatteryCapability.PATTERN))
|
for (pattern in this.container.iterator().map { it.getCapability(MatteryCapability.PATTERN).orNull() }.filterNotNull())
|
||||||
stored += pattern.storedPatterns.toLong()
|
stored += pattern.storedPatterns.toLong()
|
||||||
|
|
||||||
return if (stored > Int.MAX_VALUE) Int.MAX_VALUE else stored.toInt()
|
return if (stored > Int.MAX_VALUE) Int.MAX_VALUE else stored.toInt()
|
||||||
@ -131,8 +132,8 @@ class PatternStorageBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun insertPattern(pattern: PatternState, onlyUpdate: Boolean, simulate: Boolean): PatternInsertStatus {
|
override fun insertPattern(pattern: PatternState, onlyUpdate: Boolean, simulate: Boolean): PatternInsertStatus {
|
||||||
for (pair in this.container.iterator(MatteryCapability.PATTERN)) {
|
for (spattern in this.container.iterator().map { it.getCapability(MatteryCapability.PATTERN).orNull() }.filterNotNull()) {
|
||||||
val status = pair.second.insertPattern(pattern, onlyUpdate, simulate)
|
val status = spattern.insertPattern(pattern, onlyUpdate, simulate)
|
||||||
|
|
||||||
if (!status.isFailed) {
|
if (!status.isFailed) {
|
||||||
if (!simulate) {
|
if (!simulate) {
|
||||||
|
@ -43,4 +43,10 @@ class ContainerIterator(private val container: Container) : IContainerIterator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Container.iterator() = ContainerIterator(this)
|
fun Container.iterator(): IContainerIterator {
|
||||||
|
return if (this is MatteryContainer) {
|
||||||
|
iterator()
|
||||||
|
} else {
|
||||||
|
ContainerIterator(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -9,11 +9,8 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap
|
|||||||
import net.minecraft.world.Container
|
import net.minecraft.world.Container
|
||||||
import net.minecraft.world.item.ItemStack
|
import net.minecraft.world.item.ItemStack
|
||||||
import net.minecraft.world.item.enchantment.EnchantmentHelper.hasVanishingCurse
|
import net.minecraft.world.item.enchantment.EnchantmentHelper.hasVanishingCurse
|
||||||
import net.minecraftforge.common.capabilities.Capability
|
|
||||||
import net.minecraftforge.fluids.capability.IFluidHandler
|
import net.minecraftforge.fluids.capability.IFluidHandler
|
||||||
import ru.dbotthepony.mc.otm.core.addAll
|
import ru.dbotthepony.mc.otm.core.addAll
|
||||||
import ru.dbotthepony.mc.otm.core.collect.iterator
|
|
||||||
import ru.dbotthepony.mc.otm.core.collect.nonEmpty
|
|
||||||
import ru.dbotthepony.mc.otm.core.isNotEmpty
|
import ru.dbotthepony.mc.otm.core.isNotEmpty
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
@ -98,42 +95,6 @@ fun Container.vanishCursedItems() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Container.balance(slots: IntSet, checkForEmpty: Boolean = true) {
|
fun Container.balance(slots: IntSet, checkForEmpty: Boolean = true) {
|
||||||
if (slots.isEmpty() || checkForEmpty && !slots.any { getItem(it).isNotEmpty }) return
|
if (slots.isEmpty() || checkForEmpty && !slots.any { getItem(it).isNotEmpty }) return
|
||||||
|
|
||||||
|
@ -1,60 +0,0 @@
|
|||||||
package ru.dbotthepony.mc.otm.core.collect
|
|
||||||
|
|
||||||
import net.minecraft.world.Container
|
|
||||||
import net.minecraftforge.common.capabilities.Capability
|
|
||||||
import net.minecraftforge.common.capabilities.ICapabilityProvider
|
|
||||||
import ru.dbotthepony.mc.otm.container.iterator
|
|
||||||
import ru.dbotthepony.mc.otm.core.ifPresentK
|
|
||||||
|
|
||||||
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)
|
|
@ -1,5 +0,0 @@
|
|||||||
package ru.dbotthepony.mc.otm.core.collect
|
|
||||||
|
|
||||||
import net.minecraft.world.item.ItemStack
|
|
||||||
|
|
||||||
fun Iterator<ItemStack>.nonEmpty() = FilteredIterator(this) { !it.isEmpty }
|
|
@ -5,13 +5,11 @@ import net.minecraft.world.Container
|
|||||||
import net.minecraft.world.SimpleContainer
|
import net.minecraft.world.SimpleContainer
|
||||||
import net.minecraft.world.entity.player.Inventory
|
import net.minecraft.world.entity.player.Inventory
|
||||||
import net.minecraft.world.entity.player.Player
|
import net.minecraft.world.entity.player.Player
|
||||||
import net.minecraft.world.inventory.Slot
|
|
||||||
import net.minecraft.world.item.ItemStack
|
import net.minecraft.world.item.ItemStack
|
||||||
import net.minecraftforge.network.PacketDistributor
|
import net.minecraftforge.network.PacketDistributor
|
||||||
import ru.dbotthepony.mc.otm.block.entity.storage.ItemMonitorBlockEntity
|
import ru.dbotthepony.mc.otm.block.entity.storage.ItemMonitorBlockEntity
|
||||||
import ru.dbotthepony.mc.otm.block.entity.storage.ItemMonitorPlayerSettings
|
import ru.dbotthepony.mc.otm.block.entity.storage.ItemMonitorPlayerSettings
|
||||||
import ru.dbotthepony.mc.otm.container.get
|
import ru.dbotthepony.mc.otm.container.get
|
||||||
import ru.dbotthepony.mc.otm.core.collect.nonEmpty
|
|
||||||
import ru.dbotthepony.mc.otm.menu.MatteryPoweredMenu
|
import ru.dbotthepony.mc.otm.menu.MatteryPoweredMenu
|
||||||
import ru.dbotthepony.mc.otm.menu.MatterySlot
|
import ru.dbotthepony.mc.otm.menu.MatterySlot
|
||||||
import ru.dbotthepony.mc.otm.menu.data.INetworkedItemViewProvider
|
import ru.dbotthepony.mc.otm.menu.data.INetworkedItemViewProvider
|
||||||
@ -21,7 +19,6 @@ import ru.dbotthepony.mc.otm.registry.MMenus
|
|||||||
import ru.dbotthepony.mc.otm.storage.*
|
import ru.dbotthepony.mc.otm.storage.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
private class ResultSlot(container: Container) : MatterySlot(container, 0) {
|
private class ResultSlot(container: Container) : MatterySlot(container, 0) {
|
||||||
override fun mayPlace(itemStack: ItemStack): Boolean {
|
override fun mayPlace(itemStack: ItemStack): Boolean {
|
||||||
return false
|
return false
|
||||||
@ -198,7 +195,7 @@ class ItemMonitorMenu @JvmOverloads constructor(
|
|||||||
var maxStack = 64
|
var maxStack = 64
|
||||||
|
|
||||||
if (settings.craftingAmount == ItemMonitorPlayerSettings.Amount.FULL) {
|
if (settings.craftingAmount == ItemMonitorPlayerSettings.Amount.FULL) {
|
||||||
for (gridItem in tile.craftingGrid.iterator().nonEmpty()) {
|
for (gridItem in tile.craftingGrid.iterator()) {
|
||||||
if (!gridItem.isStackable) {
|
if (!gridItem.isStackable) {
|
||||||
hasUnstackables = true
|
hasUnstackables = true
|
||||||
break
|
break
|
||||||
@ -207,7 +204,7 @@ class ItemMonitorMenu @JvmOverloads constructor(
|
|||||||
} else {
|
} else {
|
||||||
maxStack = 0
|
maxStack = 0
|
||||||
|
|
||||||
for (gridItem in tile.craftingGrid.iterator().nonEmpty()) {
|
for (gridItem in tile.craftingGrid.iterator()) {
|
||||||
maxStack = maxStack.coerceAtLeast(gridItem.maxStackSize)
|
maxStack = maxStack.coerceAtLeast(gridItem.maxStackSize)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user