diff --git a/NookCore-Config/src/main/java/com/nookure/core/config/ConfigurationContainer.java b/NookCore-Config/src/main/java/com/nookure/core/config/ConfigurationContainer.java index 76a82e8..beff936 100644 --- a/NookCore-Config/src/main/java/com/nookure/core/config/ConfigurationContainer.java +++ b/NookCore-Config/src/main/java/com/nookure/core/config/ConfigurationContainer.java @@ -1,7 +1,11 @@ package com.nookure.core.config; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.spongepowered.configurate.CommentedConfigurationNode; import org.spongepowered.configurate.ConfigurateException; +import org.spongepowered.configurate.serialize.TypeSerializer; +import org.spongepowered.configurate.serialize.TypeSerializerCollection; import org.spongepowered.configurate.yaml.NodeStyle; import org.spongepowered.configurate.yaml.YamlConfigurationLoader; @@ -11,6 +15,9 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; + +import static java.util.Objects.requireNonNull; public class ConfigurationContainer { private final AtomicReference config; @@ -58,10 +65,40 @@ public CompletableFuture save() { } public static ConfigurationContainer load(Path path, Class clazz) throws IOException { - return load(path, clazz, "config.yml", null); + return load(path, clazz, "config.yml", null, null); + } + + public record SerializerData(Class clazz, TypeSerializer serializer) { + } + + public static ConfigurationContainer load( + @NotNull Path path, + @NotNull Class clazz, + @NotNull String fileName + ) throws IOException { + return load(path, clazz, fileName, null, null); } - public static ConfigurationContainer load(Path path, Class clazz, String fileName, String header) throws IOException { + public static ConfigurationContainer load( + @NotNull Path path, + @NotNull Class clazz, + @NotNull String fileName, + @Nullable String header + ) throws IOException { + return load(path, clazz, fileName, header, null); + } + + public static ConfigurationContainer load( + @NotNull Path path, + @NotNull Class clazz, + @NotNull String fileName, + @Nullable String header, + @Nullable Consumer serializerBuilder + ) throws IOException { + requireNonNull(path, "Path cannot be null"); + requireNonNull(clazz, "Class cannot be null"); + requireNonNull(fileName, "File name cannot be null"); + path = path.resolve(fileName); final boolean firstCreation = Files.notExists(path); final YamlConfigurationLoader loader = YamlConfigurationLoader.builder() @@ -73,6 +110,12 @@ public static ConfigurationContainer load(Path path, Class clazz, Stri opts = opts.header(header); } + if (serializerBuilder != null) { + TypeSerializerCollection.Builder builder = TypeSerializerCollection.builder(); + serializerBuilder.accept(builder); + opts = opts.serializers(builder.build()); + } + opts = opts.shouldCopyDefaults(true); return opts; })