Jelajahi Sumber

Config specific type serializers

Lortseam 4 tahun lalu
induk
melakukan
b3137606a4

+ 2 - 2
build.gradle

@@ -23,8 +23,8 @@ dependencies {
 		exclude(group: "net.fabricmc.fabric-api")
 	}
 
-	// Using modImplementation because implementation does not include dependency inside Maven pom file (see https://github.com/FabricMC/fabric-loom/issues/200)
-	modImplementation("org.spongepowered:configurate-hocon:${project.configurate_version}")
+	// Using modApi because api does not include dependency inside Maven pom file (see https://github.com/FabricMC/fabric-loom/issues/200)
+	modApi("org.spongepowered:configurate-hocon:${project.configurate_version}")
 
 	compileOnly("org.projectlombok:lombok:${project.lombok_version}")
 	annotationProcessor("org.projectlombok:lombok:${project.lombok_version}")

+ 17 - 1
src/main/java/me/lortseam/completeconfig/ConfigBuilder.java

@@ -3,6 +3,7 @@ package me.lortseam.completeconfig;
 import me.lortseam.completeconfig.api.ConfigGroup;
 import me.lortseam.completeconfig.api.ConfigOwner;
 import me.lortseam.completeconfig.gui.GuiBuilder;
+import org.spongepowered.configurate.serialize.TypeSerializerCollection;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -19,6 +20,7 @@ public final class ConfigBuilder {
     private final String[] branch;
     private final Class<? extends ConfigOwner> owner;
     private final List<ConfigGroup> topLevelGroups = new ArrayList<>();
+    private TypeSerializerCollection typeSerializers;
     private GuiBuilder guiBuilder;
 
     private ConfigBuilder(String modID, String[] branch, Class<? extends ConfigOwner> owner) {
@@ -38,6 +40,20 @@ public final class ConfigBuilder {
         return this;
     }
 
+    //TODO: Add javadoc
+    public ConfigBuilder registerTypeSerializers(TypeSerializerCollection typeSerializers) {
+        Objects.requireNonNull(typeSerializers);
+        if (this.typeSerializers == null) {
+            this.typeSerializers = typeSerializers;
+        } else {
+            this.typeSerializers = TypeSerializerCollection.builder()
+                    .registerAll(this.typeSerializers)
+                    .registerAll(typeSerializers)
+                    .build();
+        }
+        return this;
+    }
+
     /**
      * Sets a custom client GUI builder for the config.
      *
@@ -56,7 +72,7 @@ public final class ConfigBuilder {
      * @return the handler associated with the created config
      */
     public ConfigHandler finish() {
-        return ConfigHandler.registerConfig(modID, branch, owner, topLevelGroups, guiBuilder);
+        return ConfigHandler.registerConfig(modID, branch, owner, topLevelGroups, typeSerializers, guiBuilder);
     }
 
 }

+ 5 - 3
src/main/java/me/lortseam/completeconfig/ConfigHandler.java

@@ -14,6 +14,7 @@ import org.apache.logging.log4j.Logger;
 import org.spongepowered.configurate.CommentedConfigurationNode;
 import org.spongepowered.configurate.ConfigurateException;
 import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
+import org.spongepowered.configurate.serialize.TypeSerializerCollection;
 
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -33,7 +34,7 @@ public final class ConfigHandler {
         }));
     }
 
-    static ConfigHandler registerConfig(String modID, String[] branch, Class<? extends ConfigOwner> owner, List<ConfigGroup> topLevelGroups, GuiBuilder guiBuilder) {
+    static ConfigHandler registerConfig(String modID, String[] branch, Class<? extends ConfigOwner> owner, List<ConfigGroup> topLevelGroups, TypeSerializerCollection typeSerializers, GuiBuilder guiBuilder) {
         if (HANDLERS.containsKey(owner)) {
             throw new IllegalArgumentException("The specified owner " + owner + " already created a config!");
         }
@@ -49,7 +50,7 @@ public final class ConfigHandler {
         String[] subPath = ArrayUtils.add(branch, 0, modID);
         subPath[subPath.length - 1] = subPath[subPath.length - 1] + ".conf";
         Path filePath = Paths.get(FabricLoader.getInstance().getConfigDir().toString(), subPath);
-        ConfigHandler handler = new ConfigHandler(modID, filePath, topLevelGroups, guiBuilder);
+        ConfigHandler handler = new ConfigHandler(modID, filePath, topLevelGroups, typeSerializers, guiBuilder);
         HANDLERS.put(owner, handler);
         return handler;
     }
@@ -68,9 +69,10 @@ public final class ConfigHandler {
     private final Config config;
     private GuiBuilder guiBuilder;
 
-    private ConfigHandler(String modID, Path filePath, List<ConfigGroup> topLevelGroups, GuiBuilder guiBuilder) {
+    private ConfigHandler(String modID, Path filePath, List<ConfigGroup> topLevelGroups, TypeSerializerCollection typeSerializers, GuiBuilder guiBuilder) {
         loader = HoconConfigurationLoader.builder()
                 .path(filePath)
+                .defaultOptions(options -> options.serializers(builder -> builder.registerAll(typeSerializers)))
                 .build();
         config = new Config(modID, topLevelGroups);
         CommentedConfigurationNode root = load();