From 232701b8bbbb0cba6b2b16322162ca8fbe3895fc Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 29 Jul 2023 17:28:08 +0700 Subject: [PATCH] Fix ComputedField being unable to properly handle nullable types --- .../network/synchronizer/FieldSynchronizer.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/network/synchronizer/FieldSynchronizer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/network/synchronizer/FieldSynchronizer.kt index 0c5a96766..4df86bf24 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/network/synchronizer/FieldSynchronizer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/network/synchronizer/FieldSynchronizer.kt @@ -1012,8 +1012,8 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa private val codec: IStreamCodec, private val observer: (new: V) -> Unit = {} ) : AbstractField(), IField { - private var remote: V? = null - private var clientValue: V? = null + private var remote: Any? = Mark + private var clientValue: Any? = Mark init { observers.add(this) @@ -1024,7 +1024,7 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa val value = value - if (!isDirty && (remote == null || !codec.compare(remote ?: throw ConcurrentModificationException(), value))) { + if (!isDirty && (remote === Mark || !codec.compare(remote as V, value))) { notifyEndpoints(this) isDirty = true remote = codec.copy(value) @@ -1034,7 +1034,15 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa } override val value: V - get() = clientValue ?: getter.invoke() + get() { + val clientValue = clientValue + + if (clientValue === Mark) { + return getter.invoke() + } else { + return clientValue as V + } + } override fun write(stream: DataOutputStream, endpoint: Endpoint) { check(!isRemoved) { "Field was removed" }