aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/dev/isxander/yacl/config/ConfigInstance.java
diff options
context:
space:
mode:
authorXander <xander@isxander.dev>2023-04-25 16:28:41 +0100
committerGitHub <noreply@github.com>2023-04-25 16:28:41 +0100
commit13c7ba45ff201423eb8dba8a40cfb66ebb531439 (patch)
tree1e799aa9da11fbc0833bc6b7c6e6799633c2ec50 /common/src/main/java/dev/isxander/yacl/config/ConfigInstance.java
parent8ba7196ae990fe9aa98680aba1b387e385fff99c (diff)
downloadYetAnotherConfigLib-13c7ba45ff201423eb8dba8a40cfb66ebb531439.tar.gz
YetAnotherConfigLib-13c7ba45ff201423eb8dba8a40cfb66ebb531439.tar.bz2
YetAnotherConfigLib-13c7ba45ff201423eb8dba8a40cfb66ebb531439.zip
Architectury! (#61)
Diffstat (limited to 'common/src/main/java/dev/isxander/yacl/config/ConfigInstance.java')
-rw-r--r--common/src/main/java/dev/isxander/yacl/config/ConfigInstance.java48
1 files changed, 48 insertions, 0 deletions
diff --git a/common/src/main/java/dev/isxander/yacl/config/ConfigInstance.java b/common/src/main/java/dev/isxander/yacl/config/ConfigInstance.java
new file mode 100644
index 0000000..c207161
--- /dev/null
+++ b/common/src/main/java/dev/isxander/yacl/config/ConfigInstance.java
@@ -0,0 +1,48 @@
+package dev.isxander.yacl.config;
+
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ * Responsible for handing the actual config data type.
+ * Holds the instance along with a final default instance
+ * to reference default values for options and should not be changed.
+ *
+ * Abstract methods to save and load the class, implementations are responsible for
+ * how it saves and load.
+ *
+ * @param <T> config data type
+ */
+public abstract class ConfigInstance<T> {
+ private final Class<T> configClass;
+ private final T defaultInstance;
+ private T instance;
+
+ public ConfigInstance(Class<T> configClass) {
+ this.configClass = configClass;
+
+ try {
+ this.defaultInstance = this.instance = configClass.getConstructor().newInstance();
+ } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
+ throw new IllegalStateException(String.format("Could not create default instance of config for %s. Make sure there is a default constructor!", this.configClass.getSimpleName()));
+ }
+ }
+
+ public abstract void save();
+ public abstract void load();
+
+ public T getConfig() {
+ return this.instance;
+ }
+
+ protected void setConfig(T instance) {
+ this.instance = instance;
+ }
+
+ public T getDefaults() {
+ return this.defaultInstance;
+ }
+
+ public Class<T> getConfigClass() {
+ return this.configClass;
+ }
+}