diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/DynamicSynchableGroup.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/DynamicSynchableGroup.kt index 4d72a71c7..c6018b77c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/DynamicSynchableGroup.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/DynamicSynchableGroup.kt @@ -10,29 +10,39 @@ import java.io.Closeable import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.atomic.AtomicBoolean +/** + * Constructs a new [DynamicSynchableGroup] where [T] is itself an [ISynchable] + */ +fun DynamicSynchableGroup(reader: RegistryFriendlyByteBuf.() -> T, writer: T.(RegistryFriendlyByteBuf) -> Unit): DynamicSynchableGroup { + return DynamicSynchableGroup(reader, { this }, writer) +} + /** * Syncher group/set, which deals with synchables of only one type, and which are created and removed * on remote (e.g. server adding and removing synchables at will), which makes it distinct from * [SynchableGroup], in which attached synchables are created/removed manually on both sides. */ -class DynamicSynchableGroup( +class DynamicSynchableGroup( /** * Constructs new [T] instance locally, when remote created one. * Data written by [writer] must be read here, if there is any. */ private val reader: RegistryFriendlyByteBuf.() -> T, + /** + * Gets an [ISynchable] out of value [T] for synching state + */ + private val synchable: T.() -> ISynchable, + /** * Allows to write additional data to network stream during * first-time networking of [T] to remote */ - private val writer: T.(RegistryFriendlyByteBuf) -> Unit = {} + private val writer: T.(RegistryFriendlyByteBuf) -> Unit = {}, ) : ISynchable, MutableSet { - constructor(factory: () -> T) : this({ factory() }, {}) - private inner class RemoteState(val listener: Runnable) : IRemoteState { private inner class RemoteSlot(val slot: Slot, fromConstructor: Boolean) : Runnable, Closeable { - val remoteState = slot.synchable.createRemoteState(this) + val remoteState = synchable(slot.value).createRemoteState(this) val isDirty = AtomicBoolean(true) var isRemoved = false private set @@ -114,7 +124,7 @@ class DynamicSynchableGroup( firstTime.forEach { stream.writeByte(ADD_ENTRY) stream.writeVarInt(it.slot.id) - writer(it.slot.synchable, stream) + writer(it.slot.value, stream) } firstTime.clear() @@ -177,7 +187,7 @@ class DynamicSynchableGroup( } } - private data class Slot(val synchable: T, val id: Int) + private data class Slot(val value: T, val id: Int) private val remoteStates = ArrayList() private val value2slot = HashMap>() @@ -199,7 +209,7 @@ class DynamicSynchableGroup( REMOVE_ENTRY -> { val id = stream.readVarInt() val slot = checkNotNull(id2slot.remove(id)) { "No such slot with ID: $id" } - check(value2slot.remove(slot.synchable) == value2slot) + check(value2slot.remove(slot.value) == value2slot) remoteStates.forEach { it.remove(slot) } } @@ -212,7 +222,7 @@ class DynamicSynchableGroup( if (id2slot.containsKey(id)) { val slot = id2slot.remove(id)!! - check(value2slot.remove(slot.synchable) == value2slot) + check(value2slot.remove(slot.value) == value2slot) remoteStates.forEach { it.remove(slot) } } @@ -225,7 +235,7 @@ class DynamicSynchableGroup( SYNC_ENTRY -> { val id = stream.readVarInt() val slot = checkNotNull(id2slot.get(id)) { "No such slot with ID: $id" } - slot.synchable.read(stream) + synchable(slot.value).read(stream) } CLEAR -> { @@ -299,7 +309,7 @@ class DynamicSynchableGroup( override fun next(): T { val slot = parent.next() last = KOptional(slot) - return slot.synchable + return slot.value } override fun remove() {