diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-02-12 19:44:15 +0100 |
---|---|---|
committer | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-02-12 19:44:15 +0100 |
commit | 78274eb2b66b27052fe32960916fa4419202dc76 (patch) | |
tree | 92412c6a835cf07dbee6dbd232c71e299740bad6 /src/main/java/io | |
parent | 56b75c981609a89639dfd3860ca890af64d2bfbe (diff) | |
download | OneConfig-78274eb2b66b27052fe32960916fa4419202dc76.tar.gz OneConfig-78274eb2b66b27052fe32960916fa4419202dc76.tar.bz2 OneConfig-78274eb2b66b27052fe32960916fa4419202dc76.zip |
finish config backend
Diffstat (limited to 'src/main/java/io')
16 files changed, 358 insertions, 22 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/annotations/Category.java b/src/main/java/io/polyfrost/oneconfig/annotations/Category.java index 05449dc..21c5533 100644 --- a/src/main/java/io/polyfrost/oneconfig/annotations/Category.java +++ b/src/main/java/io/polyfrost/oneconfig/annotations/Category.java @@ -9,4 +9,5 @@ import java.lang.annotation.Target; @Target(ElementType.TYPE) public @interface Category { String name(); + String description() default ""; } diff --git a/src/main/java/io/polyfrost/oneconfig/annotations/Slider.java b/src/main/java/io/polyfrost/oneconfig/annotations/Slider.java index 990c70b..fa855bf 100644 --- a/src/main/java/io/polyfrost/oneconfig/annotations/Slider.java +++ b/src/main/java/io/polyfrost/oneconfig/annotations/Slider.java @@ -10,6 +10,7 @@ import java.lang.annotation.Target; public @interface Slider { String name(); String description() default ""; - int min(); - int max(); + float min(); + float max(); + float precision(); } diff --git a/src/main/java/io/polyfrost/oneconfig/core/ConfigCore.java b/src/main/java/io/polyfrost/oneconfig/core/ConfigCore.java new file mode 100644 index 0000000..8fe7c81 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/core/ConfigCore.java @@ -0,0 +1,11 @@ +package io.polyfrost.oneconfig.core; + +import io.polyfrost.oneconfig.data.ModData; +import io.polyfrost.oneconfig.interfaces.Option; + +import java.util.ArrayList; +import java.util.HashMap; + +public class ConfigCore { + public static HashMap<ModData, ArrayList<Option>> settings = new HashMap<>(); +} diff --git a/src/main/java/io/polyfrost/oneconfig/data/ModData.java b/src/main/java/io/polyfrost/oneconfig/data/ModData.java new file mode 100644 index 0000000..24ed1e5 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/data/ModData.java @@ -0,0 +1,24 @@ +package io.polyfrost.oneconfig.data; + +import io.polyfrost.oneconfig.interfaces.Config; + +public class ModData { + public final String name; + public final ModType modType; + public final String creator; + public final String version; + public Config config; + + /** + * @param name Friendly name of the mod + * @param modType Type of the mod (for example ModType.QOL) + * @param creator Creator of the mod + * @param version Version of the mod + */ + public ModData(String name, ModType modType, String creator, String version) { + this.name = name; + this.modType = modType; + this.creator = creator; + this.version = version; + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/data/ModType.java b/src/main/java/io/polyfrost/oneconfig/data/ModType.java new file mode 100644 index 0000000..18cf638 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/data/ModType.java @@ -0,0 +1,10 @@ +package io.polyfrost.oneconfig.data; + +public enum ModType { + PVP, + PERFORMANCE, + HUD, + QOL, + HYPIXEL, + OTHER +} diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigButton.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigButton.java index de21d9d..a6951a6 100644 --- a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigButton.java +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigButton.java @@ -1,4 +1,24 @@ package io.polyfrost.oneconfig.gui.elements.config; -public class OConfigButton { +import io.polyfrost.oneconfig.interfaces.Option; + +import java.lang.reflect.Field; + +public class OConfigButton extends Option { + private final String text; + + public OConfigButton(Field field, String name, String description, String text) { + super(field, name, description); + this.text = text; + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public void draw(int x, int y, int width, int mouseX, int mouseY) { + + } } diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigCategory.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigCategory.java new file mode 100644 index 0000000..0eae75a --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigCategory.java @@ -0,0 +1,24 @@ +package io.polyfrost.oneconfig.gui.elements.config; + +import io.polyfrost.oneconfig.interfaces.Option; + +import java.util.List; + +public class OConfigCategory extends Option { + public final List<Option> options; + + public OConfigCategory(String name, String description, List<Option> options) { + super(null, name, description); + this.options = options; + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public void draw(int x, int y, int width, int mouseX, int mouseY) { + + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigColor.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigColor.java index 299c4f2..fb60af7 100644 --- a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigColor.java +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigColor.java @@ -1,4 +1,24 @@ package io.polyfrost.oneconfig.gui.elements.config; -public class OConfigColor { +import io.polyfrost.oneconfig.interfaces.Option; + +import java.lang.reflect.Field; + +public class OConfigColor extends Option { + private final boolean allowAlpha; + + public OConfigColor(Field field, String name, String description, boolean allowAlpha) { + super(field, name, description); + this.allowAlpha = allowAlpha; + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public void draw(int x, int y, int width, int mouseX, int mouseY) { + + } } diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSelector.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSelector.java index cc76814..c979131 100644 --- a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSelector.java +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSelector.java @@ -1,4 +1,26 @@ package io.polyfrost.oneconfig.gui.elements.config; -public class OConfigSelector { +import io.polyfrost.oneconfig.interfaces.Option; + +import java.lang.reflect.Field; + +public class OConfigSelector extends Option { + private final String[] options; + private final int defaultSelection; + + public OConfigSelector(Field field, String name, String description, String[] options, int defaultSelection) { + super(field, name, description); + this.options = options; + this.defaultSelection = defaultSelection; + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public void draw(int x, int y, int width, int mouseX, int mouseY) { + + } } diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSlider.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSlider.java index 38bacf7..55c1222 100644 --- a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSlider.java +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSlider.java @@ -1,4 +1,28 @@ package io.polyfrost.oneconfig.gui.elements.config; -public class OConfigSlider { +import io.polyfrost.oneconfig.interfaces.Option; + +import java.lang.reflect.Field; + +public class OConfigSlider extends Option { + private final float min; + private final float max; + private final float precision; + + public OConfigSlider(Field field, String name, String description, float min, float max, float precision) { + super(field, name, description); + this.min = min; + this.max = max; + this.precision = precision; + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public void draw(int x, int y, int width, int mouseX, int mouseY) { + + } } diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSwitch.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSwitch.java index c9d6091..1fa1590 100644 --- a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSwitch.java +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigSwitch.java @@ -1,4 +1,22 @@ package io.polyfrost.oneconfig.gui.elements.config; -public class OConfigSwitch { +import io.polyfrost.oneconfig.interfaces.Option; + +import java.lang.reflect.Field; + +public class OConfigSwitch extends Option { + + public OConfigSwitch(Field field, String name, String description) { + super(field, name, description); + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public void draw(int x, int y, int width, int mouseX, int mouseY) { + + } } diff --git a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigText.java b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigText.java index 68f2b8b..195607a 100644 --- a/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigText.java +++ b/src/main/java/io/polyfrost/oneconfig/gui/elements/config/OConfigText.java @@ -1,4 +1,26 @@ package io.polyfrost.oneconfig.gui.elements.config; -public class OConfigText { +import io.polyfrost.oneconfig.interfaces.Option; + +import java.lang.reflect.Field; + +public class OConfigText extends Option { + private final String placeholder; + private final boolean hideText; + + public OConfigText(Field field, String name, String description, String placeholder, boolean hideText) { + super(field, name, description); + this.placeholder = placeholder; + this.hideText = hideText; + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public void draw(int x, int y, int width, int mouseX, int mouseY) { + + } } diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java b/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java index 48abf1c..3549456 100644 --- a/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java +++ b/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java @@ -1,49 +1,112 @@ package io.polyfrost.oneconfig.interfaces; import com.google.gson.*; +import io.polyfrost.oneconfig.annotations.*; +import io.polyfrost.oneconfig.core.ConfigCore; +import io.polyfrost.oneconfig.data.ModData; +import io.polyfrost.oneconfig.gui.elements.config.*; import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Map; public class Config { private final File configFile; - public Config(File configFile) { + Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting() + .registerTypeAdapterFactory(OneConfigTypeAdapterFactory.getStaticTypeAdapterFactory()).create(); + + /** + * @param modData information about the mod + * @param configFile file where config is stored + */ + public Config(ModData modData, File configFile) { this.configFile = configFile; if (configFile.exists()) load(); else save(); + modData.config = this; + ConfigCore.settings.put(modData, generateOptionList(this.getClass())); } - Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting() - .registerTypeAdapterFactory(OneConfigTypeAdapterFactory.getStaticTypeAdapterFactory()).create(); - + /** + * Save current config to file + */ public void save() { try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), StandardCharsets.UTF_8))) { writer.write(gson.toJson(this.getClass())); - } catch (IOException ignored) { + } catch (IOException e) { + e.printStackTrace(); } } + /** + * Load file and overwrite current values + */ public void load() { try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8))) { - processPart(new JsonParser().parse(reader).getAsJsonObject(), this.getClass()); - } catch (IOException ignored) { + deserializePart(new JsonParser().parse(reader).getAsJsonObject(), this.getClass()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Generate the option list for internal use only + * + * @param clazz target class + * @return list of options + */ + private 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))); + } + } + 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())); + } else if (field.isAnnotationPresent(ColorPicker.class)) { + ColorPicker colorPicker = field.getAnnotation(ColorPicker.class); + options.add(new OConfigColor(field, colorPicker.name(), colorPicker.description(), colorPicker.allowAlpha())); + } 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())); + } 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())); + } else if (field.isAnnotationPresent(Switch.class)) { + Switch aSwitch = field.getAnnotation(Switch.class); + options.add(new OConfigSwitch(field, aSwitch.name(), aSwitch.description())); + } 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())); + } } + return options; } - private void processPart(JsonObject json, Class<?> clazz) { + /** + * Deserialize part of config and load values + * + * @param json json to deserialize + * @param clazz target class + */ + private void deserializePart(JsonObject json, Class<?> clazz) { for (Map.Entry<String, JsonElement> element : json.entrySet()) { String name = element.getKey(); JsonElement value = element.getValue(); if (value.isJsonObject()) { for (Class<?> innerClass : clazz.getClasses()) { if (innerClass.getSimpleName().equals(name)) { - processPart(value.getAsJsonObject(), innerClass); + deserializePart(value.getAsJsonObject(), innerClass); break; } } diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java index 95a558d..df6ee87 100644 --- a/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java +++ b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java @@ -30,12 +30,10 @@ final class OneConfigTypeAdapter<T> extends TypeAdapter<Class<T>> { for (Field field : value.getFields()) { out.name(field.getName()); field.setAccessible(true); - final Object fieldValue = field.get(null); final TypeAdapter<Object> adapter = (TypeAdapter) gson.getAdapter(field.getType()); - adapter.write(out, fieldValue); + adapter.write(out, field.get(null)); } for (Class<?> clazz : value.getClasses()) { - System.out.println(clazz.toString()); out.name(clazz.getSimpleName()); final TypeAdapter<JsonElement> adapter = gson.getAdapter(JsonElement.class); adapter.write(out, parser.parse(gson.toJson(clazz))); diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/Option.java b/src/main/java/io/polyfrost/oneconfig/interfaces/Option.java new file mode 100644 index 0000000..d996b60 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/interfaces/Option.java @@ -0,0 +1,76 @@ +package io.polyfrost.oneconfig.interfaces; + +import java.lang.reflect.Field; + +@SuppressWarnings({"unused"}) +public abstract class Option { + protected final Field field; + protected final String name; + protected final String description; + + /** + * Initialize option + * + * @param field variable attached to option (null for category) + * @param name name of option + * @param description description of option + */ + public Option(Field field, String name, String description) { + this.field = field; + this.name = name; + this.description = description; + if (field != null) + field.setAccessible(true); + } + + /** + * @param object Java object to set the variable to + */ + protected void set(Object object) throws IllegalAccessException { + if (field == null) return; + field.set(null, object); + } + + /** + * @return value of variable as Java object + */ + protected Object get() throws IllegalAccessException { + if (field == null) return null; + return field.get(null); + } + + /** + * @return height of option to align other options accordingly + */ + public abstract int getHeight(); + + /** + * Function that gets called when drawing option + * + * @param x x position + * @param y y position + * @param width width of menu + * @param mouseX x position of mouse + * @param mouseY y position of mouse + */ + public abstract void draw(int x, int y, int width, int mouseX, int mouseY); + + /** + * Function that gets called when mouse is clicked + * + * @param mouseX x position of mouse + * @param mouseY y position of mouse + * @param mouseButton button that got pressed + */ + protected void onMouseClicked(int mouseX, int mouseY, int mouseButton) { + } + + /** + * Function that gets called when a key is typed + * + * @param typedChar char that has been typed + * @param keyCode code of key + */ + protected void keyTyped(char typedChar, int keyCode) { + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/test/TestConfig.java b/src/main/java/io/polyfrost/oneconfig/test/TestConfig.java index 21259ac..3cfceaa 100644 --- a/src/main/java/io/polyfrost/oneconfig/test/TestConfig.java +++ b/src/main/java/io/polyfrost/oneconfig/test/TestConfig.java @@ -3,6 +3,8 @@ package io.polyfrost.oneconfig.test; import io.polyfrost.oneconfig.annotations.Category; import io.polyfrost.oneconfig.annotations.Switch; import io.polyfrost.oneconfig.annotations.TextField; +import io.polyfrost.oneconfig.data.ModData; +import io.polyfrost.oneconfig.data.ModType; import io.polyfrost.oneconfig.interfaces.Config; import java.io.File; @@ -15,10 +17,10 @@ public class TestConfig extends Config { @Category(name = "Cool Category") public static class category { @TextField(name = "Cool text field") - public static String text = "e"; + public static String text = "Very cool text"; } public TestConfig() { - super(new File("./config/testConfig.json")); + super(new ModData("hacks", ModType.QOL, "ShadyDev", "1.0"), new File("./config/hacksConfig.json")); } } |