aboutsummaryrefslogtreecommitdiff
path: root/src/client/java/dev/isxander
diff options
context:
space:
mode:
authorisXander <xander@isxander.dev>2022-12-25 13:48:58 +0000
committerisXander <xander@isxander.dev>2022-12-25 13:50:41 +0000
commit7c78ba96a44854ee3733104b8b22bf5cbfc92c3b (patch)
tree2fd66665e008fa6fddd4db36c45f6e7ddbfe39f7 /src/client/java/dev/isxander
parentd7e09fec2804dc893cabfbf3c136acce21d78e73 (diff)
downloadYetAnotherConfigLib-7c78ba96a44854ee3733104b8b22bf5cbfc92c3b.tar.gz
YetAnotherConfigLib-7c78ba96a44854ee3733104b8b22bf5cbfc92c3b.tar.bz2
YetAnotherConfigLib-7c78ba96a44854ee3733104b8b22bf5cbfc92c3b.zip
LabelOption now has a builder
Diffstat (limited to 'src/client/java/dev/isxander')
-rw-r--r--src/client/java/dev/isxander/yacl/api/LabelOption.java27
-rw-r--r--src/client/java/dev/isxander/yacl/impl/LabelOptionImpl.java45
2 files changed, 67 insertions, 5 deletions
diff --git a/src/client/java/dev/isxander/yacl/api/LabelOption.java b/src/client/java/dev/isxander/yacl/api/LabelOption.java
index a1bbe28..0e8202b 100644
--- a/src/client/java/dev/isxander/yacl/api/LabelOption.java
+++ b/src/client/java/dev/isxander/yacl/api/LabelOption.java
@@ -2,6 +2,9 @@ package dev.isxander.yacl.api;
import dev.isxander.yacl.impl.LabelOptionImpl;
import net.minecraft.text.Text;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Collection;
/**
* A label option is an easier way of creating a label with a {@link dev.isxander.yacl.gui.controllers.LabelController}.
@@ -9,12 +12,30 @@ import net.minecraft.text.Text;
* {@link Text} styling.
*/
public interface LabelOption extends Option<Text> {
- Text label();
+ @NotNull Text label();
/**
- * Creates a new label option with the given label.
+ * Creates a new label option with the given label, skipping a builder for ease.
*/
- static LabelOption create(Text label) {
+ static LabelOption create(@NotNull Text label) {
return new LabelOptionImpl(label);
}
+
+ static Builder createBuilder() {
+ return new LabelOptionImpl.BuilderImpl();
+ }
+
+ interface Builder {
+ /**
+ * Appends a line to the label
+ */
+ Builder line(@NotNull Text line);
+
+ /**
+ * Appends multiple lines to the label
+ */
+ Builder lines(@NotNull Collection<? extends Text> lines);
+
+ LabelOption build();
+ }
}
diff --git a/src/client/java/dev/isxander/yacl/impl/LabelOptionImpl.java b/src/client/java/dev/isxander/yacl/impl/LabelOptionImpl.java
index 314c2ad..a816b82 100644
--- a/src/client/java/dev/isxander/yacl/impl/LabelOptionImpl.java
+++ b/src/client/java/dev/isxander/yacl/impl/LabelOptionImpl.java
@@ -3,12 +3,20 @@ package dev.isxander.yacl.impl;
import com.google.common.collect.ImmutableSet;
import dev.isxander.yacl.api.*;
import dev.isxander.yacl.gui.controllers.LabelController;
+import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
+import org.apache.commons.lang3.Validate;
+import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
import java.util.function.BiConsumer;
-public class LabelOptionImpl implements LabelOption {
+@ApiStatus.Internal
+public final class LabelOptionImpl implements LabelOption {
private final Text label;
private final Text name = Text.literal("Label Option");
private final Text tooltip = Text.empty();
@@ -22,7 +30,7 @@ public class LabelOptionImpl implements LabelOption {
}
@Override
- public Text label() {
+ public @NotNull Text label() {
return label;
}
@@ -110,4 +118,37 @@ public class LabelOptionImpl implements LabelOption {
public void addListener(BiConsumer<Option<Text>, Text> changedListener) {
}
+
+ @ApiStatus.Internal
+ public static final class BuilderImpl implements LabelOption.Builder {
+ private final List<Text> lines = new ArrayList<>();
+
+ @Override
+ public Builder line(@NotNull Text line) {
+ Validate.notNull(line, "`line` must not be null");
+
+ this.lines.add(line);
+ return this;
+ }
+
+ @Override
+ public Builder lines(@NotNull Collection<? extends Text> lines) {
+ this.lines.addAll(lines);
+ return this;
+ }
+
+ @Override
+ public LabelOption build() {
+ MutableText text = Text.empty();
+ Iterator<Text> iterator = lines.iterator();
+ while (iterator.hasNext()) {
+ text.append(iterator.next());
+
+ if (iterator.hasNext())
+ text.append("\n");
+ }
+
+ return new LabelOptionImpl(text);
+ }
+ }
}