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
|
p_155230_: BlockState
|
||||||
) : SynchronizedBlockEntity(p_155228_, p_155229_, p_155230_), MenuProvider, IRedstoneControlProvider {
|
) : SynchronizedBlockEntity(p_155228_, p_155229_, p_155230_), MenuProvider, IRedstoneControlProvider {
|
||||||
var customDisplayName: Component? = null
|
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
|
protected open val defaultDisplayName: Component
|
||||||
get() = level?.getBlockState(blockPos)?.block?.name ?: TextComponent("null at $blockPos")
|
get() = level?.getBlockState(blockPos)?.block?.name ?: TextComponent("null at $blockPos")
|
||||||
|
@ -43,24 +43,21 @@ abstract class AbstractRedstoneControl : INBTSerializable<CompoundTag> {
|
|||||||
class RedstoneControl(private val valueChanges: (new: Boolean, old: Boolean) -> Unit) : AbstractRedstoneControl() {
|
class RedstoneControl(private val valueChanges: (new: Boolean, old: Boolean) -> Unit) : AbstractRedstoneControl() {
|
||||||
override var redstoneSetting: RedstoneSetting = RedstoneSetting.LOW
|
override var redstoneSetting: RedstoneSetting = RedstoneSetting.LOW
|
||||||
set(level) {
|
set(level) {
|
||||||
|
if (level == field) return
|
||||||
val old = isBlockedByRedstone
|
val old = isBlockedByRedstone
|
||||||
field = level
|
field = level
|
||||||
val state = isBlockedByRedstone
|
val state = isBlockedByRedstone
|
||||||
|
|
||||||
if (old != state) {
|
|
||||||
valueChanges.invoke(state, old)
|
valueChanges.invoke(state, old)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
override var redstoneSignal: Int = 0
|
override var redstoneSignal: Int = 0
|
||||||
set(setting) {
|
set(setting) {
|
||||||
|
if (setting == field) return
|
||||||
val old = isBlockedByRedstone
|
val old = isBlockedByRedstone
|
||||||
field = setting
|
field = setting
|
||||||
val state = isBlockedByRedstone
|
val state = isBlockedByRedstone
|
||||||
if (old != state) {
|
|
||||||
valueChanges.invoke(state, old)
|
valueChanges.invoke(state, old)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class SynchronizedRedstoneControl(
|
class SynchronizedRedstoneControl(
|
||||||
@ -71,30 +68,26 @@ class SynchronizedRedstoneControl(
|
|||||||
constructor(synchronizer: FieldSynchronizer, valueChanges: (new: Boolean, old: Boolean) -> Unit) : this(synchronizer, "", valueChanges)
|
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 ->
|
override var redstoneSetting: RedstoneSetting by synchronizer.enum(RedstoneSetting.LOW, setter = { value, access, setByRemote ->
|
||||||
|
if (access.read() == value) return@enum
|
||||||
if (setByRemote) {
|
if (setByRemote) {
|
||||||
access.write(value)
|
access.write(value)
|
||||||
} else {
|
} else {
|
||||||
val old = isBlockedByRedstone
|
val old = isBlockedByRedstone
|
||||||
access.write(value)
|
access.write(value)
|
||||||
val state = isBlockedByRedstone
|
val state = isBlockedByRedstone
|
||||||
|
|
||||||
if (old != state) {
|
|
||||||
valueChanges.invoke(state, old)
|
valueChanges.invoke(state, old)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}, name = if (fieldNamePrefix != null) "${fieldNamePrefix}_redstoneSetting" else null, writeByIndices = true)
|
}, name = if (fieldNamePrefix != null) "${fieldNamePrefix}_redstoneSetting" else null, writeByIndices = true)
|
||||||
|
|
||||||
override var redstoneSignal: Int by synchronizer.int(0, setter = { value, access, setByRemote ->
|
override var redstoneSignal: Int by synchronizer.int(0, setter = { value, access, setByRemote ->
|
||||||
|
if (access.read() == value) return@int
|
||||||
if (setByRemote) {
|
if (setByRemote) {
|
||||||
access.write(value)
|
access.write(value)
|
||||||
} else {
|
} else {
|
||||||
val old = isBlockedByRedstone
|
val old = isBlockedByRedstone
|
||||||
access.write(value)
|
access.write(value)
|
||||||
val state = isBlockedByRedstone
|
val state = isBlockedByRedstone
|
||||||
|
|
||||||
if (old != state) {
|
|
||||||
valueChanges.invoke(state, old)
|
valueChanges.invoke(state, old)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}, name = if (fieldNamePrefix != null) "${fieldNamePrefix}_redstoneSignal" else null)
|
}, 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
|
import ru.dbotthepony.mc.otm.registry.MBlocks
|
||||||
|
|
||||||
class HoloSignBlockEntity(blockPos: BlockPos, blockState: BlockState) : SynchronizedBlockEntity(MBlockEntities.HOLO_SIGN, blockPos, blockState), MenuProvider, IRedstoneControlProvider {
|
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 ->
|
var text by synchronizer.string("", name = "text", setter = { value, access, remote ->
|
||||||
setChanged()
|
setChanged()
|
||||||
|
Loading…
Reference in New Issue
Block a user