From 13c7ba45ff201423eb8dba8a40cfb66ebb531439 Mon Sep 17 00:00:00 2001 From: Xander Date: Tue, 25 Apr 2023 16:28:41 +0100 Subject: Architectury! (#61) --- .../dev/isxander/yacl/config/ConfigInstance.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 common/src/main/java/dev/isxander/yacl/config/ConfigInstance.java (limited to 'common/src/main/java/dev/isxander/yacl/config/ConfigInstance.java') 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 config data type + */ +public abstract class ConfigInstance { + private final Class configClass; + private final T defaultInstance; + private T instance; + + public ConfigInstance(Class 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 getConfigClass() { + return this.configClass; + } +} -- cgit