From 305718e163f91802a4bc1c1ed6540febb2ce204e Mon Sep 17 00:00:00 2001 From: isXander Date: Tue, 11 Jun 2024 23:13:49 +0100 Subject: codec config and rewritten kotlin dsl --- .../java/dev/isxander/yacl3/test/CodecConfig.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/testmod/java/dev/isxander/yacl3/test/CodecConfig.java (limited to 'src/testmod/java/dev/isxander/yacl3/test/CodecConfig.java') 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 myInt = + register("my_int", 0, Codec.INT); + + public final ConfigEntry myString = + register("my_string", "default", Codec.STRING); + + public final ConfigEntry myIdentifier = + register("my_identifier", YACLPlatform.rl("test"), ResourceLocation.CODEC); + + public final ConfigEntry myText = + register("my_text", Component.literal("Hello"), ComponentSerialization.CODEC); + + public final ConfigEntry myInnerConfig = + register("my_inner_config", InnerCodecConfig.INSTANCE, InnerCodecConfig.INSTANCE); + + public static class InnerCodecConfig extends dev.isxander.yacl3.config.v3.CodecConfig { + 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 encoded = this.encodeStart(JsonOps.INSTANCE); // save + } +} -- cgit