瀏覽代碼

Replace Enum annotation with Dropdown annotation

Lortseam 4 年之前
父節點
當前提交
962acfd05c

+ 1 - 0
example/src/main/java/me/lortseam/completeconfig/example/Settings.java

@@ -27,6 +27,7 @@ public final class Settings implements ConfigContainer {
         private float aFloat;
         private double aDouble;
         private String string = "";
+        @ConfigEntry.Dropdown
         private AnEnum anEnum = AnEnum.FOO;
         private List<String> list = Arrays.asList("First entry", "Second entry");
         private Color color = Color.ofRGB(0, 255, 0);

+ 18 - 0
lib/src/main/java/me/lortseam/completeconfig/DropdownEntry.java

@@ -0,0 +1,18 @@
+package me.lortseam.completeconfig;
+
+import lombok.Getter;
+import me.lortseam.completeconfig.api.ConfigEntry;
+import me.lortseam.completeconfig.data.EnumEntry;
+import me.lortseam.completeconfig.data.entry.EntryOrigin;
+
+public class DropdownEntry<T extends Enum<?>> extends EnumEntry<T> {
+
+    @Getter
+    private final boolean suggestionMode;
+
+    public DropdownEntry(EntryOrigin origin) {
+        super(origin);
+        suggestionMode = origin.getAnnotation(ConfigEntry.Dropdown.class).suggestionMode();
+    }
+
+}

+ 3 - 10
lib/src/main/java/me/lortseam/completeconfig/api/ConfigEntry.java

@@ -1,7 +1,5 @@
 package me.lortseam.completeconfig.api;
 
-import me.lortseam.completeconfig.data.EnumEntry;
-
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -190,18 +188,13 @@ public @interface ConfigEntry {
     }
 
     /**
-     * Applied to an entry of type Enum.
+     * If applied, renders a dropdown menu for this entry.
      */
     @Target(ElementType.FIELD)
     @Retention(RetentionPolicy.RUNTIME)
-    @interface Enum {
+    @interface Dropdown {
 
-        /**
-         * Specifies how to render the entry.
-         *
-         * @return the desired display type
-         */
-        EnumEntry.DisplayType displayType() default EnumEntry.DisplayType.BUTTON;
+        boolean suggestionMode() default false;
 
     }
 

+ 3 - 1
lib/src/main/java/me/lortseam/completeconfig/data/Entry.java

@@ -6,6 +6,7 @@ import lombok.Getter;
 import lombok.NonNull;
 import lombok.extern.log4j.Log4j2;
 import me.lortseam.completeconfig.CompleteConfig;
+import me.lortseam.completeconfig.DropdownEntry;
 import me.lortseam.completeconfig.api.ConfigContainer;
 import me.lortseam.completeconfig.api.ConfigEntry;
 import me.lortseam.completeconfig.data.entry.EntryOrigin;
@@ -63,7 +64,8 @@ public class Entry<T> implements DataPart, Identifiable {
                 ConfigEntry.BoundedDouble bounds = origin.getAnnotation(ConfigEntry.BoundedDouble.class);
                 return new BoundedEntry<>(origin, bounds.min(), bounds.max());
             }),
-            Transformation.builder().byType(type -> Enum.class.isAssignableFrom(ReflectionUtils.getTypeClass(type))).byAnnotation(ConfigEntry.Enum.class, true).transforms(EnumEntry::new),
+            Transformation.builder().byType(type -> Enum.class.isAssignableFrom(ReflectionUtils.getTypeClass(type))).transforms(EnumEntry::new),
+            Transformation.builder().byType(type -> Enum.class.isAssignableFrom(ReflectionUtils.getTypeClass(type))).byAnnotation(ConfigEntry.Dropdown.class).transforms(DropdownEntry::new),
             Transformation.builder().byAnnotation(ConfigEntry.Color.class).transforms(ColorEntry::new),
             Transformation.builder().byType(TextColor.class).transforms(origin -> new ColorEntry<>(origin, false))
     );

+ 2 - 24
lib/src/main/java/me/lortseam/completeconfig/data/EnumEntry.java

@@ -1,41 +1,19 @@
 package me.lortseam.completeconfig.data;
 
 import com.google.common.base.CaseFormat;
-import lombok.Getter;
-import me.lortseam.completeconfig.api.ConfigEntry;
 import me.lortseam.completeconfig.data.entry.EntryOrigin;
 import net.minecraft.text.Text;
 
 import java.util.function.Function;
 
-public class EnumEntry<T extends Enum> extends Entry<T> {
-
-    @Getter
-    private final DisplayType displayType;
+public class EnumEntry<T extends Enum<?>> extends Entry<T> {
 
     public EnumEntry(EntryOrigin origin) {
         super(origin);
-        this.displayType = origin.getOptionalAnnotation(ConfigEntry.Enum.class).map(ConfigEntry.Enum::displayType).orElse(DisplayType.DEFAULT);
     }
 
-    public Function<Enum, Text> getEnumNameProvider() {
+    public final Function<Enum, Text> getValueTextSupplier() {
         return enumValue -> getTranslation().append(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, enumValue.name())).toText();
     }
 
-    public enum DisplayType {
-
-        BUTTON, DROPDOWN;
-
-        private static final DisplayType DEFAULT;
-
-        static {
-            try {
-                DEFAULT = (DisplayType) ConfigEntry.Enum.class.getDeclaredMethod("displayType").getDefaultValue();
-            } catch (NoSuchMethodException e) {
-                throw new RuntimeException(e);
-            }
-        }
-
-    }
-
 }

+ 4 - 0
lib/src/main/java/me/lortseam/completeconfig/data/entry/EntryOrigin.java

@@ -30,6 +30,10 @@ public final class EntryOrigin {
         return Objects.requireNonNull(field.getDeclaredAnnotation(annotationType), "Missing required transformation annotation");
     }
 
+    public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
+        return field.isAnnotationPresent(annotationType);
+    }
+
     public <A extends Annotation> Optional<A> getOptionalAnnotation(Class<A> annotationType) {
         return Optional.ofNullable(field.getDeclaredAnnotation(annotationType));
     }

