ソースを参照

Move config builder to config class

Lortseam 4 年 前
コミット
5671904279

+ 0 - 98
src/main/java/me/lortseam/completeconfig/ConfigBuilder.java

@@ -1,98 +0,0 @@
-package me.lortseam.completeconfig;
-
-import me.lortseam.completeconfig.api.ConfigGroup;
-import me.lortseam.completeconfig.gui.GuiBuilder;
-import me.lortseam.completeconfig.io.ConfigSource;
-import me.lortseam.completeconfig.util.TypeUtils;
-import net.fabricmc.api.EnvType;
-import net.fabricmc.api.Environment;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-import org.spongepowered.configurate.serialize.TypeSerializerCollection;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Objects;
-
-public final class ConfigBuilder {
-
-    private static final Logger LOGGER = LogManager.getLogger();
-
-    private final String modID;
-    private final String[] branch;
-    private final List<ConfigGroup> topLevelGroups = new ArrayList<>();
-    private TypeSerializerCollection typeSerializers;
-    private GuiBuilder guiBuilder;
-
-    /**
-     * Creates a new config builder for the specified mod with a custom branch.
-     *
-     * <p>The config branch determines the location of the config's save file.
-     *
-     * @param modID the ID of the mod creating the config
-     * @param branch the branch
-     */
-    public ConfigBuilder(String modID, String[] branch) {
-        this.modID = modID;
-        this.branch = branch;
-    }
-
-    /**
-     * Creates a new config builder for the specified mod.
-     *
-     * @param modID the ID of the mod creating the config
-     */
-    public ConfigBuilder(String modID) {
-        this(modID, new String[0]);
-    }
-
-    /**
-     * Adds one or more top-level groups to the config.
-     *
-     * @param groups one or more top-level groups
-     * @return this config builder
-     */
-    public ConfigBuilder add(ConfigGroup... groups) {
-        Arrays.stream(groups).forEach(Objects::requireNonNull);
-        topLevelGroups.addAll(Arrays.asList(groups));
-        return this;
-    }
-
-    /**
-     * Registers custom type serializers, applied only to this config.
-     *
-     * @param typeSerializers the type serializers
-     * @return this config builder
-     */
-    public ConfigBuilder registerTypeSerializers(TypeSerializerCollection typeSerializers) {
-        this.typeSerializers = TypeUtils.mergeSerializers(this.typeSerializers, Objects.requireNonNull(typeSerializers));
-        return this;
-    }
-
-    /**
-     * Sets a custom client GUI builder for the config.
-     *
-     * @param guiBuilder a GUI builder
-     * @return this config builder
-     */
-    @Environment(EnvType.CLIENT)
-    public ConfigBuilder setGuiBuilder(GuiBuilder guiBuilder) {
-        this.guiBuilder = Objects.requireNonNull(guiBuilder);
-        return this;
-    }
-
-    /**
-     * Completes the config creation.
-     *
-     * @return the handler associated with the created config
-     */
-    public ConfigHandler build() {
-        if (topLevelGroups.isEmpty()) {
-            LOGGER.warn("[CompleteConfig] Mod " + modID + " tried to create an empty config!");
-            return null;
-        }
-        return new ConfigHandler(new ConfigSource(ModController.of(modID), branch, typeSerializers), topLevelGroups, guiBuilder);
-    }
-
-}

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

@@ -1,15 +1,12 @@
 package me.lortseam.completeconfig;
 
-import me.lortseam.completeconfig.api.ConfigGroup;
 import me.lortseam.completeconfig.data.Config;
 import me.lortseam.completeconfig.gui.GuiBuilder;
-import me.lortseam.completeconfig.io.ConfigSource;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
 import net.minecraft.client.gui.screen.Screen;
 
 import java.util.HashSet;
