Kaynağa Gözat

Add color entry

Lortseam 4 yıl önce
ebeveyn
işleme
fbbbb514f7

+ 8 - 4
src/main/java/me/lortseam/completeconfig/ConfigSource.java

@@ -2,6 +2,7 @@ package me.lortseam.completeconfig;
 
 import lombok.AccessLevel;
 import lombok.Getter;
+import me.lortseam.completeconfig.data.ColorEntry;
 import me.lortseam.completeconfig.data.Config;
 import net.fabricmc.loader.api.FabricLoader;
 import org.apache.commons.lang3.ArrayUtils;
@@ -22,6 +23,9 @@ import java.util.Set;
 final class ConfigSource {
 
     private static final Logger LOGGER = LogManager.getLogger();
+    private static final TypeSerializerCollection CUSTOM_SERIALIZERS = TypeSerializerCollection.builder()
+            .registerExact(ColorEntry.TEXT_COLOR_SERIALIZER)
+            .build();
     private static final Set<ConfigSource> sources = new HashSet<>();
 
     @Getter(AccessLevel.PACKAGE)
@@ -40,12 +44,12 @@ final class ConfigSource {
         Path filePath = Paths.get(FabricLoader.getInstance().getConfigDir().toString(), subPath);
         loader = HoconConfigurationLoader.builder()
                 .path(filePath)
-                .defaultOptions(options -> {
+                .defaultOptions(options -> options.serializers(builder -> {
+                    builder.registerAll(CUSTOM_SERIALIZERS);
                     if (typeSerializers != null) {
-                        options.serializers(builder -> builder.registerAll(typeSerializers));
+                        builder.registerAll(typeSerializers);
                     }
-                    return options;
-                })
+                }))
                 .build();
     }
 

+ 16 - 0
src/main/java/me/lortseam/completeconfig/api/ConfigEntry.java

@@ -195,6 +195,22 @@ public @interface ConfigEntry {
 
     }
 
+    /**
+     * Specifies that the annotated field is a color entry.
+     */
+    @Target(ElementType.FIELD)
+    @Retention(RetentionPolicy.RUNTIME)
+    @interface Color {
+
+        /**
+         * Specifies whether the color has an alpha value.
+         *
+         * @return whether the color has an alpha value
+         */
+        boolean alphaMode() default false;
+
+    }
+
     /**
      * Can be applied to a field inside a POJO {@link ConfigEntryContainer} class to declare that that field should not
      * be considered to be config entry.

+ 32 - 0
src/main/java/me/lortseam/completeconfig/data/ColorEntry.java

@@ -0,0 +1,32 @@
+package me.lortseam.completeconfig.data;
+
+import lombok.Getter;
+import me.lortseam.completeconfig.api.ConfigEntryContainer;
+import me.lortseam.completeconfig.data.gui.TranslationIdentifier;
+import net.minecraft.text.TextColor;
+import org.spongepowered.configurate.serialize.CoercionFailedException;
+import org.spongepowered.configurate.serialize.ScalarSerializer;
+import org.spongepowered.configurate.serialize.TypeSerializer;
+
+import java.lang.reflect.Field;
+
+public class ColorEntry<T> extends Entry<T> {
+
+    public static final ScalarSerializer<TextColor> TEXT_COLOR_SERIALIZER = TypeSerializer.of(TextColor.class, (item, typeSupported) -> {
+        return item.getRgb();
+    }, value -> {
+        if (value instanceof Integer) {
+            return TextColor.fromRgb((int) value);
+        }
+        throw new CoercionFailedException(value, "TextColor");
+    });
+
+    @Getter
+    private final boolean alphaMode;
+
+    protected ColorEntry(Field field, ConfigEntryContainer parentObject, TranslationIdentifier parentTranslation, boolean alphaMode) {
+        super(field, parentObject, parentTranslation);
+        this.alphaMode = alphaMode;
+    }
+
+}

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

@@ -75,6 +75,9 @@ public class Entry<T> extends EntryBase<T> implements DataPart {
         } else if (field.isAnnotationPresent(ConfigEntry.EnumOptions.class)) {
             throw new IllegalAnnotationTargetException("Cannot apply enum options to non enum field " + field);
         }
+        if (field.isAnnotationPresent(ConfigEntry.Color.class)) {
+            return new ColorEntry<>(field, parentObject, parentTranslation, field.getDeclaredAnnotation(ConfigEntry.Color.class).alphaMode());
+        }
         return new Entry<>(field, parentObject, parentTranslation);
     }
 

+ 23 - 0
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.data.BoundedEntry;
+import me.lortseam.completeconfig.data.ColorEntry;
 import me.lortseam.completeconfig.data.Entry;
 import me.lortseam.completeconfig.data.EnumEntry;
 import me.shedaniel.clothconfig2.api.AbstractConfigListEntry;
@@ -12,6 +13,7 @@ import me.shedaniel.clothconfig2.impl.builders.DropdownMenuBuilder;
 import me.shedaniel.clothconfig2.impl.builders.FieldBuilder;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
+import net.minecraft.text.TextColor;
 import org.apache.commons.lang3.ArrayUtils;
 
 import java.lang.reflect.Type;
@@ -61,6 +63,10 @@ public class GuiRegistry {
         registerProvider(provider, entry -> entry instanceof EnumEntry && ((EnumEntry<?>) entry).getDisplayType() == enumDisplayType);
     }
 
+    private void registerColorProvider(GuiProvider<? extends ColorEntry<?>> provider, boolean alphaModeSupported, Type... types) {
+        registerProvider(provider, entry -> entry instanceof ColorEntry<?> && (!((ColorEntry) entry).isAlphaMode() || alphaModeSupported), types);
+    }
+
     private void registerDefaultProviders() {
        registerProvider((Entry<Boolean> entry) -> build(
                builder -> builder
@@ -96,6 +102,15 @@ public class GuiRegistry {
                        .setSaveConsumer(entry::setValue),
                entry.requiresRestart()
        ), true, int.class, Integer.class);
+       registerColorProvider((ColorEntry<Integer> entry) -> build(
+               builder -> builder
+                       .startColorField(entry.getText(), entry.getValue())
+                       .setDefaultValue(entry.getDefaultValue())
+                       .setAlphaMode(entry.isAlphaMode())
+                       .setTooltip(entry.getTooltip())
+                       .setSaveConsumer(entry::setValue),
+               entry.requiresRestart()
+       ), true, int.class, Integer.class);
        registerProvider((Entry<Long> entry) -> build(
                builder -> builder
                        .startLongField(entry.getText(), entry.getValue())
@@ -230,6 +245,14 @@ public class GuiRegistry {
                        .setSaveConsumer(entry::setValue),
                entry.requiresRestart()
        ), new TypeToken<List<String>>() {}.getType());
+       registerColorProvider((ColorEntry<TextColor> entry) -> build(
+               builder -> builder
+                       .startColorField(entry.getText(), entry.getValue())
+                       .setDefaultValue(entry.getDefaultValue())
+                       .setTooltip(entry.getTooltip())
+                       .setSaveConsumer3(entry::setValue),
+               entry.requiresRestart()
+       ), false, TextColor.class);
     }
 
     <E extends Entry<?>> Optional<GuiProvider<E>> getProvider(E entry) {