+ 8 - 10
lib/src/main/java/me/lortseam/completeconfig/data/entry/Transformation.java

@@ -73,18 +73,16 @@ public final class Transformation {
         }
 
         public Transformation transforms(Transformer transformer) {
-            if (!requiredAnnotations.isEmpty() || !optionalAnnotations.isEmpty()) {
-                by(origin -> {
-                    Set<Class<? extends Annotation>> declaredAnnotations = Arrays.stream(origin.getField().getDeclaredAnnotations()).map(Annotation::annotationType).filter(registeredAnnotations::contains).collect(Collectors.toSet());
-                    for (Class<? extends Annotation> requiredAnnotation : requiredAnnotations) {
-                        if (!declaredAnnotations.remove(requiredAnnotation)) return false;
-                    }
-                    return optionalAnnotations.containsAll(declaredAnnotations);
-                });
-            }
-            if (predicate == null) {
+            if (predicate == null && requiredAnnotations.isEmpty()) {
                 throw new IllegalStateException("Missing transformation filter");
             }
+            by(origin -> {
+                Set<Class<? extends Annotation>> declaredAnnotations = Arrays.stream(origin.getField().getDeclaredAnnotations()).map(Annotation::annotationType).filter(registeredAnnotations::contains).collect(Collectors.toSet());
+                for (Class<? extends Annotation> requiredAnnotation : requiredAnnotations) {
+                    if (!declaredAnnotations.remove(requiredAnnotation)) return false;
+                }
+                return optionalAnnotations.containsAll(declaredAnnotations);
+            });
             return new Transformation(predicate, transformer);
         }
 

+ 9 - 8
lib/src/main/java/me/lortseam/completeconfig/gui/cloth/GuiRegistry.java

@@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.MoreCollectors;
 import com.google.common.reflect.TypeToken;
 import me.lortseam.completeconfig.CompleteConfig;
+import me.lortseam.completeconfig.DropdownEntry;
 import me.lortseam.completeconfig.data.*;
 import me.lortseam.completeconfig.extensions.GuiExtension;
 import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
@@ -114,21 +115,21 @@ public final class GuiRegistry {
                             .startEnumSelector(entry.getText(), entry.getTypeClass(), entry.getValue())
                             .setDefaultValue(entry.getDefaultValue())
                             .setTooltip(entry.getTooltip())
-                            .setEnumNameProvider(entry.getEnumNameProvider())
-                            .setSaveConsumer(entry::setValue),
-                    entry -> entry.getDisplayType() == EnumEntry.DisplayType.BUTTON),
-            Provider.create(EnumEntry.class, (EnumEntry<Enum<?>> entry) -> {
+                            .setEnumNameProvider(entry.getValueTextSupplier())
+                            .setSaveConsumer(entry::setValue)),
+            Provider.create(DropdownEntry.class, (DropdownEntry<Enum<?>> entry) -> {
                 List<Enum> enumValues = Arrays.asList(((Class<? extends Enum<?>>) entry.getTypeClass()).getEnumConstants());
                 return ConfigEntryBuilder.create()
                         .startDropdownMenu(entry.getText(), DropdownMenuBuilder.TopCellElementBuilder.of(
                                 entry.getValue(),
-                                enumTranslation -> enumValues.stream().filter(enumValue -> entry.getEnumNameProvider().apply(enumValue).getString().equals(enumTranslation)).collect(MoreCollectors.toOptional()).orElse(null),
-                                entry.getEnumNameProvider()
-                        ), DropdownMenuBuilder.CellCreatorBuilder.of(entry.getEnumNameProvider()))
+                                enumTranslation -> enumValues.stream().filter(enumValue -> entry.getValueTextSupplier().apply(enumValue).getString().equals(enumTranslation)).collect(MoreCollectors.toOptional()).orElse(null),
+                                entry.getValueTextSupplier()
+                        ), DropdownMenuBuilder.CellCreatorBuilder.of(entry.getValueTextSupplier()))
                         .setSelections(enumValues)
+                        .setSuggestionMode(entry.isSuggestionMode())
                         .setDefaultValue(entry.getDefaultValue())
                         .setSaveConsumer(entry::setValue);
-            }, entry -> entry.getDisplayType() == EnumEntry.DisplayType.DROPDOWN),
+            }),
             Provider.create((Entry<List<Integer>> entry) -> ConfigEntryBuilder.create()
                             .startIntList(entry.getText(), entry.getValue())
                             .setDefaultValue(entry.getDefaultValue())