diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/dev/isxander/yacl/api/Option.java | 20 | ||||
-rw-r--r-- | src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java | 6 |
2 files changed, 24 insertions, 2 deletions
diff --git a/src/main/java/dev/isxander/yacl/api/Option.java b/src/main/java/dev/isxander/yacl/api/Option.java index b17743e..1df90a8 100644 --- a/src/main/java/dev/isxander/yacl/api/Option.java +++ b/src/main/java/dev/isxander/yacl/api/Option.java @@ -123,6 +123,8 @@ public interface Option<T> { private boolean available = true; + private boolean instant = false; + private final Set<OptionFlag> flags = new HashSet<>(); private final Class<T> typeClass; @@ -250,6 +252,17 @@ public interface Option<T> { } /** + * Instantly invokes the binder's setter when modified in the GUI. + * Prevents the user from undoing the change + * <p> + * Does not support {@link Option#flags()}! + */ + public Builder<T> instant(boolean instant) { + this.instant = instant; + return this; + } + + /** * Dictates whether the option should require a restart. * {@link Option#requiresRestart()} */ @@ -264,6 +277,7 @@ public interface Option<T> { public Option<T> build() { Validate.notNull(controlGetter, "`control` must not be null when building `Option`"); Validate.notNull(binding, "`binding` must not be null when building `Option`"); + Validate.isTrue(!instant || flags.isEmpty(), "instant application does not support option flags"); Function<T, Text> concatenatedTooltipGetter = value -> { MutableText concatenatedTooltip = Text.empty(); @@ -278,7 +292,11 @@ public interface Option<T> { return concatenatedTooltip; }; - return new OptionImpl<>(name, concatenatedTooltipGetter, controlGetter, binding, available, ImmutableSet.copyOf(flags), typeClass); + OptionImpl<T> option = new OptionImpl<>(name, concatenatedTooltipGetter, controlGetter, binding, available, ImmutableSet.copyOf(flags), typeClass); + if (instant) { + option.addListener((opt, pendingValue) -> opt.applyValue()); + } + return option; } } } diff --git a/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java b/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java index 944246d..aca966b 100644 --- a/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java +++ b/src/testmod/java/dev/isxander/yacl/test/ModMenuIntegration.java @@ -96,10 +96,14 @@ public class ModMenuIntegration implements ModMenuApi { .name(Text.of("Slider Controllers")) .option(Option.createBuilder(int.class) .name(Text.of("Int Slider that is cut off because the slider")) + .instant(true) .binding( 0, () -> TestSettings.intSlider, - (value) -> TestSettings.intSlider = value + (value) -> { + TestSettings.intSlider = value; + System.out.println("Set int value!"); + } ) .controller(opt -> new IntegerSliderController(opt, 0, 3, 1)) .build()) |