aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/dev/isxander/yacl/config/ConfigInstance.java48
-rw-r--r--src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java82
-rw-r--r--src/main/java/dev/isxander/yacl/config/YACLConfigManager.java26
-rw-r--r--src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java2
-rw-r--r--src/main/java/dev/isxander/yacl/impl/utils/YaclConstants.java8
5 files changed, 166 insertions, 0 deletions
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<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 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;
+ }
+
+ public YetAnotherConfigLib buildConfig(BiFunction<ConfigInstance<T>, YetAnotherConfigLib.Builder, YetAnotherConfigLib.Builder> builder) {
+ return builder.apply(this, YetAnotherConfigLib.createBuilder())
+ .save(this::save)
+ .build();
+ }
+
+ public abstract void save();
+ public abstract void load();
+}
diff --git a/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java b/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java
new file mode 100644
index 0000000..732492d
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java
@@ -0,0 +1,82 @@
+package dev.isxander.yacl.config;
+
+import com.google.gson.*;
+import dev.isxander.yacl.impl.utils.YaclConstants;
+import net.minecraft.text.Style;
+import net.minecraft.text.Text;
+
+import java.awt.*;
+import java.io.IOException;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.Type;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.util.function.UnaryOperator;
+
+public class GsonConfigInstance<T> extends ConfigInstance<T> {
+ private final Gson gson;
+ private final Path path;
+
+ public GsonConfigInstance(Class<T> configClass, Path path, Gson gson) {
+ this(configClass, path, gson.newBuilder());
+ }
+
+ public GsonConfigInstance(Class<T> configClass, Path path, UnaryOperator<GsonBuilder> builder) {
+ this(configClass, path, builder.apply(new GsonBuilder()));
+ }
+
+ public GsonConfigInstance(Class<T> configClass, Path path, GsonBuilder builder) {
+ super(configClass);
+ this.path = path;
+ this.gson = builder
+ .excludeFieldsWithModifiers(Modifier.TRANSIENT)
+ .registerTypeHierarchyAdapter(Text.class, new Text.Serializer())
+ .registerTypeHierarchyAdapter(Style.class, new Style.Serializer())
+ .registerTypeHierarchyAdapter(Color.class, new ColorTypeAdapter())
+ .serializeNulls()
+ .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
+ .create();
+ }
+
+ @Override
+ public void save() {
+ try {
+ YaclConstants.LOGGER.info("Saving {}...", getConfigClass().getSimpleName());
+ Files.writeString(path, gson.toJson(getConfig()), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void load() {
+ try {
+ if (Files.notExists(path)) {
+ save();
+ return;
+ }
+
+ YaclConstants.LOGGER.info("Loading {}...", getConfigClass().getSimpleName());
+ setConfig(gson.fromJson(Files.readString(path), getConfigClass()));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public Path getPath() {
+ return this.path;
+ }
+
+ public static class ColorTypeAdapter implements JsonSerializer<Color>, JsonDeserializer<Color> {
+ @Override
+ public Color deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
+ return new Color(jsonElement.getAsInt(), true);
+ }
+
+ @Override
+ public JsonElement serialize(Color color, Type type, JsonSerializationContext jsonSerializationContext) {
+ return new JsonPrimitive(color.getRGB());
+ }
+ }
+}
diff --git a/src/main/java/dev/isxander/yacl/config/YACLConfigManager.java b/src/main/java/dev/isxander/yacl/config/YACLConfigManager.java
new file mode 100644
index 0000000..d91f3f6
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl/config/YACLConfigManager.java
@@ -0,0 +1,26 @@
+package dev.isxander.yacl.config;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@SuppressWarnings("unchecked")
+public class YACLConfigManager {
+ private static final Map<Class<?>, ConfigInstance<?>> configs = new HashMap<>();
+
+ public static <T> void register(ConfigInstance<T> configInstance) {
+ configs.put(configInstance.getConfigClass(), configInstance);
+ configInstance.load();
+ }
+
+ public static <T> T getConfigData(Class<T> configClass) {
+ return ((ConfigInstance<T>) configs.get(configClass)).getConfig();
+ }
+
+ public static <T> ConfigInstance<T> getConfigInstance(Class<T> configClass) {
+ return (ConfigInstance<T>) configs.get(configClass);
+ }
+
+ public static <T, I extends ConfigInstance<T>> I getConfigInstanceType(Class<T> configClass) {
+ return (I) configs.get(configClass);
+ }
+}
diff --git a/src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java b/src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java
index 7e2afdb..38112f4 100644
--- a/src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java
+++ b/src/main/java/dev/isxander/yacl/impl/YetAnotherConfigLibImpl.java
@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
import dev.isxander.yacl.api.ConfigCategory;
import dev.isxander.yacl.api.YetAnotherConfigLib;
import dev.isxander.yacl.gui.YACLScreen;
+import dev.isxander.yacl.impl.utils.YaclConstants;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
@@ -12,6 +13,7 @@ import java.util.function.Consumer;
public record YetAnotherConfigLibImpl(Text title, ImmutableList<ConfigCategory> categories, Runnable saveFunction, Consumer<YACLScreen> initConsumer) implements YetAnotherConfigLib {
@Override
public Screen generateScreen(Screen parent) {
+ YaclConstants.LOGGER.info("Generating YACL screen");
return new YACLScreen(this, parent);
}
}
diff --git a/src/main/java/dev/isxander/yacl/impl/utils/YaclConstants.java b/src/main/java/dev/isxander/yacl/impl/utils/YaclConstants.java
new file mode 100644
index 0000000..aadeb18
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl/impl/utils/YaclConstants.java
@@ -0,0 +1,8 @@
+package dev.isxander.yacl.impl.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class YaclConstants {
+ public static final Logger LOGGER = LoggerFactory.getLogger("YetAnotherConfigLib");
+}