AbstractNetworkedInput

This commit is contained in:
DBotThePony 2023-01-26 21:15:01 +07:00
parent 0ea4d25755
commit 1842074d9e
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 58 additions and 46 deletions

View File

@ -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)

View File

@ -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<V> {
abstract val input: MatteryMenu.PlayerInput<V>
abstract val value: V
var isClient = false
fun asClient(): AbstractNetworkedInput<V> {
isClient = true
supplier = null
consumer = null
return this
}
var supplier: (() -> V)? = null
var consumer: ((V) -> Unit)? = null
fun withSupplier(func: () -> V): AbstractNetworkedInput<V> {
supplier = func
return this
}
fun withConsumer(func: (V) -> Unit): AbstractNetworkedInput<V> {
consumer = func
return this
}
fun with(state: KMutableProperty0<V>): AbstractNetworkedInput<V> {
withConsumer { state.set(it) }
withSupplier { state.get() }
return this
}
fun clear(): AbstractNetworkedInput<V> {
supplier = null
consumer = null
return this
}
fun userInput(newValue: V, ply: Player? = null) {
if (isClient) {
input.checkedInput(newValue, ply)
} else {
consumer?.invoke(newValue)
}
}
}

View File

@ -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<Boolean>() {
override val input = menu.booleanInput { consumer?.invoke(it) }
override val value by menu.mSynchronizer.bool(getter = { supplier?.invoke() ?: false })
constructor(menu: MatteryMenu, state: KMutableProperty0<Boolean>) : 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<Boolean>): 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)
}