Преглед на файлове

Add save on exit option to config builder

Lortseam преди 4 години
родител
ревизия
78303b78b5
променени са 1 файла, в които са добавени 11 реда и са изтрити 2 реда
  1. 11 2
      lib/src/main/java/me/lortseam/completeconfig/data/Config.java

+ 11 - 2
lib/src/main/java/me/lortseam/completeconfig/data/Config.java

@@ -17,6 +17,7 @@ public class Config extends Node {
     static {
     static {
         Runtime.getRuntime().addShutdownHook(new Thread(() -> {
         Runtime.getRuntime().addShutdownHook(new Thread(() -> {
             for (Config config : configs) {
             for (Config config : configs) {
+                if(!config.saveOnExit) continue;
                 config.save();
                 config.save();
             }
             }
         }));
         }));
@@ -35,10 +36,12 @@ public class Config extends Node {
     }
     }
 
 
     private final ConfigSource source;
     private final ConfigSource source;
+    private final boolean saveOnExit;
 
 
-    private Config(ConfigSource source, LinkedHashSet<ConfigContainer> children) {
+    private Config(ConfigSource source, LinkedHashSet<ConfigContainer> children, boolean saveOnExit) {
         super(TranslationIdentifier.ofRoot(source.getModID()));
         super(TranslationIdentifier.ofRoot(source.getModID()));
         this.source = source;
         this.source = source;
+        this.saveOnExit = saveOnExit;
         resolve(children);
         resolve(children);
         if (isEmpty()) {
         if (isEmpty()) {
             logger.warn("[CompleteConfig] Config of " + source + " is empty!");
             logger.warn("[CompleteConfig] Config of " + source + " is empty!");
@@ -66,6 +69,7 @@ public class Config extends Node {
         private final String modID;
         private final String modID;
         private String[] branch = new String[0];
         private String[] branch = new String[0];
         private final LinkedHashSet<ConfigContainer> children = new LinkedHashSet<>();
         private final LinkedHashSet<ConfigContainer> children = new LinkedHashSet<>();
+        private boolean saveOnExit = true;
 
 
         private Builder(String modID) {
         private Builder(String modID) {
             this.modID = modID;
             this.modID = modID;
@@ -103,6 +107,11 @@ public class Config extends Node {
             return this;
             return this;
         }
         }
 
 
+        public Builder setSaveOnExit(boolean saveOnExit) {
+            this.saveOnExit = saveOnExit;
+            return this;
+        }
+
         /**
         /**
          * Completes the config creation.
          * Completes the config creation.
          *
          *
@@ -113,7 +122,7 @@ public class Config extends Node {
                 logger.warn("[CompleteConfig] Mod " + modID + " tried to create an empty config!");
                 logger.warn("[CompleteConfig] Mod " + modID + " tried to create an empty config!");
                 return null;
                 return null;
             }
             }
-            return new Config(new ConfigSource(modID, branch), children);
+            return new Config(new ConfigSource(modID, branch), children, saveOnExit);
         }
         }
 
 
     }
     }