aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/interfaces
diff options
context:
space:
mode:
authornextdaydelivery <12willettsh@gmail.com>2022-02-13 10:46:44 +0000
committernextdaydelivery <12willettsh@gmail.com>2022-02-13 10:46:44 +0000
commit3857800292a7cc078ee05c5487b11a256682bef1 (patch)
tree8f8196234469da7fccf3ea54ff417e47ea32b5d3 /src/main/java/io/polyfrost/oneconfig/interfaces
parent78274eb2b66b27052fe32960916fa4419202dc76 (diff)
downloadOneConfig-3857800292a7cc078ee05c5487b11a256682bef1.tar.gz
OneConfig-3857800292a7cc078ee05c5487b11a256682bef1.tar.bz2
OneConfig-3857800292a7cc078ee05c5487b11a256682bef1.zip
remake theme stuff + move packages around
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/interfaces')
-rw-r--r--src/main/java/io/polyfrost/oneconfig/interfaces/Config.java126
-rw-r--r--src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java51
-rw-r--r--src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapterFactory.java30
-rw-r--r--src/main/java/io/polyfrost/oneconfig/interfaces/Option.java76
4 files changed, 0 insertions, 283 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java b/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java
deleted file mode 100644
index 3549456..0000000
--- a/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java
+++ /dev/null
@@ -1,126 +0,0 @@
-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;
-
- 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/interfaces/OneConfigTypeAdapter.java b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java
deleted file mode 100644
index df6ee87..0000000
--- a/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java
+++ /dev/null
@@ -1,51 +0,0 @@
-package io.polyfrost.oneconfig.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/interfaces/OneConfigTypeAdapterFactory.java b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapterFactory.java
deleted file mode 100644
index 6491ba2..0000000
--- a/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapterFactory.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package io.polyfrost.oneconfig.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.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/interfaces/Option.java b/src/main/java/io/polyfrost/oneconfig/interfaces/Option.java
deleted file mode 100644
index d996b60..0000000
--- a/src/main/java/io/polyfrost/oneconfig/interfaces/Option.java
+++ /dev/null
@@ -1,76 +0,0 @@
-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) {
- }
-}