move everything to uuids again

god damn it
This commit is contained in:
DBotThePony 2022-06-08 20:37:57 +07:00
parent 9a4f7c0a22
commit b93faaae42
Signed by: DBot
GPG Key ID: DCC23B5715498507
7 changed files with 58 additions and 62 deletions

View File

@ -40,12 +40,13 @@ import ru.dbotthepony.mc.otm.registry.MNames
import ru.dbotthepony.mc.otm.storage.*
import java.lang.ref.WeakReference
import java.math.BigInteger
import java.util.*
import java.util.stream.Stream
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
private class SlotTuple(val slot: Int, val stack: ItemStack)
private class TrackedTuple(override val stack: ItemStackWrapper, override val id: Long) : IStorageTuple<ItemStackWrapper> {
private class TrackedTuple(override val stack: ItemStackWrapper, override val id: UUID) : IStorageTuple<ItemStackWrapper> {
val children = Int2ObjectAVLTreeMap<SlotTuple>()
override fun toString(): String {
@ -188,9 +189,9 @@ class StorageBusBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matter
private inner class ItemHandlerComponent(private val parent: IItemHandler) : IStorageComponent<ItemStackWrapper> {
private inner class EventsSnapshot {
val index = Long2ObjectAVLTreeMap<BigInteger>()
val index = HashMap<UUID, BigInteger>()
fun change(key: Long, diff: BigInteger) {
fun change(key: UUID, diff: BigInteger) {
if (diff.isZero)
return
@ -241,8 +242,6 @@ class StorageBusBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matter
private val listeners = ArrayList<IStorageEventConsumer<ItemStackWrapper>>()
private var nextID = 0L
override fun addListener(listener: IStorageEventConsumer<ItemStackWrapper>): Boolean {
if (!listeners.contains(listener)) {
listeners.add(listener)
@ -259,7 +258,7 @@ class StorageBusBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matter
private var scanned = arrayOfNulls<ItemStack>(0)
private var scannedMap = arrayOfNulls<TrackedTuple>(0)
private val tuples = HashMap<ItemStackWrapper, TrackedTuple>()
private val index = Long2ObjectAVLTreeMap<TrackedTuple>()
private val index = HashMap<UUID, TrackedTuple>()
private fun removeTracked(slot: Int) {
scanned[slot] = null
@ -324,7 +323,7 @@ class StorageBusBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matter
var oldCount = BigInteger.ZERO
if (added) {
tuple = TrackedTuple(storageStack, nextID++)
tuple = TrackedTuple(storageStack, UUID.randomUUID())
index[tuple.id] = tuple
tuples[key] = tuple
} else {
@ -462,11 +461,11 @@ class StorageBusBlockEntity(blockPos: BlockPos, blockState: BlockState) : Matter
}
}
override fun get(id: Long): ItemStackWrapper {
override fun get(id: UUID): ItemStackWrapper {
return index[id]?.stack ?: ItemStackWrapper.EMPTY
}
override fun extractStack(id: Long, amount: BigInteger, simulate: Boolean): ItemStackWrapper {
override fun extractStack(id: UUID, amount: BigInteger, simulate: Boolean): ItemStackWrapper {
if (!amount.isPositive)
return ItemStackWrapper.EMPTY

View File

@ -37,7 +37,9 @@ import ru.dbotthepony.mc.otm.registry.MBlockEntities
import ru.dbotthepony.mc.otm.registry.MNames
import ru.dbotthepony.mc.otm.storage.*
import java.math.BigInteger
import java.util.*
import java.util.stream.Stream
import kotlin.collections.HashSet
abstract class AbstractStorageImportExport<T>(
blockType: BlockEntityType<*>,
@ -271,7 +273,7 @@ class StorageExporterBlockEntity(blockPos: BlockPos, blockState: BlockState) :
return StorageExporterMenu(containerID, inventory, this)
}
private val relevantTuples = LongArraySet()
private val relevantTuples = HashSet<UUID>()
override val storageType: StorageStackType<ItemStackWrapper>
get() = ITEM_STORAGE
@ -280,7 +282,7 @@ class StorageExporterBlockEntity(blockPos: BlockPos, blockState: BlockState) :
cell.addStorageComponent(this)
}
override fun addStack(stack: ItemStackWrapper, id: Long, provider: IStorageProvider<ItemStackWrapper>) {
override fun addStack(stack: ItemStackWrapper, id: UUID, provider: IStorageProvider<ItemStackWrapper>) {
if (!filter.match(stack.item)) {
return
}
@ -288,11 +290,11 @@ class StorageExporterBlockEntity(blockPos: BlockPos, blockState: BlockState) :
relevantTuples.add(id)
}
override fun changeStack(stack: ItemStackWrapper, id: Long, oldCount: BigInteger) {
override fun changeStack(stack: ItemStackWrapper, id: UUID, oldCount: BigInteger) {
// no-op
}
override fun removeStack(stack: ItemStackWrapper, id: Long) {
override fun removeStack(stack: ItemStackWrapper, id: UUID) {
relevantTuples.remove(id)
}
@ -317,7 +319,7 @@ class StorageExporterBlockEntity(blockPos: BlockPos, blockState: BlockState) :
override val targetCapability: Capability<IItemHandler>
get() = CapabilityItemHandler.ITEM_HANDLER_CAPABILITY
private val exportStacks: Stream<Pair<Long, ItemStackWrapper>>
private val exportStacks: Stream<Pair<UUID, ItemStackWrapper>>
get() {
val view = cell.storageGraph?.getVirtualComponent(ITEM_STORAGE) ?: return Stream.empty()
return relevantTuples.stream().map { it to view[it] }

View File

@ -27,7 +27,7 @@ abstract class AbstractMatteryDrive<T : IStorageStack> @JvmOverloads constructor
var maxDifferentStacks: Int = 0xFFFF
) : IMatteryDrive<T> {
protected val tuples = HashMap<T, IStorageTuple<T>>()
protected val tuplesByID: MutableMap<Long, IStorageTuple<T>> = Long2ObjectAVLTreeMap()
protected val tuplesByID: MutableMap<UUID, IStorageTuple<T>> = HashMap()
override var isDirty = false
set(value) {
@ -41,8 +41,6 @@ abstract class AbstractMatteryDrive<T : IStorageStack> @JvmOverloads constructor
var storedDifferentStacks = 0
protected set
protected var nextID = 0L
override var storedCount: BigInteger = BigInteger.ZERO
protected set
@ -83,7 +81,7 @@ abstract class AbstractMatteryDrive<T : IStorageStack> @JvmOverloads constructor
val copy = stack.copy() as T
copy.count = maxInsert
val state = StorageTuple(nextID++, copy)
val state = StorageTuple(UUID.randomUUID(), copy)
tuples[key] = state
tuplesByID[state.id] = state
@ -100,7 +98,7 @@ abstract class AbstractMatteryDrive<T : IStorageStack> @JvmOverloads constructor
}
@Suppress("unchecked_cast")
override fun extractStack(id: Long, amount: BigInteger, simulate: Boolean): T {
override fun extractStack(id: UUID, amount: BigInteger, simulate: Boolean): T {
val get = tuplesByID[id] ?: return storageType.empty
@Suppress("NAME_SHADOWING")
@ -194,7 +192,7 @@ abstract class AbstractMatteryDrive<T : IStorageStack> @JvmOverloads constructor
if (stack != null) {
storedCount += stack.count
storedDifferentStacks++
val tuple = StorageTuple(nextID++, stack)
val tuple = StorageTuple(UUID.randomUUID(), stack)
tuples[tuple.stack.key()] = tuple
tuplesByID[tuple.id] = tuple
}
@ -202,7 +200,7 @@ abstract class AbstractMatteryDrive<T : IStorageStack> @JvmOverloads constructor
}
}
override fun get(id: Long): T {
override fun get(id: UUID): T {
return tuplesByID[id]?.stack ?: storageType.empty
}

View File

@ -37,7 +37,7 @@ private val QIO_LOCATION = ResourceLocation(OverdriveThatMatters.MOD_ID, "item_s
private class QIOTuple(
val mekanismItem: HashedItem,
override val stack: ItemStackWrapper,
override val id: Long,
override val id: UUID,
var mark: Long
) : IStorageTuple<ItemStackWrapper>
@ -47,13 +47,11 @@ private class QIOFrequencyAccess(val parent: QIOFrequency) : IStorageComponent<I
override val storageType: StorageStackType<ItemStackWrapper>
get() = OverdriveThatMatters.INSTANCE.ITEM_STORAGE()
private val index = Long2ObjectAVLTreeMap<QIOTuple>()
private val index = HashMap<UUID, QIOTuple>()
private val tracked = HashMap<HashedItem, QIOTuple>()
private val listeners = ArrayList<IStorageEventConsumer<ItemStackWrapper>>()
private var nextID = 0L
override fun get(id: Long): ItemStackWrapper {
override fun get(id: UUID): ItemStackWrapper {
return index[id]?.stack ?: ItemStackWrapper.EMPTY
}
@ -90,7 +88,7 @@ private class QIOFrequencyAccess(val parent: QIOFrequency) : IStorageComponent<I
return inserted
}
override fun extractStack(id: Long, amount: BigInteger, simulate: Boolean): ItemStackWrapper {
override fun extractStack(id: UUID, amount: BigInteger, simulate: Boolean): ItemStackWrapper {
// Because there is no simulate method on QIO array, we have to simulate it by ourselves.
// ASSUMPTION: We can ALWAYS remove items from QIO grid.
@ -161,7 +159,7 @@ private class QIOFrequencyAccess(val parent: QIOFrequency) : IStorageComponent<I
}
}
} else {
val tuple = QIOTuple(at, ItemStackWrapper(at.stack).also { it.count = value.count.toBigInteger() }, nextID++, mark)
val tuple = QIOTuple(at, ItemStackWrapper(at.stack).also { it.count = value.count.toBigInteger() }, UUID.randomUUID(), mark)
index[tuple.id] = tuple
for (listener in listeners) {

View File

@ -155,12 +155,11 @@ class StackRemovePacket(val id: Int, val stackID: Int) {
* Creates a virtual, slotless container for Player to interaction with.
*/
open class NetworkedItemView(val ply: Player, val menu: MatteryMenu, val remote: Boolean) : IStorageEventConsumer<ItemStackWrapper> {
data class NetworkedItem constructor(val id: Int, val stack: ItemStack, val upstreamId: Long? = null)
data class NetworkedItem constructor(val id: Int, val stack: ItemStack, val upstreamId: UUID? = null)
override val storageType: StorageStackType<ItemStackWrapper>
get() = ITEM_STORAGE
protected var nextStackID = 0
// this (how client see and interact with)
val localState = Int2ObjectAVLTreeMap<NetworkedItem>()
@ -194,7 +193,7 @@ open class NetworkedItemView(val ply: Player, val menu: MatteryMenu, val remote:
}
// parent (e.g. VirtualComponent)
protected val upstreamState = Long2ObjectAVLTreeMap<NetworkedItem>()
protected val upstreamState = HashMap<UUID, NetworkedItem>()
protected val networkBacklog = ArrayList<Any>()
operator fun get(id: Int): NetworkedItem? = localState[id]
@ -232,8 +231,8 @@ open class NetworkedItemView(val ply: Player, val menu: MatteryMenu, val remote:
return localState.values.size
}
override fun addStack(stack: ItemStackWrapper, id: Long, provider: IStorageProvider<ItemStackWrapper>) = addObject(stack.stack, id)
override fun changeStack(stack: ItemStackWrapper, id: Long, oldCount: BigInteger) = changeObject(id, stack.count.toInt())
override fun addStack(stack: ItemStackWrapper, id: UUID, provider: IStorageProvider<ItemStackWrapper>) = addObject(stack.stack, id)
override fun changeStack(stack: ItemStackWrapper, id: UUID, oldCount: BigInteger) = changeObject(id, stack.count.toInt())
protected fun network(fn: () -> Any) {
if (!remote) {
@ -241,24 +240,26 @@ open class NetworkedItemView(val ply: Player, val menu: MatteryMenu, val remote:
}
}
override fun removeStack(stack: ItemStackWrapper, id: Long) {
override fun removeStack(stack: ItemStackWrapper, id: UUID) {
val get = upstreamState[id] ?: throw IllegalStateException("Unknown ItemStack with upstream id $id!")
upstreamState.remove(id)
localState.remove(get.id)
network { StackRemovePacket(menu.containerId, get.id) }
}
fun addObject(stack: ItemStack, id_upstream: Long) {
protected var nextItemID = 0
fun addObject(stack: ItemStack, id_upstream: UUID) {
check(!upstreamState.containsKey(id_upstream)) { "Already tracking ItemStack with upstream id $id_upstream!" }
val state = NetworkedItem(nextStackID++, stack.copy(), id_upstream)
val state = NetworkedItem(nextItemID++, stack.copy(), id_upstream)
this.localState[state.id] = state
upstreamState[id_upstream] = state
network { StackAddPacket(menu.containerId, state.id, stack) }
}
fun changeObject(id_upstream: Long, new_count: Int) {
fun changeObject(id_upstream: UUID, new_count: Int) {
val get = upstreamState[id_upstream] ?: throw IllegalStateException("Unknown ItemStack with upstream id $id_upstream!")
get.stack.count = new_count
network { StackChangePacket(menu.containerId, get.id, new_count) }

View File

@ -86,17 +86,17 @@ interface IStorageEventConsumer<T : IStorageStack> : IStorage<T> {
/**
* Fired on whenever an object is added (to listener) we subscribed to
*/
fun addStack(stack: T, id: Long, provider: IStorageProvider<T>)
fun addStack(stack: T, id: UUID, provider: IStorageProvider<T>)
/**
* Fired on whenever an object is changes on listener we subscribed to
*/
fun changeStack(stack: T, id: Long, oldCount: BigInteger)
fun changeStack(stack: T, id: UUID, oldCount: BigInteger)
/**
* Fired on whenever an object is removed from listener we subscribed to
*/
fun removeStack(stack: T, id: Long)
fun removeStack(stack: T, id: UUID)
}
interface IStorageAcceptor<T : IStorageStack> : IStorage<T> {
@ -112,7 +112,7 @@ interface IStorageProvider<T : IStorageStack> : IStorageEventProducer<T> {
* @param id identifier of stack
* @return stored object (not a copy). Do not edit it.
*/
operator fun get(id: Long): T
operator fun get(id: UUID): T
val stacks: Stream<IStorageTuple<T>>
@ -124,7 +124,7 @@ interface IStorageProvider<T : IStorageStack> : IStorageEventProducer<T> {
* @param simulate whenever to simulate the action or not
* @return copy of object, with amount of units actually extracted
*/
fun extractStack(id: Long, amount: BigInteger, simulate: Boolean): T
fun extractStack(id: UUID, amount: BigInteger, simulate: Boolean): T
}
fun <T : IStorageStack> IStorageProvider<T>.removeListenerAuto(listener: IStorageEventConsumer<T>): Boolean {
@ -166,11 +166,11 @@ fun <T : IStorageStack> IStorageEventConsumer<T>.removeStack(tuple: IStorageTupl
}
interface IStorageTuple<T : IStorageStack> {
val id: Long
val id: UUID
val stack: T
}
class StorageTuple<T : IStorageStack>(override val id: Long, override val stack: T) : IStorageTuple<T>
class StorageTuple<T : IStorageStack>(override val id: UUID, override val stack: T) : IStorageTuple<T>
/**
* Component which (most time) proxy other components (combine their contents into single view)

View File

@ -13,7 +13,7 @@ import kotlin.reflect.full.isSubclassOf
class RemoteTuple<T : IStorageStack>(
override val stack: T,
override val id: Long,
override val id: UUID,
val provider: IStorageProvider<T>,
val local: LocalTuple<T>
) : IStorageTuple<T> {
@ -30,7 +30,7 @@ class RemoteTuple<T : IStorageStack>(
}
}
class LocalTuple<T : IStorageStack>(override val stack: T, override val id: Long, val tuples: ArrayList<RemoteTuple<T>>) : IStorageTuple<T>
class LocalTuple<T : IStorageStack>(override val stack: T, override val id: UUID, val tuples: ArrayList<RemoteTuple<T>>) : IStorageTuple<T>
open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : IVirtualStorageComponent<T> {
constructor(type: Class<T>) : this(StorageRegistry.get(type))
@ -38,21 +38,19 @@ open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : IVir
override val storageType: StorageStackType<T> = type
// удаленный UUID -> Кортеж
protected val remoteTuples = Long2ObjectAVLTreeMap<RemoteTuple<T>>()
protected val remoteTuples = HashMap<UUID, RemoteTuple<T>>()
// локальный UUID -> Локальный кортеж
protected val localTuples = Long2ObjectAVLTreeMap<LocalTuple<T>>()
protected val localTuples = HashMap<UUID, LocalTuple<T>>()
// Стак -> Локальный кортеж стака
protected val hashedTuples: MutableMap<T, LocalTuple<T>> = HashMap()
protected val hashedTuples = HashMap<T, LocalTuple<T>>()
// ArrayList для скорости работы
protected val listeners: MutableSet<IStorageEventConsumer<T>> = ObjectArraySet()
protected val children: MutableSet<IStorage<T>> = ObjectArraySet()
protected val consumers: MutableSet<IStorageAcceptor<T>> = ObjectArraySet()
private var nextID = 0L
protected open fun onAdd(identity: IStorage<T>) {}
protected open fun onRemove(identity: IStorage<T>) {}
@ -118,7 +116,7 @@ open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : IVir
return listeners.remove(listener)
}
override fun get(id: Long): T {
override fun get(id: UUID): T {
return localTuples[id]?.stack ?: this.storageType.empty
}
@ -127,7 +125,7 @@ open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : IVir
}
@Suppress("unchecked_cast")
override fun addStack(stack: T, id: Long, provider: IStorageProvider<T>) {
override fun addStack(stack: T, id: UUID, provider: IStorageProvider<T>) {
check(!remoteTuples.containsKey(id)) { "Already tracking tuple with id $id" }
val key = stack.key()
@ -136,7 +134,7 @@ open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : IVir
val added = local == null
if (local == null) {
local = LocalTuple(stack.copy() as T, nextID++, ArrayList<RemoteTuple<T>>(1))
local = LocalTuple(stack.copy() as T, UUID.randomUUID(), ArrayList<RemoteTuple<T>>(1))
localTuples[local.id] = local
hashedTuples[key] = local
} else {
@ -159,7 +157,7 @@ open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : IVir
}
}
override fun changeStack(stack: T, id: Long, oldCount: BigInteger) {
override fun changeStack(stack: T, id: UUID, oldCount: BigInteger) {
require(stack.count.isPositive)
val tuple = remoteTuples[id] ?: throw IllegalStateException("No such tuple with id $id")
@ -174,7 +172,7 @@ open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : IVir
}
}
override fun removeStack(stack: T, id: Long) {
override fun removeStack(stack: T, id: UUID) {
val tuple = remoteTuples[id] ?: throw IllegalStateException("No such tuple with id $id")
tuple.local.stack.shrink(tuple.stack.count)
@ -212,7 +210,7 @@ open class VirtualComponent<T : IStorageStack>(type: StorageStackType<T>) : IVir
}
@Suppress("unchecked_cast")
override fun extractStack(id: Long, amount: BigInteger, simulate: Boolean): T {
override fun extractStack(id: UUID, amount: BigInteger, simulate: Boolean): T {
if (!amount.isPositive)
return this.storageType.empty
@ -318,9 +316,9 @@ open class PoweredComponent<T : IStorageStack>(open val parent: IStorageComponen
return stack
}
override fun get(id: Long) = parent[id]
override fun get(id: UUID) = parent[id]
override fun extractStack(id: Long, amount: BigInteger, simulate: Boolean): T {
override fun extractStack(id: UUID, amount: BigInteger, simulate: Boolean): T {
val required = storageType.energyPerOperation * amount
val energy = energyProvider.invoke()
val extracted = energy.extractEnergyInner(required, true)
@ -374,9 +372,9 @@ open class PoweredVirtualComponent<T : IStorageStack>(override val parent: IVirt
constructor(parent: Class<T>, energy: IMatteryEnergyStorage) : this(VirtualComponent(parent), { energy })
constructor(parent: StorageStackType<T>, energy: IMatteryEnergyStorage) : this(VirtualComponent(parent), { energy })
override fun addStack(stack: T, id: Long, provider: IStorageProvider<T>) = parent.addStack(stack, id, provider)
override fun changeStack(stack: T, id: Long, oldCount: BigInteger) = parent.changeStack(stack, id, oldCount)
override fun removeStack(stack: T, id: Long) = parent.removeStack(stack, id)
override fun addStack(stack: T, id: UUID, provider: IStorageProvider<T>) = parent.addStack(stack, id, provider)
override fun changeStack(stack: T, id: UUID, oldCount: BigInteger) = parent.changeStack(stack, id, oldCount)
override fun removeStack(stack: T, id: UUID) = parent.removeStack(stack, id)
override fun add(identity: IStorage<T>) = parent.add(identity)
override fun remove(identity: IStorage<T>) = parent.remove(identity)