diff options
author | Linnea Gräf <nea@nea.moe> | 2024-12-22 16:08:31 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-12-22 16:08:31 +0100 |
commit | 856f5e0e815c8ef3be77ef342e1b0ca91aab8b0a (patch) | |
tree | 4584afffa853e882fe3440a6e7f481d15f5ea5e7 /src | |
parent | a9a801048d694e5b250f3a23089075e9b7bd976c (diff) | |
download | Firmament-856f5e0e815c8ef3be77ef342e1b0ca91aab8b0a.tar.gz Firmament-856f5e0e815c8ef3be77ef342e1b0ca91aab8b0a.tar.bz2 Firmament-856f5e0e815c8ef3be77ef342e1b0ca91aab8b0a.zip |
fix: Add choice options to YACL integration
Diffstat (limited to 'src')
-rw-r--r-- | src/compat/yacl/java/YaclIntegration.kt | 29 | ||||
-rw-r--r-- | src/main/kotlin/features/mining/PickaxeAbility.kt | 1 | ||||
-rw-r--r-- | src/main/kotlin/gui/config/ChoiceHandler.kt | 1 | ||||
-rw-r--r-- | src/main/kotlin/gui/config/ManagedConfig.kt | 6 |
4 files changed, 33 insertions, 4 deletions
diff --git a/src/compat/yacl/java/YaclIntegration.kt b/src/compat/yacl/java/YaclIntegration.kt index 9aec501..45a0d02 100644 --- a/src/compat/yacl/java/YaclIntegration.kt +++ b/src/compat/yacl/java/YaclIntegration.kt @@ -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() diff --git a/src/main/kotlin/features/mining/PickaxeAbility.kt b/src/main/kotlin/features/mining/PickaxeAbility.kt index a184c81..94b49f9 100644 --- a/src/main/kotlin/features/mining/PickaxeAbility.kt +++ b/src/main/kotlin/features/mining/PickaxeAbility.kt @@ -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 } diff --git a/src/main/kotlin/gui/config/ChoiceHandler.kt b/src/main/kotlin/gui/config/ChoiceHandler.kt index 25e885a..2ea3efc 100644 --- a/src/main/kotlin/gui/config/ChoiceHandler.kt +++ b/src/main/kotlin/gui/config/ChoiceHandler.kt @@ -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 { diff --git a/src/main/kotlin/gui/config/ManagedConfig.kt b/src/main/kotlin/gui/config/ManagedConfig.kt index ea55f67..641b673 100644 --- a/src/main/kotlin/gui/config/ManagedConfig.kt +++ b/src/main/kotlin/gui/config/ManagedConfig.kt @@ -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 { |