aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/config
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 /src/main/java/cc/polyfrost/oneconfig/config
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
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config')
-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
3 files changed, 26 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;