aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com>2022-07-25 11:51:01 +0100
committerGitHub <noreply@github.com>2022-07-25 11:51:01 +0100
commit580fd1d5c4ec5625c813f6d593928a401a500869 (patch)
treed8a1255f6620b61203338e6471b4ed370e837e7d
parent64329ad7ccedba53c47ccd3a08b9eb97e756122c (diff)
downloadOneConfig-580fd1d5c4ec5625c813f6d593928a401a500869.tar.gz
OneConfig-580fd1d5c4ec5625c813f6d593928a401a500869.tar.bz2
OneConfig-580fd1d5c4ec5625c813f6d593928a401a500869.zip
Buttons but with a key twist (#70)
* config checker 9000 * apiDump * rename * put in package and remove javadocs * put in package and remove javadocs * button methods
-rw-r--r--api/OneConfig.api3
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/Config.java15
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/annotations/Button.java2
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java12
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigButton.java22
-rw-r--r--versions/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java16
6 files changed, 67 insertions, 3 deletions
diff --git a/api/OneConfig.api b/api/OneConfig.api
index 564976d..b7ecd78 100644
--- a/api/OneConfig.api
+++ b/api/OneConfig.api
@@ -146,6 +146,7 @@ public class cc/polyfrost/oneconfig/config/core/ConfigUtils {
public static fun findAnnotation (Lcom/google/gson/FieldAttributes;Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
public static fun findAnnotation (Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
public static fun findAnnotation (Ljava/lang/reflect/Field;Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
+ public static fun findAnnotation (Ljava/lang/reflect/Method;Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
public static fun getClassFields (Ljava/lang/Class;)Ljava/util/ArrayList;
public static fun getClassOptions (Ljava/lang/Object;)Ljava/util/ArrayList;
public static fun getField (Ljava/lang/reflect/Field;Ljava/lang/Object;)Ljava/lang/Object;
@@ -665,7 +666,9 @@ public class cc/polyfrost/oneconfig/gui/elements/Slider : cc/polyfrost/oneconfig
public class cc/polyfrost/oneconfig/gui/elements/config/ConfigButton : cc/polyfrost/oneconfig/config/elements/BasicOption {
public fun <init> (Ljava/lang/Runnable;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)V
public fun <init> (Ljava/lang/reflect/Field;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)V
+ public fun <init> (Ljava/lang/reflect/Method;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)V
public static fun create (Ljava/lang/reflect/Field;Ljava/lang/Object;)Lcc/polyfrost/oneconfig/gui/elements/config/ConfigButton;
+ public static fun create (Ljava/lang/reflect/Method;Ljava/lang/Object;)Lcc/polyfrost/oneconfig/gui/elements/config/ConfigButton;
public fun draw (JII)V
public fun getHeight ()I
}
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 = () -> {
};
diff --git a/versions/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java b/versions/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java
index 09b3fe5..3b7490c 100644
--- a/versions/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java
+++ b/versions/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java
@@ -25,6 +25,22 @@ public class TestConfig_Test extends Config {
)
public static boolean testCheckBox = true;
+ @Button(
+ name = "hello",
+ text = "click"
+ )
+ private void doSomething() {
+ UChat.chat("i was called from a nonstatic method");
+ }
+
+ @Button(
+ name = "hello2",
+ text = "click"
+ )
+ private static void doSomethingElse() {
+ UChat.chat("i was called from a static method");
+ }
+
@Info(
text = "Test Info",
type = InfoType.ERROR,