Ver código fonte

Make Config class more flexible and remove add method

Lortseam 4 anos atrás
pai
commit
06b0d7b20e

+ 1 - 0
example/src/main/java/me/lortseam/completeconfig/example/ExampleMod.java

@@ -14,6 +14,7 @@ public class ExampleMod implements ModInitializer {
     @Override
     public void onInitialize() {
         settings = new Settings();
+        settings.load();
     }
 
 }

+ 1 - 3
example/src/main/java/me/lortseam/completeconfig/example/Settings.java

@@ -13,9 +13,7 @@ import java.util.List;
 public final class Settings extends Config implements ConfigContainer {
 
     Settings() {
-        super(ExampleMod.MOD_ID, false);
-        add(this);
-        load();
+        super(ExampleMod.MOD_ID);
     }
 
     @Transitive

+ 4 - 4
lib/src/main/java/me/lortseam/completeconfig/data/BaseCollection.java

@@ -20,11 +20,11 @@ abstract class BaseCollection implements ParentDataPart, Translatable {
     private final EntrySet entries = new EntrySet(this);
     private final CollectionSet collections = new CollectionSet(this);
 
-    public java.util.Collection<Entry> getEntries() {
+    public final java.util.Collection<Entry> getEntries() {
         return Collections.unmodifiableCollection(entries);
     }
 
-    public java.util.Collection<Collection> getCollections() {
+    public final java.util.Collection<Collection> getCollections() {
         return Collections.unmodifiableCollection(collections);
     }
 
@@ -84,11 +84,11 @@ abstract class BaseCollection implements ParentDataPart, Translatable {
     }
 
     @Override
-    public Iterable<DataPart> getChildren() {
+    public final Iterable<DataPart> getChildren() {
         return Iterables.concat(entries, collections);
     }
 
-    boolean isEmpty() {
+    final boolean isEmpty() {
         return Iterables.size(getChildren()) == 0;
     }
 

+ 28 - 30
lib/src/main/java/me/lortseam/completeconfig/data/Config.java

@@ -47,9 +47,15 @@ public class Config extends BaseCollection {
      * @param modId the ID of the mod creating the config
      * @param branch the branch
      */
-    public Config(String modId, String[] branch) {
+    public Config(String modId, String[] branch, ConfigContainer... containers) {
         source = new ConfigSource(modId, branch);
         ConfigRegistry.register(this);
+        if (containers.length > 0) {
+            resolve(containers);
+            if (isEmpty()) {
+                throw new IllegalArgumentException("Config of " + source + " is empty");
+            }
+        }
     }
 
     /**
@@ -57,21 +63,21 @@ public class Config extends BaseCollection {
      *
      * @param modId the ID of the mod creating the config
      */
-    public Config(String modId) {
-        this(modId, new String[0]);
+    public Config(String modId, ConfigContainer... containers) {
+        this(modId, new String[0], containers);
     }
 
-    public ModMetadata getMod() {
+    public final ModMetadata getMod() {
         return FabricLoader.getInstance().getModContainer(source.getModId()).get().getMetadata();
     }
 
     @Override
-    public TranslationKey getTranslation() {
+    public final TranslationKey getTranslation() {
         return getTranslation(false);
     }
 
     @Environment(EnvType.CLIENT)
-    public TranslationKey getTranslation(boolean includeBranch) {
+    public final TranslationKey getTranslation(boolean includeBranch) {
         if (translation == null) {
             translation = TranslationKey.from(this);
         }
@@ -81,38 +87,31 @@ public class Config extends BaseCollection {
         return translation;
     }
 
-    /**
-     * Adds one or more containers to the config.
-     *
-     * @param containers one or more containers
-     * @return this config
-     */
-    public Config add(ConfigContainer... containers) {
-        if (loaded) {
-            throw new IllegalStateException("Cannot add container(s) after config was loaded already");
-        }
-        resolve(containers);
-        return this;
-    }
-
     /**
      * Loads the config.
-     *
-     * @return this config
      */
-    public Config load() {
-        if(!isEmpty()) {
-            source.load(this);
+    public void load() {
+        if (isEmpty()) {
+            if (this instanceof ConfigContainer) {
+                resolve((ConfigContainer) this);
+                if (isEmpty()) {
+                    throw new IllegalStateException("Config " + getClass() + " is empty");
+                }
+            } else {
+                throw new IllegalStateException("Config " + getClass() + " must either implement " + ConfigContainer.class.getSimpleName() + " or pass at least one container to the constructor");
+            }
         }
+        source.load(this);
         loaded = true;
-        return this;
     }
 
     /**
      * Saves the config.
      */
     public void save() {
-        if(isEmpty()) return;
+        if (!loaded) {
+            throw new IllegalStateException("Cannot save config before it was loaded");
+        }
         source.save(this);
     }
 
@@ -149,8 +148,6 @@ public class Config extends BaseCollection {
          *
          * @param containers one or more containers
          * @return this builder
-         *
-         * @deprecated Use {@link Config#add(ConfigContainer...)}
          */
         @Deprecated
         public Builder add(@NonNull ConfigContainer... containers) {
@@ -184,7 +181,8 @@ public class Config extends BaseCollection {
          */
         @Deprecated
         public Config build() {
-            Config config = new Config(modId, branch).add(children.toArray(new ConfigContainer[0])).load();
+            Config config = new Config(modId, branch, children.toArray(new ConfigContainer[0]));
+            config.load();
             if (main) {
                 ConfigRegistry.setMainConfig(config);
             }

+ 10 - 10
lib/src/main/java/me/lortseam/completeconfig/io/ConfigSource.java

@@ -6,7 +6,7 @@ import lombok.NonNull;
 import lombok.ToString;
 import lombok.extern.log4j.Log4j2;
 import me.lortseam.completeconfig.CompleteConfig;
-import me.lortseam.completeconfig.data.Config;
+import me.lortseam.completeconfig.data.structure.DataPart;
 import me.lortseam.completeconfig.extensions.CompleteConfigExtension;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.loader.api.FabricLoader;
@@ -69,23 +69,23 @@ public final class ConfigSource {
                 .build();
     }
 
-    public void load(Config config) {
+    public void load(DataPart data) {
         try {
-            CommentedConfigurationNode root = loader.load();
-            if (!root.isNull()) {
-                config.apply(root);
+            CommentedConfigurationNode rootNode = loader.load();
+            if (!rootNode.isNull()) {
+                data.apply(rootNode);
             }
         } catch (ConfigurateException e) {
             logger.error("Failed to load config from file", e);
         }
-        save(config);
+        save(data);
     }
 
-    public void save(Config config) {
-        CommentedConfigurationNode root = loader.createNode();
-        config.fetch(root);
+    public void save(DataPart data) {
+        CommentedConfigurationNode rootNode = loader.createNode();
+        data.fetch(rootNode);
         try {
-            loader.save(root);
+            loader.save(rootNode);
         } catch (ConfigurateException e) {
             logger.error("Failed to save config to file", e);
         }