Remove ShadowContainer and MatteryContainer
This commit is contained in:
parent
621661c9fe
commit
bdeb740df0
@ -29,7 +29,6 @@ import net.minecraft.world.phys.Vec3
|
||||
import net.neoforged.neoforge.capabilities.Capabilities
|
||||
import ru.dbotthepony.mc.otm.block.decorative.CargoCrateBlock
|
||||
import ru.dbotthepony.mc.otm.block.entity.MatteryDeviceBlockEntity
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
|
@ -19,7 +19,6 @@ import ru.dbotthepony.mc.otm.capability.fluid.BlockMatteryFluidHandler
|
||||
import ru.dbotthepony.mc.otm.capability.moveFluid
|
||||
import ru.dbotthepony.mc.otm.config.ItemsConfig
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.get
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilter
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
|
@ -19,7 +19,6 @@ import net.neoforged.neoforge.fluids.FluidStack
|
||||
import net.neoforged.neoforge.fluids.capability.IFluidHandler
|
||||
import ru.dbotthepony.mc.otm.block.entity.MatteryDeviceBlockEntity
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.immutableList
|
||||
|
@ -21,7 +21,6 @@ import ru.dbotthepony.mc.otm.capability.energy.WorkerEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.matter.MatterStorageImpl
|
||||
import ru.dbotthepony.mc.otm.capability.matter.ProfiledMatterStorage
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.UpgradeContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
|
@ -17,7 +17,6 @@ import ru.dbotthepony.mc.otm.capability.MatteryCapability
|
||||
import ru.dbotthepony.mc.otm.capability.matter.IMatterStorage
|
||||
import ru.dbotthepony.mc.otm.capability.matter.ProfiledMatterStorage
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.immutableList
|
||||
|
@ -22,7 +22,6 @@ import ru.dbotthepony.mc.otm.capability.matter.MatterStorageImpl
|
||||
import ru.dbotthepony.mc.otm.capability.matter.ProfiledMatterStorage
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot
|
||||
|
@ -26,7 +26,6 @@ import ru.dbotthepony.mc.otm.capability.energy.WorkerEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.matter.*
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.math.Decimal
|
||||
|
@ -20,7 +20,6 @@ import ru.dbotthepony.mc.otm.capability.energy.ProfiledEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.energy.WorkerEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.matter.PatternState
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
|
@ -14,7 +14,6 @@ import ru.dbotthepony.mc.otm.capability.MatteryCapability
|
||||
import ru.dbotthepony.mc.otm.capability.energy.ProfiledEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.energy.WorkerEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.menu.storage.DriveRackMenu
|
||||
|
@ -18,7 +18,6 @@ import ru.dbotthepony.mc.otm.capability.energy.ProfiledEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.energy.WorkerEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.EnhancedContainer
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.core.get
|
||||
import ru.dbotthepony.mc.otm.core.math.Decimal
|
||||
import ru.dbotthepony.mc.otm.core.util.ItemStorageStackSorter
|
||||
|
@ -20,7 +20,6 @@ import ru.dbotthepony.mc.otm.capability.matteryEnergy
|
||||
import ru.dbotthepony.mc.otm.capability.maxEnergyStoredMattery
|
||||
import ru.dbotthepony.mc.otm.capability.transcieveEnergy
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
|
@ -12,7 +12,6 @@ import ru.dbotthepony.mc.otm.capability.*
|
||||
import ru.dbotthepony.mc.otm.capability.energy.GeneratorEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.energy.ProfiledEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
|
@ -11,7 +11,6 @@ import ru.dbotthepony.mc.otm.block.entity.ItemJob
|
||||
import ru.dbotthepony.mc.otm.block.entity.JobContainer
|
||||
import ru.dbotthepony.mc.otm.block.entity.JobStatus
|
||||
import ru.dbotthepony.mc.otm.block.entity.MatteryWorkerBlockEntity
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
|
@ -17,7 +17,6 @@ import ru.dbotthepony.mc.otm.capability.moveEnergy
|
||||
import ru.dbotthepony.mc.otm.config.EnergyBalanceValues
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.math.RelativeSide
|
||||
|
@ -19,7 +19,6 @@ import ru.dbotthepony.mc.otm.capability.energy.CombinedProfiledEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.energy.ProfiledEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.moveEnergy
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.math.RelativeSide
|
||||
|
@ -16,7 +16,6 @@ import ru.dbotthepony.mc.otm.capability.extractEnergy
|
||||
import ru.dbotthepony.mc.otm.capability.maxEnergyStoredMattery
|
||||
import ru.dbotthepony.mc.otm.capability.moveEnergy
|
||||
import ru.dbotthepony.mc.otm.capability.receiveEnergy
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot
|
||||
|
@ -25,7 +25,6 @@ import ru.dbotthepony.mc.otm.capability.item.CombinedItemHandler
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.EnhancedContainer
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
|
@ -10,7 +10,6 @@ import net.neoforged.neoforge.capabilities.Capabilities
|
||||
import ru.dbotthepony.mc.otm.block.entity.MatteryDeviceBlockEntity
|
||||
import ru.dbotthepony.mc.otm.block.entity.decorative.CargoCrateBlockEntity
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.multiblock.BlockEntityTag
|
||||
|
@ -16,7 +16,6 @@ import ru.dbotthepony.mc.otm.capability.matter.ProfiledMatterStorage
|
||||
import ru.dbotthepony.mc.otm.capability.moveMatter
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.math.Decimal
|
||||
|
@ -18,7 +18,6 @@ import ru.dbotthepony.mc.otm.capability.UpgradeType
|
||||
import ru.dbotthepony.mc.otm.capability.energy.ProfiledEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.capability.energy.WorkerEnergyStorage
|
||||
import ru.dbotthepony.mc.otm.config.MachinesConfig
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.HandlerFilter
|
||||
import ru.dbotthepony.mc.otm.container.balance
|
||||
import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
|
||||
|
@ -1,498 +0,0 @@
|
||||
package ru.dbotthepony.mc.otm.container
|
||||
|
||||
import com.mojang.serialization.Codec
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder
|
||||
import it.unimi.dsi.fastutil.ints.IntAVLTreeSet
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList
|
||||
import it.unimi.dsi.fastutil.ints.IntComparators
|
||||
import it.unimi.dsi.fastutil.ints.IntSpliterator
|
||||
import it.unimi.dsi.fastutil.objects.ObjectSpliterators
|
||||
import net.minecraft.core.HolderLookup
|
||||
import net.minecraft.core.registries.BuiltInRegistries
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraft.nbt.CompoundTag
|
||||
import net.minecraft.nbt.NbtOps
|
||||
import net.minecraft.nbt.Tag
|
||||
import net.minecraft.world.Container
|
||||
import net.minecraft.world.entity.player.Player
|
||||
import net.minecraft.world.entity.player.StackedContents
|
||||
import net.minecraft.world.inventory.StackedContentsCompatible
|
||||
import net.minecraft.world.item.Item
|
||||
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.core.addSorted
|
||||
import ru.dbotthepony.mc.otm.core.collect.any
|
||||
import ru.dbotthepony.mc.otm.core.collect.count
|
||||
import ru.dbotthepony.mc.otm.core.collect.emptyIterator
|
||||
import ru.dbotthepony.mc.otm.core.collect.filter
|
||||
import ru.dbotthepony.mc.otm.core.collect.map
|
||||
import ru.dbotthepony.mc.otm.core.collect.toList
|
||||
import ru.dbotthepony.mc.otm.core.immutableList
|
||||
import ru.dbotthepony.mc.otm.core.isNotEmpty
|
||||
import ru.dbotthepony.mc.otm.data.codec.minRange
|
||||
import ru.dbotthepony.mc.otm.network.StreamCodecs
|
||||
import ru.dbotthepony.mc.otm.network.syncher.ISynchable
|
||||
import ru.dbotthepony.mc.otm.network.syncher.SynchableObservedDelegate
|
||||
import java.util.*
|
||||
import java.util.function.Consumer
|
||||
import java.util.function.Predicate
|
||||
import java.util.function.Supplier
|
||||
import java.util.stream.Stream
|
||||
import java.util.stream.StreamSupport
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
@Suppress("UNUSED")
|
||||
open class MatteryContainer(var listener: ContainerListener, private val size: Int) : IMatteryContainer, INBTSerializable<Tag?>, StackedContentsCompatible {
|
||||
constructor(watcher: Runnable, size: Int) : this({ _, _, _ -> watcher.run() }, size)
|
||||
constructor(size: Int) : this(EmptyListener, size)
|
||||
|
||||
fun interface ContainerListener {
|
||||
fun setChanged(slot: Int, new: ItemStack, old: ItemStack)
|
||||
}
|
||||
|
||||
object EmptyListener : ContainerListener {
|
||||
override fun setChanged(slot: Int, new: ItemStack, old: ItemStack) {}
|
||||
}
|
||||
|
||||
init {
|
||||
require(size >= 0) { "Invalid container size $size" }
|
||||
}
|
||||
|
||||
private val slots = Array(size) { ItemStack.EMPTY }
|
||||
private val nonEmptyFlags = BitSet()
|
||||
private var nonEmptyIndices = IntArrayList()
|
||||
private var indicesReferenced = false
|
||||
private data class Update(val slot: Int, val new: ItemStack, val old: ItemStack)
|
||||
private val queuedUpdates = ArrayList<Update>()
|
||||
private var queueUpdates = false
|
||||
|
||||
private fun cowIndices() {
|
||||
if (indicesReferenced) {
|
||||
nonEmptyIndices = IntArrayList(nonEmptyIndices)
|
||||
indicesReferenced = false
|
||||
}
|
||||
}
|
||||
|
||||
private val trackedSlots: Array<ItemStack> = Array(size) { ItemStack.EMPTY }
|
||||
private val filters: Array<Item?> = arrayOfNulls(size)
|
||||
|
||||
var changeset = 0
|
||||
private set
|
||||
|
||||
override fun clearSlotFilters() {
|
||||
Arrays.fill(filters, null)
|
||||
}
|
||||
|
||||
val synchableFilters by lazy {
|
||||
immutableList<ISynchable> {
|
||||
for (i in 0 until size) {
|
||||
accept(SynchableObservedDelegate(Delegate.Of({ filters[i] }, { filters[i] = it }), StreamCodecs.ITEM_TYPE_NULLABLE))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final override fun setSlotFilter(slot: Int, filter: Item?): Boolean {
|
||||
filters[slot] = filter
|
||||
return true
|
||||
}
|
||||
|
||||
final override fun getSlotFilter(slot: Int) = filters[slot]
|
||||
final override fun hasSlotFilter(slot: Int) = filters[slot] !== null
|
||||
final override fun isSlotForbiddenForAutomation(slot: Int) = filters[slot] === Items.AIR
|
||||
|
||||
final override fun testSlotFilter(slot: Int, itemStack: ItemStack): Boolean {
|
||||
return testSlotFilter(slot, itemStack.item)
|
||||
}
|
||||
|
||||
final override fun testSlotFilter(slot: Int, item: Item): Boolean {
|
||||
if (filters[slot] == null) {
|
||||
return true
|
||||
} else {
|
||||
return filters[slot] === item
|
||||
}
|
||||
}
|
||||
|
||||
final override fun getContainerSize() = size
|
||||
protected open fun startedIgnoringUpdates() {}
|
||||
protected open fun stoppedIgnoringUpdates() {}
|
||||
|
||||
private data class SerializedItem(val item: ItemStack, val slot: Int) {
|
||||
companion object {
|
||||
val CODEC: Codec<SerializedItem> = RecordCodecBuilder.create {
|
||||
it.group(
|
||||
ItemStack.OPTIONAL_CODEC.fieldOf("item").forGetter { it.item },
|
||||
Codec.INT.minRange(0).fieldOf("slot").forGetter { it.slot },
|
||||
).apply(it, ::SerializedItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private data class SerializedFilter(val item: Item, val slot: Int) {
|
||||
companion object {
|
||||
val CODEC: Codec<SerializedFilter> = RecordCodecBuilder.create {
|
||||
it.group(
|
||||
BuiltInRegistries.ITEM.byNameCodec().fieldOf("item").forGetter { it.item },
|
||||
Codec.INT.minRange(0).fieldOf("slot").forGetter { it.slot },
|
||||
).apply(it, ::SerializedFilter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private data class SerializedState(
|
||||
val items: List<SerializedItem>,
|
||||
val filters: List<SerializedFilter>
|
||||
) {
|
||||
companion object {
|
||||
val CODEC: Codec<SerializedState> = RecordCodecBuilder.create {
|
||||
it.group(
|
||||
Codec.list(SerializedItem.CODEC).fieldOf("items").forGetter { it.items },
|
||||
Codec.list(SerializedFilter.CODEC).fieldOf("filters").forGetter { it.filters },
|
||||
).apply(it, ::SerializedState)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun deserializeNBT(registries: HolderLookup.Provider, tag: Tag?) {
|
||||
Arrays.fill(slots, ItemStack.EMPTY)
|
||||
Arrays.fill(filters, null)
|
||||
nonEmptyFlags.clear()
|
||||
nonEmptyIndices = IntArrayList()
|
||||
|
||||
if (tag != null) {
|
||||
SerializedState.CODEC.parse(registries.createSerializationContext(NbtOps.INSTANCE), tag)
|
||||
.resultOrPartial { LOGGER.error("Error deserializing container: $it") }
|
||||
.ifPresent {
|
||||
val freeSlots = IntAVLTreeSet()
|
||||
|
||||
for (i in 0 until size)
|
||||
freeSlots.add(i)
|
||||
|
||||
for ((item, slotID) in it.items) {
|
||||
if (item.isEmpty)
|
||||
continue
|
||||
|
||||
if (freeSlots.remove(slotID)) {
|
||||
slots[slotID] = item
|
||||
// trackedSlots[slotID] = item.copy()
|
||||
} else if (freeSlots.isEmpty()) {
|
||||
break
|
||||
} else {
|
||||
val slotID = freeSlots.firstInt()
|
||||
freeSlots.remove(slotID)
|
||||
slots[slotID] = item
|
||||
// trackedSlots[slotID] = item.copy()
|
||||
}
|
||||
}
|
||||
|
||||
for ((item, index) in it.filters) {
|
||||
if (index in 0 until size)
|
||||
filters[index] = item
|
||||
}
|
||||
|
||||
setChanged()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun internalSetChanged(slot: Int, new: ItemStack, old: ItemStack) {
|
||||
if (queueUpdates) {
|
||||
queuedUpdates.add(Update(slot, new, old))
|
||||
} else {
|
||||
setChanged(slot, new, old)
|
||||
}
|
||||
}
|
||||
|
||||
private fun runUpdates() {
|
||||
for ((slot, new, old) in queuedUpdates) {
|
||||
setChanged(slot, new, old)
|
||||
}
|
||||
|
||||
queuedUpdates.clear()
|
||||
}
|
||||
|
||||
protected open fun setChanged(slot: Int, new: ItemStack, old: ItemStack) {
|
||||
listener.setChanged(slot, new, old)
|
||||
}
|
||||
|
||||
override fun serializeNBT(registries: HolderLookup.Provider): CompoundTag {
|
||||
val state = SerializedState(
|
||||
slotIterator(true).map { SerializedItem(it.item, it.slot) }.toList(size),
|
||||
filters.withIndex().iterator().filter { it.value != null }.map { SerializedFilter(it.value!!, it.index) }.toList()
|
||||
)
|
||||
|
||||
return SerializedState.CODEC.encodeStart(registries.createSerializationContext(NbtOps.INSTANCE), state)
|
||||
.resultOrPartial { throw RuntimeException("Failed to encode container contents: $it") }.get() as CompoundTag
|
||||
}
|
||||
|
||||
final override fun isEmpty(): Boolean {
|
||||
return nonEmptyIndices.isEmpty
|
||||
}
|
||||
|
||||
operator fun contains(other: ItemStack): Boolean {
|
||||
for (i in 0 until size) {
|
||||
if (ItemStack.isSameItemSameComponents(this[i], other)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
final override fun getItem(slot: Int): ItemStack {
|
||||
val item = slots[slot]
|
||||
|
||||
if (item.isEmpty) {
|
||||
if (nonEmptyFlags[slot]) {
|
||||
setChanged(slot)
|
||||
}
|
||||
|
||||
return ItemStack.EMPTY
|
||||
} else {
|
||||
if (!nonEmptyFlags[slot]) {
|
||||
setChanged(slot)
|
||||
}
|
||||
|
||||
return item
|
||||
}
|
||||
}
|
||||
|
||||
override fun fillStackedContents(contents: StackedContents) {
|
||||
for (item in iterator()) {
|
||||
contents.accountStack(item)
|
||||
}
|
||||
}
|
||||
|
||||
final override fun removeItem(slot: Int, amount: Int): ItemStack {
|
||||
if (amount <= 0 || slot < 0 || slot >= size || slots[slot].isEmpty)
|
||||
return ItemStack.EMPTY
|
||||
|
||||
val old = slots[slot].copy()
|
||||
val split = slots[slot].split(amount)
|
||||
trackedSlots[slot] = slots[slot].copy()
|
||||
changeset++
|
||||
updateEmptyFlag(slot)
|
||||
internalSetChanged(slot, if (slots[slot].isEmpty) ItemStack.EMPTY else slots[slot], old)
|
||||
|
||||
return split
|
||||
}
|
||||
|
||||
final override fun removeItemNoUpdate(slot: Int): ItemStack {
|
||||
val old = slots[slot]
|
||||
slots[slot] = ItemStack.EMPTY
|
||||
trackedSlots[slot] = ItemStack.EMPTY
|
||||
|
||||
if (old.isNotEmpty) {
|
||||
updateEmptyFlag(slot)
|
||||
changeset++
|
||||
}
|
||||
|
||||
return old
|
||||
}
|
||||
|
||||
final override fun setItem(slot: Int, itemStack: ItemStack) {
|
||||
if (slots[slot].isEmpty && itemStack.isEmpty || itemStack === slots[slot])
|
||||
return
|
||||
|
||||
val old = slots[slot]
|
||||
slots[slot] = if (itemStack.isEmpty) ItemStack.EMPTY else itemStack
|
||||
trackedSlots[slot] = if (itemStack.isEmpty) ItemStack.EMPTY else itemStack.copy()
|
||||
|
||||
updateEmptyFlag(slot)
|
||||
changeset++
|
||||
internalSetChanged(slot, itemStack, old)
|
||||
}
|
||||
|
||||
final override fun setChanged() {
|
||||
queueUpdates = true
|
||||
|
||||
try {
|
||||
for (slot in 0 until size) {
|
||||
setChanged(slot)
|
||||
}
|
||||
|
||||
runUpdates()
|
||||
} finally {
|
||||
queuedUpdates.clear()
|
||||
queueUpdates = false
|
||||
}
|
||||
}
|
||||
|
||||
final override fun setChanged(slot: Int) {
|
||||
if (!ItemStack.isSameItemSameComponents(slots[slot], trackedSlots[slot])) {
|
||||
trackedSlots[slot] = slots[slot].copy()
|
||||
updateEmptyFlag(slot)
|
||||
changeset++
|
||||
internalSetChanged(slot, slots[slot], trackedSlots[slot])
|
||||
// mojang соси))0)0))0)))))0)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateEmptyFlag(slot: Int) {
|
||||
if (slots[slot].isEmpty) {
|
||||
if (nonEmptyFlags[slot]) {
|
||||
nonEmptyFlags[slot] = false
|
||||
cowIndices()
|
||||
nonEmptyIndices.rem(slot)
|
||||
}
|
||||
} else {
|
||||
if (!nonEmptyFlags[slot]) {
|
||||
nonEmptyFlags[slot] = true
|
||||
cowIndices()
|
||||
nonEmptyIndices.addSorted(slot, IntComparators.NATURAL_COMPARATOR)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun stillValid(player: Player): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
final override fun clearContent() {
|
||||
nonEmptyFlags.clear()
|
||||
nonEmptyIndices = IntArrayList()
|
||||
|
||||
Arrays.fill(trackedSlots, ItemStack.EMPTY)
|
||||
|
||||
for (slot in 0 until size) {
|
||||
if (!slots[slot].isEmpty) {
|
||||
val old = slots[slot]
|
||||
slots[slot] = ItemStack.EMPTY
|
||||
internalSetChanged(slot, ItemStack.EMPTY, old)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private inner class Iterator : kotlin.collections.Iterator<ItemStack> {
|
||||
init {
|
||||
indicesReferenced = true
|
||||
}
|
||||
|
||||
private val parent = nonEmptyIndices.intIterator()
|
||||
private var lastIndex = -1
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return parent.hasNext()
|
||||
}
|
||||
|
||||
override fun next(): ItemStack {
|
||||
lastIndex = parent.nextInt()
|
||||
return getItem(lastIndex)
|
||||
}
|
||||
}
|
||||
|
||||
private inner class Spliterator(private val parent: IntSpliterator) : java.util.Spliterator<ItemStack> {
|
||||
override fun tryAdvance(action: Consumer<in ItemStack>): Boolean {
|
||||
return parent.tryAdvance {
|
||||
action.accept(getItem(it))
|
||||
}
|
||||
}
|
||||
|
||||
override fun trySplit(): java.util.Spliterator<ItemStack>? {
|
||||
return parent.trySplit()?.let(::Spliterator)
|
||||
}
|
||||
|
||||
override fun estimateSize(): Long {
|
||||
return parent.estimateSize()
|
||||
}
|
||||
|
||||
override fun characteristics(): Int {
|
||||
return parent.characteristics()
|
||||
}
|
||||
}
|
||||
|
||||
final override fun iterator(): kotlin.collections.Iterator<ItemStack> {
|
||||
if (isEmpty) {
|
||||
return emptyIterator()
|
||||
}
|
||||
|
||||
return Iterator()
|
||||
}
|
||||
|
||||
final override fun iterator(nonEmpty: Boolean): kotlin.collections.Iterator<ItemStack> {
|
||||
if (!nonEmpty) {
|
||||
return (0 until size).iterator().map { slots[it] }
|
||||
} else if (isEmpty) {
|
||||
return emptyIterator()
|
||||
} else {
|
||||
return Iterator()
|
||||
}
|
||||
}
|
||||
|
||||
inner class Slot(val slot: Int) : IFilteredContainerSlot {
|
||||
override var item: ItemStack
|
||||
get() = this@MatteryContainer[slot]
|
||||
set(value) { this@MatteryContainer[slot] = value }
|
||||
override val maxStackSize: Int
|
||||
get() = this@MatteryContainer.maxStackSize
|
||||
|
||||
override fun remove(): ItemStack {
|
||||
return removeItemNoUpdate(slot)
|
||||
}
|
||||
|
||||
override fun remove(count: Int): ItemStack {
|
||||
return removeItem(slot, count)
|
||||
}
|
||||
|
||||
override var filter: Item?
|
||||
get() = getSlotFilter(slot)
|
||||
set(value) { setSlotFilter(slot, value) }
|
||||
|
||||
override val isForbiddenForAutomation: Boolean
|
||||
get() = isSlotForbiddenForAutomation(slot)
|
||||
|
||||
override fun maxStackSize(item: ItemStack): Int {
|
||||
return getMaxStackSize(slot, item)
|
||||
}
|
||||
|
||||
override fun setChanged() {
|
||||
setChanged(slot)
|
||||
}
|
||||
}
|
||||
|
||||
final override fun slotIterator(): kotlin.collections.Iterator<Slot> {
|
||||
indicesReferenced = true
|
||||
return nonEmptyIndices.iterator().map { Slot(it) }
|
||||
}
|
||||
|
||||
final override fun slotIterator(nonEmpty: Boolean): kotlin.collections.Iterator<Slot> {
|
||||
if (!nonEmpty) {
|
||||
return (0 until size).iterator().map { Slot(it) }
|
||||
} else if (isEmpty) {
|
||||
return emptyIterator()
|
||||
} else {
|
||||
indicesReferenced = true
|
||||
return nonEmptyIndices.iterator().map { Slot(it) }
|
||||
}
|
||||
}
|
||||
|
||||
final override fun containerSlot(slot: Int): Slot {
|
||||
return Slot(slot)
|
||||
}
|
||||
|
||||
final override fun countItem(item: Item): Int {
|
||||
return iterator().filter { it.item == item }.count().toInt()
|
||||
}
|
||||
|
||||
final override fun hasAnyOf(items: Set<Item>): Boolean {
|
||||
return iterator().any { it.item in items }
|
||||
}
|
||||
|
||||
final override fun hasAnyMatching(predicate: Predicate<ItemStack>): Boolean {
|
||||
return iterator().any(predicate)
|
||||
}
|
||||
|
||||
final override fun spliterator(): java.util.Spliterator<ItemStack> {
|
||||
if (isEmpty) {
|
||||
return ObjectSpliterators.emptySpliterator()
|
||||
}
|
||||
|
||||
indicesReferenced = true
|
||||
return Spliterator(nonEmptyIndices.intSpliterator())
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val LOGGER = LogManager.getLogger()
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package ru.dbotthepony.mc.otm.container
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap
|
||||
import net.minecraft.world.Container
|
||||
import net.minecraft.world.item.ItemStack
|
||||
|
||||
class ShadowContainer(private val parent: Container) : IContainer by IContainer.wrap(parent) {
|
||||
private val shadowed = Int2ObjectArrayMap<ItemStack>(0)
|
||||
|
||||
override fun clearContent() {
|
||||
shadowed.clear()
|
||||
parent.clearContent()
|
||||
}
|
||||
|
||||
override fun isEmpty(): Boolean {
|
||||
return parent.isEmpty && shadowed.isEmpty()
|
||||
}
|
||||
|
||||
override fun getItem(slot: Int): ItemStack {
|
||||
return shadowed[slot] ?: parent.getItem(slot)
|
||||
}
|
||||
|
||||
override fun removeItem(slot: Int, count: Int): ItemStack {
|
||||
val shadow = shadowed[slot] ?: return parent.removeItem(slot, count)
|
||||
val copy = shadow.copyWithCount(shadow.count.coerceAtLeast(count))
|
||||
shadow.split(count)
|
||||
if (shadow.isEmpty) shadowed[slot] = ItemStack.EMPTY
|
||||
return copy
|
||||
}
|
||||
|
||||
override fun removeItemNoUpdate(slot: Int): ItemStack {
|
||||
shadowed[slot] ?: return parent.removeItemNoUpdate(slot)
|
||||
val old = shadowed[slot]
|
||||
shadowed[slot] = ItemStack.EMPTY
|
||||
return old!!
|
||||
}
|
||||
|
||||
override fun setItem(slot: Int, item: ItemStack) {
|
||||
shadowed[slot] = item
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun shadow(container: Container, slot: Int, itemStack: ItemStack): Container {
|
||||
return ShadowContainer(container).also { it[slot] = itemStack }
|
||||
}
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package ru.dbotthepony.mc.otm.container
|
||||
|
||||
import net.minecraft.world.entity.player.StackedContents
|
||||
import net.minecraft.world.inventory.CraftingContainer
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import ru.dbotthepony.mc.otm.container.util.iterator
|
||||
import ru.dbotthepony.mc.otm.core.collect.toList
|
||||
|
||||
class ShadowCraftingContainer(private val parent: CraftingContainer) : IContainer by ShadowContainer(parent), CraftingContainer {
|
||||
override fun fillStackedContents(contents: StackedContents) {
|
||||
for (item in iterator()) {
|
||||
contents.accountStack(item)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getWidth(): Int {
|
||||
return parent.width
|
||||
}
|
||||
|
||||
override fun getHeight(): Int {
|
||||
return parent.height
|
||||
}
|
||||
|
||||
override fun getItems(): MutableList<ItemStack> {
|
||||
return iterator().toList()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun shadow(container: CraftingContainer, slot: Int, itemStack: ItemStack): CraftingContainer {
|
||||
return ShadowCraftingContainer(container).also { it[slot] = itemStack }
|
||||
}
|
||||
}
|
||||
}
|
@ -23,7 +23,6 @@ import ru.dbotthepony.mc.otm.client.ShiftPressedCond
|
||||
import ru.dbotthepony.mc.otm.client.isShiftDown
|
||||
import ru.dbotthepony.mc.otm.client.minecraft
|
||||
import ru.dbotthepony.mc.otm.config.ClientConfig
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.math.Decimal
|
||||
import ru.dbotthepony.mc.otm.core.nbt.map
|
||||
|
@ -10,7 +10,6 @@ import net.minecraft.world.item.crafting.RecipeType
|
||||
import net.minecraft.world.item.crafting.SingleRecipeInput
|
||||
import ru.dbotthepony.kommons.util.getValue
|
||||
import ru.dbotthepony.mc.otm.block.entity.decorative.GrillBlockEntity
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.immutableList
|
||||
|
@ -16,7 +16,6 @@ import ru.dbotthepony.kommons.util.getValue
|
||||
import ru.dbotthepony.kommons.util.setValue
|
||||
import ru.dbotthepony.mc.otm.block.entity.decorative.PainterBlockEntity
|
||||
import ru.dbotthepony.mc.otm.player.matteryPlayer
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.set
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
import ru.dbotthepony.mc.otm.core.addAll
|
||||
|
@ -86,7 +86,6 @@ import ru.dbotthepony.mc.otm.container.IContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.IEnhancedContainer
|
||||
import ru.dbotthepony.mc.otm.container.IFilteredContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.IMatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer
|
||||
import ru.dbotthepony.mc.otm.container.get
|
||||
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
|
||||
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
|
||||
|
Loading…
Reference in New Issue
Block a user