aboutsummaryrefslogtreecommitdiff
path: root/src/testmod/java/dev/isxander/yacl3/test/CodecConfig.java
blob: 08137e690672a64591e3c319760af44f90262b33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
    }
}