1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
package dev.isxander.yacl3.impl;
import com.google.common.collect.ImmutableSet;
import dev.isxander.yacl3.api.*;
import dev.isxander.yacl3.api.controller.ControllerBuilder;
import dev.isxander.yacl3.impl.utils.YACLConstants;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import org.apache.commons.lang3.Validate;
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;
@ApiStatus.Internal
public final class OptionImpl<T> implements Option<T> {
private final Component name;
private OptionDescription description;
private final Controller<T> controller;
private final Binding<T> binding;
private boolean available;
private final ImmutableSet<OptionFlag> flags;
private T pendingValue;
private final List<BiConsumer<Option<T>, T>> listeners;
private int listenerTriggerDepth = 0;
public OptionImpl(
@NotNull Component name,
@NotNull Function<T, OptionDescription> descriptionFunction,
@NotNull Function<Option<T>, Controller<T>> controlGetter,
@NotNull Binding<T> binding,
boolean available,
ImmutableSet<OptionFlag> flags,
@NotNull Collection<BiConsumer<Option<T>, T>> listeners
) {
this.name = name;
this.binding = new SafeBinding<>(binding);
this.available = available;
this.flags = flags;
this.listeners = new ArrayList<>(listeners);
this.pendingValue = binding.getValue();
this.controller = controlGetter.apply(this);
addListener((opt, pending) -> description = descriptionFunction.apply(pending));
triggerListeners(true);
}
@Override
public @NotNull Component name() {
return name;
}
@Override
public @NotNull OptionDescription description() {
return this.description;
}
@Override
public @NotNull Component tooltip() {
return description.text();
}
@Override
public @NotNull Controller<T> controller() {
return controller;
}
@Override
public @NotNull Binding<T> binding() {
return binding;
}
@Override
public boolean available() {
return available;
}
@Override
public void setAvailable(boolean available) {
boolean changed = this.available != available;
this.available = available;
if (changed) {
if (!available) {
this.pendingValue = binding().getValue();
}
this.triggerListeners(!available);
}
}
@Override
public @NotNull ImmutableSet<OptionFlag> flags() {
return flags;
}
@Override
public boolean changed() {
return !binding().getValue().equals(pendingValue);
}
@Override
public @NotNull T pendingValue() {
return pendingValue;
}
@Override
public void requestSet(@NotNull T value) {
Validate.notNull(value, "`value` cannot be null");
pendingValue = value;
this.triggerListeners(true);
}
@Override
public boolean applyValue() {
if (changed()) {
binding().setValue(pendingValue);
return true;
}
return false;
}
@Override
public void forgetPendingValue() {
requestSet(binding().getValue());
}
@Override
public void requestSetDefault() {
requestSet(binding().defaultValue());
}
@Override
public boolean isPendingValueDefault() {
return binding().defaultValue().equals(pendingValue());
}
@Override
public void addListener(BiConsumer<Option<T>, T> changedListener) {
this.listeners.add(changedListener);
}
private void triggerListeners(boolean bypass) {
if (bypass || listenerTriggerDepth == 0) {
if (listenerTriggerDepth > 10) {
throw new IllegalStateException("Listener trigger depth exceeded 10! This means a listener triggered a listener etc etc 10 times deep. This is likely a bug in the mod using YACL!");
}
this.listenerTriggerDepth++;
for (BiConsumer<Option<T>, T> listener : listeners) {
try {
listener.accept(this, pendingValue);
} catch (Exception e) {
YACLConstants.LOGGER.error("Exception whilst triggering listener for option '%s'".formatted(name.getString()), e);
}
}
this.listenerTriggerDepth--;
}
}
@ApiStatus.Internal
public static class BuilderImpl<T> implements Builder<T> {
private Component name = Component.literal("Name not specified!").withStyle(ChatFormatting.RED);
private Function<T, OptionDescription> descriptionFunction = pending -> OptionDescription.EMPTY;
private Function<Option<T>, Controller<T>> controlGetter;
private Binding<T> binding;
private boolean available = true;
private boolean instant = false;
private final Set<OptionFlag> flags = new HashSet<>();
private final List<BiConsumer<Option<T>, T>> listeners = new ArrayList<>();
@Override
public Builder<T> name(@NotNull Component name) {
Validate.notNull(name, "`name` cannot be null");
this.name = name;
return this;
}
@Override
public Builder<T> description(@NotNull OptionDescription description) {
return description(opt -> description);
}
@Override
public Builder<T> description(@NotNull Function<T, OptionDescription> descriptionFunction) {
this.descriptionFunction = descriptionFunction;
return this;
}
@Override
public Builder<T> controller(@NotNull Function<Option<T>, ControllerBuilder<T>> controllerBuilder) {
Validate.notNull(controllerBuilder, "`controllerBuilder` cannot be null");
return customController(opt -> controllerBuilder.apply(opt).build());
}
@Override
public Builder<T> customController(@NotNull Function<Option<T>, Controller<T>> control) {
Validate.notNull(control, "`control` cannot be null");
this.controlGetter = control;
return this;
}
@Override
public Builder<T> binding(@NotNull Binding<T> binding) {
Validate.notNull(binding, "`binding` cannot be null");
this.binding = binding;
return this;
}
@Override
public Builder<T> binding(@NotNull T def, @NotNull Supplier<@NotNull T> getter, @NotNull Consumer<@NotNull T> setter) {
Validate.notNull(def, "`def` must not be null");
Validate.notNull(getter, "`getter` must not be null");
Validate.notNull(setter, "`setter` must not be null");
this.binding = Binding.generic(def, getter, setter);
return this;
}
@Override
public Builder<T> available(boolean available) {
this.available = available;
return this;
}
@Override
public Builder<T> flag(@NotNull OptionFlag... flag) {
Validate.notNull(flag, "`flag` must not be null");
this.flags.addAll(Arrays.asList(flag));
return this;
}
@Override
public Builder<T> flags(@NotNull Collection<? extends OptionFlag> flags) {
Validate.notNull(flags, "`flags` must not be null");
this.flags.addAll(flags);
return this;
}
@Override
public Builder<T> instant(boolean instant) {
this.instant = instant;
return this;
}
@Override
public Builder<T> listener(@NotNull BiConsumer<Option<T>, T> listener) {
this.listeners.add(listener);
return this;
}
@Override
public Builder<T> listeners(@NotNull Collection<BiConsumer<Option<T>, T>> listeners) {
this.listeners.addAll(listeners);
return this;
}
@Override
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");
if (instant) {
listeners.add((opt, pendingValue) -> opt.applyValue());
}
return new OptionImpl<>(name, descriptionFunction, controlGetter, binding, available, ImmutableSet.copyOf(flags), listeners);
}
}
}
|