diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-04-26 20:35:35 +0200 |
---|---|---|
committer | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-04-26 20:35:35 +0200 |
commit | aad5111d45a66d943f0bcbe885ccb28249441d11 (patch) | |
tree | 94bcedd85c1725a7a813185b0a931ffc3eb215fd /src/main/java/io/polyfrost/oneconfig/config | |
parent | 5eb52cdd6ce2e65be7eefecfea7cfdb63404f481 (diff) | |
download | OneConfig-aad5111d45a66d943f0bcbe885ccb28249441d11.tar.gz OneConfig-aad5111d45a66d943f0bcbe885ccb28249441d11.tar.bz2 OneConfig-aad5111d45a66d943f0bcbe885ccb28249441d11.zip |
config system rewrite
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/config')
16 files changed, 124 insertions, 218 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/config/OneConfigConfig.java b/src/main/java/io/polyfrost/oneconfig/config/OneConfigConfig.java index bfb123a..f10111c 100644 --- a/src/main/java/io/polyfrost/oneconfig/config/OneConfigConfig.java +++ b/src/main/java/io/polyfrost/oneconfig/config/OneConfigConfig.java @@ -1,7 +1,7 @@ package io.polyfrost.oneconfig.config; import com.google.gson.JsonParser; -import io.polyfrost.oneconfig.config.data.ModData; +import io.polyfrost.oneconfig.config.data.Mod; import io.polyfrost.oneconfig.config.interfaces.Config; import java.awt.*; @@ -48,7 +48,7 @@ public class OneConfigConfig extends Config { } @Override - public void init(ModData modData) { + public void init(Mod mod) { if (new File("OneConfig/" + configFile).exists()) load(); else save(); } diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/Button.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/Button.java deleted file mode 100644 index 3ba1078..0000000 --- a/src/main/java/io/polyfrost/oneconfig/config/annotations/Button.java +++ /dev/null @@ -1,18 +0,0 @@ -package io.polyfrost.oneconfig.config.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -public @interface Button { - String name(); - - String description() default ""; - - String text() default "Button"; - - int size() default 1; -} diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/Category.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/Category.java deleted file mode 100644 index 83b7af4..0000000 --- a/src/main/java/io/polyfrost/oneconfig/config/annotations/Category.java +++ /dev/null @@ -1,16 +0,0 @@ -package io.polyfrost.oneconfig.config.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -public @interface Category { - String name(); - - String description() default ""; - - int size() default 1; -} diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/ColorPicker.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/ColorPicker.java deleted file mode 100644 index 6854bc2..0000000 --- a/src/main/java/io/polyfrost/oneconfig/config/annotations/ColorPicker.java +++ /dev/null @@ -1,18 +0,0 @@ -package io.polyfrost.oneconfig.config.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -public @interface ColorPicker { - String name(); - - String description() default ""; - - boolean allowAlpha() default true; - - int size() default 1; -} diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/HudComponent.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/HudComponent.java deleted file mode 100644 index 00a2e74..0000000 --- a/src/main/java/io/polyfrost/oneconfig/config/annotations/HudComponent.java +++ /dev/null @@ -1,16 +0,0 @@ -package io.polyfrost.oneconfig.config.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -public @interface HudComponent { - String name(); - - String description() default ""; - - int size() default 1; -} diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/Option.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/Option.java new file mode 100644 index 0000000..8abb303 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/config/annotations/Option.java @@ -0,0 +1,42 @@ +package io.polyfrost.oneconfig.config.annotations; + +import io.polyfrost.oneconfig.config.data.OptionType; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Option { + /** + * The name of the page that will be displayed to the user + */ + String name(); + + /** + * The description of the page that will be displayed to the user + */ + String description() default ""; + + /** + * The type of the option + */ + OptionType type(); + + /** + * The category of the component + */ + String category() default "general"; + + /** + * The subcategory of the component (displayed as header) + */ + String subcategory(); + + /** + * The width of the option (1 = half width, 2 = full width) + */ + int size() default 1; +} diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/Selector.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/Selector.java deleted file mode 100644 index 00d88d8..0000000 --- a/src/main/java/io/polyfrost/oneconfig/config/annotations/Selector.java +++ /dev/null @@ -1,20 +0,0 @@ -package io.polyfrost.oneconfig.config.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -public @interface Selector { - String name(); - - String description() default ""; - - String[] options(); - - int defaultSelection() default 0; - - int size() default 1; -} diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/Slider.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/Slider.java deleted file mode 100644 index ea19da5..0000000 --- a/src/main/java/io/polyfrost/oneconfig/config/annotations/Slider.java +++ /dev/null @@ -1,22 +0,0 @@ -package io.polyfrost.oneconfig.config.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -public @interface Slider { - String name(); - - String description() default ""; - - float min(); - - float max(); - - float precision(); - - int size() default 1; -} diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/Switch.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/Switch.java deleted file mode 100644 index 8ff2f11..0000000 --- a/src/main/java/io/polyfrost/oneconfig/config/annotations/Switch.java +++ /dev/null @@ -1,16 +0,0 @@ -package io.polyfrost.oneconfig.config.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -public @interface Switch { - String name(); - - String description() default ""; - - int size() default 1; -} diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/TextField.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/TextField.java deleted file mode 100644 index 0df1426..0000000 --- a/src/main/java/io/polyfrost/oneconfig/config/annotations/TextField.java +++ /dev/null @@ -1,20 +0,0 @@ -package io.polyfrost.oneconfig.config.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -public @interface TextField { - String name(); - - String description() default ""; - - String placeholder() default ""; - - boolean hideText() default false; - - int size() default 1; -} diff --git a/src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java b/src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java index 9bb2e85..9be25e9 100644 --- a/src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java +++ b/src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java @@ -1,26 +1,24 @@ package io.polyfrost.oneconfig.config.core; -import io.polyfrost.oneconfig.config.data.ModData; -import io.polyfrost.oneconfig.config.interfaces.Option; +import io.polyfrost.oneconfig.config.data.Mod; import io.polyfrost.oneconfig.hud.HudCore; import java.util.ArrayList; -import java.util.HashMap; public class ConfigCore { - public static HashMap<ModData, ArrayList<Option>> settings = new HashMap<>(); + public static ArrayList<Mod> OneConfigMods = new ArrayList<>(); public static void saveAll() { - for (ModData modData : settings.keySet()) { + for (Mod modData : OneConfigMods) { modData.config.save(); } } public static void reInitAll() { - ArrayList<ModData> data = new ArrayList<>(settings.keySet()); - settings.clear(); + ArrayList<Mod> data = new ArrayList<>(OneConfigMods); + OneConfigMods.clear(); HudCore.huds.clear(); - for (ModData modData : data) { + for (Mod modData : data) { modData.config.init(modData); } } diff --git a/src/main/java/io/polyfrost/oneconfig/config/data/ModData.java b/src/main/java/io/polyfrost/oneconfig/config/data/Mod.java index c5e6633..435fb32 100644 --- a/src/main/java/io/polyfrost/oneconfig/config/data/ModData.java +++ b/src/main/java/io/polyfrost/oneconfig/config/data/Mod.java @@ -2,12 +2,13 @@ package io.polyfrost.oneconfig.config.data; import io.polyfrost.oneconfig.config.interfaces.Config; -public class ModData { +public class Mod { public final String name; public final ModType modType; public final String creator; public final String version; public Config config; + public OptionPage defaultPage = new OptionPage(""); /** * @param name Friendly name of the mod @@ -15,7 +16,7 @@ public class ModData { * @param creator Creator of the mod * @param version Version of the mod */ - public ModData(String name, ModType modType, String creator, String version) { + public Mod(String name, ModType modType, String creator, String version) { this.name = name; this.modType = modType; this.creator = creator; diff --git a/src/main/java/io/polyfrost/oneconfig/config/data/OptionPage.java b/src/main/java/io/polyfrost/oneconfig/config/data/OptionPage.java new file mode 100644 index 0000000..021786d --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/config/data/OptionPage.java @@ -0,0 +1,20 @@ +package io.polyfrost.oneconfig.config.data; + +import io.polyfrost.oneconfig.config.interfaces.BasicOption; + +import java.util.ArrayList; +import java.util.HashMap; + +public class OptionPage { + public final String name; + /** + * Depth 1 = categories + * Depth 2 = subcategories + * Depth 3 = list of options + */ + public final HashMap<String, HashMap<String, ArrayList<BasicOption>>> categories = new HashMap<>(); + + public OptionPage(String name) { + this.name = name; + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/config/data/OptionType.java b/src/main/java/io/polyfrost/oneconfig/config/data/OptionType.java new file mode 100644 index 0000000..22c92c0 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/config/data/OptionType.java @@ -0,0 +1,6 @@ +package io.polyfrost.oneconfig.config.data; + +public enum OptionType { + PAGE, + SWITCH +} diff --git a/src/main/java/io/polyfrost/oneconfig/config/interfaces/Option.java b/src/main/java/io/polyfrost/oneconfig/config/interfaces/BasicOption.java index bd9c034..42c826e 100644 --- a/src/main/java/io/polyfrost/oneconfig/config/interfaces/Option.java +++ b/src/main/java/io/polyfrost/oneconfig/config/interfaces/BasicOption.java @@ -3,7 +3,7 @@ package io.polyfrost.oneconfig.config.interfaces; import java.lang.reflect.Field; @SuppressWarnings({"unused"}) -public abstract class Option { +public abstract class BasicOption { protected final Field field; protected final String name; protected final String description; @@ -17,7 +17,7 @@ public abstract class Option { * @param description description of option * @param size size of option, 0 for single column, 1 for double. */ - public Option(Field field, String name, String description, int size) { + public BasicOption(Field field, String name, String description, int size) { this.field = field; this.name = name; this.description = description; diff --git a/src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java b/src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java index f0efcb0..c68162e 100644 --- a/src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java +++ b/src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java @@ -1,23 +1,20 @@ package io.polyfrost.oneconfig.config.interfaces; import com.google.gson.*; -import io.polyfrost.oneconfig.config.annotations.*; -import io.polyfrost.oneconfig.config.core.ConfigCore; -import io.polyfrost.oneconfig.config.data.ModData; +import io.polyfrost.oneconfig.config.annotations.Option; +import io.polyfrost.oneconfig.config.data.Mod; +import io.polyfrost.oneconfig.config.data.OptionPage; import io.polyfrost.oneconfig.config.profiles.Profiles; -import io.polyfrost.oneconfig.gui.elements.config.*; -import io.polyfrost.oneconfig.hud.HudCore; -import io.polyfrost.oneconfig.hud.interfaces.BasicHud; +import io.polyfrost.oneconfig.gui.elements.config.ConfigPage; +import io.polyfrost.oneconfig.gui.elements.config.ConfigSwitch; +import io.polyfrost.oneconfig.test.TestConfig; import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Map; -import java.util.Optional; +import java.util.*; public class Config { protected final String configFile; @@ -27,16 +24,16 @@ public class Config { * @param modData information about the mod * @param configFile file where config is stored */ - public Config(ModData modData, String configFile) { + public Config(Mod modData, String configFile) { this.configFile = configFile; init(modData); } - public void init(ModData modData) { + public void init(Mod mod) { if (Profiles.getProfileFile(configFile).exists()) load(); else save(); - modData.config = this; - ConfigCore.settings.put(modData, generateOptionList(this.getClass())); + mod.config = this; + generateOptionList(this.getClass(), mod.defaultPage); } /** @@ -62,63 +59,51 @@ public class Config { } /** - * Generate the option list for internal use only + * Generate the option list, for internal use only * * @param clazz target class - * @return list of options + * @param page page to add options too */ - protected ArrayList<Option> generateOptionList(Class<?> clazz) { - ArrayList<Option> options = new ArrayList<>(); - for (Class<?> innerClass : clazz.getClasses()) { - if (innerClass.isAnnotationPresent(Category.class)) { - Category category = innerClass.getAnnotation(Category.class); - options.add(new OConfigCategory(category.name(), category.description(), generateOptionList(innerClass), category.size())); + protected void generateOptionList(Class<?> clazz, OptionPage page) { + for (Field field : clazz.getDeclaredFields()) { + System.out.println(field); + if (!field.isAnnotationPresent(Option.class)) { + processCustomOption(field, page); + continue; } - } - for (Field field : clazz.getFields()) { - if (field.isAnnotationPresent(Button.class)) { - Button button = field.getAnnotation(Button.class); - options.add(new OConfigButton(field, button.name(), button.description(), button.text(), button.size())); - } else if (field.isAnnotationPresent(ColorPicker.class)) { - ColorPicker colorPicker = field.getAnnotation(ColorPicker.class); - options.add(new OConfigColor(field, colorPicker.name(), colorPicker.description(), colorPicker.allowAlpha(), colorPicker.size())); - } else if (field.isAnnotationPresent(Selector.class)) { - Selector selector = field.getAnnotation(Selector.class); - options.add(new OConfigSelector(field, selector.name(), selector.description(), selector.options(), selector.defaultSelection(), selector.size())); - } else if (field.isAnnotationPresent(Slider.class)) { - Slider slider = field.getAnnotation(Slider.class); - options.add(new OConfigSlider(field, slider.name(), slider.description(), slider.min(), slider.max(), slider.precision(), slider.size())); - } else if (field.isAnnotationPresent(Switch.class)) { - Switch aSwitch = field.getAnnotation(Switch.class); - options.add(new OConfigSwitch(field, aSwitch.name(), aSwitch.description(), aSwitch.size())); - } else if (field.isAnnotationPresent(TextField.class)) { - TextField textField = field.getAnnotation(TextField.class); - options.add(new OConfigText(field, textField.name(), textField.description(), textField.placeholder(), textField.hideText(), textField.size())); - } else if (field.isAnnotationPresent(HudComponent.class)) { - HudComponent hudComponent = field.getAnnotation(HudComponent.class); - options.add(new OConfigHud(field, hudComponent.name(), hudComponent.description(), hudComponent.size())); - try { - Object hud = field.get(BasicHud.class); - HudCore.huds.add((BasicHud) hud); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } else { - Option customOption = processCustomOption(field); - if (customOption != null) options.add(customOption); + Option option = field.getAnnotation(Option.class); + if (!page.categories.containsKey(option.category())) + page.categories.put(option.category(), new HashMap<>()); + if (!page.categories.get(option.category()).containsKey(option.subcategory())) + page.categories.get(option.category()).put(option.subcategory(), new ArrayList<>()); + ArrayList<BasicOption> options = page.categories.get(option.category()).get(option.subcategory()); + switch (option.type()) { + case PAGE: + OptionPage newPage = new OptionPage(option.name()); + try { + field.setAccessible(true); + Object object = field.get(clazz); + generateOptionList(object.getClass(), newPage); + System.out.println(newPage.categories); + options.add(new ConfigPage(field, option.name(), option.description(), option.size(), newPage)); + } catch (IllegalAccessException e) { + continue; + } + break; + case SWITCH: + options.add(new ConfigSwitch(field, option.name(), option.description(), option.size())); + break; } } - return options; } /** * Overwrite this method to add your own custom option types * * @param field target field - * @return custom option + * @param page page to add options too */ - protected Option processCustomOption(Field field) { - return null; + protected void processCustomOption(Field field, OptionPage page) { } /** |