Skip to content

Commit 10bd86f

Browse files
committed
🐛 Fix ListOption.Builder not having listener methods
1 parent 305a723 commit 10bd86f

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/client/java/dev/isxander/yacl/api/ListOption.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.jetbrains.annotations.NotNull;
88

99
import java.util.*;
10+
import java.util.function.BiConsumer;
1011
import java.util.function.Consumer;
1112
import java.util.function.Function;
1213
import java.util.function.Supplier;
@@ -132,6 +133,20 @@ interface Builder<T> {
132133
*/
133134
Builder<T> collapsed(boolean collapsible);
134135

136+
/**
137+
* Adds a listener to the option. Invoked upon changing any of the list's entries.
138+
*
139+
* @see Option#addListener(BiConsumer)
140+
*/
141+
ListOption.Builder<T> listener(@NotNull BiConsumer<Option<List<T>>, List<T>> listener);
142+
143+
/**
144+
* Adds multiple listeners to the option. Invoked upon changing of any of the list's entries.
145+
*
146+
* @see Option#addListener(BiConsumer)
147+
*/
148+
ListOption.Builder<T> listeners(@NotNull Collection<BiConsumer<Option<List<T>>, List<T>>> listeners);
149+
135150
ListOption<T> build();
136151
}
137152
}

src/client/java/dev/isxander/yacl/impl/ListOptionImpl.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public final class ListOptionImpl<T> implements ListOption<T> {
3131
private final List<BiConsumer<Option<List<T>>, List<T>>> listeners;
3232
private final List<Runnable> refreshListeners;
3333

34-
public ListOptionImpl(@NotNull Component name, @NotNull Component tooltip, @NotNull Binding<List<T>> binding, @NotNull T initialValue, @NotNull Class<T> typeClass, @NotNull Function<ListOptionEntry<T>, Controller<T>> controllerFunction, ImmutableSet<OptionFlag> flags, boolean collapsed, boolean available) {
34+
public ListOptionImpl(@NotNull Component name, @NotNull Component tooltip, @NotNull Binding<List<T>> binding, @NotNull T initialValue, @NotNull Class<T> typeClass, @NotNull Function<ListOptionEntry<T>, Controller<T>> controllerFunction, ImmutableSet<OptionFlag> flags, boolean collapsed, boolean available, Collection<BiConsumer<Option<List<T>>, List<T>>> listeners) {
3535
this.name = name;
3636
this.tooltip = tooltip;
3737
this.binding = binding;
@@ -43,6 +43,7 @@ public ListOptionImpl(@NotNull Component name, @NotNull Component tooltip, @NotN
4343
this.flags = flags;
4444
this.available = available;
4545
this.listeners = new ArrayList<>();
46+
this.listeners.addAll(listeners);
4647
this.refreshListeners = new ArrayList<>();
4748
callListeners();
4849
}
@@ -219,6 +220,7 @@ public static final class BuilderImpl<T> implements ListOption.Builder<T> {
219220
private T initialValue;
220221
private boolean collapsed = false;
221222
private boolean available = true;
223+
private final List<BiConsumer<Option<List<T>>, List<T>>> listeners = new ArrayList<>();
222224
private final Class<T> typeClass;
223225

224226
public BuilderImpl(Class<T> typeClass) {
@@ -303,6 +305,18 @@ public ListOption.Builder<T> collapsed(boolean collapsible) {
303305
return this;
304306
}
305307

308+
@Override
309+
public ListOption.Builder<T> listener(@NotNull BiConsumer<Option<List<T>>, List<T>> listener) {
310+
this.listeners.add(listener);
311+
return this;
312+
}
313+
314+
@Override
315+
public ListOption.Builder<T> listeners(@NotNull Collection<BiConsumer<Option<List<T>>, List<T>>> listeners) {
316+
this.listeners.addAll(listeners);
317+
return this;
318+
}
319+
306320
@Override
307321
public ListOption<T> build() {
308322
Validate.notNull(controllerFunction, "`controller` must not be null");
@@ -318,7 +332,7 @@ public ListOption<T> build() {
318332
concatenatedTooltip.append(line);
319333
}
320334

321-
return new ListOptionImpl<>(name, concatenatedTooltip, binding, initialValue, typeClass, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available);
335+
return new ListOptionImpl<>(name, concatenatedTooltip, binding, initialValue, typeClass, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available, listeners);
322336
}
323337
}
324338
}

0 commit comments

Comments
 (0)