diff options
author | isXander <xandersmith2008@gmail.com> | 2022-12-14 18:53:30 +0000 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2022-12-14 18:53:30 +0000 |
commit | 51f4ae2d8e0a6cdcc7d50a037143f48a6132214a (patch) | |
tree | 9a48b8faf7c419cf9295e79b80e11e27fe8671b2 /src/client/java/dev/isxander/yacl/api | |
parent | 3d1f7eb6c149c14ef9eea98d2d8caa6768f8c51c (diff) | |
download | YetAnotherConfigLib-51f4ae2d8e0a6cdcc7d50a037143f48a6132214a.tar.gz YetAnotherConfigLib-51f4ae2d8e0a6cdcc7d50a037143f48a6132214a.tar.bz2 YetAnotherConfigLib-51f4ae2d8e0a6cdcc7d50a037143f48a6132214a.zip |
lots of minor fixes with lists and abstract builders
Diffstat (limited to 'src/client/java/dev/isxander/yacl/api')
7 files changed, 69 insertions, 508 deletions
diff --git a/src/client/java/dev/isxander/yacl/api/ButtonOption.java b/src/client/java/dev/isxander/yacl/api/ButtonOption.java index 1124a9a..2025840 100644 --- a/src/client/java/dev/isxander/yacl/api/ButtonOption.java +++ b/src/client/java/dev/isxander/yacl/api/ButtonOption.java @@ -20,31 +20,16 @@ public interface ButtonOption extends Option<BiConsumer<YACLScreen, ButtonOption BiConsumer<YACLScreen, ButtonOption> action(); static Builder createBuilder() { - return new Builder(); + return new ButtonOptionImpl.BuilderImpl(); } - class Builder { - private Text name; - private final List<Text> tooltipLines = new ArrayList<>(); - private boolean available = true; - private Function<ButtonOption, Controller<BiConsumer<YACLScreen, ButtonOption>>> controlGetter; - private BiConsumer<YACLScreen, ButtonOption> action; - - private Builder() { - - } - + interface Builder { /** * Sets the name to be used by the option. * * @see Option#name() */ - public Builder name(@NotNull Text name) { - Validate.notNull(name, "`name` cannot be null"); - - this.name = name; - return this; - } + Builder name(@NotNull Text name); /** * Sets the tooltip to be used by the option. @@ -53,19 +38,9 @@ public interface ButtonOption extends Option<BiConsumer<YACLScreen, ButtonOption * * @param tooltips text lines - merged with a new-line on {@link Option.Builder#build()}. */ - public Builder tooltip(@NotNull Text... tooltips) { - Validate.notNull(tooltips, "`tooltips` cannot be empty"); - - tooltipLines.addAll(List.of(tooltips)); - return this; - } - - public Builder action(@NotNull BiConsumer<YACLScreen, ButtonOption> action) { - Validate.notNull(action, "`action` cannot be null"); + Builder tooltip(@NotNull Text... tooltips); - this.action = action; - return this; - } + Builder action(@NotNull BiConsumer<YACLScreen, ButtonOption> action); /** * Action to be executed upon button press @@ -73,22 +48,14 @@ public interface ButtonOption extends Option<BiConsumer<YACLScreen, ButtonOption * @see ButtonOption#action() */ @Deprecated - public Builder action(@NotNull Consumer<YACLScreen> action) { - Validate.notNull(action, "`action` cannot be null"); - - this.action = (screen, button) -> action.accept(screen); - return this; - } + Builder action(@NotNull Consumer<YACLScreen> action); /** * Sets if the option can be configured * * @see Option#available() */ - public Builder available(boolean available) { - this.available = available; - return this; - } + Builder available(boolean available); /** * Sets the controller for the option. @@ -96,28 +63,8 @@ public interface ButtonOption extends Option<BiConsumer<YACLScreen, ButtonOption * * @see dev.isxander.yacl.gui.controllers */ - public Builder controller(@NotNull Function<ButtonOption, Controller<BiConsumer<YACLScreen, ButtonOption>>> control) { - Validate.notNull(control, "`control` cannot be null"); - - this.controlGetter = control; - return this; - } - - public ButtonOption build() { - Validate.notNull(name, "`name` must not be null when building `Option`"); - Validate.notNull(controlGetter, "`control` must not be null when building `Option`"); - Validate.notNull(action, "`action` 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; - - concatenatedTooltip.append(line); - } + Builder controller(@NotNull Function<ButtonOption, Controller<BiConsumer<YACLScreen, ButtonOption>>> control); - return new ButtonOptionImpl(name, concatenatedTooltip, action, available, controlGetter); - } + ButtonOption build(); } } diff --git a/src/client/java/dev/isxander/yacl/api/ConfigCategory.java b/src/client/java/dev/isxander/yacl/api/ConfigCategory.java index 19c7f72..eecb9cb 100644 --- a/src/client/java/dev/isxander/yacl/api/ConfigCategory.java +++ b/src/client/java/dev/isxander/yacl/api/ConfigCategory.java @@ -39,32 +39,16 @@ public interface ConfigCategory { * Creates a builder to construct a {@link ConfigCategory} */ static Builder createBuilder() { - return new Builder(); + return new ConfigCategoryImpl.BuilderImpl(); } - class Builder { - private Text name; - - private final List<Option<?>> rootOptions = new ArrayList<>(); - private final List<OptionGroup> groups = new ArrayList<>(); - - private final List<Text> tooltipLines = new ArrayList<>(); - - private Builder() { - - } - + interface 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; - } + Builder name(@NotNull Text name); /** * Adds an option to the root group of the category. @@ -74,17 +58,7 @@ public interface ConfigCategory { * @see ConfigCategory#groups() * @see OptionGroup#isRoot() */ - public Builder option(@NotNull Option<?> option) { - Validate.notNull(option, "`option` must not be null"); - - if (option instanceof ListOption<?> listOption) { - YACLConstants.LOGGER.warn("Adding list option as an option is not supported! Rerouting to group!"); - return group(listOption); - } - - this.rootOptions.add(option); - return this; - } + Builder option(@NotNull Option<?> option); /** * Adds multiple options to the root group of the category. @@ -94,39 +68,21 @@ public interface ConfigCategory { * @see ConfigCategory#groups() * @see OptionGroup#isRoot() */ - public Builder options(@NotNull Collection<Option<?>> options) { - Validate.notNull(options, "`options` must not be null"); - - if (options.stream().anyMatch(ListOption.class::isInstance)) - throw new UnsupportedOperationException("List options must not be added as an option but a group!"); - - this.rootOptions.addAll(options); - return this; - } + Builder options(@NotNull Collection<Option<?>> options); /** * Adds an option group. * To add an option to the root group, use {@link Builder#option(Option)} * To construct a group, use {@link OptionGroup#createBuilder()} */ - public Builder group(@NotNull OptionGroup group) { - Validate.notNull(group, "`group` must not be null"); - - this.groups.add(group); - return this; - } + Builder group(@NotNull OptionGroup group); /** * Adds multiple option groups. * To add multiple options to the root group, use {@link Builder#options(Collection)} * To construct a group, use {@link OptionGroup#createBuilder()} */ - public Builder groups(@NotNull Collection<OptionGroup> groups) { - Validate.notEmpty(groups, "`groups` must not be empty"); - - this.groups.addAll(groups); - return this; - } + Builder groups(@NotNull Collection<OptionGroup> groups); /** * Sets the tooltip to be used by the category. @@ -135,32 +91,8 @@ public interface ConfigCategory { * * @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; - } - - public ConfigCategory build() { - Validate.notNull(name, "`name` must not be null to build `ConfigCategory`"); - - List<OptionGroup> combinedGroups = new ArrayList<>(); - combinedGroups.add(new OptionGroupImpl(Text.empty(), Text.empty(), ImmutableList.copyOf(rootOptions), false, true)); - combinedGroups.addAll(groups); - - Validate.notEmpty(combinedGroups, "at least one option must be added to build `ConfigCategory`"); - - MutableText concatenatedTooltip = Text.empty(); - boolean first = true; - for (Text line : tooltipLines) { - if (!first) concatenatedTooltip.append("\n"); - first = false; - - concatenatedTooltip.append(line); - } + Builder tooltip(@NotNull Text... tooltips); - return new ConfigCategoryImpl(name, ImmutableList.copyOf(combinedGroups), concatenatedTooltip); - } + ConfigCategory build(); } } diff --git a/src/client/java/dev/isxander/yacl/api/ListOption.java b/src/client/java/dev/isxander/yacl/api/ListOption.java index 895898c..54ed3a5 100644 --- a/src/client/java/dev/isxander/yacl/api/ListOption.java +++ b/src/client/java/dev/isxander/yacl/api/ListOption.java @@ -51,36 +51,17 @@ public interface ListOption<T> extends OptionGroup, Option<List<T>> { void addRefreshListener(Runnable changedListener); static <T> Builder<T> createBuilder(Class<T> typeClass) { - return new Builder<>(typeClass); + return new ListOptionImpl.BuilderImpl<>(typeClass); } - class Builder<T> { - private Text name = Text.empty(); - private final List<Text> tooltipLines = new ArrayList<>(); - private Function<ListOptionEntry<T>, Controller<T>> controllerFunction; - private Binding<List<T>> binding = null; - private final Set<OptionFlag> flags = new HashSet<>(); - private T initialValue; - private boolean collapsed = false; - private boolean available = true; - private final Class<T> typeClass; - - private Builder(Class<T> typeClass) { - this.typeClass = typeClass; - } - + interface Builder<T> { /** * Sets name of the list, for UX purposes, a name should always be given, * but isn't enforced. * * @see ListOption#name() */ - public Builder<T> name(@NotNull Text name) { - Validate.notNull(name, "`name` must not be null"); - - this.name = name; - return this; - } + Builder<T> name(@NotNull Text name); /** * Sets the tooltip to be used by the list. It is displayed like a normal @@ -91,22 +72,12 @@ public interface ListOption<T> extends OptionGroup, Option<List<T>> { * * @param tooltips text lines - merged with a new-line on {@link Builder#build()}. */ - public Builder<T> tooltip(@NotNull Text... tooltips) { - Validate.notEmpty(tooltips, "`tooltips` cannot be empty"); - - tooltipLines.addAll(List.of(tooltips)); - return this; - } + Builder<T> tooltip(@NotNull Text... tooltips); /** * Sets the value that is used when creating new entries */ - public Builder<T> initial(@NotNull T initialValue) { - Validate.notNull(initialValue, "`initialValue` cannot be empty"); - - this.initialValue = initialValue; - return this; - } + Builder<T> initial(@NotNull T initialValue); /** * Sets the controller for the option. @@ -114,12 +85,7 @@ public interface ListOption<T> extends OptionGroup, Option<List<T>> { * * @see dev.isxander.yacl.gui.controllers */ - public Builder<T> controller(@NotNull Function<ListOptionEntry<T>, Controller<T>> control) { - Validate.notNull(control, "`control` cannot be null"); - - this.controllerFunction = control; - return this; - } + Builder<T> controller(@NotNull Function<ListOptionEntry<T>, Controller<T>> control); /** * Sets the binding for the option. @@ -127,12 +93,7 @@ public interface ListOption<T> extends OptionGroup, Option<List<T>> { * * @see Binding */ - public Builder<T> binding(@NotNull Binding<List<T>> binding) { - Validate.notNull(binding, "`binding` cannot be null"); - - this.binding = binding; - return this; - } + Builder<T> binding(@NotNull Binding<List<T>> binding); /** * Sets the binding for the option. @@ -143,48 +104,28 @@ public interface ListOption<T> extends OptionGroup, Option<List<T>> { * @param setter should set the option to the supplied value * @see Binding */ - public Builder<T> binding(@NotNull List<T> def, @NotNull Supplier<@NotNull List<T>> getter, @NotNull Consumer<@NotNull List<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; - } + Builder<T> binding(@NotNull List<T> def, @NotNull Supplier<@NotNull List<T>> getter, @NotNull Consumer<@NotNull List<T>> setter); /** * Sets if the option can be configured * * @see Option#available() */ - public Builder<T> available(boolean available) { - this.available = available; - return this; - } + Builder<T> available(boolean available); /** * Adds a flag to the option. * Upon applying changes, all flags are executed. * {@link Option#flags()} */ - public Builder<T> flag(@NotNull OptionFlag... flag) { - Validate.notNull(flag, "`flag` must not be null"); - - this.flags.addAll(Arrays.asList(flag)); - return this; - } + Builder<T> flag(@NotNull OptionFlag... flag); /** * Adds a flag to the option. * Upon applying changes, all flags are executed. * {@link Option#flags()} */ - public Builder<T> flags(@NotNull Collection<OptionFlag> flags) { - Validate.notNull(flags, "`flags` must not be null"); - - this.flags.addAll(flags); - return this; - } + Builder<T> flags(@NotNull Collection<OptionFlag> flags); /** * Dictates if the group should be collapsed by default. @@ -192,26 +133,8 @@ public interface ListOption<T> extends OptionGroup, Option<List<T>> { * * @see OptionGroup#collapsed() */ - public Builder<T> collapsed(boolean collapsible) { - this.collapsed = collapsible; - return this; - } - - public ListOption<T> build() { - Validate.notNull(controllerFunction, "`controller` must not be null"); - Validate.notNull(binding, "`binding` must not be null"); - Validate.notNull(initialValue, "`initialValue` must not be null"); - - MutableText concatenatedTooltip = Text.empty(); - boolean first = true; - for (Text line : tooltipLines) { - if (!first) concatenatedTooltip.append("\n"); - first = false; - - concatenatedTooltip.append(line); - } - - return new ListOptionImpl<>(name, concatenatedTooltip, binding, initialValue, typeClass, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available); - } + Builder<T> collapsed(boolean collapsible); + + ListOption<T> build(); } } diff --git a/src/client/java/dev/isxander/yacl/api/Option.java b/src/client/java/dev/isxander/yacl/api/Option.java index 394723f..406931f 100644 --- a/src/client/java/dev/isxander/yacl/api/Option.java +++ b/src/client/java/dev/isxander/yacl/api/Option.java @@ -119,43 +119,16 @@ public interface Option<T> { * @param typeClass used to capture the type */ static <T> Builder<T> createBuilder(Class<T> typeClass) { - return new Builder<>(typeClass); + return new OptionImpl.BuilderImpl<>(typeClass); } - class Builder<T> { - private Text name = Text.literal("Name not specified!").formatted(Formatting.RED); - - private final List<Function<T, Text>> tooltipGetters = new ArrayList<>(); - - 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 Class<T> typeClass; - - private final List<BiConsumer<Option<T>, T>> listeners = new ArrayList<>(); - - private Builder(Class<T> typeClass) { - this.typeClass = typeClass; - } - + interface Builder<T> { /** * Sets the name to be used by the option. * * @see Option#name() */ - public Builder<T> name(@NotNull Text name) { - Validate.notNull(name, "`name` cannot be null"); - - this.name = name; - return this; - } + Builder<T> name(@NotNull Text name); /** * Sets the tooltip to be used by the option. @@ -163,13 +136,7 @@ public interface Option<T> { * * @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; - } + Builder<T> tooltip(@NotNull Function<T, Text>... tooltipGetter); /** * Sets the tooltip to be used by the option. @@ -178,12 +145,7 @@ public interface Option<T> { * * @param tooltips text lines - merged with a new-line on {@link Builder#build()}. */ - public Builder<T> tooltip(@NotNull Text... tooltips) { - Validate.notNull(tooltips, "`tooltips` cannot be empty"); - - this.tooltipGetters.addAll(Stream.of(tooltips).map(text -> (Function<T, Text>) t -> text).toList()); - return this; - } + Builder<T> tooltip(@NotNull Text... tooltips); /** * Sets the controller for the option. @@ -191,12 +153,7 @@ public interface Option<T> { * * @see dev.isxander.yacl.gui.controllers */ - public Builder<T> controller(@NotNull Function<Option<T>, Controller<T>> control) { - Validate.notNull(control, "`control` cannot be null"); - - this.controlGetter = control; - return this; - } + Builder<T> controller(@NotNull Function<Option<T>, Controller<T>> control); /** * Sets the binding for the option. @@ -204,12 +161,7 @@ public interface Option<T> { * * @see Binding */ - public Builder<T> binding(@NotNull Binding<T> binding) { - Validate.notNull(binding, "`binding` cannot be null"); - - this.binding = binding; - return this; - } + Builder<T> binding(@NotNull Binding<T> binding); /** * Sets the binding for the option. @@ -220,48 +172,28 @@ public interface Option<T> { * @param setter should set the option to the supplied value * @see Binding */ - 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; - } + Builder<T> binding(@NotNull T def, @NotNull Supplier<@NotNull T> getter, @NotNull Consumer<@NotNull T> setter); /** * Sets if the option can be configured * * @see Option#available() */ - public Builder<T> available(boolean available) { - this.available = available; - return this; - } + Builder<T> available(boolean available); /** * Adds a flag to the option. * Upon applying changes, all flags are executed. * {@link Option#flags()} */ - public Builder<T> flag(@NotNull OptionFlag... flag) { - Validate.notNull(flag, "`flag` must not be null"); - - this.flags.addAll(Arrays.asList(flag)); - return this; - } + Builder<T> flag(@NotNull OptionFlag... flag); /** * Adds a flag to the option. * Upon applying changes, all flags are executed. * {@link Option#flags()} */ - public Builder<T> flags(@NotNull Collection<OptionFlag> flags) { - Validate.notNull(flags, "`flags` must not be null"); - - this.flags.addAll(flags); - return this; - } + Builder<T> flags(@NotNull Collection<OptionFlag> flags); /** * Instantly invokes the binder's setter when modified in the GUI. @@ -269,66 +201,22 @@ public interface Option<T> { * <p> * Does not support {@link Option#flags()}! */ - public Builder<T> instant(boolean instant) { - this.instant = instant; - return this; - } + Builder<T> instant(boolean instant); /** * Adds a listener to the option. Invoked upon changing the pending value. * * @see Option#addListener(BiConsumer) */ - public Builder<T> listener(@NotNull BiConsumer<Option<T>, T> listener) { - this.listeners.add(listener); - return this; - } + Builder<T> listener(@NotNull BiConsumer<Option<T>, T> listener); /** * Adds multiple listeners to the option. Invoked upon changing the pending value. * * @see Option#addListener(BiConsumer) */ - public Builder<T> listeners(@NotNull Collection<BiConsumer<Option<T>, T>> listeners) { - this.listeners.addAll(listeners); - return this; - } + Builder<T> listeners(@NotNull Collection<BiConsumer<Option<T>, T>> listeners); - /** - * Dictates whether the option should require a restart. - * {@link Option#requiresRestart()} - */ - @Deprecated - public Builder<T> requiresRestart(boolean requiresRestart) { - if (requiresRestart) flag(OptionFlag.GAME_RESTART); - else flags.remove(OptionFlag.GAME_RESTART); - - return this; - } - - 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"); - - 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)); - } - - return concatenatedTooltip; - }; - - if (instant) { - listeners.add((opt, pendingValue) -> opt.applyValue()); - } - - return new OptionImpl<>(name, concatenatedTooltipGetter, controlGetter, binding, available, ImmutableSet.copyOf(flags), typeClass, listeners); - } + Option<T> build(); } } diff --git a/src/client/java/dev/isxander/yacl/api/OptionGroup.java b/src/client/java/dev/isxander/yacl/api/OptionGroup.java index 6cc6c7f..8dd9c14 100644 --- a/src/client/java/dev/isxander/yacl/api/OptionGroup.java +++ b/src/client/java/dev/isxander/yacl/api/OptionGroup.java @@ -48,30 +48,16 @@ public interface OptionGroup { * Creates a builder to construct a {@link OptionGroup} */ static Builder createBuilder() { - return new Builder(); + return new OptionGroupImpl.BuilderImpl(); } - class Builder { - private Text name = Text.empty(); - private final List<Text> tooltipLines = new ArrayList<>(); - private final List<Option<?>> options = new ArrayList<>(); - private boolean collapsed = false; - - private Builder() { - - } - + interface Builder { /** * Sets name of the group, can be {@link Text#empty()} to just separate options, like sodium. * * @see OptionGroup#name() */ - public Builder name(@NotNull Text name) { - Validate.notNull(name, "`name` must not be null"); - - this.name = name; - return this; - } + Builder name(@NotNull Text name); /** * Sets the tooltip to be used by the option group. @@ -80,12 +66,7 @@ public interface OptionGroup { * * @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; - } + Builder tooltip(@NotNull Text... tooltips); /** * Adds an option to group. @@ -93,15 +74,7 @@ public interface OptionGroup { * * @see OptionGroup#options() */ - public Builder option(@NotNull Option<?> option) { - Validate.notNull(option, "`option` must not be null"); - - if (option instanceof ListOption<?>) - throw new UnsupportedOperationException("List options must not be added as an option but a group!"); - - this.options.add(option); - return this; - } + Builder option(@NotNull Option<?> option); /** * Adds multiple options to group. @@ -109,39 +82,15 @@ public interface OptionGroup { * * @see OptionGroup#options() */ - public Builder options(@NotNull Collection<? extends Option<?>> options) { - Validate.notEmpty(options, "`options` must not be empty"); - - if (options.stream().anyMatch(ListOption.class::isInstance)) - throw new UnsupportedOperationException("List options must not be added as an option but a group!"); - - this.options.addAll(options); - return this; - } + Builder options(@NotNull Collection<? extends Option<?>> options); /** * Dictates if the group should be collapsed by default * * @see OptionGroup#collapsed() */ - public Builder collapsed(boolean collapsible) { - this.collapsed = collapsible; - return this; - } - - public OptionGroup build() { - Validate.notEmpty(options, "`options` must not be empty to build `OptionGroup`"); - - MutableText concatenatedTooltip = Text.empty(); - boolean first = true; - for (Text line : tooltipLines) { - if (!first) concatenatedTooltip.append("\n"); - first = false; - - concatenatedTooltip.append(line); - } + Builder collapsed(boolean collapsible); - return new OptionGroupImpl(name, concatenatedTooltip, ImmutableList.copyOf(options), collapsed, false); - } + OptionGroup build(); } } diff --git a/src/client/java/dev/isxander/yacl/api/PlaceholderCategory.java b/src/client/java/dev/isxander/yacl/api/PlaceholderCategory.java index de7441c..7bcc821 100644 --- a/src/client/java/dev/isxander/yacl/api/PlaceholderCategory.java +++ b/src/client/java/dev/isxander/yacl/api/PlaceholderCategory.java @@ -24,31 +24,16 @@ public interface PlaceholderCategory extends ConfigCategory { BiFunction<MinecraftClient, YACLScreen, Screen> screen(); static Builder createBuilder() { - return new Builder(); + return new PlaceholderCategoryImpl.BuilderImpl(); } - class Builder { - private Text name; - - private final List<Text> tooltipLines = new ArrayList<>(); - - private BiFunction<MinecraftClient, YACLScreen, Screen> screenFunction; - - private Builder() { - - } - + interface 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; - } + Builder name(@NotNull Text name); /** * Sets the tooltip to be used by the category. @@ -57,38 +42,15 @@ public interface PlaceholderCategory extends ConfigCategory { * * @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; - } + Builder tooltip(@NotNull Text... tooltips); /** * 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); - } + Builder screen(@NotNull BiFunction<MinecraftClient, YACLScreen, Screen> screenFunction); - return new PlaceholderCategoryImpl(name, screenFunction, concatenatedTooltip); - } + PlaceholderCategory build(); } } diff --git a/src/client/java/dev/isxander/yacl/api/YetAnotherConfigLib.java b/src/client/java/dev/isxander/yacl/api/YetAnotherConfigLib.java index ae6c060..f9a71d3 100644 --- a/src/client/java/dev/isxander/yacl/api/YetAnotherConfigLib.java +++ b/src/client/java/dev/isxander/yacl/api/YetAnotherConfigLib.java @@ -52,7 +52,7 @@ public interface YetAnotherConfigLib { * Creates a builder to construct YACL */ static Builder createBuilder() { - return new Builder(); + return new YetAnotherConfigLibImpl.BuilderImpl(); } /** @@ -63,27 +63,13 @@ public interface YetAnotherConfigLib { return builder.build(configInstance.getDefaults(), configInstance.getConfig(), createBuilder().save(configInstance::save)).build(); } - class Builder { - private Text title; - private final List<ConfigCategory> categories = new ArrayList<>(); - private Runnable saveFunction = () -> {}; - private Consumer<YACLScreen> initConsumer = screen -> {}; - - private Builder() { - - } - + interface Builder { /** * Sets title of GUI for Minecraft narration * * @see YetAnotherConfigLib#title() */ - public Builder title(@NotNull Text title) { - Validate.notNull(title, "`title` cannot be null"); - - this.title = title; - return this; - } + Builder title(@NotNull Text title); /** * Adds a new category. @@ -91,12 +77,7 @@ public interface YetAnotherConfigLib { * * @see YetAnotherConfigLib#categories() */ - public Builder category(@NotNull ConfigCategory category) { - Validate.notNull(category, "`category` cannot be null"); - - this.categories.add(category); - return this; - } + Builder category(@NotNull ConfigCategory category); /** * Adds multiple categories at once. @@ -104,44 +85,23 @@ public interface YetAnotherConfigLib { * * @see YetAnotherConfigLib#categories() */ - public Builder categories(@NotNull Collection<? extends ConfigCategory> categories) { - Validate.notNull(categories, "`categories` cannot be null"); - - this.categories.addAll(categories); - return this; - } + Builder categories(@NotNull Collection<? extends ConfigCategory> categories); /** * Used to define a save function for when user clicks the Save Changes button * * @see YetAnotherConfigLib#saveFunction() */ - public Builder save(@NotNull Runnable saveFunction) { - Validate.notNull(saveFunction, "`saveFunction` cannot be null"); - - this.saveFunction = saveFunction; - return this; - } + Builder save(@NotNull Runnable saveFunction); /** * Defines a consumer that is accepted every time the YACL screen initialises * * @see YetAnotherConfigLib#initConsumer() */ - public Builder screenInit(@NotNull Consumer<YACLScreen> initConsumer) { - Validate.notNull(initConsumer, "`initConsumer` cannot be null"); - - this.initConsumer = initConsumer; - return this; - } - - public YetAnotherConfigLib build() { - Validate.notNull(title, "`title must not be null to build `YetAnotherConfigLib`"); - Validate.notEmpty(categories, "`categories` must not be empty to build `YetAnotherConfigLib`"); - Validate.isTrue(!categories.stream().allMatch(category -> category instanceof PlaceholderCategory), "At least one regular category is required to build `YetAnotherConfigLib`"); + Builder screenInit(@NotNull Consumer<YACLScreen> initConsumer); - return new YetAnotherConfigLibImpl(title, ImmutableList.copyOf(categories), saveFunction, initConsumer); - } + YetAnotherConfigLib build(); } @FunctionalInterface |