From 98f29ec5c30d23999fce37d7daf7aba8f10f25d3 Mon Sep 17 00:00:00 2001 From: isXander Date: Wed, 16 Aug 2023 12:56:15 +0100 Subject: Add @OverrideName and @OverrideFormat --- .../yacl3/api/controller/ControllerBuilder.java | 1 + .../api/controller/ValueFormattableController.java | 7 ++++- .../yacl3/api/controller/ValueFormatter.java | 7 +++++ .../yacl3/config/v2/api/ReadOnlyFieldAccess.java | 4 +++ .../config/v2/api/autogen/OverrideFormatter.java | 17 +++++++++++ .../yacl3/config/v2/api/autogen/OverrideName.java | 18 ++++++++++++ .../config/v2/api/autogen/SimpleOptionFactory.java | 14 +++++++-- .../config/v2/impl/ReflectionFieldAccess.java | 7 +++++ .../yacl3/config/v2/impl/autogen/AutoGenUtils.java | 34 ++++++++++++++++++++++ .../yacl3/config/v2/impl/autogen/BooleanImpl.java | 2 +- .../config/v2/impl/autogen/DoubleFieldImpl.java | 2 +- .../config/v2/impl/autogen/DoubleSliderImpl.java | 2 +- .../config/v2/impl/autogen/EnumCyclerImpl.java | 4 +-- .../config/v2/impl/autogen/FloatFieldImpl.java | 2 +- .../config/v2/impl/autogen/FloatSliderImpl.java | 2 +- .../yacl3/config/v2/impl/autogen/IntFieldImpl.java | 2 +- .../config/v2/impl/autogen/IntSliderImpl.java | 2 +- .../config/v2/impl/autogen/LongFieldImpl.java | 2 +- .../config/v2/impl/autogen/LongSliderImpl.java | 2 +- .../dev/isxander/yacl3/gui/ValueFormatters.java | 15 ++++++++++ .../yacl3/gui/controllers/BooleanController.java | 11 +++++-- .../controllers/cycling/CyclingListController.java | 11 +++++-- .../gui/controllers/cycling/EnumController.java | 5 ++++ .../controllers/slider/DoubleSliderController.java | 11 +++++-- .../controllers/slider/FloatSliderController.java | 11 +++++-- .../slider/IntegerSliderController.java | 11 +++++-- .../controllers/slider/LongSliderController.java | 11 +++++-- .../string/number/DoubleFieldController.java | 5 ++++ .../string/number/FloatFieldController.java | 5 ++++ .../string/number/IntegerFieldController.java | 5 ++++ .../string/number/LongFieldController.java | 5 ++++ .../string/number/NumberFieldController.java | 7 +++-- .../controller/BooleanControllerBuilderImpl.java | 15 +++++----- .../CyclingListControllerBuilderImpl.java | 10 +++---- .../DoubleFieldControllerBuilderImpl.java | 7 +++-- .../DoubleSliderControllerBuilderImpl.java | 7 +++-- .../impl/controller/EnumControllerBuilderImpl.java | 13 +++++++-- .../FloatFieldControllerBuilderImpl.java | 7 +++-- .../FloatSliderControllerBuilderImpl.java | 7 +++-- .../IntegerFieldControllerBuilderImpl.java | 7 +++-- .../IntegerSliderControllerBuilderImpl.java | 7 +++-- .../controller/LongFieldControllerBuilderImpl.java | 7 +++-- .../LongSliderControllerBuilderImpl.java | 7 +++-- .../yacl3/mixin/ContainerEventHandlerMixin.java | 2 +- 44 files changed, 264 insertions(+), 76 deletions(-) create mode 100644 common/src/main/java/dev/isxander/yacl3/api/controller/ValueFormatter.java create mode 100644 common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/OverrideFormatter.java create mode 100644 common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/OverrideName.java create mode 100644 common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/AutoGenUtils.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/ValueFormatters.java (limited to 'common/src/main') diff --git a/common/src/main/java/dev/isxander/yacl3/api/controller/ControllerBuilder.java b/common/src/main/java/dev/isxander/yacl3/api/controller/ControllerBuilder.java index 0cd3a55..bbd49a7 100644 --- a/common/src/main/java/dev/isxander/yacl3/api/controller/ControllerBuilder.java +++ b/common/src/main/java/dev/isxander/yacl3/api/controller/ControllerBuilder.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.api.controller; import dev.isxander.yacl3.api.Controller; import org.jetbrains.annotations.ApiStatus; +@FunctionalInterface public interface ControllerBuilder { @ApiStatus.Internal Controller build(); diff --git a/common/src/main/java/dev/isxander/yacl3/api/controller/ValueFormattableController.java b/common/src/main/java/dev/isxander/yacl3/api/controller/ValueFormattableController.java index af55e55..b886318 100644 --- a/common/src/main/java/dev/isxander/yacl3/api/controller/ValueFormattableController.java +++ b/common/src/main/java/dev/isxander/yacl3/api/controller/ValueFormattableController.java @@ -5,5 +5,10 @@ import net.minecraft.network.chat.Component; import java.util.function.Function; public interface ValueFormattableController> extends ControllerBuilder { - B valueFormatter(Function formatter); + B formatValue(ValueFormatter formatter); + + @Deprecated + default B valueFormatter(Function formatter) { + return formatValue(formatter::apply); + } } diff --git a/common/src/main/java/dev/isxander/yacl3/api/controller/ValueFormatter.java b/common/src/main/java/dev/isxander/yacl3/api/controller/ValueFormatter.java new file mode 100644 index 0000000..aecaf65 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/api/controller/ValueFormatter.java @@ -0,0 +1,7 @@ +package dev.isxander.yacl3.api.controller; + +import net.minecraft.network.chat.Component; + +public interface ValueFormatter { + Component format(T value); +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/api/ReadOnlyFieldAccess.java b/common/src/main/java/dev/isxander/yacl3/config/v2/api/ReadOnlyFieldAccess.java index 1a1c29a..566d60d 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/api/ReadOnlyFieldAccess.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/api/ReadOnlyFieldAccess.java @@ -1,6 +1,8 @@ package dev.isxander.yacl3.config.v2.api; +import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import java.util.Optional; /** * An abstract interface for accessing properties of an instance of a field. @@ -29,4 +31,6 @@ public interface ReadOnlyFieldAccess { * @return the class of the field. */ Class typeClass(); + + Optional getAnnotation(Class annotationClass); } diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/OverrideFormatter.java b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/OverrideFormatter.java new file mode 100644 index 0000000..f97395c --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/OverrideFormatter.java @@ -0,0 +1,17 @@ +package dev.isxander.yacl3.config.v2.api.autogen; + +import dev.isxander.yacl3.api.controller.ValueFormatter; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Allows you to specify a custom {@link ValueFormatter} for a field. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface OverrideFormatter { + Class> value(); +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/OverrideName.java b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/OverrideName.java new file mode 100644 index 0000000..8b73cec --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/OverrideName.java @@ -0,0 +1,18 @@ +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; + +/** + * Overrides the name of an auto-generated option. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface OverrideName { + /** + * The translation key to use for the option's name. + */ + String value() default ""; +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/SimpleOptionFactory.java b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/SimpleOptionFactory.java index 1816fbc..bf886ae 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/SimpleOptionFactory.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/api/autogen/SimpleOptionFactory.java @@ -6,6 +6,7 @@ import dev.isxander.yacl3.api.OptionFlag; import dev.isxander.yacl3.api.controller.ControllerBuilder; import dev.isxander.yacl3.config.v2.api.ConfigField; import dev.isxander.yacl3.config.v2.impl.FieldBackedBinding; +import dev.isxander.yacl3.config.v2.impl.autogen.AutoGenUtils; import net.minecraft.client.Minecraft; import net.minecraft.locale.Language; import net.minecraft.network.chat.Component; @@ -14,6 +15,7 @@ import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.Nullable; import java.lang.annotation.Annotation; +import java.util.Optional; import java.util.Set; public abstract class SimpleOptionFactory implements OptionFactory { @@ -23,7 +25,14 @@ public abstract class SimpleOptionFactory implements Op .name(this.name(annotation, field, optionAccess)) .description(v -> this.description(v, annotation, field, optionAccess).build()) .binding(new FieldBackedBinding<>(field.access(), field.defaultAccess())) - .controller(opt -> this.createController(annotation, field, optionAccess, opt)) + .controller(opt -> { + ControllerBuilder builder = this.createController(annotation, field, optionAccess, opt); + + Optional customFormatter = field.access().getAnnotation(OverrideFormatter.class); + AutoGenUtils.addCustomFormatterToController(builder, customFormatter, field.access()); + + return builder; + }) .available(this.available(annotation, field, optionAccess)) .flags(this.flags(annotation, field, optionAccess)) .listener((opt, v) -> this.listener(annotation, field, optionAccess, opt, v)) @@ -36,7 +45,8 @@ public abstract class SimpleOptionFactory implements Op protected abstract ControllerBuilder createController(A annotation, ConfigField field, OptionAccess storage, Option option); protected MutableComponent name(A annotation, ConfigField field, OptionAccess storage) { - return Component.translatable(this.getTranslationKey(field, null)); + Optional customName = field.access().getAnnotation(OverrideName.class); + return Component.translatable(customName.map(OverrideName::value).orElse(this.getTranslationKey(field, null))); } protected OptionDescription.Builder description(T value, A annotation, ConfigField field, OptionAccess storage) { 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 8bb7fc0..e102344 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 @@ -3,8 +3,10 @@ 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.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Type; +import java.util.Optional; public record ReflectionFieldAccess(Field field, Object instance) implements FieldAccess { @Override @@ -39,4 +41,9 @@ public record ReflectionFieldAccess(Field field, Object instance) implements public Class typeClass() { return (Class) field.getType(); } + + @Override + public Optional getAnnotation(Class annotationClass) { + return Optional.ofNullable(field.getAnnotation(annotationClass)); + } } diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/AutoGenUtils.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/AutoGenUtils.java new file mode 100644 index 0000000..ecd4157 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/AutoGenUtils.java @@ -0,0 +1,34 @@ +package dev.isxander.yacl3.config.v2.impl.autogen; + +import dev.isxander.yacl3.api.controller.ControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormattableController; +import dev.isxander.yacl3.api.controller.ValueFormatter; +import dev.isxander.yacl3.config.v2.api.ReadOnlyFieldAccess; +import dev.isxander.yacl3.config.v2.api.autogen.OverrideFormatter; +import org.jetbrains.annotations.ApiStatus; + +import java.util.Optional; + +@ApiStatus.Internal +public final class AutoGenUtils { + public static void addCustomFormatterToController(ControllerBuilder controller, Optional formatter, ReadOnlyFieldAccess field) { + formatter.ifPresent(formatterClass -> { + if (controller instanceof ValueFormattableController) { + ValueFormattableController typedBuilder; + try { + typedBuilder = (ValueFormattableController) controller; + } catch (ClassCastException e) { + throw new YACLAutoGenException("'%s': The formatter class on @CustomFormatter is of incorrect type. Expected %s, got %s.".formatted(field.name(), field.type().getTypeName(), formatterClass.value().getTypeParameters()[0].getName())); + } + + try { + typedBuilder.formatValue((ValueFormatter) formatterClass.value().getConstructor().newInstance()); + } catch (Exception e) { + throw new YACLAutoGenException("'%s': Failed to instantiate formatter class %s.".formatted(field.name(), formatterClass.value().getName()), e); + } + } else { + throw new YACLAutoGenException("Attempted to use @CustomFormatter on an option factory for field '%s' that uses a controller that does not support this.".formatted(field.name())); + } + }); + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/BooleanImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/BooleanImpl.java index 78b42e2..b41836a 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/BooleanImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/BooleanImpl.java @@ -18,7 +18,7 @@ public class BooleanImpl extends SimpleOptionFactory case ON_OFF -> builder.onOffFormatter(); case YES_NO -> builder.yesNoFormatter(); case TRUE_FALSE -> builder.trueFalseFormatter(); - case CUSTOM -> builder.valueFormatter(v -> Component.translatable(getTranslationKey(field, "fmt." + v))); + case CUSTOM -> builder.formatValue(v -> Component.translatable(getTranslationKey(field, "fmt." + v))); } return builder; } 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 index 16b9654..d53d0f6 100644 --- 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 @@ -14,7 +14,7 @@ public class DoubleFieldImpl extends SimpleOptionFactory { @Override protected ControllerBuilder createController(DoubleField annotation, ConfigField field, OptionAccess storage, Option option) { return DoubleFieldControllerBuilder.create(option) - .valueFormatter(v -> { + .formatValue(v -> { String key = null; if (v == annotation.min()) key = getTranslationKey(field, "fmt.min"); diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/DoubleSliderImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/DoubleSliderImpl.java index e1281d2..5221012 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/DoubleSliderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/DoubleSliderImpl.java @@ -14,7 +14,7 @@ public class DoubleSliderImpl extends SimpleOptionFactory @Override protected ControllerBuilder createController(DoubleSlider annotation, ConfigField field, OptionAccess storage, Option option) { return DoubleSliderControllerBuilder.create(option) - .valueFormatter(v -> { + .formatValue(v -> { String key = null; if (v == annotation.min()) key = getTranslationKey(field, "fmt.min"); diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/EnumCyclerImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/EnumCyclerImpl.java index f577c40..f15d862 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/EnumCyclerImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/EnumCyclerImpl.java @@ -33,9 +33,9 @@ public class EnumCyclerImpl extends SimpleOptionFactory> { var builder = CyclingListControllerBuilder.create(option) .values(values); if (NameableEnum.class.isAssignableFrom(field.access().typeClass())) { - builder.valueFormatter(v -> ((NameableEnum) v).getDisplayName()); + builder.formatValue(v -> ((NameableEnum) v).getDisplayName()); } else { - builder.valueFormatter(v -> Component.translatable("yacl3.config.enum.%s.%s".formatted(field.access().typeClass().getSimpleName(), v.name().toLowerCase()))); + builder.formatValue(v -> Component.translatable("yacl3.config.enum.%s.%s".formatted(field.access().typeClass().getSimpleName(), v.name().toLowerCase()))); } return builder; } 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 index a5eace2..def5169 100644 --- 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 @@ -14,7 +14,7 @@ public class FloatFieldImpl extends SimpleOptionFactory { @Override protected ControllerBuilder createController(FloatField annotation, ConfigField field, OptionAccess storage, Option option) { return FloatFieldControllerBuilder.create(option) - .valueFormatter(v -> { + .formatValue(v -> { String key = null; if (v == annotation.min()) key = getTranslationKey(field, "fmt.min"); diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/FloatSliderImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/FloatSliderImpl.java index ec735a0..6424bea 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/FloatSliderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/FloatSliderImpl.java @@ -14,7 +14,7 @@ public class FloatSliderImpl extends SimpleOptionFactory { @Override protected ControllerBuilder createController(FloatSlider annotation, ConfigField field, OptionAccess storage, Option option) { return FloatSliderControllerBuilder.create(option) - .valueFormatter(v -> { + .formatValue(v -> { String key = null; if (v == annotation.min()) key = getTranslationKey(field, "fmt.min"); 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 index 51e42a8..e5f4e71 100644 --- 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 @@ -14,7 +14,7 @@ public class IntFieldImpl extends SimpleOptionFactory { @Override protected ControllerBuilder createController(IntField annotation, ConfigField field, OptionAccess storage, Option option) { return IntegerFieldControllerBuilder.create(option) - .valueFormatter(v -> { + .formatValue(v -> { String key = getTranslationKey(field, "fmt." + v); if (Language.getInstance().has(key)) return Component.translatable(key); diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/IntSliderImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/IntSliderImpl.java index 1b6c09f..d650837 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/IntSliderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/IntSliderImpl.java @@ -14,7 +14,7 @@ public class IntSliderImpl extends SimpleOptionFactory { @Override protected ControllerBuilder createController(IntSlider annotation, ConfigField field, OptionAccess storage, Option option) { return IntegerSliderControllerBuilder.create(option) - .valueFormatter(v -> { + .formatValue(v -> { String key = getTranslationKey(field, "fmt." + v); if (Language.getInstance().has(key)) return Component.translatable(key); 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 index 09e0255..41d20ca 100644 --- 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 @@ -14,7 +14,7 @@ public class LongFieldImpl extends SimpleOptionFactory { @Override protected ControllerBuilder createController(LongField annotation, ConfigField field, OptionAccess storage, Option option) { return LongFieldControllerBuilder.create(option) - .valueFormatter(v -> { + .formatValue(v -> { String key = getTranslationKey(field, "fmt." + v); if (Language.getInstance().has(key)) return Component.translatable(key); diff --git a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/LongSliderImpl.java b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/LongSliderImpl.java index 6317e81..3c1f778 100644 --- a/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/LongSliderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/config/v2/impl/autogen/LongSliderImpl.java @@ -14,7 +14,7 @@ public class LongSliderImpl extends SimpleOptionFactory { @Override protected ControllerBuilder createController(LongSlider annotation, ConfigField field, OptionAccess storage, Option option) { return LongSliderControllerBuilder.create(option) - .valueFormatter(v -> { + .formatValue(v -> { String key = getTranslationKey(field, "fmt." + v); if (Language.getInstance().has(key)) return Component.translatable(key); diff --git a/common/src/main/java/dev/isxander/yacl3/gui/ValueFormatters.java b/common/src/main/java/dev/isxander/yacl3/gui/ValueFormatters.java new file mode 100644 index 0000000..3771fc3 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/gui/ValueFormatters.java @@ -0,0 +1,15 @@ +package dev.isxander.yacl3.gui; + +import dev.isxander.yacl3.api.controller.ValueFormatter; +import net.minecraft.network.chat.Component; + +public final class ValueFormatters { + public static final PercentFormatter PERCENT = new PercentFormatter(); + + public static final class PercentFormatter implements ValueFormatter { + @Override + public Component format(Float value) { + return Component.literal(String.format("%.0f%%", value * 100)); + } + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/BooleanController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/BooleanController.java index 9f21755..6d2c2b3 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/BooleanController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/BooleanController.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.gui.controllers; import com.mojang.blaze3d.platform.InputConstants; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.api.utils.Dimension; import dev.isxander.yacl3.gui.AbstractWidget; import dev.isxander.yacl3.gui.YACLScreen; @@ -34,7 +35,7 @@ public class BooleanController implements Controller { : CommonComponents.GUI_NO; private final Option option; - private final Function valueFormatter; + private final ValueFormatter valueFormatter; private final boolean coloured; /** @@ -67,10 +68,14 @@ public class BooleanController implements Controller { */ public BooleanController(Option option, Function valueFormatter, boolean coloured) { this.option = option; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; this.coloured = coloured; } + public static BooleanController createInternal(Option option, ValueFormatter formatter, boolean coloured) { + return new BooleanController(option, formatter::format, coloured); + } + /** * {@inheritDoc} */ @@ -84,7 +89,7 @@ public class BooleanController implements Controller { */ @Override public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); + return valueFormatter.format(option().pendingValue()); } /** diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/CyclingListController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/CyclingListController.java index 44fa765..28bc623 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/CyclingListController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/CyclingListController.java @@ -2,6 +2,7 @@ package dev.isxander.yacl3.gui.controllers.cycling; import com.google.common.collect.ImmutableList; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import net.minecraft.network.chat.Component; import java.util.function.Function; @@ -12,7 +13,7 @@ import java.util.function.Function; */ public class CyclingListController implements ICyclingController { private final Option option; - private final Function valueFormatter; + private final ValueFormatter valueFormatter; private final ImmutableList values; /** @@ -33,10 +34,14 @@ public class CyclingListController implements ICyclingController { */ public CyclingListController(Option option, Iterable values, Function valueFormatter) { this.option = option; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; this.values = ImmutableList.copyOf(values); } + public static CyclingListController createInternal(Option option, Iterable values, ValueFormatter formatter) { + return new CyclingListController<>(option, values, formatter::format); + } + /** * {@inheritDoc} */ @@ -50,7 +55,7 @@ public class CyclingListController implements ICyclingController { */ @Override public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); + return valueFormatter.format(option().pendingValue()); } /** diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/EnumController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/EnumController.java index d2df8f8..5a6a912 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/EnumController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/EnumController.java @@ -2,6 +2,7 @@ package dev.isxander.yacl3.gui.controllers.cycling; import dev.isxander.yacl3.api.NameableEnum; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import net.minecraft.network.chat.Component; import net.minecraft.util.OptionEnum; @@ -40,4 +41,8 @@ public class EnumController> extends CyclingListController public EnumController(Option option, Function valueFormatter, T[] availableValues) { super(option, Arrays.asList(availableValues), valueFormatter); } + + public static > EnumController createInternal(Option option, ValueFormatter formatter, T[] values) { + return new EnumController<>(option, formatter::format, values); + } } diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/DoubleSliderController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/DoubleSliderController.java index d2c0e2e..89308a8 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/DoubleSliderController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/DoubleSliderController.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.gui.controllers.slider; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import net.minecraft.network.chat.Component; import org.apache.commons.lang3.Validate; @@ -19,7 +20,7 @@ public class DoubleSliderController implements ISliderController { private final double min, max, interval; - private final Function valueFormatter; + private final ValueFormatter valueFormatter; /** * Constructs a {@link ISliderController} for doubles @@ -52,7 +53,11 @@ public class DoubleSliderController implements ISliderController { this.min = min; this.max = max; this.interval = interval; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; + } + + public static DoubleSliderController createInternal(Option option, double min, double max, double interval, ValueFormatter formatter) { + return new DoubleSliderController(option, min, max, interval, formatter::format); } /** @@ -68,7 +73,7 @@ public class DoubleSliderController implements ISliderController { */ @Override public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); + return valueFormatter.format(option().pendingValue()); } /** diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/FloatSliderController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/FloatSliderController.java index f9fa574..79246dd 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/FloatSliderController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/FloatSliderController.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.gui.controllers.slider; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import net.minecraft.network.chat.Component; import org.apache.commons.lang3.Validate; @@ -19,7 +20,7 @@ public class FloatSliderController implements ISliderController { private final float min, max, interval; - private final Function valueFormatter; + private final ValueFormatter valueFormatter; /** * Constructs a {@link ISliderController} for floats @@ -52,7 +53,11 @@ public class FloatSliderController implements ISliderController { this.min = min; this.max = max; this.interval = interval; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; + } + + public static FloatSliderController createInternal(Option option, float min, float max, float interval, ValueFormatter formatter) { + return new FloatSliderController(option, min, max, interval, formatter::format); } /** @@ -68,7 +73,7 @@ public class FloatSliderController implements ISliderController { */ @Override public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); + return valueFormatter.format(option().pendingValue()); } /** diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/IntegerSliderController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/IntegerSliderController.java index ea658ad..bcb551d 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/IntegerSliderController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/IntegerSliderController.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.gui.controllers.slider; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import net.minecraft.network.chat.Component; import org.apache.commons.lang3.Validate; @@ -16,7 +17,7 @@ public class IntegerSliderController implements ISliderController { private final int min, max, interval; - private final Function valueFormatter; + private final ValueFormatter valueFormatter; /** * Constructs a {@link ISliderController} for integers @@ -49,7 +50,11 @@ public class IntegerSliderController implements ISliderController { this.min = min; this.max = max; this.interval = interval; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; + } + + public static IntegerSliderController createInternal(Option option, int min, int max, int interval, ValueFormatter formatter) { + return new IntegerSliderController(option, min, max, interval, formatter::format); } /** @@ -65,7 +70,7 @@ public class IntegerSliderController implements ISliderController { */ @Override public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); + return valueFormatter.format(option().pendingValue()); } /** diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/LongSliderController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/LongSliderController.java index 336f064..105bd46 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/LongSliderController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/slider/LongSliderController.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.gui.controllers.slider; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import net.minecraft.network.chat.Component; import org.apache.commons.lang3.Validate; @@ -16,7 +17,7 @@ public class LongSliderController implements ISliderController { private final long min, max, interval; - private final Function valueFormatter; + private final ValueFormatter valueFormatter; /** * Constructs a {@link ISliderController} for longs @@ -49,7 +50,11 @@ public class LongSliderController implements ISliderController { this.min = min; this.max = max; this.interval = interval; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; + } + + public static LongSliderController createInternal(Option option, long min, long max, long interval, ValueFormatter formatter) { + return new LongSliderController(option, min, max, interval, formatter::format); } /** @@ -65,7 +70,7 @@ public class LongSliderController implements ISliderController { */ @Override public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); + return valueFormatter.format(option().pendingValue()); } /** diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/DoubleFieldController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/DoubleFieldController.java index 3f70a12..8445839 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/DoubleFieldController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/DoubleFieldController.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.gui.controllers.string.number; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.DoubleSliderController; import net.minecraft.network.chat.Component; @@ -62,6 +63,10 @@ public class DoubleFieldController extends NumberFieldController { this(option, -Double.MAX_VALUE, Double.MAX_VALUE, DoubleSliderController.DEFAULT_FORMATTER); } + public static DoubleFieldController createInternal(Option option, double min, double max, ValueFormatter formatter) { + return new DoubleFieldController(option, min, max, formatter::format); + } + /** * {@inheritDoc} */ diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/FloatFieldController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/FloatFieldController.java index 57737f7..78281c8 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/FloatFieldController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/FloatFieldController.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.gui.controllers.string.number; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.FloatSliderController; import net.minecraft.network.chat.Component; @@ -62,6 +63,10 @@ public class FloatFieldController extends NumberFieldController { this(option, -Float.MAX_VALUE, Float.MAX_VALUE, FloatSliderController.DEFAULT_FORMATTER); } + public static FloatFieldController createInternal(Option option, float min, float max, ValueFormatter formatter) { + return new FloatFieldController(option, min, max, formatter::format); + } + /** * {@inheritDoc} */ diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/IntegerFieldController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/IntegerFieldController.java index 445e984..476da9c 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/IntegerFieldController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/IntegerFieldController.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.gui.controllers.string.number; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.IntegerSliderController; import net.minecraft.network.chat.Component; @@ -62,6 +63,10 @@ public class IntegerFieldController extends NumberFieldController { this(option, -Integer.MAX_VALUE, Integer.MAX_VALUE, IntegerSliderController.DEFAULT_FORMATTER); } + public static IntegerFieldController createInternal(Option option, int min, int max, ValueFormatter formatter) { + return new IntegerFieldController(option, min, max, formatter::format); + } + @Override public boolean isInputValid(String input) { return input.matches("(?:-?\\d+|)"); diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/LongFieldController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/LongFieldController.java index fe2ef2c..d6f4f19 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/LongFieldController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/LongFieldController.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.gui.controllers.string.number; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.LongSliderController; import net.minecraft.network.chat.Component; @@ -62,6 +63,10 @@ public class LongFieldController extends NumberFieldController { this(option, -Long.MAX_VALUE, Long.MAX_VALUE, LongSliderController.DEFAULT_FORMATTER); } + public static LongFieldController createInternal(Option option, long min, long max, ValueFormatter formatter) { + return new LongFieldController(option, min, max, formatter::format); + } + @Override public boolean isInputValid(String input) { return input.matches("(?:-?\\d+|)"); diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java index f484aad..b164ea3 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java @@ -1,6 +1,7 @@ package dev.isxander.yacl3.gui.controllers.string.number; import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.api.utils.Dimension; import dev.isxander.yacl3.gui.AbstractWidget; import dev.isxander.yacl3.gui.YACLScreen; @@ -20,11 +21,11 @@ import java.util.function.Function; */ public abstract class NumberFieldController implements ISliderController, IStringController { private final Option option; - private final Function displayFormatter; + private final ValueFormatter displayFormatter; public NumberFieldController(Option option, Function displayFormatter) { this.option = option; - this.displayFormatter = displayFormatter; + this.displayFormatter = displayFormatter::apply; } @Override @@ -50,7 +51,7 @@ public abstract class NumberFieldController implements ISlider @Override public Component formatValue() { - return displayFormatter.apply(option().pendingValue()); + return displayFormatter.format(option().pendingValue()); } @Override diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/BooleanControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/BooleanControllerBuilderImpl.java index 208f78e..063a177 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/controller/BooleanControllerBuilderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/BooleanControllerBuilderImpl.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.impl.controller; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.BooleanControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.BooleanController; import net.minecraft.network.chat.Component; import org.apache.commons.lang3.Validate; @@ -11,7 +12,7 @@ import java.util.function.Function; public class BooleanControllerBuilderImpl extends AbstractControllerBuilderImpl implements BooleanControllerBuilder { private boolean coloured = false; - private Function formatter = BooleanController.ON_OFF_FORMATTER; + private ValueFormatter formatter = BooleanController.ON_OFF_FORMATTER::apply; public BooleanControllerBuilderImpl(Option option) { super(option); @@ -24,8 +25,8 @@ public class BooleanControllerBuilderImpl extends AbstractControllerBuilderImpl< } @Override - public BooleanControllerBuilder valueFormatter(Function formatter) { - Validate.notNull(formatter); + public BooleanControllerBuilder formatValue(ValueFormatter formatter) { + Validate.notNull(formatter, "formatter cannot be null"); this.formatter = formatter; return this; @@ -33,24 +34,24 @@ public class BooleanControllerBuilderImpl extends AbstractControllerBuilderImpl< @Override public BooleanControllerBuilder onOffFormatter() { - this.formatter = BooleanController.ON_OFF_FORMATTER; + this.formatter = BooleanController.ON_OFF_FORMATTER::apply; return this; } @Override public BooleanControllerBuilder yesNoFormatter() { - this.formatter = BooleanController.YES_NO_FORMATTER; + this.formatter = BooleanController.YES_NO_FORMATTER::apply; return this; } @Override public BooleanControllerBuilder trueFalseFormatter() { - this.formatter = BooleanController.TRUE_FALSE_FORMATTER; + this.formatter = BooleanController.TRUE_FALSE_FORMATTER::apply; return this; } @Override public Controller build() { - return new BooleanController(option, formatter, coloured); + return BooleanController.createInternal(option, formatter, coloured); } } diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/CyclingListControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/CyclingListControllerBuilderImpl.java index 63045f2..8e2e481 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/controller/CyclingListControllerBuilderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/CyclingListControllerBuilderImpl.java @@ -4,14 +4,12 @@ import com.google.common.collect.ImmutableList; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.CyclingListControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.cycling.CyclingListController; -import net.minecraft.network.chat.Component; - -import java.util.function.Function; public final class CyclingListControllerBuilderImpl extends AbstractControllerBuilderImpl implements CyclingListControllerBuilder { private Iterable values; - private Function formatter = null; + private ValueFormatter formatter = null; public CyclingListControllerBuilderImpl(Option option) { super(option); @@ -31,13 +29,13 @@ public final class CyclingListControllerBuilderImpl extends AbstractControlle } @Override - public CyclingListControllerBuilder valueFormatter(Function formatter) { + public CyclingListControllerBuilder formatValue(ValueFormatter formatter) { this.formatter = formatter; return this; } @Override public Controller build() { - return new CyclingListController<>(option, values, formatter); + return CyclingListController.createInternal(option, values, formatter); } } diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/DoubleFieldControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/DoubleFieldControllerBuilderImpl.java index 56e1d44..8d84e7d 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/controller/DoubleFieldControllerBuilderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/DoubleFieldControllerBuilderImpl.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.impl.controller; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.DoubleFieldControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.DoubleSliderController; import dev.isxander.yacl3.gui.controllers.string.number.DoubleFieldController; import net.minecraft.network.chat.Component; @@ -12,7 +13,7 @@ import java.util.function.Function; public class DoubleFieldControllerBuilderImpl extends AbstractControllerBuilderImpl implements DoubleFieldControllerBuilder { private double min = Double.MIN_VALUE; private double max = Double.MAX_VALUE; - private Function formatter = DoubleSliderController.DEFAULT_FORMATTER; + private ValueFormatter formatter = DoubleSliderController.DEFAULT_FORMATTER::apply; public DoubleFieldControllerBuilderImpl(Option option) { super(option); @@ -38,13 +39,13 @@ public class DoubleFieldControllerBuilderImpl extends AbstractControllerBuilderI } @Override - public DoubleFieldControllerBuilder valueFormatter(Function formatter) { + public DoubleFieldControllerBuilder formatValue(ValueFormatter formatter) { this.formatter = formatter; return this; } @Override public Controller build() { - return new DoubleFieldController(option, min, max, formatter); + return DoubleFieldController.createInternal(option, min, max, formatter); } } diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/DoubleSliderControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/DoubleSliderControllerBuilderImpl.java index a636abf..b696d57 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/controller/DoubleSliderControllerBuilderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/DoubleSliderControllerBuilderImpl.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.impl.controller; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.DoubleSliderControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.DoubleSliderController; import net.minecraft.network.chat.Component; @@ -11,7 +12,7 @@ import java.util.function.Function; public class DoubleSliderControllerBuilderImpl extends AbstractControllerBuilderImpl implements DoubleSliderControllerBuilder { private double min, max; private double step; - private Function formatter = DoubleSliderController.DEFAULT_FORMATTER; + private ValueFormatter formatter = DoubleSliderController.DEFAULT_FORMATTER::apply; public DoubleSliderControllerBuilderImpl(Option option) { super(option); @@ -31,13 +32,13 @@ public class DoubleSliderControllerBuilderImpl extends AbstractControllerBuilder } @Override - public DoubleSliderControllerBuilder valueFormatter(Function formatter) { + public DoubleSliderControllerBuilder formatValue(ValueFormatter formatter) { this.formatter = formatter; return this; } @Override public Controller build() { - return new DoubleSliderController(option, min, max, step, formatter); + return DoubleSliderController.createInternal(option, min, max, step, formatter); } } diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/EnumControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/EnumControllerBuilderImpl.java index 3237dbf..04ee2a0 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/controller/EnumControllerBuilderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/EnumControllerBuilderImpl.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.impl.controller; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.EnumControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.cycling.EnumController; import net.minecraft.network.chat.Component; @@ -10,7 +11,7 @@ import java.util.function.Function; public class EnumControllerBuilderImpl> extends AbstractControllerBuilderImpl implements EnumControllerBuilder { private Class enumClass; - private Function formatter = EnumController.getDefaultFormatter(); + private ValueFormatter formatter = null; public EnumControllerBuilderImpl(Option option) { super(option); @@ -23,13 +24,19 @@ public class EnumControllerBuilderImpl> extends AbstractContro } @Override - public EnumControllerBuilder valueFormatter(Function formatter) { + public EnumControllerBuilder formatValue(ValueFormatter formatter) { this.formatter = formatter; return this; } @Override public Controller build() { - return new EnumController<>(option, formatter, enumClass.getEnumConstants()); + ValueFormatter formatter = this.formatter; + if (formatter == null) { + Function formatFunction = EnumController.getDefaultFormatter(); + formatter = formatFunction::apply; + } + + return EnumController.createInternal(option, formatter, enumClass.getEnumConstants()); } } diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/FloatFieldControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/FloatFieldControllerBuilderImpl.java index 09dbce0..08fefd0 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/controller/FloatFieldControllerBuilderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/FloatFieldControllerBuilderImpl.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.impl.controller; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.FloatFieldControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.FloatSliderController; import dev.isxander.yacl3.gui.controllers.string.number.FloatFieldController; import net.minecraft.network.chat.Component; @@ -12,7 +13,7 @@ import java.util.function.Function; public class FloatFieldControllerBuilderImpl extends AbstractControllerBuilderImpl implements FloatFieldControllerBuilder { private float min = Float.MIN_VALUE; private float max = Float.MAX_VALUE; - private Function formatter = FloatSliderController.DEFAULT_FORMATTER; + private ValueFormatter formatter = FloatSliderController.DEFAULT_FORMATTER::apply; public FloatFieldControllerBuilderImpl(Option option) { super(option); @@ -38,13 +39,13 @@ public class FloatFieldControllerBuilderImpl extends AbstractControllerBuilderIm } @Override - public FloatFieldControllerBuilder valueFormatter(Function formatter) { + public FloatFieldControllerBuilder formatValue(ValueFormatter formatter) { this.formatter = formatter; return this; } @Override public Controller build() { - return new FloatFieldController(option, min, max, formatter); + return FloatFieldController.createInternal(option, min, max, formatter); } } diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/FloatSliderControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/FloatSliderControllerBuilderImpl.java index c9699fb..9b2d75b 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/controller/FloatSliderControllerBuilderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/FloatSliderControllerBuilderImpl.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.impl.controller; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.FloatSliderControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.FloatSliderController; import net.minecraft.network.chat.Component; @@ -11,7 +12,7 @@ import java.util.function.Function; public class FloatSliderControllerBuilderImpl extends AbstractControllerBuilderImpl implements FloatSliderControllerBuilder { private float min, max; private float step; - private Function formatter = FloatSliderController.DEFAULT_FORMATTER; + private ValueFormatter formatter = FloatSliderController.DEFAULT_FORMATTER::apply; public FloatSliderControllerBuilderImpl(Option option) { super(option); @@ -31,13 +32,13 @@ public class FloatSliderControllerBuilderImpl extends AbstractControllerBuilderI } @Override - public FloatSliderControllerBuilder valueFormatter(Function formatter) { + public FloatSliderControllerBuilder formatValue(ValueFormatter formatter) { this.formatter = formatter; return this; } @Override public Controller build() { - return new FloatSliderController(option, min, max, step, formatter); + return FloatSliderController.createInternal(option, min, max, step, formatter); } } diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/IntegerFieldControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/IntegerFieldControllerBuilderImpl.java index 73b51a0..1435c49 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/controller/IntegerFieldControllerBuilderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/IntegerFieldControllerBuilderImpl.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.impl.controller; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.IntegerFieldControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.IntegerSliderController; import dev.isxander.yacl3.gui.controllers.string.number.IntegerFieldController; import net.minecraft.network.chat.Component; @@ -12,7 +13,7 @@ import java.util.function.Function; public class IntegerFieldControllerBuilderImpl extends AbstractControllerBuilderImpl implements IntegerFieldControllerBuilder { private int min = Integer.MIN_VALUE; private int max = Integer.MAX_VALUE; - private Function formatter = IntegerSliderController.DEFAULT_FORMATTER; + private ValueFormatter formatter = IntegerSliderController.DEFAULT_FORMATTER::apply; public IntegerFieldControllerBuilderImpl(Option option) { super(option); @@ -38,13 +39,13 @@ public class IntegerFieldControllerBuilderImpl extends AbstractControllerBuilder } @Override - public IntegerFieldControllerBuilder valueFormatter(Function formatter) { + public IntegerFieldControllerBuilder formatValue(ValueFormatter formatter) { this.formatter = formatter; return this; } @Override public Controller build() { - return new IntegerFieldController(option, min, max, formatter); + return IntegerFieldController.createInternal(option, min, max, formatter); } } diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/IntegerSliderControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/IntegerSliderControllerBuilderImpl.java index 106c22a..b9395a0 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/controller/IntegerSliderControllerBuilderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/IntegerSliderControllerBuilderImpl.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.impl.controller; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.IntegerSliderControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.IntegerSliderController; import net.minecraft.network.chat.Component; @@ -11,7 +12,7 @@ import java.util.function.Function; public class IntegerSliderControllerBuilderImpl extends AbstractControllerBuilderImpl implements IntegerSliderControllerBuilder { private int min, max; private int step; - private Function formatter = IntegerSliderController.DEFAULT_FORMATTER; + private ValueFormatter formatter = IntegerSliderController.DEFAULT_FORMATTER::apply; public IntegerSliderControllerBuilderImpl(Option option) { super(option); @@ -31,13 +32,13 @@ public class IntegerSliderControllerBuilderImpl extends AbstractControllerBuilde } @Override - public IntegerSliderControllerBuilder valueFormatter(Function formatter) { + public IntegerSliderControllerBuilder formatValue(ValueFormatter formatter) { this.formatter = formatter; return this; } @Override public Controller build() { - return new IntegerSliderController(option, min, max, step, formatter); + return IntegerSliderController.createInternal(option, min, max, step, formatter); } } diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/LongFieldControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/LongFieldControllerBuilderImpl.java index 88cb610..c7a3ea4 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/controller/LongFieldControllerBuilderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/LongFieldControllerBuilderImpl.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.impl.controller; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.LongFieldControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.LongSliderController; import dev.isxander.yacl3.gui.controllers.string.number.LongFieldController; import net.minecraft.network.chat.Component; @@ -12,7 +13,7 @@ import java.util.function.Function; public class LongFieldControllerBuilderImpl extends AbstractControllerBuilderImpl implements LongFieldControllerBuilder { private long min = Long.MIN_VALUE; private long max = Long.MAX_VALUE; - private Function formatter = LongSliderController.DEFAULT_FORMATTER; + private ValueFormatter formatter = LongSliderController.DEFAULT_FORMATTER::apply; public LongFieldControllerBuilderImpl(Option option) { super(option); @@ -38,13 +39,13 @@ public class LongFieldControllerBuilderImpl extends AbstractControllerBuilderImp } @Override - public LongFieldControllerBuilder valueFormatter(Function formatter) { + public LongFieldControllerBuilder formatValue(ValueFormatter formatter) { this.formatter = formatter; return this; } @Override public Controller build() { - return new LongFieldController(option, min, max, formatter); + return LongFieldController.createInternal(option, min, max, formatter); } } diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/LongSliderControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/LongSliderControllerBuilderImpl.java index 2bd932a..5eda424 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/controller/LongSliderControllerBuilderImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/LongSliderControllerBuilderImpl.java @@ -3,6 +3,7 @@ package dev.isxander.yacl3.impl.controller; import dev.isxander.yacl3.api.Controller; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.LongSliderControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.LongSliderController; import net.minecraft.network.chat.Component; @@ -11,7 +12,7 @@ import java.util.function.Function; public class LongSliderControllerBuilderImpl extends AbstractControllerBuilderImpl implements LongSliderControllerBuilder { private long min, max; private long step; - private Function formatter = LongSliderController.DEFAULT_FORMATTER; + private ValueFormatter formatter = LongSliderController.DEFAULT_FORMATTER::apply; public LongSliderControllerBuilderImpl(Option option) { super(option); @@ -31,13 +32,13 @@ public class LongSliderControllerBuilderImpl extends AbstractControllerBuilderIm } @Override - public LongSliderControllerBuilder valueFormatter(Function formatter) { + public LongSliderControllerBuilder formatValue(ValueFormatter formatter) { this.formatter = formatter; return this; } @Override public Controller build() { - return new LongSliderController(option, min, max, step, formatter); + return LongSliderController.createInternal(option, min, max, step, formatter); } } diff --git a/common/src/main/java/dev/isxander/yacl3/mixin/ContainerEventHandlerMixin.java b/common/src/main/java/dev/isxander/yacl3/mixin/ContainerEventHandlerMixin.java index 37f0c33..9fe8c58 100644 --- a/common/src/main/java/dev/isxander/yacl3/mixin/ContainerEventHandlerMixin.java +++ b/common/src/main/java/dev/isxander/yacl3/mixin/ContainerEventHandlerMixin.java @@ -23,7 +23,7 @@ public interface ContainerEventHandlerMixin { * Without this, navigating right from the option list focuses the tab bar, not the action buttons/description. */ @Redirect(method = {"nextFocusPathVaguelyInDirection", "nextFocusPathInDirection"}, at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/components/events/ContainerEventHandler;children()Ljava/util/List;")) - private List modifyFocusCandidates(ContainerEventHandler instance, ScreenRectangle screenArea, ScreenDirection direction, @Nullable GuiEventListener focused, FocusNavigationEvent event) { + default List modifyFocusCandidates(ContainerEventHandler instance, ScreenRectangle screenArea, ScreenDirection direction, @Nullable GuiEventListener focused, FocusNavigationEvent event) { if (direction.getAxis() == ScreenAxis.HORIZONTAL) return instance.children().stream().filter(child -> !(child instanceof TabNavigationBar)).toList(); return instance.children(); -- cgit