aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/dev/isxander/yacl/config/ConfigInstance.java4
-rw-r--r--src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java13
-rw-r--r--src/main/java/dev/isxander/yacl/config/YACLConfigManager.java60
-rw-r--r--src/testmod/java/dev/isxander/yacl/test/config/Entrypoint.java8
4 files changed, 19 insertions, 66 deletions
diff --git a/src/main/java/dev/isxander/yacl/config/ConfigInstance.java b/src/main/java/dev/isxander/yacl/config/ConfigInstance.java
index 5271ea2..a7c013c 100644
--- a/src/main/java/dev/isxander/yacl/config/ConfigInstance.java
+++ b/src/main/java/dev/isxander/yacl/config/ConfigInstance.java
@@ -47,9 +47,7 @@ public abstract class ConfigInstance<T> {
}
public YetAnotherConfigLib buildConfig(BiFunction<ConfigInstance<T>, YetAnotherConfigLib.Builder, YetAnotherConfigLib.Builder> builder) {
- return builder.apply(this, YetAnotherConfigLib.createBuilder())
- .save(this::save)
- .build();
+ return builder.apply(this, YetAnotherConfigLib.createBuilder().save(this::save)).build();
}
public abstract void save();
diff --git a/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java b/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java
index 0f9311a..6f517cc 100644
--- a/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java
+++ b/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java
@@ -14,10 +14,23 @@ import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.function.UnaryOperator;
+/**
+ * Uses GSON to serialize and deserialize config data from JSON to a file.
+ *
+ * You can exclude fields from serialization by marking them as transient.
+ * {@link Text}, {@link Style} and {@link Color} have default type adapters, so there is no need to provide them in your GSON instance.
+ * GSON is automatically configured to format fields as {@code lower_camel_case}.
+ *
+ * @param <T> config data type
+ */
public class GsonConfigInstance<T> extends ConfigInstance<T> {
private final Gson gson;
private final Path path;
+ public GsonConfigInstance(Class<T> configClass, Path path) {
+ this(configClass, path, new GsonBuilder());
+ }
+
public GsonConfigInstance(Class<T> configClass, Path path, Gson gson) {
this(configClass, path, gson.newBuilder());
}
diff --git a/src/main/java/dev/isxander/yacl/config/YACLConfigManager.java b/src/main/java/dev/isxander/yacl/config/YACLConfigManager.java
deleted file mode 100644
index a312d03..0000000
--- a/src/main/java/dev/isxander/yacl/config/YACLConfigManager.java
+++ /dev/null
@@ -1,60 +0,0 @@
-package dev.isxander.yacl.config;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Simple storage of config instances ({@link ConfigInstance}) for ease of access.
- */
-@SuppressWarnings("unchecked")
-public class YACLConfigManager {
- private static final Map<Class<?>, ConfigInstance<?>> configs = new HashMap<>();
-
- /**
- * Registers and loads a config instance
- *
- * @param configInstance config to register
- * @param <T> config data type
- */
- public static <T> void register(ConfigInstance<T> configInstance) {
- configs.put(configInstance.getConfigClass(), configInstance);
- configInstance.load();
- }
-
- /**
- * Retrieves config data for a certain config.
- * <p>
- * Shorthand of {@code YACLConfigManager.getConfigInstance(configClass).getConfig()}
- *
- * @param configClass config data to get
- * @return config data
- * @param <T> config data type
- */
- public static <T> T getConfigData(Class<T> configClass) {
- return ((ConfigInstance<T>) configs.get(configClass)).getConfig();
- }
-
- /**
- * Retrieves the config instance for a certain config.
- *
- * @param configClass config data type instance is bound to
- * @return config instance
- * @param <T> config data type
- */
- public static <T> ConfigInstance<T> getConfigInstance(Class<T> configClass) {
- return (ConfigInstance<T>) configs.get(configClass);
- }
-
- /**
- * Very similar to {@link YACLConfigManager#getConfigInstance(Class)} but can retrieve
- * a certain implementation of {@link ConfigInstance}
- *
- * @param configClass config data type is bound to
- * @return config instance
- * @param <T> config data type
- * @param <U> config instance type
- */
- public static <T, U extends ConfigInstance<T>> U getConfigInstanceType(Class<T> configClass) {
- return (U) configs.get(configClass);
- }
-}
diff --git a/src/testmod/java/dev/isxander/yacl/test/config/Entrypoint.java b/src/testmod/java/dev/isxander/yacl/test/config/Entrypoint.java
index 7875700..39baffa 100644
--- a/src/testmod/java/dev/isxander/yacl/test/config/Entrypoint.java
+++ b/src/testmod/java/dev/isxander/yacl/test/config/Entrypoint.java
@@ -3,17 +3,19 @@ package dev.isxander.yacl.test.config;
import com.google.gson.Gson;
import dev.isxander.yacl.config.ConfigInstance;
import dev.isxander.yacl.config.GsonConfigInstance;
-import dev.isxander.yacl.config.YACLConfigManager;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.loader.api.FabricLoader;
public class Entrypoint implements ClientModInitializer {
+ private static GsonConfigInstance<ConfigData> config;
+
@Override
public void onInitializeClient() {
- YACLConfigManager.register(new GsonConfigInstance<>(ConfigData.class, FabricLoader.getInstance().getConfigDir().resolve("yacl-test.json"), new Gson()));
+ config = new GsonConfigInstance<>(ConfigData.class, FabricLoader.getInstance().getConfigDir().resolve("yacl-test.json"));
+ config.load();
}
public static ConfigInstance<ConfigData> getConfig() {
- return YACLConfigManager.getConfigInstance(ConfigData.class);
+ return config;
}
}