diff options
author | isXander <xandersmith2008@gmail.com> | 2022-11-13 20:13:39 +0000 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2022-11-13 20:13:39 +0000 |
commit | 29770a651daa38a18b99eecd5ecf90cf90ceeb0f (patch) | |
tree | 3aa03fb50e98eccc5c6422c35c575b7bb3541e9e /src/main | |
parent | 6f3dc40a6bc554b6decb685cb6d1eb6370b1eea6 (diff) | |
download | YetAnotherConfigLib-29770a651daa38a18b99eecd5ecf90cf90ceeb0f.tar.gz YetAnotherConfigLib-29770a651daa38a18b99eecd5ecf90cf90ceeb0f.tar.bz2 YetAnotherConfigLib-29770a651daa38a18b99eecd5ecf90cf90ceeb0f.zip |
changelog & remove YACLConfigManager.java as it was actually pretty useless, document GsonConfigInstance as well
Diffstat (limited to 'src/main')
3 files changed, 14 insertions, 63 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); - } -} |