fix: Add choice options to YACL integration
This commit is contained in:
@@ -10,3 +10,6 @@ archives_base_name=Firmament
|
||||
maven_group=moe.nea.firmament
|
||||
|
||||
firmament.compiletimerepohash=a6116d945491d7c57c93d43f51250f93d62d8434
|
||||
|
||||
# TODO: remove after https://github.com/google/ksp/issues/2072
|
||||
ksp.incremental=false
|
||||
|
||||
@@ -11,9 +11,11 @@ import dev.isxander.yacl3.api.OptionGroup
|
||||
import dev.isxander.yacl3.api.YetAnotherConfigLib
|
||||
import dev.isxander.yacl3.api.controller.ControllerBuilder
|
||||
import dev.isxander.yacl3.api.controller.DoubleSliderControllerBuilder
|
||||
import dev.isxander.yacl3.api.controller.EnumControllerBuilder
|
||||
import dev.isxander.yacl3.api.controller.IntegerSliderControllerBuilder
|
||||
import dev.isxander.yacl3.api.controller.StringControllerBuilder
|
||||
import dev.isxander.yacl3.api.controller.TickBoxControllerBuilder
|
||||
import dev.isxander.yacl3.api.controller.ValueFormatter
|
||||
import dev.isxander.yacl3.gui.YACLScreen
|
||||
import dev.isxander.yacl3.gui.tab.ListHolderWidget
|
||||
import kotlin.time.Duration
|
||||
@@ -23,8 +25,10 @@ import net.minecraft.client.gui.Element
|
||||
import net.minecraft.client.gui.screen.Screen
|
||||
import net.minecraft.text.Text
|
||||
import moe.nea.firmament.gui.config.BooleanHandler
|
||||
import moe.nea.firmament.gui.config.ChoiceHandler
|
||||
import moe.nea.firmament.gui.config.ClickHandler
|
||||
import moe.nea.firmament.gui.config.DurationHandler
|
||||
import moe.nea.firmament.gui.config.EnumRenderer
|
||||
import moe.nea.firmament.gui.config.FirmamentConfigScreenProvider
|
||||
import moe.nea.firmament.gui.config.HudMeta
|
||||
import moe.nea.firmament.gui.config.HudMetaHandler
|
||||
@@ -89,6 +93,10 @@ class YaclIntegration : FirmamentConfigScreenProvider {
|
||||
}
|
||||
.build()
|
||||
|
||||
is ChoiceHandler<*> -> return createDefaultBinding {
|
||||
createChoiceBinding(handler as ChoiceHandler<*>, managedOption as ManagedOption<*>, it as Option<*>)
|
||||
}.build()
|
||||
|
||||
is BooleanHandler -> return createDefaultBinding(TickBoxControllerBuilder::create).build()
|
||||
is StringHandler -> return createDefaultBinding(StringControllerBuilder::create).build()
|
||||
is IntegerHandler -> return createDefaultBinding {
|
||||
@@ -114,6 +122,27 @@ class YaclIntegration : FirmamentConfigScreenProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private enum class Sacrifice {}
|
||||
|
||||
private fun createChoiceBinding(
|
||||
handler: ChoiceHandler<*>,
|
||||
managedOption: ManagedOption<*>,
|
||||
option: Option<*>
|
||||
): ControllerBuilder<Any> {
|
||||
val b = EnumControllerBuilder.create(option as Option<Sacrifice>)
|
||||
b.enumClass(handler.enumClass as Class<Sacrifice>)
|
||||
/**
|
||||
* This is a function with E to avoid realizing the Sacrifice outside of a `X<E>` wrapper.
|
||||
*/
|
||||
fun <E : Enum<*>> makeValueFormatter(): ValueFormatter<E> {
|
||||
return ValueFormatter<E> {
|
||||
(handler.renderer as EnumRenderer<E>).getName(managedOption as ManagedOption<E>, it)
|
||||
}
|
||||
}
|
||||
b.formatValue(makeValueFormatter())
|
||||
return b as ControllerBuilder<Any>
|
||||
}
|
||||
|
||||
|
||||
fun buildConfig(): YetAnotherConfigLib {
|
||||
return YetAnotherConfigLib.createBuilder()
|
||||
|
||||
@@ -50,7 +50,6 @@ object PickaxeAbility : FirmamentFeature {
|
||||
val drillFuelBar by toggle("fuel-bar") { true }
|
||||
val blockOnPrivateIsland by choice(
|
||||
"block-on-dynamic",
|
||||
BlockPickaxeAbility.entries,
|
||||
) {
|
||||
BlockPickaxeAbility.ONLY_DESTRUCTIVE
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import moe.nea.firmament.util.ErrorUtil
|
||||
import moe.nea.firmament.util.json.KJsonOps
|
||||
|
||||
class ChoiceHandler<E>(
|
||||
val enumClass: Class<E>,
|
||||
val universe: List<E>,
|
||||
) : ManagedConfig.OptionHandler<E> where E : Enum<E>, E : StringIdentifiable {
|
||||
val codec = StringIdentifiable.createCodec {
|
||||
|
||||
@@ -118,17 +118,17 @@ abstract class ManagedConfig(
|
||||
|
||||
protected fun <E> choice(
|
||||
propertyName: String,
|
||||
universe: List<E>,
|
||||
enumClass: Class<E>,
|
||||
default: () -> E
|
||||
): ManagedOption<E> where E : Enum<E>, E : StringIdentifiable {
|
||||
return option(propertyName, default, ChoiceHandler(universe))
|
||||
return option(propertyName, default, ChoiceHandler(enumClass, enumClass.enumConstants.toList()))
|
||||
}
|
||||
|
||||
protected inline fun <reified E> choice(
|
||||
propertyName: String,
|
||||
noinline default: () -> E
|
||||
): ManagedOption<E> where E : Enum<E>, E : StringIdentifiable {
|
||||
return choice(propertyName, enumEntries<E>(), default)
|
||||
return choice(propertyName, E::class.java, default)
|
||||
}
|
||||
|
||||
private fun <E> createStringIdentifiable(x: () -> Array<out E>): Codec<E> where E : Enum<E>, E : StringIdentifiable {
|
||||
|
||||
Reference in New Issue
Block a user