diff options
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config')
6 files changed, 62 insertions, 8 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java b/src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java index 8736e56..eddb490 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java @@ -14,4 +14,11 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.TYPE}) public @interface Exclude { + ExcludeType type() default ExcludeType.ALL; + + enum ExcludeType { + ALL, + CONFIG, + HUD + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig.java b/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig.java index ee3b97f..bfad00c 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig.java @@ -114,8 +114,8 @@ public class VigilanceConfig extends Config { try { Field field = valueBackedPropertyValue.getClass().getDeclaredField("obj"); field.setAccessible(true); - return (Field) field.get(valueBackedPropertyValue); - } catch (IllegalAccessException | NoSuchFieldException e) { + return field; + } catch (NoSuchFieldException e) { throw new RuntimeException(e); } } else if (data.getValue() instanceof KPropertyBackedPropertyValue) { 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 75bd98d..14e8d01 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java @@ -8,6 +8,7 @@ import cc.polyfrost.oneconfig.config.elements.OptionSubcategory; import cc.polyfrost.oneconfig.config.migration.Migrator; import cc.polyfrost.oneconfig.gui.elements.config.*; import cc.polyfrost.oneconfig.internal.config.annotations.Option; +import com.google.gson.FieldAttributes; import org.jetbrains.annotations.Nullable; import java.lang.annotation.Annotation; @@ -56,6 +57,8 @@ public class ConfigUtils { parentClass = clazz; } for (Field field : fields) { + Exclude exclude = findAnnotation(field, Exclude.class); + if (exclude != null && exclude.type() != Exclude.ExcludeType.CONFIG) continue; Option option = findAnnotation(field, Option.class); if (option == null) continue; options.add(getOption(option, field, object)); @@ -91,6 +94,25 @@ public class ConfigUtils { return null; } + public static <T extends Annotation> T findAnnotation(FieldAttributes field, Class<T> annotationType) { + T annotation = field.getAnnotation(annotationType); + if (annotation != null) return annotation; + for (Annotation ann : field.getAnnotations()) { + if (ann.annotationType().isAnnotationPresent(annotationType)) + return ann.annotationType().getAnnotation(annotationType); + } + return null; + } + + public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationType) { + if (clazz.isAnnotationPresent(annotationType)) return clazz.getAnnotation(annotationType); + for (Annotation ann : clazz.getDeclaredAnnotations()) { + if (ann.annotationType().isAnnotationPresent(annotationType)) + return ann.annotationType().getAnnotation(annotationType); + } + return null; + } + public static Object getField(Field field, Object parent) { try { field.setAccessible(true); diff --git a/src/main/java/cc/polyfrost/oneconfig/config/gson/ExclusionUtils.java b/src/main/java/cc/polyfrost/oneconfig/config/gson/ExclusionUtils.java new file mode 100644 index 0000000..3981134 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/gson/ExclusionUtils.java @@ -0,0 +1,15 @@ +package cc.polyfrost.oneconfig.config.gson; + +public class ExclusionUtils { + protected static boolean isSuperClassOf(Class<?> clazz, Class<?> parentClass) { + Class<?> tempClass = clazz; + Class<?> lastClass; + if (tempClass == parentClass) return true; + while (true) { + lastClass = tempClass; + tempClass = tempClass.getSuperclass(); + if (tempClass == lastClass) return false; + if (tempClass == parentClass) return true; + } + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java b/src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java index 8efa0b7..7679b9c 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java @@ -1,18 +1,22 @@ package cc.polyfrost.oneconfig.config.gson; +import cc.polyfrost.oneconfig.config.Config; import cc.polyfrost.oneconfig.config.annotations.Exclude; import cc.polyfrost.oneconfig.config.annotations.NonProfileSpecific; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; -public class NonProfileSpecificExclusionStrategy implements ExclusionStrategy { +public class NonProfileSpecificExclusionStrategy extends ExclusionUtils implements ExclusionStrategy { /** * @param f the field object that is under test * @return true if the field should be ignored; otherwise false */ @Override public boolean shouldSkipField(FieldAttributes f) { - return f.getAnnotation(NonProfileSpecific.class) == null || f.getAnnotation(Exclude.class) != null; + if (isSuperClassOf(f.getDeclaredClass(), Config.class)) return true; + if (f.getAnnotation(NonProfileSpecific.class) == null) return true; + Exclude exclude = f.getAnnotation(Exclude.class); + return exclude != null && exclude.type() != Exclude.ExcludeType.HUD; } /** @@ -21,6 +25,7 @@ public class NonProfileSpecificExclusionStrategy implements ExclusionStrategy { */ @Override public boolean shouldSkipClass(Class<?> clazz) { - return clazz.getAnnotation(Exclude.class) != null; + Exclude exclude = clazz.getAnnotation(Exclude.class); + return exclude != null && exclude.type() != Exclude.ExcludeType.HUD; } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java b/src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java index c94e8bd..45f3a04 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java @@ -1,18 +1,22 @@ package cc.polyfrost.oneconfig.config.gson; +import cc.polyfrost.oneconfig.config.Config; import cc.polyfrost.oneconfig.config.annotations.Exclude; import cc.polyfrost.oneconfig.config.annotations.NonProfileSpecific; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; -public class ProfileExclusionStrategy implements ExclusionStrategy { +public class ProfileExclusionStrategy extends ExclusionUtils implements ExclusionStrategy { /** * @param f the field object that is under test * @return true if the field should be ignored; otherwise false */ @Override public boolean shouldSkipField(FieldAttributes f) { - return f.getAnnotation(NonProfileSpecific.class) != null || f.getAnnotation(Exclude.class) != null; + if (isSuperClassOf(f.getDeclaredClass(), Config.class)) return true; + if (f.getAnnotation(NonProfileSpecific.class) != null) return true; + Exclude exclude = f.getAnnotation(Exclude.class); + return exclude != null && exclude.type() != Exclude.ExcludeType.HUD; } /** @@ -21,6 +25,7 @@ public class ProfileExclusionStrategy implements ExclusionStrategy { */ @Override public boolean shouldSkipClass(Class<?> clazz) { - return clazz.getAnnotation(NonProfileSpecific.class) != null || clazz.getAnnotation(Exclude.class) != null; + Exclude exclude = clazz.getAnnotation(Exclude.class); + return exclude != null && exclude.type() != Exclude.ExcludeType.HUD; } } |