aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/dev/isxander/yacl/api')
-rw-r--r--src/main/java/dev/isxander/yacl/api/ConfigCategory.java2
-rw-r--r--src/main/java/dev/isxander/yacl/api/Option.java42
-rw-r--r--src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java94
3 files changed, 127 insertions, 11 deletions
diff --git a/src/main/java/dev/isxander/yacl/api/ConfigCategory.java b/src/main/java/dev/isxander/yacl/api/ConfigCategory.java
index 1b2a2bc..27c3e95 100644
--- a/src/main/java/dev/isxander/yacl/api/ConfigCategory.java
+++ b/src/main/java/dev/isxander/yacl/api/ConfigCategory.java
@@ -90,7 +90,7 @@ public interface ConfigCategory {
* @see OptionGroup#isRoot()
*/
public Builder options(@NotNull Collection<Option<?>> options) {
- Validate.notEmpty(options, "`options` must not be empty");
+ Validate.notNull(options, "`options` must not be null");
this.rootOptions.addAll(options);
return this;
diff --git a/src/main/java/dev/isxander/yacl/api/Option.java b/src/main/java/dev/isxander/yacl/api/Option.java
index ff72c4c..1c3b006 100644
--- a/src/main/java/dev/isxander/yacl/api/Option.java
+++ b/src/main/java/dev/isxander/yacl/api/Option.java
@@ -9,9 +9,11 @@ import org.apache.commons.lang3.Validate;
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;
+import java.util.stream.Stream;
public interface Option<T> {
/**
@@ -95,6 +97,8 @@ public interface Option<T> {
*/
void requestSetDefault();
+ void addListener(BiConsumer<Option<T>, T> changedListener);
+
/**
* Creates a builder to construct an {@link Option}
*
@@ -108,7 +112,7 @@ public interface Option<T> {
class Builder<T> {
private Text name = Text.literal("Name not specified!").formatted(Formatting.RED);
- private final List<Text> tooltipLines = new ArrayList<>();
+ private final List<Function<T, Text>> tooltipGetters = new ArrayList<>();
private Function<Option<T>, Controller<T>> controlGetter;
@@ -138,6 +142,20 @@ public interface Option<T> {
/**
* Sets the tooltip to be used by the option.
+ * No need to wrap the text yourself, the gui does this itself.
+ *
+ * @param tooltipGetter function to get tooltip depending on value {@link Builder#build()}.
+ */
+ @SafeVarargs
+ public final Builder<T> tooltip(@NotNull Function<T, Text>... tooltipGetter) {
+ Validate.notNull(tooltipGetter, "`tooltipGetter` cannot be null");
+
+ this.tooltipGetters.addAll(List.of(tooltipGetter));
+ return this;
+ }
+
+ /**
+ * Sets the tooltip to be used by the option.
* Can be invoked twice to append more lines.
* No need to wrap the text yourself, the gui does this itself.
*
@@ -146,7 +164,7 @@ public interface Option<T> {
public Builder<T> tooltip(@NotNull Text... tooltips) {
Validate.notNull(tooltips, "`tooltips` cannot be empty");
- tooltipLines.addAll(List.of(tooltips));
+ this.tooltipGetters.addAll(Stream.of(tooltips).map(text -> (Function<T, Text>) t -> text).toList());
return this;
}
@@ -244,16 +262,20 @@ public interface Option<T> {
Validate.notNull(controlGetter, "`control` must not be null when building `Option`");
Validate.notNull(binding, "`binding` must not be null when building `Option`");
- MutableText concatenatedTooltip = Text.empty();
- boolean first = true;
- for (Text line : tooltipLines) {
- if (!first) concatenatedTooltip.append("\n");
- first = false;
+ Function<T, Text> concatenatedTooltipGetter = value -> {
+ MutableText concatenatedTooltip = Text.empty();
+ boolean first = true;
+ for (Function<T, Text> line : tooltipGetters) {
+ if (!first) concatenatedTooltip.append("\n");
+ first = false;
+
+ concatenatedTooltip.append(line.apply(value));
+ }
- concatenatedTooltip.append(line);
- }
+ return concatenatedTooltip;
+ };
- return new OptionImpl<>(name, concatenatedTooltip, controlGetter, binding, available, ImmutableSet.copyOf(flags), typeClass);
+ return new OptionImpl<>(name, concatenatedTooltipGetter, controlGetter, binding, available, ImmutableSet.copyOf(flags), typeClass);
}
}
}
diff --git a/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java b/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java
new file mode 100644
index 0000000..de7441c
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl/api/PlaceholderCategory.java
@@ -0,0 +1,94 @@
+package dev.isxander.yacl.api;
+
+import dev.isxander.yacl.gui.YACLScreen;
+import dev.isxander.yacl.impl.PlaceholderCategoryImpl;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.client.gui.screen.Screen;
+import net.minecraft.text.MutableText;
+import net.minecraft.text.Text;
+import org.apache.commons.lang3.Validate;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.BiFunction;
+
+/**
+ * A placeholder category that actually just opens another screen,
+ * instead of displaying options
+ */
+public interface PlaceholderCategory extends ConfigCategory {
+ /**
+ * Function to create a screen to open upon changing to this category
+ */
+ BiFunction<MinecraftClient, YACLScreen, Screen> screen();
+
+ static Builder createBuilder() {
+ return new Builder();
+ }
+
+ class Builder {
+ private Text name;
+
+ private final List<Text> tooltipLines = new ArrayList<>();
+
+ private BiFunction<MinecraftClient, YACLScreen, Screen> screenFunction;
+
+ private Builder() {
+
+ }
+
+ /**
+ * Sets name of the category
+ *
+ * @see ConfigCategory#name()
+ */
+ public Builder name(@NotNull Text name) {
+ Validate.notNull(name, "`name` cannot be null");
+
+ this.name = name;
+ return this;
+ }
+
+ /**
+ * Sets the tooltip to be used by the category.
+ * Can be invoked twice to append more lines.
+ * No need to wrap the text yourself, the gui does this itself.
+ *
+ * @param tooltips text lines - merged with a new-line on {@link Builder#build()}.
+ */
+ public Builder tooltip(@NotNull Text... tooltips) {
+ Validate.notEmpty(tooltips, "`tooltips` cannot be empty");
+
+ tooltipLines.addAll(List.of(tooltips));
+ return this;
+ }
+
+ /**
+ * Screen to open upon selecting this category
+ *
+ * @see PlaceholderCategory#screen()
+ */
+ public Builder screen(@NotNull BiFunction<MinecraftClient, YACLScreen, Screen> screenFunction) {
+ Validate.notNull(screenFunction, "`screenFunction` cannot be null");
+
+ this.screenFunction = screenFunction;
+ return this;
+ }
+
+ public PlaceholderCategory build() {
+ Validate.notNull(name, "`name` must not be null to build `ConfigCategory`");
+
+ MutableText concatenatedTooltip = Text.empty();
+ boolean first = true;
+ for (Text line : tooltipLines) {
+ if (!first) concatenatedTooltip.append("\n");
+ first = false;
+
+ concatenatedTooltip.append(line);
+ }
+
+ return new PlaceholderCategoryImpl(name, screenFunction, concatenatedTooltip);
+ }
+ }
+}