From 558b120e2d4924a84860c3f84415ab573040a293 Mon Sep 17 00:00:00 2001 From: isXander Date: Sat, 12 Nov 2022 11:05:16 +0000 Subject: REAL config library --- .../dev/isxander/yacl/config/ConfigInstance.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/dev/isxander/yacl/config/ConfigInstance.java (limited to 'src/main/java/dev/isxander/yacl/config/ConfigInstance.java') diff --git a/src/main/java/dev/isxander/yacl/config/ConfigInstance.java b/src/main/java/dev/isxander/yacl/config/ConfigInstance.java new file mode 100644 index 0000000..e85a645 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/config/ConfigInstance.java @@ -0,0 +1,48 @@ +package dev.isxander.yacl.config; + +import dev.isxander.yacl.api.YetAnotherConfigLib; + +import java.lang.reflect.InvocationTargetException; +import java.util.function.BiFunction; + +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 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; + } + + public YetAnotherConfigLib buildConfig(BiFunction, YetAnotherConfigLib.Builder, YetAnotherConfigLib.Builder> builder) { + return builder.apply(this, YetAnotherConfigLib.createBuilder()) + .save(this::save) + .build(); + } + + public abstract void save(); + public abstract void load(); +} -- cgit