aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/config
diff options
context:
space:
mode:
authorDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-07-04 15:03:56 +0200
committerDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-07-04 15:03:56 +0200
commit5c51b22ee6ebd2b286603ff2f0dc5799963ab4c3 (patch)
tree7b3f08a451e22ec89872becc2edb433bb98e921b /src/main/java/cc/polyfrost/oneconfig/config
parent08d0986b2d6beaff2554f1b821dcefc996cb8c11 (diff)
downloadOneConfig-5c51b22ee6ebd2b286603ff2f0dc5799963ab4c3.tar.gz
OneConfig-5c51b22ee6ebd2b286603ff2f0dc5799963ab4c3.tar.bz2
OneConfig-5c51b22ee6ebd2b286603ff2f0dc5799963ab4c3.zip
reseting system
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/Config.java24
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java16
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java8
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java7
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java7
5 files changed, 51 insertions, 11 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java
index 9557728..3ca029e 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java
@@ -38,6 +38,7 @@ public class Config {
transient protected final String configFile;
transient protected final Gson gson = new GsonBuilder().setExclusionStrategies(new ProfileExclusionStrategy()).excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().create();
transient protected final Gson nonProfileSpecificGson = new GsonBuilder().setExclusionStrategies(new NonProfileSpecificExclusionStrategy()).excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().create();
+ transient protected final HashMap<Field, Object> defaults = new HashMap<>();
transient public Mod mod;
public transient boolean hasBeenInitialized = false;
public boolean enabled;
@@ -45,7 +46,7 @@ public class Config {
/**
* @param modData information about the mod
* @param configFile file where config is stored
- * @param enabled whether the mod is enabled or not
+ * @param enabled whether the mod is enabled or not
*/
public Config(Mod modData, String configFile, boolean enabled) {
this.configFile = configFile;
@@ -306,18 +307,19 @@ public class Config {
}
/**
+ * @param field The field to get the default value from
+ * @return The default value of the given field
+ */
+ public Object getDefault(Field field) {
+ return defaults.get(field);
+ }
+
+ /**
* Reset this config file to its defaults.
- * @return true if successful, false if not.
- * @deprecated <b>not implemented yet.</b>
*/
- @Deprecated
- public boolean reset() {
- try {
- // TODO
- } catch (Exception e) {
- e.printStackTrace();
- return false;
+ public void reset() {
+ for (BasicOption option : optionNames.values()) {
+ option.reset(this);
}
- return true;
}
}
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java
index 86b339b..6f9b47a 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java
@@ -1,5 +1,7 @@
package cc.polyfrost.oneconfig.config.elements;
+import cc.polyfrost.oneconfig.config.Config;
+
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.function.Supplier;
@@ -86,6 +88,20 @@ public abstract class BasicOption {
}
/**
+ * Reset the field to its default value
+ *
+ * @param config The config the field is in
+ */
+ public void reset(Config config) {
+ Object object = config.getDefault(field);
+ if (object == null) return;
+ try {
+ set(object);
+ } catch (IllegalAccessException ignored) {
+ }
+ }
+
+ /**
* @return If the option is enabled, based on the dependencies
*/
public boolean isEnabled() {
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java
index 4a37bfc..f144f60 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java
@@ -1,7 +1,15 @@
package cc.polyfrost.oneconfig.config.elements;
+import cc.polyfrost.oneconfig.config.Config;
+
import java.util.ArrayList;
public class OptionCategory {
public final ArrayList<OptionSubcategory> subcategories = new ArrayList<>();
+
+ public void reset(Config config) {
+ for (OptionSubcategory subcategory : subcategories) {
+ subcategory.reset(config);
+ }
+ }
}
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java
index 5eaac8b..916e1d9 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java
@@ -1,5 +1,6 @@
package cc.polyfrost.oneconfig.config.elements;
+import cc.polyfrost.oneconfig.config.Config;
import cc.polyfrost.oneconfig.config.data.Mod;
import java.util.LinkedHashMap;
@@ -13,4 +14,10 @@ public class OptionPage {
this.name = name;
this.mod = mod;
}
+
+ public void reset(Config config) {
+ for (OptionCategory subcategory : categories.values()) {
+ subcategory.reset(config);
+ }
+ }
}
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java
index 365ddb6..e1342d4 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java
@@ -1,5 +1,6 @@
package cc.polyfrost.oneconfig.config.elements;
+import cc.polyfrost.oneconfig.config.Config;
import cc.polyfrost.oneconfig.internal.assets.Colors;
import cc.polyfrost.oneconfig.gui.OneConfigGui;
import cc.polyfrost.oneconfig.gui.elements.config.ConfigPageButton;
@@ -104,4 +105,10 @@ public class OptionSubcategory {
public String getName() {
return name;
}
+
+ public void reset(Config config) {
+ for (BasicOption option : options) {
+ options.remove(config);
+ }
+ }
}