Always call a callback when one of fields change
This commit is contained in:
parent
4c7f68e66d
commit
e6018bd9d7
@ -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")
|
||||
|
@ -43,23 +43,20 @@ 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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user