aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/config
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/config')
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/annotations/Button.java14
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/annotations/Category.java13
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/annotations/ColorPicker.java14
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/annotations/Selector.java15
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/annotations/Slider.java16
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/annotations/Switch.java13
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/annotations/TextField.java15
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java11
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/data/ModData.java24
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/data/ModType.java10
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java126
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapter.java51
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapterFactory.java30
-rw-r--r--src/main/java/io/polyfrost/oneconfig/config/interfaces/Option.java76
14 files changed, 428 insertions, 0 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/Button.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/Button.java
new file mode 100644
index 0000000..449d297
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/annotations/Button.java
@@ -0,0 +1,14 @@
+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";
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/Category.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/Category.java
new file mode 100644
index 0000000..05b5277
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/annotations/Category.java
@@ -0,0 +1,13 @@
+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 "";
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/ColorPicker.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/ColorPicker.java
new file mode 100644
index 0000000..feee4b4
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/annotations/ColorPicker.java
@@ -0,0 +1,14 @@
+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;
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/Selector.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/Selector.java
new file mode 100644
index 0000000..8b476ab
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/annotations/Selector.java
@@ -0,0 +1,15 @@
+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;
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/Slider.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/Slider.java
new file mode 100644
index 0000000..cf8bfcd
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/annotations/Slider.java
@@ -0,0 +1,16 @@
+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();
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/Switch.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/Switch.java
new file mode 100644
index 0000000..19ec1db
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/annotations/Switch.java
@@ -0,0 +1,13 @@
+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 "";
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/config/annotations/TextField.java b/src/main/java/io/polyfrost/oneconfig/config/annotations/TextField.java
new file mode 100644
index 0000000..7b5837c
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/annotations/TextField.java
@@ -0,0 +1,15 @@
+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;
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java b/src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java
new file mode 100644
index 0000000..ff7ed28
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/core/ConfigCore.java
@@ -0,0 +1,11 @@
+package io.polyfrost.oneconfig.config.core;
+
+import io.polyfrost.oneconfig.config.data.ModData;
+import io.polyfrost.oneconfig.config.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/config/data/ModData.java b/src/main/java/io/polyfrost/oneconfig/config/data/ModData.java
new file mode 100644
index 0000000..c5e6633
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/data/ModData.java
@@ -0,0 +1,24 @@
+package io.polyfrost.oneconfig.config.data;
+
+import io.polyfrost.oneconfig.config.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/config/data/ModType.java b/src/main/java/io/polyfrost/oneconfig/config/data/ModType.java
new file mode 100644
index 0000000..8b6e7de
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/data/ModType.java
@@ -0,0 +1,10 @@
+package io.polyfrost.oneconfig.config.data;
+
+public enum ModType {
+ PVP,
+ PERFORMANCE,
+ HUD,
+ QOL,
+ HYPIXEL,
+ OTHER
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java b/src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java
new file mode 100644
index 0000000..ce73f0c
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/interfaces/Config.java
@@ -0,0 +1,126 @@
+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.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;
+
+ 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()));
+ }
+
+ /**
+ * 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 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))) {
+ 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;
+ }
+
+ /**
+ * 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)) {
+ deserializePart(value.getAsJsonObject(), innerClass);
+ break;
+ }
+ }
+ } else {
+ try {
+ Field field = clazz.getField(name);
+ TypeAdapter<?> adapter = gson.getAdapter(field.getType());
+ Object object = adapter.fromJsonTree(value);
+ field.setAccessible(true);
+ field.set(null, object);
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapter.java b/src/main/java/io/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapter.java
new file mode 100644
index 0000000..a07e7c8
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapter.java
@@ -0,0 +1,51 @@
+package io.polyfrost.oneconfig.config.interfaces;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+
+final class OneConfigTypeAdapter<T> extends TypeAdapter<Class<T>> {
+
+ private final Gson gson;
+ private final JsonParser parser = new JsonParser();
+
+ private OneConfigTypeAdapter(final Gson gson) {
+ this.gson = gson;
+ }
+
+ static <T> TypeAdapter<Class<T>> getStaticTypeAdapter(final Gson gson) {
+ return new OneConfigTypeAdapter<>(gson);
+ }
+
+ @Override
+ public void write(final JsonWriter out, final Class<T> value) throws IOException {
+ try {
+ out.beginObject();
+ for (Field field : value.getFields()) {
+ out.name(field.getName());
+ field.setAccessible(true);
+ final TypeAdapter<Object> adapter = (TypeAdapter) gson.getAdapter(field.getType());
+ adapter.write(out, field.get(null));
+ }
+ for (Class<?> clazz : value.getClasses()) {
+ out.name(clazz.getSimpleName());
+ final TypeAdapter<JsonElement> adapter = gson.getAdapter(JsonElement.class);
+ adapter.write(out, parser.parse(gson.toJson(clazz)));
+ }
+ out.endObject();
+ } catch (final IllegalAccessException ex) {
+ throw new IOException(ex);
+ }
+ }
+
+ @Override
+ public Class<T> read(final JsonReader in) throws IOException {
+ return null;
+ }
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapterFactory.java b/src/main/java/io/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapterFactory.java
new file mode 100644
index 0000000..af57163
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/interfaces/OneConfigTypeAdapterFactory.java
@@ -0,0 +1,30 @@
+package io.polyfrost.oneconfig.config.interfaces;
+
+import com.google.gson.Gson;
+import com.google.gson.TypeAdapter;
+import com.google.gson.TypeAdapterFactory;
+import com.google.gson.reflect.TypeToken;
+
+import java.lang.reflect.Type;
+
+import static io.polyfrost.oneconfig.config.interfaces.OneConfigTypeAdapter.getStaticTypeAdapter;
+
+public class OneConfigTypeAdapterFactory implements TypeAdapterFactory {
+
+ private static final TypeAdapterFactory staticTypeAdapterFactory = new OneConfigTypeAdapterFactory();
+
+ public static TypeAdapterFactory getStaticTypeAdapterFactory() {
+ return staticTypeAdapterFactory;
+ }
+
+ @Override
+ public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) {
+ final Type type = typeToken.getType();
+ if (type.equals(Class.class)) {
+ @SuppressWarnings("unchecked") final TypeAdapter<T> castStaticTypeAdapter = (TypeAdapter<T>) getStaticTypeAdapter(gson);
+ return castStaticTypeAdapter;
+ }
+ return null;
+ }
+
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/config/interfaces/Option.java b/src/main/java/io/polyfrost/oneconfig/config/interfaces/Option.java
new file mode 100644
index 0000000..5980a63
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/config/interfaces/Option.java
@@ -0,0 +1,76 @@
+package io.polyfrost.oneconfig.config.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) {
+ }
+}