Always call a callback when one of fields change

This commit is contained in:
DBotThePony 2023-01-29 22:38:55 +07:00
parent 4c7f68e66d
commit e6018bd9d7
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 15 additions and 17 deletions

View File

@ -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")

View File

@ -43,24 +43,21 @@ abstract class AbstractRedstoneControl : INBTSerializable<CompoundTag> {
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)
}
}
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)
}
}
}
class SynchronizedRedstoneControl(
@ -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)
}
}
}, 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)
}
}
}, name = if (fieldNamePrefix != null) "${fieldNamePrefix}_redstoneSignal" else null)
}

View File

@ -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()