From 6cc5659c4335a5d1adafafe63c2c3ddbaf1a9de4 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 25 Mar 2023 11:33:27 +0700 Subject: [PATCH] Make menu widgets be just containers for synchronized fields --- .../widget/HorizontalPowerGaugePanel.kt | 8 +-- .../client/screen/widget/MatterGaugePanel.kt | 14 ++--- .../client/screen/widget/PatternGaugePanel.kt | 6 +- .../client/screen/widget/PowerGaugePanel.kt | 6 +- .../screen/widget/ProgressGaugePanel.kt | 14 ++--- .../ru/dbotthepony/mc/otm/menu/MatteryMenu.kt | 31 ---------- .../mc/otm/menu/MatteryPoweredMenu.kt | 2 +- .../mc/otm/menu/matter/MatterBottlerMenu.kt | 20 +++---- .../menu/matter/MatterCapacitorBankMenu.kt | 13 ++-- .../otm/menu/matter/MatterDecomposerMenu.kt | 12 ++-- .../menu/matter/MatterReconstructorMenu.kt | 8 +-- .../mc/otm/menu/matter/MatterRecyclerMenu.kt | 23 +++----- .../otm/menu/matter/MatterReplicatorMenu.kt | 10 +--- .../mc/otm/menu/matter/MatterScannerMenu.kt | 11 ++-- .../mc/otm/menu/matter/PatternStorageMenu.kt | 13 ++-- .../mc/otm/menu/tech/ChemicalGeneratorMenu.kt | 9 ++- .../mc/otm/menu/tech/CobblerMenu.kt | 10 ++-- .../mc/otm/menu/tech/PlatePressMenu.kt | 3 +- .../mc/otm/menu/widget/AbstractWidget.kt | 10 ---- .../mc/otm/menu/widget/LevelGaugeWidget.kt | 59 +++++++++++-------- .../mc/otm/menu/widget/ProgressGaugeWidget.kt | 39 +++++++----- 21 files changed, 136 insertions(+), 185 deletions(-) delete mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/AbstractWidget.kt diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/HorizontalPowerGaugePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/HorizontalPowerGaugePanel.kt index 2cf60554e..e04e54281 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/HorizontalPowerGaugePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/HorizontalPowerGaugePanel.kt @@ -22,21 +22,21 @@ open class HorizontalPowerGaugePanel( if (height >= 18f) { if (flop) { GAUGE_BACKGROUND_TALL.render(stack, height = height, width = this.width, winding = UVWindingOrder.U1_V0_U0_V1) - val width = this.width * widget.percentage() + val width = this.width * widget.percentage GAUGE_FOREGROUND_TALL.renderPartial(stack, x = this.width - width, height = height, width = width, winding = UVWindingOrder.U1_V0_U0_V1) } else { GAUGE_BACKGROUND_TALL.render(stack, height = height, width = this.width) - val width = this.width * widget.percentage() + val width = this.width * widget.percentage GAUGE_FOREGROUND_TALL.renderPartial(stack, height = height, width = width) } } else { if (flop) { GAUGE_BACKGROUND.render(stack, height = height, width = this.width, winding = UVWindingOrder.U1_V0_U0_V1) - val width = this.width * widget.percentage() + val width = this.width * widget.percentage GAUGE_FOREGROUND.renderPartial(stack, x = this.width - width, height = height, width = width, winding = UVWindingOrder.U1_V0_U0_V1) } else { GAUGE_BACKGROUND.render(stack, height = height, width = this.width) - val width = this.width * widget.percentage() + val width = this.width * widget.percentage GAUGE_FOREGROUND.renderPartial(stack, height = height, width = width) } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/MatterGaugePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/MatterGaugePanel.kt index 2e8eed2b5..64072038c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/MatterGaugePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/MatterGaugePanel.kt @@ -43,9 +43,9 @@ open class MatterGaugePanel @JvmOverloads constructor( return mutableListOf( TranslatableComponent( "otm.gui.matter.percentage_level", - String.format("%.2f", widget.percentage() * 100.0) + String.format("%.2f", widget.percentage * 100.0) ), - formatMatterLevel(widget.level(), widget.maxLevel(), formatAsReadable = ShiftPressedCond) + formatMatterLevel(widget.level, widget.maxLevel, formatAsReadable = ShiftPressedCond) ) } @@ -54,22 +54,22 @@ open class MatterGaugePanel @JvmOverloads constructor( lastAbsoluteX = absoluteX lastAbsoluteY = absoluteY lastDraw = nanoTime - lastLevel = widget.percentage() + lastLevel = widget.percentage } else { - val diff = ((lastAbsoluteX - absoluteX).pow(2f) + (lastAbsoluteY - absoluteY).pow(2f)).pow(0.5f) / 10f + (lastLevel - widget.percentage()).absoluteValue * 1.5f + val diff = ((lastAbsoluteX - absoluteX).pow(2f) + (lastAbsoluteY - absoluteY).pow(2f)).pow(0.5f) / 10f + (lastLevel - widget.percentage).absoluteValue * 1.5f lastAbsoluteX = absoluteX lastAbsoluteY = absoluteY wavesStrength = (wavesStrength + diff - (nanoTime - lastDraw) / 400_000_000f).coerceAtLeast(0.5f).coerceAtMost(16f) lastDraw = nanoTime - lastLevel = widget.percentage() + lastLevel = widget.percentage } GAUGE_BACKGROUND.render(stack) - val height = this.height * (1f - widget.percentage()) + val height = this.height * (1f - widget.percentage) - if (widget.percentage() > 0.01f) { + if (widget.percentage > 0.01f) { RenderSystem.setShader(GameRenderer::getPositionTexShader) RenderSystem.enableTexture() RenderSystem.enableBlend() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PatternGaugePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PatternGaugePanel.kt index 69ceb56f2..1c7f8d339 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PatternGaugePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PatternGaugePanel.kt @@ -23,15 +23,15 @@ open class PatternGaugePanel @JvmOverloads constructor( return mutableListOf( TranslatableComponent( "otm.gui.pattern.percentage_level", - String.format("%.2f", widget.percentage() * 100.0) + String.format("%.2f", widget.percentage * 100.0) ), - TranslatableComponent("otm.gui.pattern.format", widget.level().toString(0), widget.maxLevel().toString(0)) + TranslatableComponent("otm.gui.pattern.format", widget.level.toString(0), widget.maxLevel.toString(0)) ) } override fun innerRender(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float) { GAUGE_BACKGROUND.render(stack) - val height = this.height * widget.percentage() + val height = this.height * widget.percentage GAUGE_FOREGROUND.renderPartial(stack, y = this.height - height, height = height) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PowerGaugePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PowerGaugePanel.kt index 1f248a14c..1f955c3d7 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PowerGaugePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/PowerGaugePanel.kt @@ -25,13 +25,13 @@ open class PowerGaugePanel @JvmOverloads constructor( protected open fun makeTooltip(): MutableList { return mutableListOf( - TranslatableComponent("otm.gui.power.percentage_level", String.format("%.2f", widget.percentage() * 100.0)), - formatPowerLevel(widget.level(), widget.maxLevel(), formatAsReadable = ShiftPressedCond) + TranslatableComponent("otm.gui.power.percentage_level", String.format("%.2f", widget.percentage * 100.0)), + formatPowerLevel(widget.level, widget.maxLevel, formatAsReadable = ShiftPressedCond) ) } override fun innerRender(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float) { - val height = this.height * widget.percentage() + val height = this.height * widget.percentage if (width >= 18f) { GAUGE_BACKGROUND_WIDE.render(stack, width = width, height = this.height) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/ProgressGaugePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/ProgressGaugePanel.kt index 530506aa3..31ffc38ed 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/ProgressGaugePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/widget/ProgressGaugePanel.kt @@ -28,11 +28,11 @@ open class ProgressGaugePanel @JvmOverloads constructor( protected open fun makeTooltip(): MutableList { val tooltip: MutableList - if (widget.isStuck()) { + if (widget.isStuck) { tooltip = mutableListOf( TranslatableComponent( "otm.gui.progress_widget", - String.format("%.2f", widget.percentage() * 100f) + String.format("%.2f", widget.percentage * 100f) ), TranslatableComponent("otm.gui.progress_widget_stuck").withStyle(ChatFormatting.DARK_RED) ) @@ -40,7 +40,7 @@ open class ProgressGaugePanel @JvmOverloads constructor( tooltip = mutableListOf( TranslatableComponent( "otm.gui.progress_widget", - String.format("%.2f", widget.percentage() * 100f) + String.format("%.2f", widget.percentage * 100f) ) ) } @@ -49,21 +49,21 @@ open class ProgressGaugePanel @JvmOverloads constructor( } override fun innerRender(stack: PoseStack, mouseX: Float, mouseY: Float, partialTick: Float) { - if (widget.isStuck() && tickCount % 40 <= 20) { + if (widget.isStuck && tickCount % 40 <= 20) { RenderSystem.setShaderColor(0.75f, 0.4f, 0.4f, 1f) } if (flop) { GAUGE_BACKGROUND.render(stack, height = height, width = this.width, winding = UVWindingOrder.U1_V0_U0_V1) - val width = (this.width * widget.percentage()).roundToInt().toFloat() + val width = (this.width * widget.percentage).roundToInt().toFloat() GAUGE_FOREGROUND.renderPartial(stack, x = this.width - width, height = height, width = width, winding = UVWindingOrder.U1_V0_U0_V1) } else { GAUGE_BACKGROUND.render(stack, height = height, width = this.width) - val width = (this.width * widget.percentage()).roundToInt().toFloat() + val width = (this.width * widget.percentage).roundToInt().toFloat() GAUGE_FOREGROUND.renderPartial(stack, height = height, width = width) } - if (widget.isStuck() && tickCount % 40 <= 20) { + if (widget.isStuck && tickCount % 40 <= 20) { RenderSystem.setShaderColor(1f, 1f, 1f, 1f) } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt index 4032c8289..d2cb0f0a3 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt @@ -36,7 +36,6 @@ import ru.dbotthepony.mc.otm.core.util.IStreamCodec import ru.dbotthepony.mc.otm.core.util.ItemValueCodec import ru.dbotthepony.mc.otm.core.util.NullValueCodec import ru.dbotthepony.mc.otm.core.util.VarIntValueCodec -import ru.dbotthepony.mc.otm.menu.widget.AbstractWidget import ru.dbotthepony.mc.otm.network.synchronizer.FieldSynchronizer import ru.dbotthepony.mc.otm.network.synchronizer.IField import ru.dbotthepony.mc.otm.network.MatteryPacket @@ -74,9 +73,6 @@ abstract class MatteryMenu @JvmOverloads protected constructor( val mSynchronizer = FieldSynchronizer() val ply: Player get() = inventory.player - private val _matteryWidgets = ArrayList() - val matteryWidgets: List = Collections.unmodifiableList(_matteryWidgets) - private val _playerInventorySlots = ArrayList() private val _playerInventorySlots2 = ArrayList() private val _playerHotbarSlots = ArrayList() @@ -223,25 +219,6 @@ abstract class MatteryMenu @JvmOverloads protected constructor( return result } - fun addWidget(widget: AbstractWidget): Int { - val indexOf = _matteryWidgets.indexOf(widget) - - if (indexOf == -1) { - _matteryWidgets.add(widget) - return _matteryWidgets.size - 1 - } - - return indexOf - } - - fun getWidget(index: Int): AbstractWidget? { - if (index !in _matteryWidgets.indices) { - return null - } - - return _matteryWidgets[index] - } - open inner class InventorySlot(container: Container, index: Int, x: Int = 0, y: Int = 0) : UserFilteredSlot(container, index, x, y) { override fun mayPlace(itemStack: ItemStack): Boolean { return !isInventorySlotLocked(index) && super.mayPlace(itemStack) @@ -351,10 +328,6 @@ abstract class MatteryMenu @JvmOverloads protected constructor( } override fun broadcastChanges() { - for (widget in _matteryWidgets) { - widget.updateServer() - } - val payload = mSynchronizer.collectNetworkPayload() if (payload != null) { @@ -375,10 +348,6 @@ abstract class MatteryMenu @JvmOverloads protected constructor( } override fun broadcastFullState() { - for (widget in _matteryWidgets) { - widget.updateServer() - } - mSynchronizer.invalidate() val payload = mSynchronizer.collectNetworkPayload() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryPoweredMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryPoweredMenu.kt index 844cd97be..95f99d198 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryPoweredMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryPoweredMenu.kt @@ -18,7 +18,7 @@ abstract class MatteryPoweredMenu protected constructor( ) : MatteryMenu(menuType, containerID, inventory, tile) { val powerWidget = LevelGaugeWidget(this, tile?.matteryEnergy) val batterySlot = BatterySlot(tile?.batteryContainer ?: SimpleContainer(1), 0) - val redstone = EnumInputWithFeedback(this, RedstoneSetting::class.java) + val redstone = EnumInputWithFeedback(this) init { if (tile != null) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterBottlerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterBottlerMenu.kt index ec591c7c7..dd089b9ed 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterBottlerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterBottlerMenu.kt @@ -23,24 +23,18 @@ class MatterBottlerMenu @JvmOverloads constructor( ) : MatteryPoweredMenu( MMenus.MATTER_BOTTLER, p_38852_, inventory, tile ) { - val workFlow: BooleanInputWithFeedback - - val progressWidget: ProgressGaugeWidget - val matterWidget: LevelGaugeWidget - + val workFlow = BooleanInputWithFeedback(this) + val progressWidget = ProgressGaugeWidget(this) + val matterWidget = LevelGaugeWidget(this) val storageSlots: List init { val container = tile?.container ?: SimpleContainer(6) - if (tile == null) { - progressWidget = ProgressGaugeWidget(this) - matterWidget = LevelGaugeWidget(this) - workFlow = BooleanInputWithFeedback(this) - } else { - progressWidget = ProgressGaugeWidget(this) { tile.getWorkProgress() } - matterWidget = LevelGaugeWidget(this, tile.matter) - workFlow = BooleanInputWithFeedback(this, tile::isBottling) + if (tile != null) { + progressWidget.with(tile::getWorkProgress) + matterWidget.with(tile.matter) + workFlow.with(tile::isBottling) } storageSlots = immutableList(6) { index -> diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterCapacitorBankMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterCapacitorBankMenu.kt index 6582c186a..c8ca49197 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterCapacitorBankMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterCapacitorBankMenu.kt @@ -17,18 +17,15 @@ class MatterCapacitorBankMenu @JvmOverloads constructor( ) : MatteryMenu( MMenus.MATTER_CAPACITOR_BANK, p_38852_, inventory, tile ) { - val matterGauge: LevelGaugeWidget - val totalMatterGauge: LevelGaugeWidget + val matterGauge = LevelGaugeWidget(this) + val totalMatterGauge = LevelGaugeWidget(this) val storageSlots: List init { - if (tile == null) { - matterGauge = LevelGaugeWidget(this) - totalMatterGauge = LevelGaugeWidget(this) - } else { - matterGauge = LevelGaugeWidget(this, tile) - totalMatterGauge = LevelGaugeWidget(this, { + if (tile != null) { + matterGauge.with(tile) + totalMatterGauge.with({ tile.matterNode.graph.getMatterStorageLevel() }, { tile.matterNode.graph.getMatterStorageMaxLevel() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterDecomposerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterDecomposerMenu.kt index cbe45dfdc..7050d6813 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterDecomposerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterDecomposerMenu.kt @@ -25,8 +25,8 @@ class MatterDecomposerMenu @JvmOverloads constructor( val input: MatterySlot val outputMain: MachineOutputSlot val outputStacking: MachineOutputSlot - val progressWidget: ProgressGaugeWidget - val matterWidget: LevelGaugeWidget + val progressWidget = ProgressGaugeWidget(this) + val matterWidget = LevelGaugeWidget(this, tile?.getCapability(MatteryCapability.MATTER)?.orNull()) init { val container = tile?.container ?: SimpleContainer(3) @@ -40,12 +40,8 @@ class MatterDecomposerMenu @JvmOverloads constructor( outputMain = MachineOutputSlot(container, 1) outputStacking = MachineOutputSlot(container, 2) - matterWidget = LevelGaugeWidget(this, tile?.getCapability(MatteryCapability.MATTER)?.orNull()) - - if (tile == null) { - progressWidget = ProgressGaugeWidget(this) - } else { - progressWidget = ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess) + if (tile != null) { + progressWidget.with(tile::workProgress, tile::isUnableToProcess) } addStorageSlot(outputMain) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterReconstructorMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterReconstructorMenu.kt index 9a296b8cc..c57423d91 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterReconstructorMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterReconstructorMenu.kt @@ -27,7 +27,7 @@ class MatterReconstructorMenu( } val equipment = makeEquipmentSlots(mapMoveToExternal = true) - val progress: ProgressGaugeWidget + val progress = ProgressGaugeWidget(this) val redstoneControl = EnumInputWithFeedback(this) val energyConfig = EnergyPlayerInput(this) @@ -43,10 +43,8 @@ class MatterReconstructorMenu( energyConfig.configure(tile.energyConfig) } - if (tile == null) { - progress = ProgressGaugeWidget(this) - } else { - progress = ProgressGaugeWidget(this, tile::visualProgress, tile::isUnableToProcess) + if (tile != null) { + progress.with(tile::visualProgress, tile::isUnableToProcess) } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterRecyclerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterRecyclerMenu.kt index b025ddf03..55d216cce 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterRecyclerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterRecyclerMenu.kt @@ -16,23 +16,18 @@ class MatterRecyclerMenu @JvmOverloads constructor( inventory: Inventory, tile: MatterRecyclerBlockEntity? = null ) : MatteryPoweredMenu(MMenus.MATTER_RECYCLER, containerID, inventory, tile) { - val input: MatterySlot - val progress: ProgressGaugeWidget + val input = object : MatterySlot(tile?.container ?: SimpleContainer(1), 0) { + override fun mayPlace(itemStack: ItemStack): Boolean { + return itemStack.item is MatterDustItem && (itemStack.item as MatterDustItem).getMatterValue(itemStack) != null + } + } + + val progress = ProgressGaugeWidget(this) val matter = LevelGaugeWidget(this, tile?.matter) init { - val container = tile?.container ?: SimpleContainer(1) - - input = object : MatterySlot(container, 0) { - override fun mayPlace(itemStack: ItemStack): Boolean { - return itemStack.item is MatterDustItem && (itemStack.item as MatterDustItem).getMatterValue(itemStack) != null - } - } - - if (tile == null) { - progress = ProgressGaugeWidget(this) - } else { - progress = ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess) + if (tile != null) { + progress.with(tile::workProgress, tile::isUnableToProcess) } addStorageSlot(input) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterReplicatorMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterReplicatorMenu.kt index 71350a85a..f44597064 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterReplicatorMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterReplicatorMenu.kt @@ -18,8 +18,8 @@ class MatterReplicatorMenu @JvmOverloads constructor( ) : MatteryPoweredMenu( MMenus.MATTER_REPLICATOR, p_38852_, inventory, tile ) { - val matter: LevelGaugeWidget - val progress: ProgressGaugeWidget + val matter = LevelGaugeWidget(this, tile?.matter) + val progress = ProgressGaugeWidget(this) val storageSlots: List init { @@ -30,11 +30,7 @@ class MatterReplicatorMenu @JvmOverloads constructor( } if (tile != null) { - matter = LevelGaugeWidget(this, tile.matter) - progress = ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess) - } else { - matter = LevelGaugeWidget(this) - progress = ProgressGaugeWidget(this) + progress.with(tile::workProgress, tile::isUnableToProcess) } addInventorySlots() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterScannerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterScannerMenu.kt index e218d2630..c8dbf3f0d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterScannerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/MatterScannerMenu.kt @@ -21,8 +21,8 @@ class MatterScannerMenu @JvmOverloads constructor( MMenus.MATTER_SCANNER, p_38852_, inventory, tile ) { val input: MatterySlot - val progress: ProgressGaugeWidget - val patterns: LevelGaugeWidget + val progress = ProgressGaugeWidget(this) + val patterns = LevelGaugeWidget(this) init { val container = tile?.container ?: SimpleContainer(1) @@ -34,13 +34,10 @@ class MatterScannerMenu @JvmOverloads constructor( addStorageSlot(input) if (tile != null) { - progress = ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess) - patterns = LevelGaugeWidget(this, + progress.with(tile::workProgress, tile::isUnableToProcess) + patterns.with( { Decimal(tile.matterNode.graph.patternCount) }, { Decimal(tile.matterNode.graph.patternCapacity) }) - } else { - progress = ProgressGaugeWidget(this) - patterns = LevelGaugeWidget(this) } addInventorySlots() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/PatternStorageMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/PatternStorageMenu.kt index a5163cd6a..6de88f8b0 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/PatternStorageMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/matter/PatternStorageMenu.kt @@ -17,18 +17,15 @@ class PatternStorageMenu @JvmOverloads constructor( ) : MatteryMenu( MMenus.PATTERN_STORAGE, p_38852_, inventory, tile ) { - val storedThis: LevelGaugeWidget - val storedGrid: LevelGaugeWidget + val storedThis = LevelGaugeWidget(this) + val storedGrid = LevelGaugeWidget(this) val storageSlots: List init { - if (tile == null) { - storedThis = LevelGaugeWidget(this) - storedGrid = LevelGaugeWidget(this) - } else { - storedThis = LevelGaugeWidget(this, tile) - storedGrid = LevelGaugeWidget(this, { + if (tile != null) { + storedThis.with(tile) + storedGrid.with({ Decimal(tile.matterNode.graph.patternCount) }, { Decimal(tile.matterNode.graph.patternCapacity) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/ChemicalGeneratorMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/ChemicalGeneratorMenu.kt index d164355ab..8cf626104 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/ChemicalGeneratorMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/ChemicalGeneratorMenu.kt @@ -54,14 +54,18 @@ class ChemicalGeneratorMenu @JvmOverloads constructor(id: Int, inv: Inventory, t } } - val progress = ProgressGaugeWidget(this) { 1f - tile!!.workTicks.toFloat() / tile.workTicksTotal } + val progress = ProgressGaugeWidget(this) val energy = LevelGaugeWidget(this, tile?.energy) - var burnTime by mSynchronizer.int() + var burnTime by mSynchronizer.int().property init { addStorageSlot(fuelSlot) addStorageSlot(batterySlot) addStorageSlot(residueSlot) + + if (tile != null) { + progress.with { 1f - tile.workTicks.toFloat() / tile.workTicksTotal } + } } init { @@ -70,7 +74,6 @@ class ChemicalGeneratorMenu @JvmOverloads constructor(id: Int, inv: Inventory, t override fun broadcastChanges() { super.broadcastChanges() - progress.updateServer() burnTime = (tile as ChemicalGeneratorBlockEntity).workTicks } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/CobblerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/CobblerMenu.kt index 0602bbf95..6dda03a43 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/CobblerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/CobblerMenu.kt @@ -19,15 +19,13 @@ class CobblerMenu @JvmOverloads constructor( ) : MatteryMenu(MMenus.COBBLESTONE_GENERATOR, p_38852_, inventory, tile) { val storageSlots = (tile?.container ?: SimpleContainer(CobblerBlockEntity.CONTAINER_SIZE)).let { c -> immutableList(c.containerSize) { addStorageSlot(MachineOutputSlot(c, it)) } } val redstone = EnumInputWithFeedback(this) - val itemConfig = ItemHandlerPlayerInput(this, false, false) + val itemConfig = ItemHandlerPlayerInput(this) - val progress: ProgressGaugeWidget + val progress = ProgressGaugeWidget(this) init { - if (tile == null) - progress = ProgressGaugeWidget(this) - else { - progress = ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess) + if (tile != null) { + progress.with(tile::workProgress, tile::isUnableToProcess) redstone.with(tile.redstoneControl::redstoneSetting) itemConfig.configure(tile.itemConfig) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/PlatePressMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/PlatePressMenu.kt index 512bfae03..8893a870c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/PlatePressMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/PlatePressMenu.kt @@ -20,7 +20,7 @@ class PlatePressMenu @JvmOverloads constructor( val inputSlot = MatterySlot(tile?.inputContainer ?: SimpleContainer(1), 0) val outputSlot = MachineOutputSlot(tile?.outputContainer ?: SimpleContainer(1), 0) { tile?.popExperience(ply as ServerPlayer) } - val progressGauge = if (tile != null) ProgressGaugeWidget(this, tile::workProgress, tile::isUnableToProcess) else ProgressGaugeWidget(this) + val progressGauge = ProgressGaugeWidget(this) val itemConfig = ItemHandlerPlayerInput(this, allowPush = true) val energyConfig = EnergyPlayerInput(this, allowPull = true) @@ -31,6 +31,7 @@ class PlatePressMenu @JvmOverloads constructor( addInventorySlots() if (tile != null) { + progressGauge.with(tile::workProgress, tile::isUnableToProcess) itemConfig.configure(tile.itemConfig) energyConfig.configure(tile.energyConfig) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/AbstractWidget.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/AbstractWidget.kt deleted file mode 100644 index cfd9a6147..000000000 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/AbstractWidget.kt +++ /dev/null @@ -1,10 +0,0 @@ -package ru.dbotthepony.mc.otm.menu.widget - -import net.minecraft.world.inventory.ContainerData -import ru.dbotthepony.mc.otm.menu.MatteryMenu - -abstract class AbstractWidget(val menu: MatteryMenu,) { - val slotID: Int = menu.addWidget(this) - - abstract fun updateServer() -} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/LevelGaugeWidget.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/LevelGaugeWidget.kt index f2ab00129..5b06f9c59 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/LevelGaugeWidget.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/LevelGaugeWidget.kt @@ -4,46 +4,42 @@ import ru.dbotthepony.mc.otm.capability.energy.IMatteryEnergyStorage import ru.dbotthepony.mc.otm.capability.matter.IMatterStorage import ru.dbotthepony.mc.otm.capability.matter.IPatternStorage import ru.dbotthepony.mc.otm.core.math.Decimal +import ru.dbotthepony.mc.otm.core.util.DecimalValueCodec import ru.dbotthepony.mc.otm.menu.MatteryMenu @Suppress("unused") -class LevelGaugeWidget(menu: MatteryMenu) : AbstractWidget(menu) { +class LevelGaugeWidget(menu: MatteryMenu) { var levelProvider = { Decimal.ONE } var maxLevelProvider = { Decimal.ONE } - var level by menu.mSynchronizer.fraction() - private set - var maxLevel by menu.mSynchronizer.fraction() - private set + val level by menu.mSynchronizer.ComputedField(getter = { levelProvider.invoke() }, codec = DecimalValueCodec) + val maxLevel by menu.mSynchronizer.ComputedField(getter = { maxLevelProvider.invoke() }, codec = DecimalValueCodec) constructor( menu: MatteryMenu, power: IMatteryEnergyStorage? ) : this(menu) { - if (power == null) return - - this.levelProvider = power::batteryLevel - this.maxLevelProvider = power::maxBatteryLevel + if (power != null) { + with(power) + } } constructor( menu: MatteryMenu, matter: IMatterStorage? ) : this(menu) { - if (matter == null) return - - this.levelProvider = matter::storedMatter - this.maxLevelProvider = matter::maxStoredMatter + if (matter != null) { + with(matter) + } } constructor( menu: MatteryMenu, patterns: IPatternStorage? ) : this(menu) { - if (patterns == null) return - - this.levelProvider = { Decimal(patterns.storedPatterns) } - this.maxLevelProvider = { Decimal(patterns.patternCapacity) } + if (patterns != null) { + with(patterns) + } } constructor( @@ -55,12 +51,29 @@ class LevelGaugeWidget(menu: MatteryMenu) : AbstractWidget(menu) { this.maxLevelProvider = maxLevel } - override fun updateServer() { - level = levelProvider.invoke() - maxLevel = maxLevelProvider.invoke() + val percentage: Float get() = level.percentage(maxLevel) + + fun with(level: () -> Decimal, maxLevel: () -> Decimal): LevelGaugeWidget { + this.levelProvider = level + this.maxLevelProvider = maxLevel + return this } - fun level(): Decimal = level - fun maxLevel(): Decimal = maxLevel - fun percentage(): Float = level.percentage(maxLevel) + fun with(patterns: IPatternStorage): LevelGaugeWidget { + this.levelProvider = { Decimal(patterns.storedPatterns) } + this.maxLevelProvider = { Decimal(patterns.patternCapacity) } + return this + } + + fun with(power: IMatteryEnergyStorage): LevelGaugeWidget { + this.levelProvider = power::batteryLevel + this.maxLevelProvider = power::maxBatteryLevel + return this + } + + fun with(matter: IMatterStorage): LevelGaugeWidget { + this.levelProvider = matter::storedMatter + this.maxLevelProvider = matter::maxStoredMatter + return this + } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/ProgressGaugeWidget.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/ProgressGaugeWidget.kt index 148a968e9..5b6560a2c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/ProgressGaugeWidget.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/widget/ProgressGaugeWidget.kt @@ -1,35 +1,42 @@ package ru.dbotthepony.mc.otm.menu.widget +import mekanism.api.functions.FloatSupplier import ru.dbotthepony.mc.otm.menu.MatteryMenu +import java.util.function.BooleanSupplier @Suppress("unused") -class ProgressGaugeWidget(menu: MatteryMenu) : AbstractWidget(menu) { - var progress = {0f} - var stuck = {false} - var progressContainer by menu.mSynchronizer.short() - var stuckContainer by menu.mSynchronizer.bool().property +class ProgressGaugeWidget(menu: MatteryMenu) { + var progressSupplier: FloatSupplier = FloatSupplier { 0f } + var stuckSupplier: BooleanSupplier = BooleanSupplier { false } + + val percentage by menu.mSynchronizer.ComputedFloatField(getter = { progressSupplier.asFloat }) + val isStuck by menu.mSynchronizer.ComputedBooleanField(getter = { stuckSupplier.asBoolean }) constructor( menu: MatteryMenu, - progress: () -> Float + progress: FloatSupplier ) : this(menu) { - this.progress = progress + this.progressSupplier = progress } constructor( menu: MatteryMenu, - progress: () -> Float, - stuck: () -> Boolean, + progress: FloatSupplier, + stuck: BooleanSupplier, ) : this(menu) { - this.progress = progress - this.stuck = stuck + this.progressSupplier = progress + this.stuckSupplier = stuck } - override fun updateServer() { - progressContainer = (Math.max(Math.min(progress.invoke(), 1f), 0f) * 10_000f).toInt().toShort() - stuckContainer = stuck.invoke() + fun with(progress: FloatSupplier): ProgressGaugeWidget { + this.progressSupplier = progress + this.stuckSupplier = BooleanSupplier { false } + return this } - fun isStuck(): Boolean = stuckContainer - fun percentage(): Float = progressContainer / 10_000f + fun with(progress: FloatSupplier, stuck: BooleanSupplier): ProgressGaugeWidget { + this.progressSupplier = progress + this.stuckSupplier = stuck + return this + } }