-import java.util.List;
 import java.util.Set;
 
 public final class ConfigHandler {
@@ -27,8 +24,8 @@ public final class ConfigHandler {
     private final Config config;
     private GuiBuilder guiBuilder;
 
-    ConfigHandler(ConfigSource source, List<ConfigGroup> topLevelGroups, GuiBuilder guiBuilder) {
-        config = new Config(source, topLevelGroups);
+    public ConfigHandler(Config config, GuiBuilder guiBuilder) {
+        this.config = config;
         this.guiBuilder = guiBuilder;
         handlers.add(this);
         config.load();

+ 99 - 1
src/main/java/me/lortseam/completeconfig/data/Config.java

@@ -1,16 +1,37 @@
 package me.lortseam.completeconfig.data;
 
+import me.lortseam.completeconfig.ConfigHandler;
+import me.lortseam.completeconfig.ModController;
+import me.lortseam.completeconfig.gui.GuiBuilder;
 import me.lortseam.completeconfig.io.ConfigSource;
 import me.lortseam.completeconfig.api.ConfigGroup;
 import me.lortseam.completeconfig.data.gui.TranslationIdentifier;
+import me.lortseam.completeconfig.util.TypeUtils;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.spongepowered.configurate.serialize.TypeSerializerCollection;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+import java.util.Objects;
 
 public class Config extends CollectionMap {
 
+    /**
+     * Creates a new config builder for the specified mod.
+     *
+     * @param modID the ID of the mod creating the config
+     */
+    public static Builder builder(String modID) {
+        return new Builder(modID);
+    }
+
     private final ConfigSource source;
 
-    public Config(ConfigSource source, List<ConfigGroup> topLevelGroups) {
+    private Config(ConfigSource source, List<ConfigGroup> topLevelGroups) {
         super(new TranslationIdentifier(source.getModID()));
         this.source = source;
         for (ConfigGroup group : topLevelGroups) {
@@ -34,4 +55,81 @@ public class Config extends CollectionMap {
         source.save(this);
     }
 
+    public final static class Builder {
+
+        private static final Logger LOGGER = LogManager.getLogger();
+
+        private final String modID;
+        private String[] branch = new String[0];
+        private final List<ConfigGroup> topLevelGroups = new ArrayList<>();
+        private TypeSerializerCollection typeSerializers;
+        private GuiBuilder guiBuilder;
+
+        private Builder(String modID) {
+            this.modID = Objects.requireNonNull(modID);
+        }
+
+        /**
+         * Sets the branch.
+         *
+         * <p>The branch determines the location of the config's save file.
+         *
+         * @param branch the branch
+         * @return this builder
+         */
+        public Builder setBranch(String[] branch) {
+            this.branch = Objects.requireNonNull(branch);
+            return this;
+        }
+
+        /**
+         * Adds one or more top-level groups to the config.
+         *
+         * @param groups one or more top-level groups
+         * @return this builder
+         */
+        public Builder add(ConfigGroup... groups) {
+            Arrays.stream(groups).forEach(Objects::requireNonNull);
+            topLevelGroups.addAll(Arrays.asList(groups));
+            return this;
+        }
+
+        /**
+         * Registers custom type serializers, applied only to this config.
+         *
+         * @param typeSerializers the type serializers
+         * @return this builder
+         */
+        public Builder registerTypeSerializers(TypeSerializerCollection typeSerializers) {
+            this.typeSerializers = TypeUtils.mergeSerializers(this.typeSerializers, Objects.requireNonNull(typeSerializers));
+            return this;
+        }
+
+        /**
+         * Sets a custom client GUI builder for the config.
+         *
+         * @param guiBuilder a GUI builder
+         * @return this builder
+         */
+        @Environment(EnvType.CLIENT)
+        public Builder setGuiBuilder(GuiBuilder guiBuilder) {
+            this.guiBuilder = Objects.requireNonNull(guiBuilder);
+            return this;
+        }
+
+        /**
+         * Completes the config creation.
+         *
+         * @return the handler associated with the created config
+         */
+        public ConfigHandler build() {
+            if (topLevelGroups.isEmpty()) {
+                LOGGER.warn("[CompleteConfig] Mod " + modID + " tried to create an empty config!");
+                return null;
+            }
+            return new ConfigHandler(new Config(new ConfigSource(ModController.of(modID), branch, typeSerializers), topLevelGroups), guiBuilder);
+        }
+
+    }
+
 }