From 04fe933f4c24817100f3101f088accf55a621f8a Mon Sep 17 00:00:00 2001 From: isxander Date: Thu, 11 Apr 2024 18:43:06 +0100 Subject: Extremely fragile and broken multiversion build with stonecutter --- .../dev/isxander/yacl3/impl/ButtonOptionImpl.java | 205 +++++++++++ .../isxander/yacl3/impl/ConfigCategoryImpl.java | 134 +++++++ .../isxander/yacl3/impl/GenericBindingImpl.java | 35 ++ .../yacl3/impl/HiddenNameListOptionEntry.java | 109 ++++++ .../dev/isxander/yacl3/impl/LabelOptionImpl.java | 160 ++++++++ .../isxander/yacl3/impl/ListOptionEntryImpl.java | 154 ++++++++ .../dev/isxander/yacl3/impl/ListOptionImpl.java | 402 +++++++++++++++++++++ .../isxander/yacl3/impl/OptionDescriptionImpl.java | 133 +++++++ .../dev/isxander/yacl3/impl/OptionGroupImpl.java | 121 +++++++ .../java/dev/isxander/yacl3/impl/OptionImpl.java | 295 +++++++++++++++ .../yacl3/impl/PlaceholderCategoryImpl.java | 99 +++++ .../java/dev/isxander/yacl3/impl/SafeBinding.java | 29 ++ .../yacl3/impl/YetAnotherConfigLibImpl.java | 122 +++++++ .../controller/AbstractControllerBuilderImpl.java | 12 + .../controller/BooleanControllerBuilderImpl.java | 57 +++ .../controller/ColorControllerBuilderImpl.java | 27 ++ .../CyclingListControllerBuilderImpl.java | 41 +++ .../DoubleFieldControllerBuilderImpl.java | 51 +++ .../DoubleSliderControllerBuilderImpl.java | 44 +++ .../DropdownStringControllerBuilderImpl.java | 49 +++ .../impl/controller/EnumControllerBuilderImpl.java | 42 +++ .../EnumDropdownControllerBuilderImpl.java | 27 ++ .../FloatFieldControllerBuilderImpl.java | 51 +++ .../FloatSliderControllerBuilderImpl.java | 44 +++ .../IntegerFieldControllerBuilderImpl.java | 51 +++ .../IntegerSliderControllerBuilderImpl.java | 44 +++ .../impl/controller/ItemControllerBuilderImpl.java | 18 + .../controller/LongFieldControllerBuilderImpl.java | 51 +++ .../LongSliderControllerBuilderImpl.java | 44 +++ .../controller/StringControllerBuilderImpl.java | 17 + .../controller/TickBoxControllerBuilderImpl.java | 17 + .../yacl3/impl/utils/DimensionIntegerImpl.java | 115 ++++++ .../isxander/yacl3/impl/utils/YACLConstants.java | 8 + 33 files changed, 2808 insertions(+) create mode 100644 src/main/java/dev/isxander/yacl3/impl/ButtonOptionImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/ConfigCategoryImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/GenericBindingImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/HiddenNameListOptionEntry.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/LabelOptionImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/ListOptionEntryImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/OptionGroupImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/OptionImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/PlaceholderCategoryImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/SafeBinding.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/YetAnotherConfigLibImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/AbstractControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/BooleanControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/ColorControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/CyclingListControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/DoubleFieldControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/DoubleSliderControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/DropdownStringControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/EnumControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/EnumDropdownControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/FloatFieldControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/FloatSliderControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/IntegerFieldControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/IntegerSliderControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/ItemControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/LongFieldControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/LongSliderControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/StringControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/controller/TickBoxControllerBuilderImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/utils/DimensionIntegerImpl.java create mode 100644 src/main/java/dev/isxander/yacl3/impl/utils/YACLConstants.java (limited to 'src/main/java/dev/isxander/yacl3/impl') diff --git a/src/main/java/dev/isxander/yacl3/impl/ButtonOptionImpl.java b/src/main/java/dev/isxander/yacl3/impl/ButtonOptionImpl.java new file mode 100644 index 0000000..170b8e0 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/ButtonOptionImpl.java @@ -0,0 +1,205 @@ +package dev.isxander.yacl3.impl; + +import com.google.common.collect.ImmutableSet; +import dev.isxander.yacl3.api.*; +import dev.isxander.yacl3.gui.YACLScreen; +import dev.isxander.yacl3.gui.controllers.ActionController; +import net.minecraft.network.chat.Component; +import org.apache.commons.lang3.Validate; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +@ApiStatus.Internal +public final class ButtonOptionImpl implements ButtonOption { + private final Component name; + private final OptionDescription description; + private final BiConsumer action; + private boolean available; + private final Controller> controller; + private final Binding> binding; + + public ButtonOptionImpl( + @NotNull Component name, + @Nullable OptionDescription description, + @NotNull BiConsumer action, + @Nullable Component text, + boolean available + ) { + this.name = name; + this.description = description; + this.action = action; + this.available = available; + this.controller = text != null ? new ActionController(this, text) : new ActionController(this); + this.binding = new EmptyBinderImpl(); + } + + @Override + public @NotNull Component name() { + return name; + } + + @Override + public @NotNull OptionDescription description() { + return description; + } + + @Override + public @NotNull Component tooltip() { + return description().text(); + } + + @Override + public BiConsumer action() { + return action; + } + + @Override + public boolean available() { + return available; + } + + @Override + public void setAvailable(boolean available) { + this.available = available; + } + + @Override + public @NotNull Controller> controller() { + return controller; + } + + @Override + public @NotNull Binding> binding() { + return binding; + } + + @Override + public @NotNull ImmutableSet flags() { + return ImmutableSet.of(); + } + + @Override + public boolean changed() { + return false; + } + + @Override + public @NotNull BiConsumer pendingValue() { + throw new UnsupportedOperationException(); + } + + @Override + public void requestSet(@NotNull BiConsumer value) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean applyValue() { + return false; + } + + @Override + public void forgetPendingValue() { + + } + + @Override + public void requestSetDefault() { + + } + + @Override + public boolean isPendingValueDefault() { + throw new UnsupportedOperationException(); + } + + @Override + public void addListener(BiConsumer>, BiConsumer> changedListener) { + + } + + private static class EmptyBinderImpl implements Binding> { + @Override + public void setValue(BiConsumer value) { + + } + + @Override + public BiConsumer getValue() { + throw new UnsupportedOperationException(); + } + + @Override + public BiConsumer defaultValue() { + throw new UnsupportedOperationException(); + } + } + + @ApiStatus.Internal + public static final class BuilderImpl implements Builder { + private Component name; + private Component text = null; + private OptionDescription description = OptionDescription.EMPTY; + private boolean available = true; + private BiConsumer action; + + @Override + public Builder name(@NotNull Component name) { + Validate.notNull(name, "`name` cannot be null"); + + this.name = name; + return this; + } + + @Override + public Builder text(@NotNull Component text) { + Validate.notNull(text, "`text` cannot be null"); + + this.text = text; + return this; + } + + @Override + public Builder description(@NotNull OptionDescription description) { + Validate.notNull(description, "`description` cannot be null"); + + this.description = description; + return this; + } + + @Override + public Builder action(@NotNull BiConsumer action) { + Validate.notNull(action, "`action` cannot be null"); + + this.action = action; + return this; + } + + @Override + @Deprecated + public Builder action(@NotNull Consumer action) { + Validate.notNull(action, "`action` cannot be null"); + + this.action = (screen, button) -> action.accept(screen); + return this; + } + + @Override + public Builder available(boolean available) { + this.available = available; + return this; + } + + @Override + public ButtonOption build() { + Validate.notNull(name, "`name` must not be null when building `ButtonOption`"); + Validate.notNull(action, "`action` must not be null when building `ButtonOption`"); + + return new ButtonOptionImpl(name, description, action, text, available); + } + } +} diff --git a/src/main/java/dev/isxander/yacl3/impl/ConfigCategoryImpl.java b/src/main/java/dev/isxander/yacl3/impl/ConfigCategoryImpl.java new file mode 100644 index 0000000..400abf6 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/ConfigCategoryImpl.java @@ -0,0 +1,134 @@ +package dev.isxander.yacl3.impl; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl3.api.*; +import dev.isxander.yacl3.impl.utils.YACLConstants; +import net.minecraft.network.chat.CommonComponents; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +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.List; + +@ApiStatus.Internal +public final class ConfigCategoryImpl implements ConfigCategory { + private final Component name; + private final ImmutableList groups; + private final Component tooltip; + + public ConfigCategoryImpl(Component name, ImmutableList groups, Component tooltip) { + this.name = name; + this.groups = groups; + this.tooltip = tooltip; + } + + @Override + public @NotNull Component name() { + return name; + } + + @Override + public @NotNull ImmutableList groups() { + return groups; + } + + @Override + public @NotNull Component tooltip() { + return tooltip; + } + + @ApiStatus.Internal + public static final class BuilderImpl implements Builder { + private Component name; + + private final List> rootOptions = new ArrayList<>(); + private final List groups = new ArrayList<>(); + + private final List tooltipLines = new ArrayList<>(); + + @Override + public Builder name(@NotNull Component name) { + Validate.notNull(name, "`name` cannot be null"); + + this.name = name; + return this; + } + + @Override + 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; + } + + @Override + public Builder options(@NotNull Collection> 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; + } + + @Override + public Builder group(@NotNull OptionGroup group) { + Validate.notNull(group, "`group` must not be null"); + + this.groups.add(group); + return this; + } + + @Override + public Builder groups(@NotNull Collection groups) { + Validate.notEmpty(groups, "`groups` must not be empty"); + + this.groups.addAll(groups); + return this; + } + + @Override + public Builder tooltip(@NotNull Component... tooltips) { + Validate.notEmpty(tooltips, "`tooltips` cannot be empty"); + + tooltipLines.addAll(List.of(tooltips)); + return this; + } + + @Override + public ConfigCategory build() { + Validate.notNull(name, "`name` must not be null to build `ConfigCategory`"); + + List combinedGroups = new ArrayList<>(); + combinedGroups.add(new OptionGroupImpl(CommonComponents.EMPTY, OptionDescription.EMPTY, ImmutableList.copyOf(rootOptions), false, true)); + combinedGroups.addAll(groups); + + Validate.notEmpty(combinedGroups, "at least one option must be added to build `ConfigCategory`"); + + MutableComponent concatenatedTooltip = Component.empty(); + boolean first = true; + for (Component line : tooltipLines) { + if (line.getContents() == CommonComponents.EMPTY.getContents()) + continue; + + if (!first) concatenatedTooltip.append("\n"); + first = false; + + concatenatedTooltip.append(line); + } + + return new ConfigCategoryImpl(name, ImmutableList.copyOf(combinedGroups), concatenatedTooltip); + } + } +} diff --git a/src/main/java/dev/isxander/yacl3/impl/GenericBindingImpl.java b/src/main/java/dev/isxander/yacl3/impl/GenericBindingImpl.java new file mode 100644 index 0000000..972c891 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/GenericBindingImpl.java @@ -0,0 +1,35 @@ +package dev.isxander.yacl3.impl; + +import dev.isxander.yacl3.api.Binding; + +import java.util.function.Consumer; +import java.util.function.Supplier; + +public final class GenericBindingImpl implements Binding { + private final T def; + private final Supplier getter; + private final Consumer setter; + + public GenericBindingImpl(T def, Supplier getter, Consumer setting) { + this.def = def; + this.getter = getter; + this.setter = setting; + } + + + @Override + public void setValue(T value) { + setter.accept(value); + } + + @Override + public T getValue() { + return getter.get(); + } + + @Override + public T defaultValue() { + return def; + } + +} diff --git a/src/main/java/dev/isxander/yacl3/impl/HiddenNameListOptionEntry.java b/src/main/java/dev/isxander/yacl3/impl/HiddenNameListOptionEntry.java new file mode 100644 index 0000000..64588f2 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/HiddenNameListOptionEntry.java @@ -0,0 +1,109 @@ +package dev.isxander.yacl3.impl; + +import com.google.common.collect.ImmutableSet; +import dev.isxander.yacl3.api.*; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.NotNull; + +import java.util.function.BiConsumer; + +public class HiddenNameListOptionEntry implements ListOptionEntry { + private final ListOptionEntry option; + + public HiddenNameListOptionEntry(ListOptionEntry option) { + this.option = option; + } + + @Override + public @NotNull Component name() { + return Component.empty(); + } + + @Override + public @NotNull OptionDescription description() { + return option.description(); + } + + @Override + @Deprecated + public @NotNull Component tooltip() { + return option.tooltip(); + } + + @Override + public @NotNull Controller controller() { + return option.controller(); + } + + @Override + public @NotNull Binding binding() { + return option.binding(); + } + + @Override + public boolean available() { + return option.available(); + } + + @Override + public void setAvailable(boolean available) { + option.setAvailable(available); + } + + @Override + public ListOption parentGroup() { + return option.parentGroup(); + } + + @Override + public @NotNull ImmutableSet flags() { + return option.flags(); + } + + @Override + public boolean changed() { + return option.changed(); + } + + @Override + public @NotNull T pendingValue() { + return option.pendingValue(); + } + + @Override + public void requestSet(@NotNull T value) { + option.requestSet(value); + } + + @Override + public boolean applyValue() { + return option.applyValue(); + } + + @Override + public void forgetPendingValue() { + option.forgetPendingValue(); + } + + @Override + public void requestSetDefault() { + option.requestSetDefault(); + } + + @Override + public boolean isPendingValueDefault() { + return option.isPendingValueDefault(); + } + + @Override + public boolean canResetToDefault() { + return option.canResetToDefault(); + } + + @Override + public void addListener(BiConsumer, T> changedListener) { + option.addListener(changedListener); + } + + +} diff --git a/src/main/java/dev/isxander/yacl3/impl/LabelOptionImpl.java b/src/main/java/dev/isxander/yacl3/impl/LabelOptionImpl.java new file mode 100644 index 0000000..2bd2e10 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/LabelOptionImpl.java @@ -0,0 +1,160 @@ +package dev.isxander.yacl3.impl; + +import com.google.common.collect.ImmutableSet; +import dev.isxander.yacl3.api.*; +import dev.isxander.yacl3.gui.controllers.LabelController; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +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; + +@ApiStatus.Internal +public final class LabelOptionImpl implements LabelOption { + private final Component label; + private final Component name = Component.literal("Label Option"); + private final OptionDescription description; + private final Component tooltip = Component.empty(); + private final LabelController labelController; + private final Binding binding; + + public LabelOptionImpl(Component label) { + Validate.notNull(label, "`label` must not be null"); + + this.label = label; + this.labelController = new LabelController(this); + this.binding = Binding.immutable(label); + this.description = OptionDescription.createBuilder() + .text(this.label) + .build(); + } + + @Override + public @NotNull Component label() { + return label; + } + + @Override + public @NotNull Component name() { + return name; + } + + @Override + public @NotNull OptionDescription description() { + return description; + } + + @Override + public @NotNull Component tooltip() { + return tooltip; + } + + @Override + public @NotNull Controller controller() { + return labelController; + } + + @Override + public @NotNull Binding binding() { + return binding; + } + + @Override + public boolean available() { + return true; + } + + @Override + public void setAvailable(boolean available) { + throw new UnsupportedOperationException("Label options cannot be disabled."); + } + + @Override + public @NotNull ImmutableSet flags() { + return ImmutableSet.of(); + } + + @Override + public boolean changed() { + return false; + } + + @Override + public @NotNull Component pendingValue() { + return label; + } + + @Override + public void requestSet(@NotNull Component value) { + + } + + @Override + public boolean applyValue() { + return false; + } + + @Override + public void forgetPendingValue() { + + } + + @Override + public void requestSetDefault() { + + } + + @Override + public boolean isPendingValueDefault() { + return true; + } + + @Override + public boolean canResetToDefault() { + return false; + } + + @Override + public void addListener(BiConsumer, Component> changedListener) { + + } + + @ApiStatus.Internal + public static final class BuilderImpl implements Builder { + private final List lines = new ArrayList<>(); + + @Override + public Builder line(@NotNull Component line) { + Validate.notNull(line, "`line` must not be null"); + + this.lines.add(line); + return this; + } + + @Override + public Builder lines(@NotNull Collection lines) { + this.lines.addAll(lines); + return this; + } + + @Override + public LabelOption build() { + MutableComponent text = Component.empty(); + Iterator iterator = lines.iterator(); + while (iterator.hasNext()) { + text.append(iterator.next()); + + if (iterator.hasNext()) + text.append("\n"); + } + + return new LabelOptionImpl(text); + } + } +} diff --git a/src/main/java/dev/isxander/yacl3/impl/ListOptionEntryImpl.java b/src/main/java/dev/isxander/yacl3/impl/ListOptionEntryImpl.java new file mode 100644 index 0000000..1cd5e55 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/ListOptionEntryImpl.java @@ -0,0 +1,154 @@ +package dev.isxander.yacl3.impl; + +import dev.isxander.yacl3.api.*; +import dev.isxander.yacl3.api.utils.Dimension; +import dev.isxander.yacl3.gui.AbstractWidget; +import dev.isxander.yacl3.gui.YACLScreen; +import dev.isxander.yacl3.gui.controllers.ListEntryWidget; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +import java.util.function.BiConsumer; +import java.util.function.Function; + +@ApiStatus.Internal +public final class ListOptionEntryImpl implements ListOptionEntry { + private final ListOptionImpl group; + + private T value; + + private final Binding binding; + private final Controller controller; + + ListOptionEntryImpl(ListOptionImpl group, T initialValue, @NotNull Function, Controller> controlGetter) { + this.group = group; + this.value = initialValue; + this.binding = new EntryBinding(); + this.controller = new EntryController<>(controlGetter.apply(new HiddenNameListOptionEntry<>(this)), this); + } + + @Override + public @NotNull Component name() { + return group.name(); + } + + @Override + public @NotNull OptionDescription description() { + return group.description(); + } + + @Override + public @NotNull Component tooltip() { + return group.tooltip(); + } + + @Override + public @NotNull Controller controller() { + return controller; + } + + @Override + public @NotNull Binding binding() { + return binding; + } + + @Override + public boolean available() { + return parentGroup().available(); + } + + @Override + public void setAvailable(boolean available) { + + } + + @Override + public ListOption parentGroup() { + return group; + } + + @Override + public boolean changed() { + return false; + } + + @Override + public @NotNull T pendingValue() { + return value; + } + + @Override + public void requestSet(@NotNull T value) { + binding.setValue(value); + } + + @Override + public boolean applyValue() { + return false; + } + + @Override + public void forgetPendingValue() { + + } + + @Override + public void requestSetDefault() { + + } + + @Override + public boolean isPendingValueDefault() { + return false; + } + + @Override + public boolean canResetToDefault() { + return false; + } + + @Override + public void addListener(BiConsumer, T> changedListener) { + + } + + /** + * Open in case mods need to find the real controller type. + */ + @ApiStatus.Internal + public record EntryController(Controller controller, ListOptionEntryImpl entry) implements Controller { + @Override + public Option option() { + return controller.option(); + } + + @Override + public Component formatValue() { + return controller.formatValue(); + } + + @Override + public AbstractWidget provideWidget(YACLScreen screen, Dimension widgetDimension) { + return new ListEntryWidget(screen, entry, controller.provideWidget(screen, widgetDimension)); + } + } + + private class EntryBinding implements Binding { + @Override + public void setValue(T newValue) { + value = newValue; + group.callListeners(true); + } + + @Override + public T getValue() { + return value; + } + + @Override + public T defaultValue() { + throw new UnsupportedOperationException(); + } + } +} diff --git a/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java b/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java new file mode 100644 index 0000000..c77d55f --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java @@ -0,0 +1,402 @@ +package dev.isxander.yacl3.impl; + +import com.google.common.collect.ImmutableList; +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.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; +import java.util.stream.Collectors; + +@ApiStatus.Internal +public final class ListOptionImpl implements ListOption { + private final Component name; + private final OptionDescription description; + private final Binding> binding; + private final Supplier initialValue; + private final List> entries; + private final boolean collapsed; + private boolean available; + private final int minimumNumberOfEntries; + private final int maximumNumberOfEntries; + private final boolean insertEntriesAtEnd; + private final ImmutableSet flags; + private final EntryFactory entryFactory; + + private final List>, List>> listeners; + private final List refreshListeners; + private int listenerTriggerDepth = 0; + + public ListOptionImpl(@NotNull Component name, @NotNull OptionDescription description, @NotNull Binding> binding, @NotNull Supplier initialValue, @NotNull Function, Controller> controllerFunction, ImmutableSet flags, boolean collapsed, boolean available, int minimumNumberOfEntries, int maximumNumberOfEntries, boolean insertEntriesAtEnd, Collection>, List>> listeners) { + this.name = name; + this.description = description; + this.binding = new SafeBinding<>(binding); + this.initialValue = initialValue; + this.entryFactory = new EntryFactory(controllerFunction); + this.entries = createEntries(binding().getValue()); + this.collapsed = collapsed; + this.flags = flags; + this.available = available; + this.minimumNumberOfEntries = minimumNumberOfEntries; + this.maximumNumberOfEntries = maximumNumberOfEntries; + this.insertEntriesAtEnd = insertEntriesAtEnd; + this.listeners = new ArrayList<>(); + this.listeners.addAll(listeners); + this.refreshListeners = new ArrayList<>(); + callListeners(true); + } + + @Override + public @NotNull Component name() { + return this.name; + } + + @Override + public @NotNull OptionDescription description() { + return this.description; + } + + @Override + public @NotNull Component tooltip() { + return description().text(); + } + + @Override + public @NotNull ImmutableList> options() { + return ImmutableList.copyOf(entries); + } + + @Override + public @NotNull Controller> controller() { + throw new UnsupportedOperationException(); + } + + @Override + public @NotNull Binding> binding() { + return binding; + } + + @Override + public boolean collapsed() { + return collapsed; + } + + @Override + public @NotNull ImmutableSet flags() { + return flags; + } + + @Override + public @NotNull ImmutableList pendingValue() { + return ImmutableList.copyOf(entries.stream().map(Option::pendingValue).toList()); + } + + @Override + public void insertEntry(int index, ListOptionEntry entry) { + entries.add(index, (ListOptionEntry) entry); + onRefresh(); + } + + @Override + public ListOptionEntry insertNewEntry() { + ListOptionEntry newEntry = entryFactory.create(initialValue.get()); + if (insertEntriesAtEnd) { + entries.add(newEntry); + } else { + // insert at top + entries.add(0, newEntry); + } + onRefresh(); + return newEntry; + } + + @Override + public void removeEntry(ListOptionEntry entry) { + if (entries.remove(entry)) + onRefresh(); + } + + @Override + public int indexOf(ListOptionEntry entry) { + return entries.indexOf(entry); + } + + @Override + public void requestSet(@NotNull List value) { + entries.clear(); + entries.addAll(createEntries(value)); + onRefresh(); + } + + @Override + public boolean changed() { + return !binding().getValue().equals(pendingValue()); + } + + @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 boolean available() { + return available; + } + + @Override + public void setAvailable(boolean available) { + boolean changed = this.available != available; + + this.available = available; + + if (changed) + callListeners(false); + } + + @Override + public int numberOfEntries() { + return this.entries.size(); + } + @Override + public int maximumNumberOfEntries() { + return this.maximumNumberOfEntries; + } + @Override + public int minimumNumberOfEntries() { + return this.minimumNumberOfEntries; + } + + @Override + public void addListener(BiConsumer>, List> changedListener) { + this.listeners.add(changedListener); + } + + @Override + public void addRefreshListener(Runnable changedListener) { + this.refreshListeners.add(changedListener); + } + + @Override + public boolean isRoot() { + return false; + } + + private List> createEntries(Collection values) { + return values.stream().map(entryFactory::create).collect(Collectors.toList()); + } + + void callListeners(boolean bypass) { + List pendingValue = pendingValue(); + 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>, List> 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--; + } + } + + private void onRefresh() { + refreshListeners.forEach(Runnable::run); + callListeners(true); + } + + private class EntryFactory { + private final Function, Controller> controllerFunction; + + private EntryFactory(Function, Controller> controllerFunction) { + this.controllerFunction = controllerFunction; + } + + public ListOptionEntry create(T initialValue) { + return new ListOptionEntryImpl<>(ListOptionImpl.this, initialValue, controllerFunction); + } + } + + @ApiStatus.Internal + public static final class BuilderImpl implements Builder { + private Component name = Component.empty(); + private OptionDescription description = OptionDescription.EMPTY; + private Function, Controller> controllerFunction; + private Binding> binding = null; + private final Set flags = new HashSet<>(); + private Supplier initialValue; + private boolean collapsed = false; + private boolean available = true; + private int minimumNumberOfEntries = 0; + private int maximumNumberOfEntries = Integer.MAX_VALUE; + private boolean insertEntriesAtEnd = false; + private final List>, List>> listeners = new ArrayList<>(); + + @Override + public Builder name(@NotNull Component name) { + Validate.notNull(name, "`name` must not be null"); + + this.name = name; + return this; + } + + @Override + public Builder description(@NotNull OptionDescription description) { + Validate.notNull(description, "`description` must not be null"); + + this.description = description; + return this; + } + + @Override + public Builder initial(@NotNull Supplier initialValue) { + Validate.notNull(initialValue, "`initialValue` cannot be empty"); + + this.initialValue = initialValue; + return this; + } + + @Override + public Builder initial(@NotNull T initialValue) { + Validate.notNull(initialValue, "`initialValue` cannot be empty"); + + this.initialValue = () -> initialValue; + return this; + } + + @Override + public Builder controller(@NotNull Function, ControllerBuilder> controller) { + Validate.notNull(controller, "`controller` cannot be null"); + + this.controllerFunction = opt -> controller.apply(opt).build(); + return this; + } + + @Override + public Builder customController(@NotNull Function, Controller> control) { + Validate.notNull(control, "`control` cannot be null"); + + this.controllerFunction = control; + return this; + } + + @Override + public Builder binding(@NotNull Binding> binding) { + Validate.notNull(binding, "`binding` cannot be null"); + + this.binding = binding; + return this; + } + + @Override + public Builder binding(@NotNull List def, @NotNull Supplier<@NotNull List> getter, @NotNull Consumer<@NotNull List> 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 available(boolean available) { + this.available = available; + return this; + } + + @Override + public Builder minimumNumberOfEntries(int number) { + this.minimumNumberOfEntries = number; + return this; + } + + @Override + public Builder maximumNumberOfEntries(int number) { + this.maximumNumberOfEntries = number; + return this; + } + + @Override + public Builder insertEntriesAtEnd(boolean insertAtEnd) { + this.insertEntriesAtEnd = insertAtEnd; + return this; + } + + @Override + public Builder flag(@NotNull OptionFlag... flag) { + Validate.notNull(flag, "`flag` must not be null"); + + this.flags.addAll(Arrays.asList(flag)); + return this; + } + + @Override + public Builder flags(@NotNull Collection flags) { + Validate.notNull(flags, "`flags` must not be null"); + + this.flags.addAll(flags); + return this; + } + + @Override + public Builder collapsed(boolean collapsible) { + this.collapsed = collapsible; + return this; + } + + @Override + public Builder listener(@NotNull BiConsumer>, List> listener) { + this.listeners.add(listener); + return this; + } + + @Override + public Builder listeners(@NotNull Collection>, List>> listeners) { + this.listeners.addAll(listeners); + return this; + } + + @Override + public ListOption 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"); + + return new ListOptionImpl<>(name, description, binding, initialValue, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available, minimumNumberOfEntries, maximumNumberOfEntries, insertEntriesAtEnd, listeners); + } + } +} diff --git a/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java b/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java new file mode 100644 index 0000000..67fa6a6 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/OptionDescriptionImpl.java @@ -0,0 +1,133 @@ +package dev.isxander.yacl3.impl; + +import dev.isxander.yacl3.api.OptionDescription; +import dev.isxander.yacl3.gui.image.ImageRenderer; +import dev.isxander.yacl3.gui.image.ImageRendererManager; +import dev.isxander.yacl3.gui.image.impl.AnimatedDynamicTextureImage; +import dev.isxander.yacl3.gui.image.impl.DynamicTextureImage; +import dev.isxander.yacl3.gui.image.impl.ResourceTextureImage; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.resources.ResourceLocation; +import org.apache.commons.lang3.Validate; + +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Path; +import java.util.*; +import java.util.concurrent.CompletableFuture; + +public record OptionDescriptionImpl(Component text, CompletableFuture> image) implements OptionDescription { + public static class BuilderImpl implements Builder { + private final List descriptionLines = new ArrayList<>(); + private CompletableFuture> image = CompletableFuture.completedFuture(Optional.empty()); + private boolean imageUnset = true; + + @Override + public Builder text(Component... description) { + this.descriptionLines.addAll(Arrays.asList(description)); + return this; + } + + @Override + public Builder text(Collection lines) { + this.descriptionLines.addAll(lines); + return this; + } + + @Override + public Builder image(ResourceLocation image, int width, int height) { + Validate.isTrue(imageUnset, "Image already set!"); + Validate.isTrue(width > 0, "Width must be greater than 0!"); + Validate.isTrue(height > 0, "Height must be greater than 0!"); + + this.image = ImageRendererManager.registerImage(image, ResourceTextureImage.createFactory(image, 0, 0, width, height, width, height)).thenApply(Optional::of); + imageUnset = false; + return this; + } + + @Override + public Builder image(ResourceLocation image, float u, float v, int width, int height, int textureWidth, int textureHeight) { + Validate.isTrue(imageUnset, "Image already set!"); + Validate.isTrue(width > 0, "Width must be greater than 0!"); + Validate.isTrue(height > 0, "Height must be greater than 0!"); + + this.image = ImageRendererManager.registerImage(image, ResourceTextureImage.createFactory(image, u, v, width, height, textureWidth, textureHeight)).thenApply(Optional::of); + imageUnset = false; + return this; + } + + @Override + public Builder image(Path path, ResourceLocation uniqueLocation) { + Validate.isTrue(imageUnset, "Image already set!"); + + this.image = ImageRendererManager.registerImage(uniqueLocation, DynamicTextureImage.fromPath(path, uniqueLocation)).thenApply(Optional::of); + imageUnset = false; + return this; + } + + @Override + public Builder gifImage(ResourceLocation image) { + Validate.isTrue(imageUnset, "Image already set!"); + + this.image = ImageRendererManager.registerImage(image, AnimatedDynamicTextureImage.createGIFFromTexture(image)).thenApply(Optional::of); + imageUnset = false; + return this; + } + + @Override + public Builder gifImage(Path path, ResourceLocation uniqueLocation) { + Validate.isTrue(imageUnset, "Image already set!"); + + this.image = ImageRendererManager.registerImage(uniqueLocation, AnimatedDynamicTextureImage.createGIFFromPath(path, uniqueLocation)).thenApply(Optional::of); + imageUnset = false; + return this; + } + + @Override + public Builder webpImage(ResourceLocation image) { + Validate.isTrue(imageUnset, "Image already set!"); + + Optional completedImage = ImageRendererManager.getImage(image); + if (completedImage.isPresent()) { + this.image = CompletableFuture.completedFuture(completedImage); + } else { + this.image = ImageRendererManager.registerImage(image, AnimatedDynamicTextureImage.createWEBPFromTexture(image)).thenApply(Optional::of); + } + + imageUnset = false; + return this; + } + + @Override + public Builder webpImage(Path path, ResourceLocation uniqueLocation) { + Validate.isTrue(imageUnset, "Image already set!"); + + this.image = ImageRendererManager.registerImage(uniqueLocation, AnimatedDynamicTextureImage.createWEBPFromPath(path, uniqueLocation)).thenApply(Optional::of); + imageUnset = false; + return this; + } + + @Override + public Builder customImage(CompletableFuture> image) { + Validate.notNull(image, "Image cannot be null!"); + Validate.isTrue(imageUnset, "Image already set!"); + + this.image = image; + this.imageUnset = false; + return this; + } + + @Override + public OptionDescription build() { + MutableComponent concatenatedDescription = Component.empty(); + Iterator iter = descriptionLines.iterator(); + while (iter.hasNext()) { + concatenatedDescription.append(iter.next()); + if (iter.hasNext()) concatenatedDescription.append("\n"); + } + + return new OptionDescriptionImpl(concatenatedDescription, image); + } + } +} diff --git a/src/main/java/dev/isxander/yacl3/impl/OptionGroupImpl.java b/src/main/java/dev/isxander/yacl3/impl/OptionGroupImpl.java new file mode 100644 index 0000000..7805b29 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/OptionGroupImpl.java @@ -0,0 +1,121 @@ +package dev.isxander.yacl3.impl; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl3.api.ListOption; +import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.OptionDescription; +import dev.isxander.yacl3.api.OptionGroup; +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.ArrayList; +import java.util.Collection; +import java.util.List; + +@ApiStatus.Internal +public final class OptionGroupImpl implements OptionGroup { + private final @NotNull Component name; + private final @NotNull OptionDescription description; + private final ImmutableList> options; + private final boolean collapsed; + private final boolean isRoot; + + public OptionGroupImpl(@NotNull Component name, @NotNull OptionDescription description, ImmutableList> options, boolean collapsed, boolean isRoot) { + this.name = name; + this.description = description; + this.options = options; + this.collapsed = collapsed; + this.isRoot = isRoot; + } + + @Override + public @NotNull Component name() { + return name; + } + + @Override + public OptionDescription description() { + return description; + } + + @Override + public @NotNull Component tooltip() { + return description.text(); + } + + @Override + public @NotNull ImmutableList> options() { + return options; + } + + @Override + public boolean collapsed() { + return collapsed; + } + + @Override + public boolean isRoot() { + return isRoot; + } + + @ApiStatus.Internal + public static final class BuilderImpl implements Builder { + private Component name = Component.empty(); + private OptionDescription description = OptionDescription.EMPTY; + private final List> options = new ArrayList<>(); + private boolean collapsed = false; + + @Override + public Builder name(@NotNull Component name) { + Validate.notNull(name, "`name` must not be null"); + + this.name = name; + return this; + } + + @Override + public Builder description(@NotNull OptionDescription description) { + Validate.notNull(description, "`description` must not be null"); + + this.description = description; + return this; + } + + @Override + 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; + } + + @Override + public Builder options(@NotNull Collection> 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; + } + + @Override + public Builder collapsed(boolean collapsible) { + this.collapsed = collapsible; + return this; + } + + @Override + public OptionGroup build() { + Validate.notEmpty(options, "`options` must not be empty to build `OptionGroup`"); + + return new OptionGroupImpl(name, description, ImmutableList.copyOf(options), collapsed, false); + } + } +} diff --git a/src/main/java/dev/isxander/yacl3/impl/OptionImpl.java b/src/main/java/dev/isxander/yacl3/impl/OptionImpl.java new file mode 100644 index 0000000..afe9517 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/OptionImpl.java @@ -0,0 +1,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 implements Option { + private final Component name; + private OptionDescription description; + private final Controller controller; + private final Binding binding; + private boolean available; + + private final ImmutableSet flags; + + private T pendingValue; + + private final List, T>> listeners; + private int listenerTriggerDepth = 0; + + public OptionImpl( + @NotNull Component name, + @NotNull Function descriptionFunction, + @NotNull Function, Controller> controlGetter, + @NotNull Binding binding, + boolean available, + ImmutableSet flags, + @NotNull Collection, 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 controller() { + return controller; + } + + @Override + public @NotNull Binding 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 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, 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, 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 implements Builder { + private Component name = Component.literal("Name not specified!").withStyle(ChatFormatting.RED); + + private Function descriptionFunction = pending -> OptionDescription.EMPTY; + + private Function, Controller> controlGetter; + + private Binding binding; + + private boolean available = true; + + private boolean instant = false; + + private final Set flags = new HashSet<>(); + + private final List, T>> listeners = new ArrayList<>(); + + @Override + public Builder name(@NotNull Component name) { + Validate.notNull(name, "`name` cannot be null"); + + this.name = name; + return this; + } + + @Override + public Builder description(@NotNull OptionDescription description) { + return description(opt -> description); + } + + @Override + public Builder description(@NotNull Function descriptionFunction) { + this.descriptionFunction = descriptionFunction; + return this; + } + + @Override + public Builder controller(@NotNull Function, ControllerBuilder> controllerBuilder) { + Validate.notNull(controllerBuilder, "`controllerBuilder` cannot be null"); + + return customController(opt -> controllerBuilder.apply(opt).build()); + } + + @Override + public Builder customController(@NotNull Function, Controller> control) { + Validate.notNull(control, "`control` cannot be null"); + + this.controlGetter = control; + return this; + } + + @Override + public Builder binding(@NotNull Binding binding) { + Validate.notNull(binding, "`binding` cannot be null"); + + this.binding = binding; + return this; + } + + @Override + public Builder 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 available(boolean available) { + this.available = available; + return this; + } + + @Override + public Builder flag(@NotNull OptionFlag... flag) { + Validate.notNull(flag, "`flag` must not be null"); + + this.flags.addAll(Arrays.asList(flag)); + return this; + } + + @Override + public Builder flags(@NotNull Collection flags) { + Validate.notNull(flags, "`flags` must not be null"); + + this.flags.addAll(flags); + return this; + } + + @Override + public Builder instant(boolean instant) { + this.instant = instant; + return this; + } + + @Override + public Builder listener(@NotNull BiConsumer, T> listener) { + this.listeners.add(listener); + return this; + } + + @Override + public Builder listeners(@NotNull Collection, T>> listeners) { + this.listeners.addAll(listeners); + return this; + } + + @Override + public Option 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); + } + } +} diff --git a/src/main/java/dev/isxander/yacl3/impl/PlaceholderCategoryImpl.java b/src/main/java/dev/isxander/yacl3/impl/PlaceholderCategoryImpl.java new file mode 100644 index 0000000..5e836a3 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/impl/PlaceholderCategoryImpl.java @@ -0,0 +1,99 @@ +package dev.isxander.yacl3.impl; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl3.api.OptionGroup; +import dev.isxander.yacl3.api.PlaceholderCategory; +import dev.isxander.yacl3.gui.YACLScreen; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import org.apache.commons.lang3.Validate; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiFunction; + +@ApiStatus.Internal +public final class PlaceholderCategoryImpl implements PlaceholderCategory { + private final Component name; + private final BiFunction screen; + private final Component tooltip; + + public PlaceholderCategoryImpl(Component name, BiFunction screen, Component tooltip) { + this.name = name; + this.screen = screen; + this.tooltip = tooltip; + } + + @Override + public @NotNull ImmutableList groups() { + return ImmutableList.of(); + } + + @Override + public @NotNull Component name() { + return name; + } + + @Override + public BiFunction screen() { + return screen; + } + + @Override + public @NotNull Component tooltip() { + return tooltip; + } + + @ApiStatus.Internal + public static final class BuilderImpl implements Builder { + private Component name; + + private final List tooltipLines = new ArrayList<>(); + + private BiFunction screenFunction; + + @Override + public Builder name(@NotNull Component name) { + Validate.notNull(name, "`name` cannot be null"); + + this.name = name; + return this; + } + + @Override + public Builder tooltip(@NotNull Component... tooltips) { + Validate.notEmpty(tooltips, "`tooltips` cannot be empty"); + + tooltipLines.addAll(List.of(tooltips)); + return this; + } + + @Override + public Builder screen(@NotNull BiFunction screenFunction) { + Validate.notNull(screenFunction, "`screenFunction` cannot be null"); + + this.screenFunction = screenFunction; + return this; + } + + @Override + public PlaceholderCategory build() { + Validate.notNull(name, "`name` must not be null to build `ConfigCategory`"); + + MutableCo