From 3e30af10ab48aa64fa659834a863a4e3067d5542 Mon Sep 17 00:00:00 2001 From: isXander Date: Tue, 15 Aug 2023 13:52:03 +0100 Subject: Add number fields --- .../yacl3/config/v2/api/autogen/DoubleField.java | 16 ++++++++++++ .../yacl3/config/v2/api/autogen/FloatField.java | 16 ++++++++++++ .../yacl3/config/v2/api/autogen/IntField.java | 14 +++++++++++ .../yacl3/config/v2/api/autogen/LongField.java | 14 +++++++++++ .../config/v2/impl/ConfigClassHandlerImpl.java | 9 ++++--- .../config/v2/impl/ReflectionFieldAccess.java | 5 ++-- .../config/v2/impl/autogen/DoubleFieldImpl.java | 29 ++++++++++++++++++++++ .../config/v2/impl/autogen/FloatFieldImpl.java | 29 ++++++++++++++++++++++ .../yacl3/config/v2/impl/autogen/IntFieldImpl.java | 25 +++++++++++++++++++ .../config/v2/impl/autogen/LongFieldImpl.java | 25 +++++++++++++++++++ .../v2/impl/autogen/OptionFactoryRegistry.java | 4 +++ 11 files changed, 181 insertions(+), 5 deletions(-) create mode 100644 common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/DoubleField.java create mode 100644 common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/FloatField.java create mode 100644 common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/IntField.java create mode 100644 common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/LongField.java create mode 100644 common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/DoubleFieldImpl.java create mode 100644 common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/FloatFieldImpl.java create mode 100644 common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/IntFieldImpl.java create mode 100644 common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/LongFieldImpl.java (limited to 'common/src/main') diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/DoubleField.java b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/DoubleField.java new file mode 100644 index 0000000..225fe8e --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/DoubleField.java @@ -0,0 +1,16 @@ +package dev.isxander.yacl3.config.v2.api.autogen; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface DoubleField { + double min(); + + double max(); + + String format() default "%.2f"; +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/FloatField.java b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/FloatField.java new file mode 100644 index 0000000..a14b36a --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/FloatField.java @@ -0,0 +1,16 @@ +package dev.isxander.yacl3.config.v2.api.autogen; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface FloatField { + float min(); + + float max(); + + String format() default "%.1f"; +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/IntField.java b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/IntField.java new file mode 100644 index 0000000..88b5827 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/IntField.java @@ -0,0 +1,14 @@ +package dev.isxander.yacl3.config.v2.api.autogen; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface IntField { + int min(); + + int max(); +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/LongField.java b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/LongField.java new file mode 100644 index 0000000..80a29ad --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/LongField.java @@ -0,0 +1,14 @@ +package dev.isxander.yacl3.config.v2.api.autogen; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface LongField { + long min(); + + long max(); +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ConfigClassHandlerImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ConfigClassHandlerImpl.java index 7c0c2a9..df0af15 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ConfigClassHandlerImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ConfigClassHandlerImpl.java @@ -6,6 +6,7 @@ import dev.isxander.yacl3.config.v2.api.autogen.AutoGen; import dev.isxander.yacl3.config.v2.api.autogen.OptionStorage; import dev.isxander.yacl3.config.v2.impl.autogen.OptionFactoryRegistry; import dev.isxander.yacl3.config.v2.impl.autogen.OptionStorageImpl; +import dev.isxander.yacl3.config.v2.impl.autogen.YACLAutoGenException; import dev.isxander.yacl3.platform.YACLPlatform; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; @@ -36,7 +37,7 @@ public class ConfigClassHandlerImpl implements ConfigClassHandler { this.instance = constructor.newInstance(); this.defaults = constructor.newInstance(); } catch (Exception e) { - throw new IllegalArgumentException("Failed to create instance of config class '%s' with no-args constructor.".formatted(configClass.getName()), e); + throw new YACLAutoGenException("Failed to create instance of config class '%s' with no-args constructor.".formatted(configClass.getName()), e); } this.fields = Arrays.stream(configClass.getDeclaredFields()) @@ -79,7 +80,9 @@ public class ConfigClassHandlerImpl implements ConfigClassHandler { @Override public YetAnotherConfigLib generateGui() { - Validate.isTrue(supportsAutoGen(), "Auto GUI generation is not supported for this config class. You either need to enable it in the builder or you are attempting to create a GUI in a dedicated server environment."); + if (!supportsAutoGen()) { + throw new YACLAutoGenException("Auto GUI generation is not supported for this config class. You either need to enable it in the builder or you are attempting to create a GUI in a dedicated server environment."); + } OptionStorageImpl storage = new OptionStorageImpl(); Map categories = new LinkedHashMap<>(); @@ -117,7 +120,7 @@ public class ConfigClassHandlerImpl implements ConfigClassHandler { private Option createOption(ConfigField configField, OptionStorage storage) { return OptionFactoryRegistry.createOption(((ReflectionFieldAccess) configField.access()).field(), configField, storage) - .orElseThrow(() -> new IllegalStateException("Failed to create option for field %s".formatted(configField.access().name()))); + .orElseThrow(() -> new YACLAutoGenException("Failed to create option for field %s".formatted(configField.access().name()))); } @Override diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ReflectionFieldAccess.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ReflectionFieldAccess.java index 7bba083..8bb7fc0 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ReflectionFieldAccess.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/ReflectionFieldAccess.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.config.v2.impl; import dev.isxander.yacl3.config.v2.api.FieldAccess; +import dev.isxander.yacl3.config.v2.impl.autogen.YACLAutoGenException; import java.lang.reflect.Field; import java.lang.reflect.Type; @@ -11,7 +12,7 @@ public record ReflectionFieldAccess(Field field, Object instance) implements try { return (T) field.get(instance); } catch (IllegalAccessException e) { - throw new RuntimeException(e); + throw new YACLAutoGenException("Failed to access field '%s'".formatted(name()), e); } } @@ -20,7 +21,7 @@ public record ReflectionFieldAccess(Field field, Object instance) implements try { field.set(instance, value); } catch (IllegalAccessException e) { - throw new RuntimeException(e); + throw new YACLAutoGenException("Failed to set field '%s'".formatted(name()), e); } } diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/DoubleFieldImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/DoubleFieldImpl.java new file mode 100644 index 0000000..46dace4 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/DoubleFieldImpl.java @@ -0,0 +1,29 @@ +package dev.isxander.yacl3.config.v2.impl.autogen; + +import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ControllerBuilder; +import dev.isxander.yacl3.api.controller.DoubleFieldControllerBuilder; +import dev.isxander.yacl3.config.v2.api.ConfigField; +import dev.isxander.yacl3.config.v2.api.autogen.DoubleField; +import dev.isxander.yacl3.config.v2.api.autogen.OptionStorage; +import dev.isxander.yacl3.config.v2.api.autogen.SimpleOptionFactory; +import net.minecraft.locale.Language; +import net.minecraft.network.chat.Component; + +public class DoubleFieldImpl extends SimpleOptionFactory { + @Override + protected ControllerBuilder createController(DoubleField annotation, ConfigField field, OptionStorage storage, Option option) { + return DoubleFieldControllerBuilder.create(option) + .valueFormatter(v -> { + String key = null; + if (v == annotation.min()) + key = getTranslationKey(field, "fmt.min"); + else if (v == annotation.max()) + key = getTranslationKey(field, "fmt.max"); + if (key != null && Language.getInstance().has(key)) + return Component.translatable(key); + return Component.translatable(String.format(annotation.format(), v)); + }) + .range(annotation.min(), annotation.max()); + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/FloatFieldImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/FloatFieldImpl.java new file mode 100644 index 0000000..1c613f9 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/FloatFieldImpl.java @@ -0,0 +1,29 @@ +package dev.isxander.yacl3.config.v2.impl.autogen; + +import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ControllerBuilder; +import dev.isxander.yacl3.api.controller.FloatFieldControllerBuilder; +import dev.isxander.yacl3.config.v2.api.ConfigField; +import dev.isxander.yacl3.config.v2.api.autogen.FloatField; +import dev.isxander.yacl3.config.v2.api.autogen.OptionStorage; +import dev.isxander.yacl3.config.v2.api.autogen.SimpleOptionFactory; +import net.minecraft.locale.Language; +import net.minecraft.network.chat.Component; + +public class FloatFieldImpl extends SimpleOptionFactory { + @Override + protected ControllerBuilder createController(FloatField annotation, ConfigField field, OptionStorage storage, Option option) { + return FloatFieldControllerBuilder.create(option) + .valueFormatter(v -> { + String key = null; + if (v == annotation.min()) + key = getTranslationKey(field, "fmt.min"); + else if (v == annotation.max()) + key = getTranslationKey(field, "fmt.max"); + if (key != null && Language.getInstance().has(key)) + return Component.translatable(key); + return Component.translatable(String.format(annotation.format(), v)); + }) + .range(annotation.min(), annotation.max()); + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/IntFieldImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/IntFieldImpl.java new file mode 100644 index 0000000..26d6c1f --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/IntFieldImpl.java @@ -0,0 +1,25 @@ +package dev.isxander.yacl3.config.v2.impl.autogen; + +import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ControllerBuilder; +import dev.isxander.yacl3.api.controller.IntegerFieldControllerBuilder; +import dev.isxander.yacl3.config.v2.api.ConfigField; +import dev.isxander.yacl3.config.v2.api.autogen.IntField; +import dev.isxander.yacl3.config.v2.api.autogen.OptionStorage; +import dev.isxander.yacl3.config.v2.api.autogen.SimpleOptionFactory; +import net.minecraft.locale.Language; +import net.minecraft.network.chat.Component; + +public class IntFieldImpl extends SimpleOptionFactory { + @Override + protected ControllerBuilder createController(IntField annotation, ConfigField field, OptionStorage storage, Option option) { + return IntegerFieldControllerBuilder.create(option) + .valueFormatter(v -> { + String key = getTranslationKey(field, "fmt." + v); + if (Language.getInstance().has(key)) + return Component.translatable(key); + return Component.literal(Integer.toString(v)); + }) + .range(annotation.min(), annotation.max()); + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/LongFieldImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/LongFieldImpl.java new file mode 100644 index 0000000..fd38763 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/LongFieldImpl.java @@ -0,0 +1,25 @@ +package dev.isxander.yacl3.config.v2.impl.autogen; + +import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ControllerBuilder; +import dev.isxander.yacl3.api.controller.LongSliderControllerBuilder; +import dev.isxander.yacl3.config.v2.api.ConfigField; +import dev.isxander.yacl3.config.v2.api.autogen.LongField; +import dev.isxander.yacl3.config.v2.api.autogen.OptionStorage; +import dev.isxander.yacl3.config.v2.api.autogen.SimpleOptionFactory; +import net.minecraft.locale.Language; +import net.minecraft.network.chat.Component; + +public class LongFieldImpl extends SimpleOptionFactory { + @Override + protected ControllerBuilder createController(LongField annotation, ConfigField field, OptionStorage storage, Option option) { + return LongSliderControllerBuilder.create(option) + .valueFormatter(v -> { + String key = getTranslationKey(field, "fmt." + v); + if (Language.getInstance().has(key)) + return Component.translatable(key); + return Component.literal(Long.toString(v)); + }) + .range(annotation.min(), annotation.max()); + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/OptionFactoryRegistry.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/OptionFactoryRegistry.java index 28d8bb3..7e440a4 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/OptionFactoryRegistry.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/OptionFactoryRegistry.java @@ -24,6 +24,10 @@ public class OptionFactoryRegistry { registerOptionFactory(LongSlider.class, new LongSliderImpl()); registerOptionFactory(FloatSlider.class, new FloatSliderImpl()); registerOptionFactory(DoubleSlider.class, new DoubleSliderImpl()); + registerOptionFactory(IntField.class, new IntFieldImpl()); + registerOptionFactory(LongField.class, new LongFieldImpl()); + registerOptionFactory(FloatField.class, new FloatFieldImpl()); + registerOptionFactory(DoubleField.class, new DoubleFieldImpl()); registerOptionFactory(EnumCycler.class, new EnumCyclerImpl()); registerOptionFactory(StringField.class, new StringFieldImpl()); registerOptionFactory(ColorRGBA.class, new ColorRGBAImpl()); -- cgit