To avoid name conflicts, move dynamic synchable group map implementation to asMap property

This commit is contained in:
DBotThePony 2025-04-10 22:29:52 +07:00
parent 1cfa2be1dd
commit 77443ee801
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -50,7 +50,7 @@ class DynamicSynchableGroup<T : Any>(
* first-time networking of [T] to remote
*/
private val writer: T.(RegistryFriendlyByteBuf) -> Unit = {},
) : ISynchable, ReferenceSet<T>, Int2ReferenceMap<T> {
) : ISynchable, ReferenceSet<T> {
private inner class RemoteState(val listener: Runnable) : IRemoteState {
private inner class RemoteSlot(val slot: Slot<T>, fromConstructor: Boolean) : Runnable, Closeable {
val remoteState = synchable(slot.value).createRemoteState(this)
@ -367,6 +367,30 @@ class DynamicSynchableGroup<T : Any>(
return any
}
/**
* Representation of this synchable group as an [Int2ReferenceMap]. Changes made to [DynamicSynchableGroup] are reflected in this map
* and vice versa. Entries can be put at arbitrary keys, but negative keys should be avoided, since they are not efficiently
* networked, and hence will create a lot of network traffic if synchables with negative keys get frequently updated.
*/
val asMap: Int2ReferenceMap<T> by lazy {
object : Int2ReferenceMap<T> {
override fun isEmpty(): Boolean {
return this@DynamicSynchableGroup.isEmpty()
}
override fun remove(key: Int): T? {
val slot = id2slot.remove(key) ?: return null
removeBySlot(slot)
return slot.value
}
override fun getOrDefault(key: Int, defaultValue: T): T {
return super.getOrDefault(key, defaultValue)
}
override val size: Int
get() = this@DynamicSynchableGroup.size
override fun put(key: Int, value: T): T? {
val slot = Slot(value, key)
val oldSlot = id2slot.put(key, slot)
@ -674,7 +698,9 @@ class DynamicSynchableGroup<T : Any>(
}
override val values: ReferenceCollection<T>
get() = this
get() = this@DynamicSynchableGroup
}
}
companion object {
private const val END = 0