diff options
Diffstat (limited to 'src/testmod')
4 files changed, 151 insertions, 83 deletions
diff --git a/src/testmod/java/dev/isxander/yacl3/test/CodecConfig.java b/src/testmod/java/dev/isxander/yacl3/test/CodecConfig.java new file mode 100644 index 0000000..08137e6 --- /dev/null +++ b/src/testmod/java/dev/isxander/yacl3/test/CodecConfig.java @@ -0,0 +1,53 @@ +package dev.isxander.yacl3.test; + +import com.google.gson.JsonElement; +import com.mojang.serialization.Codec; +import com.mojang.serialization.DataResult; +import com.mojang.serialization.JsonOps; +import dev.isxander.yacl3.config.v3.ConfigEntry; +import dev.isxander.yacl3.config.v3.JsonFileCodecConfig; +import dev.isxander.yacl3.platform.YACLPlatform; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.ComponentSerialization; +import net.minecraft.resources.ResourceLocation; + +public class CodecConfig extends JsonFileCodecConfig { + public static final CodecConfig INSTANCE = new CodecConfig(); + + public final ConfigEntry<Integer> myInt = + register("my_int", 0, Codec.INT); + + public final ConfigEntry<String> myString = + register("my_string", "default", Codec.STRING); + + public final ConfigEntry<ResourceLocation> myIdentifier = + register("my_identifier", YACLPlatform.rl("test"), ResourceLocation.CODEC); + + public final ConfigEntry<Component> myText = + register("my_text", Component.literal("Hello"), ComponentSerialization.CODEC); + + public final ConfigEntry<InnerCodecConfig> myInnerConfig = + register("my_inner_config", InnerCodecConfig.INSTANCE, InnerCodecConfig.INSTANCE); + + public static class InnerCodecConfig extends dev.isxander.yacl3.config.v3.CodecConfig<InnerCodecConfig> { + public static final InnerCodecConfig INSTANCE = new InnerCodecConfig(); + } + + public CodecConfig() { + super(YACLPlatform.getConfigDir().resolve("codec_config.json")); + } + + void test() { + loadFromFile(); // load like this + saveToFile(); // save like this + + this.myInt.get(); + this.myInt.set(5); + this.myInt.defaultValue(); + + // or if you just extend Config instead of JsonFileConfig: + JsonElement element = null; + this.decode(element, JsonOps.INSTANCE); // load + DataResult<JsonElement> encoded = this.encodeStart(JsonOps.INSTANCE); // save + } +} diff --git a/src/testmod/java/dev/isxander/yacl3/test/Entrypoint.java b/src/testmod/java/dev/isxander/yacl3/test/Entrypoint.java index 2c4875f..dca6070 100644 --- a/src/testmod/java/dev/isxander/yacl3/test/Entrypoint.java +++ b/src/testmod/java/dev/isxander/yacl3/test/Entrypoint.java @@ -1,4 +1,4 @@ -/*? if neoforge { *//* +/*? if neoforge {*//* package dev.isxander.yacl3.test; import net.neoforged.fml.common.Mod; diff --git a/src/testmod/java/dev/isxander/yacl3/test/GuiTest.java b/src/testmod/java/dev/isxander/yacl3/test/GuiTest.java index 07e0098..a515fe0 100644 --- a/src/testmod/java/dev/isxander/yacl3/test/GuiTest.java +++ b/src/testmod/java/dev/isxander/yacl3/test/GuiTest.java @@ -53,7 +53,7 @@ public class GuiTest { .option(ButtonOption.createBuilder() .name(Component.literal("Kotlin DSL Test")) .action((screen, opt) -> { - Minecraft.getInstance().setScreen(DslTestKt.kotlinDslGui(screen)); + Minecraft.getInstance().setScreen(CodecConfigKt.INSTANCE.generateConfigScreen(screen)); }) .build()) .group(OptionGroup.createBuilder() diff --git a/src/testmod/kotlin/dev/isxander/yacl3/test/DslTest.kt b/src/testmod/kotlin/dev/isxander/yacl3/test/DslTest.kt index a3ed7cc..9335fb4 100644 --- a/src/testmod/kotlin/dev/isxander/yacl3/test/DslTest.kt +++ b/src/testmod/kotlin/dev/isxander/yacl3/test/DslTest.kt @@ -1,131 +1,146 @@ package dev.isxander.yacl3.test +import com.mojang.serialization.Codec import dev.isxander.yacl3.api.OptionFlag -import dev.isxander.yacl3.api.controller.BooleanControllerBuilder -import dev.isxander.yacl3.api.controller.IntegerSliderControllerBuilder +import dev.isxander.yacl3.config.v3.JsonFileCodecConfig +import dev.isxander.yacl3.config.v3.register import dev.isxander.yacl3.dsl.* import dev.isxander.yacl3.platform.YACLPlatform import net.minecraft.client.gui.screens.Screen import net.minecraft.network.chat.Component +import net.minecraft.network.chat.ComponentSerialization import net.minecraft.resources.ResourceLocation -object Foo { - var bar = true - var baz = 0 -} +object CodecConfigKt : JsonFileCodecConfig(YACLPlatform.getConfigDir().resolve("codec_config_kt.json")) { + val myInt by register<Int>(0, Codec.INT) -fun kotlinDslGui(parent: Screen?) = YetAnotherConfigLib("namespace") { - // default title with translation key: - // `yacl3.config.namespace.title` - /* NO CODE REQUIRED */ + val myString by register<String>("default", Codec.STRING) - // or set the title - title(Component.literal("A cool title")) + val myBoolean by register<Boolean>(false, Codec.BOOL) + val myIdentifier by register<ResourceLocation>(YACLPlatform.rl("test"), ResourceLocation.CODEC) - // usual save function - save { - // run your save function! - } + val myText by register<Component>(Component.literal("Hello, World!"), ComponentSerialization.CODEC) - // get access to an option from the very root of the dsl! - categories["testCategory"]["testGroup"].getOption("testOption").onReady { - // do something with it + init { + if (!loadFromFile()) { + saveToFile() + } } - val testCategory by categories.registering { - // default name with translation key: - // `yacl3.config.namespace.testCategory.testGroup.name` + fun generateConfigScreen(lastScreen: Screen?) = YetAnotherConfigLib("namespace") { + // default title with translation key: + // `yacl3.config.namespace.title` /* NO CODE REQUIRED */ - // or set the name - name { Component.literal("A cool category") } + // or set the title + title(Component.literal("A cool title")) - // custom tooltip - tooltipBuilder { - // add a line like this - +Component.translatable("somecustomkey") - // or like this - text(Component.translatable("somecustomkey")) + // usual save function + save { + // run your save function! + saveToFile() + } - // or like this - text { Component.translatable("somecustomkey") } + // get access to an option from the very root of the dsl! + categories["testCategory"]["testGroup"].futureRef<String>("myIntOption").onReady { + // do something with it } - // you can declare things with strings - group("testGroup") { + val testCategory by categories.registering { // default name with translation key: // `yacl3.config.namespace.testCategory.testGroup.name` /* NO CODE REQUIRED */ // or set the name - name { Component.literal("A cool group") } + name { Component.literal("A cool category") } + + // custom tooltip + tooltip { + // add a line like this + +Component.translatable("somecustomkey") + // or like this + text(Component.translatable("somecustomkey")) - // custom description builder: - descriptionBuilder { - // blah blah blah + // or like this + text { Component.translatable("somecustomkey") } } - // default description with translation key: - // `yacl3.config.namespace.testCategory.testGroup.description.1-5` - // not compatible with custom description builder - useDefaultDescription(lines = 5) + // creates a label with the id `testLabel` + val testLabel by rootOptions.registeringLabel - // you can define opts/groups/categories using this delegate syntax - val testOption by options.registering { // type is automatically inferred from binding + // you can declare things with strings + groups.register("testGroup") { // default name with translation key: - // `yacl3.config.namespace.testCategory.testGroup.testOption.name` + // `yacl3.config.namespace.testCategory.testGroup.name` /* NO CODE REQUIRED */ - // custom description builder: - descriptionBuilder { value -> // changes the desc based on the current value - // description with translation key: - // `yacl3.config.namespace.testCategory.testGroup.testOption.description.1-5` - addDefaultDescription(lines = 5) + // or set the name + name { Component.literal("A cool group") } + - text { Component.translatable("somecustomkey") } - webpImage(YACLPlatform.rl("namespace", "image.png")) + // custom description builder: + descriptionBuilder { + // default description with translation key: + // `yacl3.config.namespace.testCategory.testGroup.description.1-5` + // not compatible with custom description builder + addDefaultText(lines = 5) } - // KProperties are cool! - binding(Foo::bar, Foo.bar) + // you can define opts/groups/categories using this delegate syntax + val myIntOption by options.registering<Int> { // type is automatically inferred from binding + // default name with translation key: + // `yacl3.config.namespace.testCategory.testGroup.testOption.name` + /* NO CODE REQUIRED */ - // you can access other options like this! - // `options` field is from the enclosing group dsl - listener { opt, newVal -> - options.get<Int>("otherTestOption").onReady { it.setAvailable(newVal) } - } + // custom description builder: + descriptionBuilderDyn { value -> // changes the desc based on the current value + // description with translation key: + // `yacl3.config.namespace.testCategory.testGroup.testOption.description.1-5` + addDefaultText(lines = 5) - // or even get an access to them before creation - options.get<Int>("otherTestOption").onReady { - // do something with it - } + text { Component.translatable("somecustomkey") } + webpImage(YACLPlatform.rl("namespace", "image.png")) + } - // you can set available with a block - available { true } + // Codecs! + binding = myInt.asBinding() - // regular controller stuff - // this will be DSLed at some point - controller(BooleanControllerBuilder::create) { - // blah blah blah - } + // you can access other options like this! + // `options` field is from the enclosing group dsl + listener { opt, newVal -> + options.futureRef<String>("myString").onReady { - // flags as usual - flag(OptionFlag.ASSET_RELOAD) - } + } + } + + // or use a delegated property to create a reference to an option + val myStringOption by options.ref<String>() // nullable + + // you can set available with a property + available = true + // ...or a block + available { true } + + // cool custom controller functions + controller = slider(range = 5..10, step = 1) - val otherTestOption by options.registering { // type is automatically inferred from binding - controller(IntegerSliderControllerBuilder::create) { - range(0, 100) - step(5) + // flags as usual + flag(OptionFlag.ASSET_RELOAD) } - binding(Foo::baz, Foo.baz) + // codec config api automatically sets binding and name + val myStringOption = options.register(myString) { + controller = stringField() + } - // blah blah blah other stuff + val myBooleanOption = options.register(myBoolean) { + // custom formatters for these cool controller functions + controller = textSwitch { bool -> Component.literal(bool.toString()) } + } } } - } -}.generateScreen(parent) + }.generateScreen(lastScreen) +} |