From 85dde24e59e98e3b197a3b94c16c3375df98f866 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 26 Mar 2025 19:06:39 +0700 Subject: [PATCH] Update energy counter menu code --- .../mc/otm/menu/tech/EnergyCounterMenu.kt | 25 ++++--------------- .../mc/otm/network/syncher/SynchableGroup.kt | 4 +++ 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt index 5849e82e4..e95e7e6e2 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt @@ -17,15 +17,16 @@ import ru.dbotthepony.mc.otm.menu.input.EnumInputWithFeedback import ru.dbotthepony.mc.otm.menu.input.IntInputWithFeedback import ru.dbotthepony.mc.otm.registry.game.MMenus import java.math.BigDecimal +import java.util.function.Supplier class EnergyCounterMenu( p_38852_: Int, inventory: Inventory, tile: EnergyCounterBlockEntity? = null ) : MatteryMenu(MMenus.ENERGY_COUNTER, p_38852_, inventory, tile) { - var passed by mSynchronizer.decimal() - var lastTick by mSynchronizer.decimal() - var maxIO by mSynchronizer.decimal() + var passed by mSynchronizer.computedDecimal { tile?.passed ?: Decimal.ZERO } + var lastTick by mSynchronizer.computedDecimal { tile?.lastTick ?: Decimal.ZERO } + var maxIO by mSynchronizer.computedDecimal { tile?.ioLimit ?: Decimal.ZERO } val history5s = tile?.history5s ?: DecimalHistoryChart(1, 100) val history15s = tile?.history15s ?: DecimalHistoryChart(3, 100) @@ -62,7 +63,7 @@ class EnergyCounterMenu( } } - var inputDirection: Direction by mSynchronizer.enum(Direction.UP) + val inputDirection: Direction by mSynchronizer.computedEnum { tile?.blockState?.getValue(EnergyCounterBlock.INPUT_DIRECTION) ?: Direction.UP} val maxIOInput = decimalInput { if (tile is EnergyCounterBlockEntity) { @@ -73,20 +74,4 @@ class EnergyCounterMenu( } } } - - override fun beforeBroadcast() { - super.beforeBroadcast() - - if (tile is EnergyCounterBlockEntity) { - passed = tile.passed - lastTick = tile.lastTick - inputDirection = tile.blockState.getValue(EnergyCounterBlock.INPUT_DIRECTION) - - maxIO = tile.ioLimit?.toDecimal() ?: -Decimal.ONE - } - } - - companion object { - private val MINUS_ONE = -BigDecimal.ONE - } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/SynchableGroup.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/SynchableGroup.kt index 88813c500..9e7f0749c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/SynchableGroup.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/SynchableGroup.kt @@ -270,6 +270,10 @@ class SynchableGroup : Observer, ISynchable, Iterable { return add(ListenableDelegate.maskSmart(value, getter, setter), MatteryStreamCodec.Enum(value::class.java)) } + inline fun > computedEnum(value: Supplier): SynchableDelegate { + return computed(value, MatteryStreamCodec.Enum(E::class.java)) + } + fun decimal(value: Decimal = Decimal.ZERO, setter: DelegateSetter = DelegateSetter.passthrough(), getter: DelegateGetter = DelegateGetter.passthrough()): SynchableDelegate { return add(ListenableDelegate.maskSmart(value, getter, setter), StreamCodecs.DECIMAL) }