aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisXander <xander@isxander.dev>2023-10-22 21:58:20 +0100
committerisXander <xander@isxander.dev>2023-10-22 21:58:20 +0100
commitec17d3cc159e58b1bcd8fffbb91d98f1df5c97e4 (patch)
tree11f0a5f22920986c4f26b33b9c52c15062fd10d1
parent0e09749df42cd86389ed4b66adbc1149e961179c (diff)
downloadYetAnotherConfigLib-ec17d3cc159e58b1bcd8fffbb91d98f1df5c97e4.tar.gz
YetAnotherConfigLib-ec17d3cc159e58b1bcd8fffbb91d98f1df5c97e4.tar.bz2
YetAnotherConfigLib-ec17d3cc159e58b1bcd8fffbb91d98f1df5c97e4.zip
More option add methods so there is less reason to break the constant builder chain
-rw-r--r--common/src/main/java/dev/isxander/yacl3/api/ConfigCategory.java48
-rw-r--r--common/src/main/java/dev/isxander/yacl3/api/OptionAddable.java36
-rw-r--r--common/src/main/java/dev/isxander/yacl3/api/OptionGroup.java43
3 files changed, 122 insertions, 5 deletions
diff --git a/common/src/main/java/dev/isxander/yacl3/api/ConfigCategory.java b/common/src/main/java/dev/isxander/yacl3/api/ConfigCategory.java
index d47b3ce..b3d68fc 100644
--- a/common/src/main/java/dev/isxander/yacl3/api/ConfigCategory.java
+++ b/common/src/main/java/dev/isxander/yacl3/api/ConfigCategory.java
@@ -6,6 +6,7 @@ import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
+import java.util.function.Supplier;
/**
* Separates {@link Option}s or {@link OptionGroup}s into multiple distinct sections.
@@ -47,7 +48,7 @@ public interface ConfigCategory {
/**
* Adds an option to the root group of the category.
* To add to another group, use {@link Builder#group(OptionGroup)}.
- * To construct an option, use {@link Option#createBuilder(Class)}
+ * To construct an option, use {@link Option#createBuilder()}
*
* @see ConfigCategory#groups()
* @see OptionGroup#isRoot()
@@ -56,9 +57,52 @@ public interface ConfigCategory {
Builder option(@NotNull Option<?> option);
/**
+ * Adds an option to the root group of the category.
+ * To add to another group, use {@link Builder#group(OptionGroup)}.
+ * To construct an option, use {@link Option#createBuilder()}
+ *
+ * @param optionSupplier to be called to initialise the option. called immediately
+ * @return this
+ */
+ @Override
+ default Builder option(@NotNull Supplier<@NotNull Option<?>> optionSupplier) {
+ OptionAddable.super.option(optionSupplier);
+ return this;
+ }
+
+ /**
+ * Adds an option to the root group of the category if a condition is met.
+ * To add to another group, use {@link Builder#group(OptionGroup)}.
+ * To construct an option, use {@link Option#createBuilder()}
+ *
+ * @param condition only if true is the option added
+ * @return this
+ */
+ @Override
+ default Builder optionIf(boolean condition, @NotNull Option<?> option) {
+ OptionAddable.super.optionIf(condition, option);
+ return this;
+ }
+
+ /**
+ * Adds an option to the root group of the category if a condition is met.
+ * To add to another group, use {@link Builder#group(OptionGroup)}.
+ * To construct an option, use {@link Option#createBuilder()}
+ *
+ * @param condition only if true is the option added
+ * @param optionSupplier to be called to initialise the option. called immediately only if condition is true
+ * @return this
+ */
+ @Override
+ default Builder optionIf(boolean condition, @NotNull Supplier<@NotNull Option<?>> optionSupplier) {
+ OptionAddable.super.optionIf(condition, optionSupplier);
+ return this;
+ }
+
+ /**
* Adds multiple options to the root group of the category.
* To add to another group, use {@link Builder#groups(Collection)}.
- * To construct an option, use {@link Option#createBuilder(Class)}
+ * To construct an option, use {@link Option#createBuilder()}
*
* @see ConfigCategory#groups()
* @see OptionGroup#isRoot()
diff --git a/common/src/main/java/dev/isxander/yacl3/api/OptionAddable.java b/common/src/main/java/dev/isxander/yacl3/api/OptionAddable.java
index 97ab352..606e8ca 100644
--- a/common/src/main/java/dev/isxander/yacl3/api/OptionAddable.java
+++ b/common/src/main/java/dev/isxander/yacl3/api/OptionAddable.java
@@ -3,17 +3,49 @@ package dev.isxander.yacl3.api;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
+import java.util.function.Supplier;
public interface OptionAddable {
/**
* Adds an option to an abstract builder.
- * To construct an option, use {@link Option#createBuilder(Class)}
+ * To construct an option, use {@link Option#createBuilder()}
*/
OptionAddable option(@NotNull Option<?> option);
/**
+ * Adds an option to an abstract builder.
+ * To construct an option, use {@link Option#createBuilder()}
+ * @param optionSupplier to be called to initialise the option. called immediately
+ */
+ default OptionAddable option(@NotNull Supplier<@NotNull Option<?>> optionSupplier) {
+ return option(optionSupplier.get());
+ }
+
+ /**
+ * Adds an option to an abstract builder if a condition is met.
+ * To construct an option, use {@link Option#createBuilder()}
+ * @param condition only if true is the option added
+ * @param option the option to add
+ * @return this
+ */
+ default OptionAddable optionIf(boolean condition, @NotNull Option<?> option) {
+ return condition ? option(option) : this;
+ }
+
+ /**
+ * Adds an option to an abstract builder if a condition is met.
+ * To construct an option, use {@link Option#createBuilder()}
+ * @param condition only if true is the option added
+ * @param optionSupplier to be called to initialise the option. called immediately only if condition is true
+ * @return this
+ */
+ default OptionAddable optionIf(boolean condition, @NotNull Supplier<@NotNull Option<?>> optionSupplier) {
+ return condition ? option(optionSupplier) : this;
+ }
+
+ /**
* Adds multiple options to an abstract builder.
- * To construct an option, use {@link Option#createBuilder(Class)}
+ * To construct an option, use {@link Option#createBuilder()}
*/
OptionAddable options(@NotNull Collection<? extends Option<?>> options);
}
diff --git a/common/src/main/java/dev/isxander/yacl3/api/OptionGroup.java b/common/src/main/java/dev/isxander/yacl3/api/OptionGroup.java
index ff31966..8f183b9 100644
--- a/common/src/main/java/dev/isxander/yacl3/api/OptionGroup.java
+++ b/common/src/main/java/dev/isxander/yacl3/api/OptionGroup.java
@@ -6,6 +6,7 @@ import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
+import java.util.function.Supplier;
/**
* Serves as a separator between multiple chunks of options
@@ -70,8 +71,48 @@ public interface OptionGroup {
Builder option(@NotNull Option<?> option);
/**
+ * Adds an option to this group.
+ * To construct an option, use {@link Option#createBuilder()}
+ *
+ * @param optionSupplier to be called to initialise the option. called immediately
+ * @return this
+ */
+ @Override
+ default Builder option(@NotNull Supplier<@NotNull Option<?>> optionSupplier) {
+ OptionAddable.super.option(optionSupplier);
+ return this;
+ }
+
+ /**
+ * Adds an option to this group if a condition is met.
+ * To construct an option, use {@link Option#createBuilder()}
+ *
+ * @param condition only if true is the option added
+ * @return this
+ */
+ @Override
+ default Builder optionIf(boolean condition, @NotNull Option<?> option) {
+ OptionAddable.super.optionIf(condition, option);
+ return this;
+ }
+
+ /**
+ * Adds an option to this group if a condition is met.
+ * To construct an option, use {@link Option#createBuilder()}
+ *
+ * @param condition only if true is the option added
+ * @param optionSupplier to be called to initialise the option. called immediately only if condition is true
+ * @return this
+ */
+ @Override
+ default Builder optionIf(boolean condition, @NotNull Supplier<@NotNull Option<?>> optionSupplier) {
+ OptionAddable.super.optionIf(condition, optionSupplier);
+ return this;
+ }
+
+ /**
* Adds multiple options to group.
- * To construct an option, use {@link Option#createBuilder(Class)}
+ * To construct an option, use {@link Option#createBuilder()}
*
* @see OptionGroup#options()
*/