From 1842074d9e5ff72e8c9a07e8cf597c03f16e4855 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 26 Jan 2023 21:15:01 +0700 Subject: [PATCH] AbstractNetworkedInput --- .../mc/otm/menu/MatterBottlerMenu.kt | 3 +- .../otm/menu/input/AbstractNetworkedInput.kt | 53 +++++++++++++++++++ .../otm/menu/input/NetworkedBooleanInput.kt | 48 ++--------------- 3 files changed, 58 insertions(+), 46 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/AbstractNetworkedInput.kt diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterBottlerMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterBottlerMenu.kt index 4dd49a0fa..4f19a8041 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterBottlerMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatterBottlerMenu.kt @@ -34,7 +34,8 @@ class MatterBottlerMenu @JvmOverloads constructor( if (tile == null) { progressWidget = ProgressGaugeWidget(this) matterWidget = LevelGaugeWidget(this) - workFlow = NetworkedBooleanInput(this).asClient() + workFlow = NetworkedBooleanInput(this) + workFlow.asClient() } else { progressWidget = ProgressGaugeWidget(this) { tile.getWorkProgress() } matterWidget = LevelGaugeWidget(this, tile.matter) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/AbstractNetworkedInput.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/AbstractNetworkedInput.kt new file mode 100644 index 000000000..567fbaf7c --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/AbstractNetworkedInput.kt @@ -0,0 +1,53 @@ +package ru.dbotthepony.mc.otm.menu.input + +import net.minecraft.world.entity.player.Player +import ru.dbotthepony.mc.otm.menu.MatteryMenu +import kotlin.reflect.KMutableProperty0 + +abstract class AbstractNetworkedInput { + abstract val input: MatteryMenu.PlayerInput + abstract val value: V + + var isClient = false + + fun asClient(): AbstractNetworkedInput { + isClient = true + supplier = null + consumer = null + return this + } + + var supplier: (() -> V)? = null + var consumer: ((V) -> Unit)? = null + + fun withSupplier(func: () -> V): AbstractNetworkedInput { + supplier = func + return this + } + + fun withConsumer(func: (V) -> Unit): AbstractNetworkedInput { + consumer = func + return this + } + + fun with(state: KMutableProperty0): AbstractNetworkedInput { + withConsumer { state.set(it) } + withSupplier { state.get() } + return this + } + + fun clear(): AbstractNetworkedInput { + supplier = null + consumer = null + return this + } + + fun userInput(newValue: V, ply: Player? = null) { + if (isClient) { + input.checkedInput(newValue, ply) + } else { + consumer?.invoke(newValue) + } + } + +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/NetworkedBooleanInput.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/NetworkedBooleanInput.kt index 7d41df71f..63fb35eb9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/NetworkedBooleanInput.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/NetworkedBooleanInput.kt @@ -4,56 +4,14 @@ import net.minecraft.world.entity.player.Player import ru.dbotthepony.mc.otm.menu.MatteryMenu import kotlin.reflect.KMutableProperty0 -class NetworkedBooleanInput(menu: MatteryMenu) { - val input = menu.booleanInput { consumer?.invoke(it) } - val value by menu.mSynchronizer.bool(getter = { supplier?.invoke() ?: false }) +class NetworkedBooleanInput(menu: MatteryMenu) : AbstractNetworkedInput() { + override val input = menu.booleanInput { consumer?.invoke(it) } + override val value by menu.mSynchronizer.bool(getter = { supplier?.invoke() ?: false }) constructor(menu: MatteryMenu, state: KMutableProperty0) : this(menu) { with(state) } - var isClient = false - - fun asClient(): NetworkedBooleanInput { - isClient = true - supplier = null - consumer = null - return this - } - - var supplier: (() -> Boolean)? = null - var consumer: ((Boolean) -> Unit)? = null - - fun withSupplier(func: () -> Boolean): NetworkedBooleanInput { - supplier = func - return this - } - - fun withConsumer(func: (Boolean) -> Unit): NetworkedBooleanInput { - consumer = func - return this - } - - fun with(state: KMutableProperty0): NetworkedBooleanInput { - withConsumer { state.set(it) } - withSupplier { state.get() } - return this - } - - fun clear(): NetworkedBooleanInput { - supplier = null - consumer = null - return this - } - - fun userInput(newValue: Boolean, ply: Player? = null) { - if (isClient) { - input.checkedInput(newValue, ply) - } else { - consumer?.invoke(newValue) - } - } - fun switchValue(ply: Player? = null) { userInput(!value, ply) }