diff options
Diffstat (limited to 'common/src/main/java/dev/isxander/yacl3')
44 files changed, 264 insertions, 76 deletions
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<T> { @ApiStatus.Internal Controller<T> 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<T, B extends ValueFormattableController<T, B>> extends ControllerBuilder<T> { - B valueFormatter(Function<T, Component> formatter); + B formatValue(ValueFormatter<T> formatter); + + @Deprecated + default B valueFormatter(Function<T, Component> 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<T> { + 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<T> { * @return the class of the field. */ Class<T> typeClass(); + + <A extends Annotation> Optional<A> getAnnotation(Class<A> 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<? extends ValueFormatter<?>> 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<A extends Annotation, T> implements OptionFactory<A, T> { @@ -23,7 +25,14 @@ public abstract class SimpleOptionFactory<A extends Annotation, T> 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<T> builder = this.createController(annotation, field, optionAccess, opt); + + Optional<OverrideFormatter> 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<A extends Annotation, T> implements Op protected abstract ControllerBuilder<T> createController(A annotation, ConfigField<T> field, OptionAccess storage, Option<T> option); protected MutableComponent name(A annotation, ConfigField<T> field, OptionAccess storage) { - return Component.translatable(this.getTranslationKey(field, null)); + Optional<OverrideName> 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<T> 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<T>(Field field, Object instance) implements FieldAccess<T> { @Override @@ -39,4 +41,9 @@ public record ReflectionFieldAccess<T>(Field field, Object instance) implements public Class<T> typeClass() { return (Class<T>) field.getType(); } + + @Override + public <A extends Annotation> Optional<A> getAnnotation(Class<A> 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 <T> void addCustomFormatterToController(ControllerBuilder<T> controller, Optional<OverrideFormatter> formatter, ReadOnlyFieldAccess<T> field) { + formatter.ifPresent(formatterClass -> { + if (controller instanceof ValueFormattableController<?,?>) { + ValueFormattableController<T, ?> typedBuilder; + try { + typedBuilder = (ValueFormattableController<T, ?>) 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<T>) 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<Boolean, java.lang.Boolean> 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<DoubleField, Double> { @Override protected ControllerBuilder<Double> createController(DoubleField annotation, ConfigField<Double> field, OptionAccess storage, Option<Double> 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<DoubleSlider, Double> @Override protected ControllerBuilder<Double> createController(DoubleSlider annotation, ConfigField<Double> field, OptionAccess storage, Option<Double> 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<EnumCycler, Enum<?>> { 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<FloatField, Float> { @Override protected ControllerBuilder<Float> createController(FloatField annotation, ConfigField<Float> field, OptionAccess storage, Option<Float> 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<FloatSlider, Float> { @Override protected ControllerBuilder<Float> createController(FloatSlider annotation, ConfigField<Float> field, OptionAccess storage, Option<Float> 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<IntField, Integer> { @Override protected ControllerBuilder<Integer> createController(IntField annotation, ConfigField<Integer> field, OptionAccess storage, Option<Integer> 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<IntSlider, Integer> { @Override protected ControllerBuilder<Integer> createController(IntSlider annotation, ConfigField<Integer> field, OptionAccess storage, Option<Integer> 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<LongField, Long> { @Override protected ControllerBuilder<Long> createController(LongField annotation, ConfigField<Long> field, OptionAccess storage, Option<Long> 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<LongSlider, Long> { @Override protected ControllerBuilder<Long> createController(LongSlider annotation, ConfigField<Long> field, OptionAccess storage, Option<Long> 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<Float> { + @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<Boolean> { : CommonComponents.GUI_NO; private final Option<Boolean> option; - private final Function<Boolean, Component> valueFormatter; + private final ValueFormatter<Boolean> valueFormatter; private final boolean coloured; /** @@ -67,10 +68,14 @@ public class BooleanController implements Controller<Boolean> { */ public BooleanController(Option<Boolean> option, Function<Boolean, Component> valueFormatter, boolean coloured) { this.option = option; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; this.coloured = coloured; } + public static BooleanController createInternal(Option<Boolean> option, ValueFormatter<Boolean> formatter, boolean coloured) { + return new BooleanController(option, formatter::format, coloured); + } + /** * {@inheritDoc} */ @@ -84,7 +89,7 @@ public class BooleanController implements Controller<Boolean> { */ @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<T> implements ICyclingController<T> { private final Option<T> option; - private final Function<T, Component> valueFormatter; + private final ValueFormatter<T> valueFormatter; private final ImmutableList<T> values; /** @@ -33,10 +34,14 @@ public class CyclingListController<T> implements ICyclingController<T> { */ public CyclingListController(Option<T> option, Iterable<? extends T> values, Function<T, Component> valueFormatter) { this.option = option; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; this.values = ImmutableList.copyOf(values); } + public static <T> CyclingListController<T> createInternal(Option<T> option, Iterable<? extends T> values, ValueFormatter<T> formatter) { + return new CyclingListController<>(option, values, formatter::format); + } + /** * {@inheritDoc} */ @@ -50,7 +55,7 @@ public class CyclingListController<T> implements ICyclingController<T> { */ @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<T extends Enum<T>> extends CyclingListController<T> public EnumController(Option<T> option, Function<T, Component> valueFormatter, T[] availableValues) { super(option, Arrays.asList(availableValues), valueFormatter); } + + public static <T extends Enum<T>> EnumController<T> createInternal(Option<T> option, ValueFormatter<T> 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<Double> { private final double min, max, interval; - private final Function<Double, Component> valueFormatter; + private final ValueFormatter<Double> valueFormatter; /** * Constructs a {@link ISliderController} for doubles @@ -52,7 +53,11 @@ public class DoubleSliderController implements ISliderController<Double> { this.min = min; this.max = max; this.interval = interval; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; + } + + public static DoubleSliderController createInternal(Option<Double> option, double min, double max, double interval, ValueFormatter<Double> formatter) { + return new DoubleSliderController(option, min, max, interval, formatter::format); } /** @@ -68,7 +73,7 @@ public class DoubleSliderController implements ISliderController<Double> { */ @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<Float> { private final float min, max, interval; - private final Function<Float, Component> valueFormatter; + private final ValueFormatter<Float> valueFormatter; /** * Constructs a {@link ISliderController} for floats @@ -52,7 +53,11 @@ public class FloatSliderController implements ISliderController<Float> { this.min = min; this.max = max; this.interval = interval; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; + } + + public static FloatSliderController createInternal(Option<Float> option, float min, float max, float interval, ValueFormatter<Float> formatter) { + return new FloatSliderController(option, min, max, interval, formatter::format); } /** @@ -68,7 +73,7 @@ public class FloatSliderController implements ISliderController<Float> { */ @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<Integer> { private final int min, max, interval; - private final Function<Integer, Component> valueFormatter; + private final ValueFormatter<Integer> valueFormatter; /** * Constructs a {@link ISliderController} for integers @@ -49,7 +50,11 @@ public class IntegerSliderController implements ISliderController<Integer> { this.min = min; this.max = max; this.interval = interval; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; + } + + public static IntegerSliderController createInternal(Option<Integer> option, int min, int max, int interval, ValueFormatter<Integer> formatter) { + return new IntegerSliderController(option, min, max, interval, formatter::format); } /** @@ -65,7 +70,7 @@ public class IntegerSliderController implements ISliderController<Integer> { */ @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<Long> { private final long min, max, interval; - private final Function<Long, Component> valueFormatter; + private final ValueFormatter<Long> valueFormatter; /** * Constructs a {@link ISliderController} for longs @@ -49,7 +50,11 @@ public class LongSliderController implements ISliderController<Long> { this.min = min; this.max = max; this.interval = interval; - this.valueFormatter = valueFormatter; + this.valueFormatter = valueFormatter::apply; + } + + public static LongSliderController createInternal(Option<Long> option, long min, long max, long interval, ValueFormatter<Long> formatter) { + return new LongSliderController(option, min, max, interval, formatter::format); } /** @@ -65,7 +70,7 @@ public class LongSliderController implements ISliderController<Long> { */ @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<Double> { this(option, -Double.MAX_VALUE, Double.MAX_VALUE, DoubleSliderController.DEFAULT_FORMATTER); } + public static DoubleFieldController createInternal(Option<Double> option, double min, double max, ValueFormatter<Double> 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<Float> { this(option, -Float.MAX_VALUE, Float.MAX_VALUE, FloatSliderController.DEFAULT_FORMATTER); } + public static FloatFieldController createInternal(Option<Float> option, float min, float max, ValueFormatter<Float> 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<Integer> { this(option, -Integer.MAX_VALUE, Integer.MAX_VALUE, IntegerSliderController.DEFAULT_FORMATTER); } + public static IntegerFieldController createInternal(Option<Integer> option, int min, int max, ValueFormatter<Integer> 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<Long> { this(option, -Long.MAX_VALUE, Long.MAX_VALUE, LongSliderController.DEFAULT_FORMATTER); } + public static LongFieldController createInternal(Option<Long> option, long min, long max, ValueFormatter<Long> 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<T extends Number> implements ISliderController<T>, IStringController<T> { private final Option<T> option; - private final Function<T, Component> displayFormatter; + private final ValueFormatter<T> displayFormatter; public NumberFieldController(Option<T> option, Function<T, Component> displayFormatter) { this.option = option; - this.displayFormatter = displayFormatter; + this.displayFormatter = displayFormatter::apply; } @Override @@ -50,7 +51,7 @@ public abstract class NumberFieldController<T extends Number> 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<Boolean> implements BooleanControllerBuilder { private boolean coloured = false; - private Function<Boolean, Component> formatter = BooleanController.ON_OFF_FORMATTER; + private ValueFormatter<Boolean> formatter = BooleanController.ON_OFF_FORMATTER::apply; public BooleanControllerBuilderImpl(Option<Boolean> option) { super(option); @@ -24,8 +25,8 @@ public class BooleanControllerBuilderImpl extends AbstractControllerBuilderImpl< } @Override - public BooleanControllerBuilder valueFormatter(Function<Boolean, Component> formatter) { - Validate.notNull(formatter); + public BooleanControllerBuilder formatValue(ValueFormatter<Boolean> 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<Boolean> 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<T> extends AbstractControllerBuilderImpl<T> implements CyclingListControllerBuilder<T> { private Iterable<? extends T> values; - private Function<T, Component> formatter = null; + private ValueFormatter<T> formatter = null; public CyclingListControllerBuilderImpl(Option<T> option) { super(option); @@ -31,13 +29,13 @@ public final class CyclingListControllerBuilderImpl<T> extends AbstractControlle } @Override - public CyclingListControllerBuilder<T> valueFormatter(Function<T, Component> formatter) { + public CyclingListControllerBuilder<T> formatValue(ValueFormatter<T> formatter) { this.formatter = formatter; return this; } @Override public Controller<T> 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<Double> implements DoubleFieldControllerBuilder { private double min = Double.MIN_VALUE; private double max = Double.MAX_VALUE; - private Function<Double, Component> formatter = DoubleSliderController.DEFAULT_FORMATTER; + private ValueFormatter<Double> formatter = DoubleSliderController.DEFAULT_FORMATTER::apply; public DoubleFieldControllerBuilderImpl(Option<Double> option) { super(option); @@ -38,13 +39,13 @@ public class DoubleFieldControllerBuilderImpl extends AbstractControllerBuilderI } @Override - public DoubleFieldControllerBuilder valueFormatter(Function<Double, Component> formatter) { + public DoubleFieldControllerBuilder formatValue(ValueFormatter<Double> formatter) { this.formatter = formatter; return this; } @Override public Controller<Double> 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<Double> implements DoubleSliderControllerBuilder { private double min, max; private double step; - private Function<Double, Component> formatter = DoubleSliderController.DEFAULT_FORMATTER; + private ValueFormatter<Double> formatter = DoubleSliderController.DEFAULT_FORMATTER::apply; public DoubleSliderControllerBuilderImpl(Option<Double> option) { super(option); @@ -31,13 +32,13 @@ public class DoubleSliderControllerBuilderImpl extends AbstractControllerBuilder } @Override - public DoubleSliderControllerBuilder valueFormatter(Function<Double, Component> formatter) { + public DoubleSliderControllerBuilder formatValue(ValueFormatter<Double> formatter) { this.formatter = formatter; return this; } @Override public Controller<Double> 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<T extends Enum<T>> extends AbstractControllerBuilderImpl<T> implements EnumControllerBuilder<T> { private Class<T> enumClass; - private Function<T, Component> formatter = EnumController.getDefaultFormatter(); + private ValueFormatter<T> formatter = null; public EnumControllerBuilderImpl(Option<T> option) { super(option); @@ -23,13 +24,19 @@ public class EnumControllerBuilderImpl<T extends Enum<T>> extends AbstractContro } @Override - public EnumControllerBuilder<T> valueFormatter(Function<T, Component> formatter) { + public EnumControllerBuilder<T> formatValue(ValueFormatter<T> formatter) { this.formatter = formatter; return this; } @Override public Controller<T> build() { - return new EnumController<>(option, formatter, enumClass.getEnumConstants()); + ValueFormatter<T> formatter = this.formatter; + if (formatter == null) { + Function<T, Component> 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<Float> implements FloatFieldControllerBuilder { private float min = Float.MIN_VALUE; private float max = Float.MAX_VALUE; - private Function<Float, Component> formatter = FloatSliderController.DEFAULT_FORMATTER; + private ValueFormatter<Float> formatter = FloatSliderController.DEFAULT_FORMATTER::apply; public FloatFieldControllerBuilderImpl(Option<Float> option) { super(option); @@ -38,13 +39,13 @@ public class FloatFieldControllerBuilderImpl extends AbstractControllerBuilderIm } @Override - public FloatFieldControllerBuilder valueFormatter(Function<Float, Component> formatter) { + public FloatFieldControllerBuilder formatValue(ValueFormatter<Float> formatter) { this.formatter = formatter; return this; } @Override public Controller<Float> 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<Float> implements FloatSliderControllerBuilder { private float min, max; private float step; - private Function<Float, Component> formatter = FloatSliderController.DEFAULT_FORMATTER; + private ValueFormatter<Float> formatter = FloatSliderController.DEFAULT_FORMATTER::apply; public FloatSliderControllerBuilderImpl(Option<Float> option) { super(option); @@ -31,13 +32,13 @@ public class FloatSliderControllerBuilderImpl extends AbstractControllerBuilderI } @Override - public FloatSliderControllerBuilder valueFormatter(Function<Float, Component> formatter) { + public FloatSliderControllerBuilder formatValue(ValueFormatter<Float> formatter) { this.formatter = formatter; return this; } @Override public Controller<Float> 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<Integer> implements IntegerFieldControllerBuilder { private int min = Integer.MIN_VALUE; private int max = Integer.MAX_VALUE; - private Function<Integer, Component> formatter = IntegerSliderController.DEFAULT_FORMATTER; + private ValueFormatter<Integer> formatter = IntegerSliderController.DEFAULT_FORMATTER::apply; public IntegerFieldControllerBuilderImpl(Option<Integer> option) { super(option); @@ -38,13 +39,13 @@ public class IntegerFieldControllerBuilderImpl extends AbstractControllerBuilder } @Override - public IntegerFieldControllerBuilder valueFormatter(Function<Integer, Component> formatter) { + public IntegerFieldControllerBuilder formatValue(ValueFormatter<Integer> formatter) { this.formatter = formatter; return this; } @Override public Controller<Integer> 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<Integer> implements IntegerSliderControllerBuilder { private int min, max; private int step; - private Function<Integer, Component> formatter = IntegerSliderController.DEFAULT_FORMATTER; + private ValueFormatter<Integer> formatter = IntegerSliderController.DEFAULT_FORMATTER::apply; public IntegerSliderControllerBuilderImpl(Option<Integer> option) { super(option); @@ -31,13 +32,13 @@ public class IntegerSliderControllerBuilderImpl extends AbstractControllerBuilde } @Override - public IntegerSliderControllerBuilder valueFormatter(Function<Integer, Component> formatter) { + public IntegerSliderControllerBuilder formatValue(ValueFormatter<Integer> formatter) { this.formatter = formatter; return this; } @Override public Controller<Integer> 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<Long> implements LongFieldControllerBuilder { private long min = Long.MIN_VALUE; private long max = Long.MAX_VALUE; - private Function<Long, Component> formatter = LongSliderController.DEFAULT_FORMATTER; + private ValueFormatter<Long> formatter = LongSliderController.DEFAULT_FORMATTER::apply; public LongFieldControllerBuilderImpl(Option<Long> option) { super(option); @@ -38,13 +39,13 @@ public class LongFieldControllerBuilderImpl extends AbstractControllerBuilderImp } @Override - public LongFieldControllerBuilder valueFormatter(Function<Long, Component> formatter) { + public LongFieldControllerBuilder formatValue(ValueFormatter<Long> formatter) { this.formatter = formatter; return this; } @Override public Controller<Long> 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<Long> implements LongSliderControllerBuilder { private long min, max; private long step; - private Function<Long, Component> formatter = LongSliderController.DEFAULT_FORMATTER; + private ValueFormatter<Long> formatter = LongSliderController.DEFAULT_FORMATTER::apply; public LongSliderControllerBuilderImpl(Option<Long> option) { super(option); @@ -31,13 +32,13 @@ public class LongSliderControllerBuilderImpl extends AbstractControllerBuilderIm } @Override - public LongSliderControllerBuilder valueFormatter(Function<Long, Component> formatter) { + public LongSliderControllerBuilder formatValue(ValueFormatter<Long> formatter) { this.formatter = formatter; return this; } @Override public Controller<Long> 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(); |