From e6018bd9d7ebfdcdd0e94cb38ed6885282f872e8 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 29 Jan 2023 22:38:55 +0700 Subject: [PATCH] Always call a callback when one of fields change --- .../mc/otm/block/entity/MatteryBlockEntity.kt | 7 +++++- .../mc/otm/block/entity/RedstoneControl.kt | 23 +++++++------------ .../entity/decorative/HoloSignBlockEntity.kt | 2 +- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt index 8b187d490..31fe08a0a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/MatteryBlockEntity.kt @@ -27,7 +27,12 @@ abstract class MatteryBlockEntity( p_155230_: BlockState ) : SynchronizedBlockEntity(p_155228_, p_155229_, p_155230_), MenuProvider, IRedstoneControlProvider { var customDisplayName: Component? = null - override val redstoneControl: AbstractRedstoneControl = RedstoneControl(::redstoneStatusUpdated) + override val redstoneControl: AbstractRedstoneControl = RedstoneControl { new, old -> + if (new != old) + redstoneStatusUpdated(new, old) + else + setChangedLight() + } protected open val defaultDisplayName: Component get() = level?.getBlockState(blockPos)?.block?.name ?: TextComponent("null at $blockPos") diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/RedstoneControl.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/RedstoneControl.kt index 68a45e1df..5e112c7ae 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/RedstoneControl.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/RedstoneControl.kt @@ -43,23 +43,20 @@ abstract class AbstractRedstoneControl : INBTSerializable { class RedstoneControl(private val valueChanges: (new: Boolean, old: Boolean) -> Unit) : AbstractRedstoneControl() { override var redstoneSetting: RedstoneSetting = RedstoneSetting.LOW set(level) { + if (level == field) return val old = isBlockedByRedstone field = level val state = isBlockedByRedstone - - if (old != state) { - valueChanges.invoke(state, old) - } + valueChanges.invoke(state, old) } override var redstoneSignal: Int = 0 set(setting) { + if (setting == field) return val old = isBlockedByRedstone field = setting val state = isBlockedByRedstone - if (old != state) { - valueChanges.invoke(state, old) - } + valueChanges.invoke(state, old) } } @@ -71,30 +68,26 @@ class SynchronizedRedstoneControl( constructor(synchronizer: FieldSynchronizer, valueChanges: (new: Boolean, old: Boolean) -> Unit) : this(synchronizer, "", valueChanges) override var redstoneSetting: RedstoneSetting by synchronizer.enum(RedstoneSetting.LOW, setter = { value, access, setByRemote -> + if (access.read() == value) return@enum if (setByRemote) { access.write(value) } else { val old = isBlockedByRedstone access.write(value) val state = isBlockedByRedstone - - if (old != state) { - valueChanges.invoke(state, old) - } + valueChanges.invoke(state, old) } }, name = if (fieldNamePrefix != null) "${fieldNamePrefix}_redstoneSetting" else null, writeByIndices = true) override var redstoneSignal: Int by synchronizer.int(0, setter = { value, access, setByRemote -> + if (access.read() == value) return@int if (setByRemote) { access.write(value) } else { val old = isBlockedByRedstone access.write(value) val state = isBlockedByRedstone - - if (old != state) { - valueChanges.invoke(state, old) - } + valueChanges.invoke(state, old) } }, name = if (fieldNamePrefix != null) "${fieldNamePrefix}_redstoneSignal" else null) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/HoloSignBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/HoloSignBlockEntity.kt index f5b3abfbc..9429d3251 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/HoloSignBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/decorative/HoloSignBlockEntity.kt @@ -17,7 +17,7 @@ import ru.dbotthepony.mc.otm.registry.MBlockEntities import ru.dbotthepony.mc.otm.registry.MBlocks class HoloSignBlockEntity(blockPos: BlockPos, blockState: BlockState) : SynchronizedBlockEntity(MBlockEntities.HOLO_SIGN, blockPos, blockState), MenuProvider, IRedstoneControlProvider { - override val redstoneControl = SynchronizedRedstoneControl(synchronizer) { _, _ -> } + override val redstoneControl = SynchronizedRedstoneControl(synchronizer) { _, _ -> setChanged() } var text by synchronizer.string("", name = "text", setter = { value, access, remote -> setChanged()