From 10bd86f0be7e5544e354fbb946962e25da413db9 Mon Sep 17 00:00:00 2001 From: isXander Date: Tue, 4 Apr 2023 11:19:55 +0100 Subject: 🐛 Fix ListOption.Builder not having listener methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/java/dev/isxander/yacl/api/ListOption.java | 15 +++++++++++++++ .../java/dev/isxander/yacl/impl/ListOptionImpl.java | 18 ++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/client/java/dev/isxander/yacl/api/ListOption.java b/src/client/java/dev/isxander/yacl/api/ListOption.java index adbdc29..afba8ee 100644 --- a/src/client/java/dev/isxander/yacl/api/ListOption.java +++ b/src/client/java/dev/isxander/yacl/api/ListOption.java @@ -7,6 +7,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import java.util.*; +import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; @@ -132,6 +133,20 @@ public interface ListOption extends OptionGroup, Option> { */ Builder collapsed(boolean collapsible); + /** + * Adds a listener to the option. Invoked upon changing any of the list's entries. + * + * @see Option#addListener(BiConsumer) + */ + ListOption.Builder listener(@NotNull BiConsumer>, List> listener); + + /** + * Adds multiple listeners to the option. Invoked upon changing of any of the list's entries. + * + * @see Option#addListener(BiConsumer) + */ + ListOption.Builder listeners(@NotNull Collection>, List>> listeners); + ListOption build(); } } diff --git a/src/client/java/dev/isxander/yacl/impl/ListOptionImpl.java b/src/client/java/dev/isxander/yacl/impl/ListOptionImpl.java index e7230b0..27e90ad 100644 --- a/src/client/java/dev/isxander/yacl/impl/ListOptionImpl.java +++ b/src/client/java/dev/isxander/yacl/impl/ListOptionImpl.java @@ -31,7 +31,7 @@ public final class ListOptionImpl implements ListOption { private final List>, List>> listeners; private final List refreshListeners; - public ListOptionImpl(@NotNull Component name, @NotNull Component tooltip, @NotNull Binding> binding, @NotNull T initialValue, @NotNull Class typeClass, @NotNull Function, Controller> controllerFunction, ImmutableSet flags, boolean collapsed, boolean available) { + public ListOptionImpl(@NotNull Component name, @NotNull Component tooltip, @NotNull Binding> binding, @NotNull T initialValue, @NotNull Class typeClass, @NotNull Function, Controller> controllerFunction, ImmutableSet flags, boolean collapsed, boolean available, Collection>, List>> listeners) { this.name = name; this.tooltip = tooltip; this.binding = binding; @@ -43,6 +43,7 @@ public final class ListOptionImpl implements ListOption { this.flags = flags; this.available = available; this.listeners = new ArrayList<>(); + this.listeners.addAll(listeners); this.refreshListeners = new ArrayList<>(); callListeners(); } @@ -219,6 +220,7 @@ public final class ListOptionImpl implements ListOption { private T initialValue; private boolean collapsed = false; private boolean available = true; + private final List>, List>> listeners = new ArrayList<>(); private final Class typeClass; public BuilderImpl(Class typeClass) { @@ -303,6 +305,18 @@ public final class ListOptionImpl implements ListOption { return this; } + @Override + public ListOption.Builder listener(@NotNull BiConsumer>, List> listener) { + this.listeners.add(listener); + return this; + } + + @Override + public ListOption.Builder listeners(@NotNull Collection>, List>> listeners) { + this.listeners.addAll(listeners); + return this; + } + @Override public ListOption build() { Validate.notNull(controllerFunction, "`controller` must not be null"); @@ -318,7 +332,7 @@ public final class ListOptionImpl implements ListOption { concatenatedTooltip.append(line); } - return new ListOptionImpl<>(name, concatenatedTooltip, binding, initialValue, typeClass, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available); + return new ListOptionImpl<>(name, concatenatedTooltip, binding, initialValue, typeClass, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available, listeners); } } } -- cgit