diff options
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig')
4 files changed, 48 insertions, 3 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java index 1b5c5e6..70b82a8 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java @@ -1,5 +1,6 @@ package cc.polyfrost.oneconfig.config; +import cc.polyfrost.oneconfig.config.annotations.Button; import cc.polyfrost.oneconfig.config.annotations.CustomOption; import cc.polyfrost.oneconfig.config.annotations.HUD; import cc.polyfrost.oneconfig.config.annotations.Page; @@ -14,6 +15,7 @@ import cc.polyfrost.oneconfig.config.gson.NonProfileSpecificExclusionStrategy; import cc.polyfrost.oneconfig.config.gson.ProfileExclusionStrategy; import cc.polyfrost.oneconfig.config.profiles.Profiles; import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigButton; import cc.polyfrost.oneconfig.gui.elements.config.ConfigPageButton; import cc.polyfrost.oneconfig.gui.pages.ModConfigPage; import cc.polyfrost.oneconfig.hud.HUDUtils; @@ -29,6 +31,7 @@ import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -120,10 +123,11 @@ public class Config { * @param migrate whether the migrator should be run */ protected void generateOptionList(Object instance, OptionPage page, Mod mod, boolean migrate) { + String pagePath = page.equals(mod.defaultPage) ? "" : page.name + "."; for (Field field : instance.getClass().getDeclaredFields()) { Option option = ConfigUtils.findAnnotation(field, Option.class); CustomOption customOption = ConfigUtils.findAnnotation(field, CustomOption.class); - String optionName = (page.equals(mod.defaultPage) ? "" : page.name + ".") + field.getName(); + String optionName = pagePath + field.getName(); if (option != null) { BasicOption configOption = ConfigUtils.addOptionToPage(page, option, field, instance, migrate ? mod.migrator : null); optionNames.put(optionName, configOption); @@ -145,6 +149,15 @@ public class Config { HUDUtils.addHudOptions(page, field, instance, this); } } + for (Method method : instance.getClass().getDeclaredMethods()) { + Button button = ConfigUtils.findAnnotation(method, Button.class); + String optionName = pagePath + method.getName(); + if (button != null) { + ConfigButton option = ConfigButton.create(method, instance); + ConfigUtils.getSubCategory(page, button.category(), button.subcategory()).options.add(option); + optionNames.put(optionName, option); + } + } } /** diff --git a/src/main/java/cc/polyfrost/oneconfig/config/annotations/Button.java b/src/main/java/cc/polyfrost/oneconfig/config/annotations/Button.java index 300651f..58e3112 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/annotations/Button.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/annotations/Button.java @@ -9,7 +9,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) +@Target({ElementType.FIELD, ElementType.METHOD}) @Option(type = OptionType.BUTTON) public @interface Button { String name(); diff --git a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java index bacfe8b..c4c6d33 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java @@ -14,6 +14,7 @@ import org.jetbrains.annotations.Nullable; import java.lang.annotation.Annotation; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; @@ -59,7 +60,7 @@ public class ConfigUtils { // I have tried to check for supertype classes like Boolean other ways. // but they actually don't extend their primitive types (because that is impossible) so isAssignableFrom doesn't work. for (Class<?> clazz : expectedType) { - if(field.getType().equals(clazz)) return; + if (field.getType().equals(clazz)) return; } throw new InvalidTypeException("Field " + field.getName() + " in config " + field.getDeclaringClass().getName() + " is annotated as a " + type.toString() + ", but is not of valid type, expected " + Arrays.toString(expectedType) + " (found " + field.getType() + ")"); } @@ -116,6 +117,15 @@ public class ConfigUtils { return null; } + public static <T extends Annotation> T findAnnotation(Method method, Class<T> annotationType) { + if (method.isAnnotationPresent(annotationType)) return method.getAnnotation(annotationType); + for (Annotation ann : method.getDeclaredAnnotations()) { + if (ann.annotationType().isAnnotationPresent(annotationType)) + return ann.annotationType().getAnnotation(annotationType); + } + return null; + } + public static <T extends Annotation> T findAnnotation(FieldAttributes field, Class<T> annotationType) { T annotation = field.getAnnotation(annotationType); if (annotation != null) return annotation; diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java index 9879ac2..1480ff8 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java @@ -9,6 +9,8 @@ import cc.polyfrost.oneconfig.renderer.font.Fonts; import cc.polyfrost.oneconfig.utils.color.ColorPalette; import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Arrays; public class ConfigButton extends BasicOption { private final BasicButton button; @@ -25,11 +27,31 @@ public class ConfigButton extends BasicOption { this.button.setClickAction(getRunnableFromField(field, parent)); } + public ConfigButton(Method method, Object parent, String name, String category, String subcategory, int size, String text) { + super(null, parent, name, category, subcategory, size); + this.button = new BasicButton(size == 1 ? 128 : 256, 32, text, BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY); + this.button.setClickAction(() -> { + try { + method.invoke(parent); + } catch (Exception e) { + if (e.getMessage() != null && e.getMessage().contains("wrong number of arguments")) { + throw new IllegalArgumentException("Button method " + method.getDeclaringClass().getName() + "." + method.getName() + "(" + Arrays.toString(method.getGenericParameterTypes()) + ") must take no arguments!"); + } else e.printStackTrace(); + } + }); + } + public static ConfigButton create(Field field, Object parent) { Button button = field.getAnnotation(Button.class); return new ConfigButton(field, parent, button.name(), button.category(), button.subcategory(), button.size(), button.text()); } + public static ConfigButton create(Method method, Object parent) { + method.setAccessible(true); + Button button = method.getAnnotation(Button.class); + return new ConfigButton(method, parent, button.name(), button.category(), button.subcategory(), button.size(), button.text()); + } + private static Runnable getRunnableFromField(Field field, Object parent) { Runnable runnable = () -> { }; |