diff options
author | isXander <xandersmith2008@gmail.com> | 2023-05-23 23:17:12 +0100 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2023-05-23 23:17:12 +0100 |
commit | aba5085302c6ad8eec1aa9ed25a9a86758d95d34 (patch) | |
tree | 4a69464f6030d65337aab97035ea282e69a071fa /common/src/main | |
parent | 3b8e4e76b4893fc910e6af1024c3710b6f87954b (diff) | |
download | YetAnotherConfigLib-aba5085302c6ad8eec1aa9ed25a9a86758d95d34.tar.gz YetAnotherConfigLib-aba5085302c6ad8eec1aa9ed25a9a86758d95d34.tar.bz2 YetAnotherConfigLib-aba5085302c6ad8eec1aa9ed25a9a86758d95d34.zip |
add list option descriptions + warn with deprecated tooltips
Diffstat (limited to 'common/src/main')
5 files changed, 63 insertions, 30 deletions
diff --git a/common/src/main/java/dev/isxander/yacl/api/ListOption.java b/common/src/main/java/dev/isxander/yacl/api/ListOption.java index afba8ee..c30f359 100644 --- a/common/src/main/java/dev/isxander/yacl/api/ListOption.java +++ b/common/src/main/java/dev/isxander/yacl/api/ListOption.java @@ -61,6 +61,8 @@ public interface ListOption<T> extends OptionGroup, Option<List<T>> { */ Builder<T> name(@NotNull Component name); + Builder<T> description(@NotNull OptionDescription description); + /** * Sets the tooltip to be used by the list. It is displayed like a normal * group when you hover over the name. Entries do not allow a tooltip. @@ -70,6 +72,7 @@ public interface ListOption<T> extends OptionGroup, Option<List<T>> { * * @param tooltips text lines - merged with a new-line on {@link dev.isxander.yacl.api.ListOption.Builder#build()}. */ + @Deprecated Builder<T> tooltip(@NotNull Component... tooltips); /** diff --git a/common/src/main/java/dev/isxander/yacl/gui/ScrollableNavigationBar.java b/common/src/main/java/dev/isxander/yacl/gui/ScrollableNavigationBar.java index e2d9ab8..62c63db 100644 --- a/common/src/main/java/dev/isxander/yacl/gui/ScrollableNavigationBar.java +++ b/common/src/main/java/dev/isxander/yacl/gui/ScrollableNavigationBar.java @@ -61,6 +61,10 @@ public class ScrollableNavigationBar extends TabNavigationBar { layout.setX(layout.getX() - this.scrollOffset); } + public int getScrollOffset() { + return scrollOffset; + } + @Override public void setFocused(@Nullable GuiEventListener child) { super.setFocused(child); diff --git a/common/src/main/java/dev/isxander/yacl/impl/ListOptionImpl.java b/common/src/main/java/dev/isxander/yacl/impl/ListOptionImpl.java index 24fe2b1..a5536a4 100644 --- a/common/src/main/java/dev/isxander/yacl/impl/ListOptionImpl.java +++ b/common/src/main/java/dev/isxander/yacl/impl/ListOptionImpl.java @@ -3,8 +3,8 @@ package dev.isxander.yacl.impl; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import dev.isxander.yacl.api.*; +import dev.isxander.yacl.impl.utils.YACLConstants; 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; @@ -218,7 +218,8 @@ public final class ListOptionImpl<T> implements ListOption<T> { @ApiStatus.Internal public static final class BuilderImpl<T> implements Builder<T> { private Component name = Component.empty(); - private final List<Component> tooltipLines = new ArrayList<>(); + private OptionDescription description = null; + private OptionDescription.Builder legacyBuilder = null; private Function<ListOptionEntry<T>, Controller<T>> controllerFunction; private Binding<List<T>> binding = null; private final Set<OptionFlag> flags = new HashSet<>(); @@ -241,10 +242,22 @@ public final class ListOptionImpl<T> implements ListOption<T> { } @Override + public Builder<T> description(@NotNull OptionDescription description) { + Validate.isTrue(legacyBuilder == null, "Cannot set description when deprecated `tooltip` method is used"); + Validate.notNull(description, "`description` must not be null"); + + this.description = description; + return this; + } + + @Override public Builder<T> tooltip(@NotNull Component... tooltips) { + Validate.isTrue(description == null, "Cannot use deprecated `tooltip` method when `description` in use."); Validate.notEmpty(tooltips, "`tooltips` cannot be empty"); - tooltipLines.addAll(List.of(tooltips)); + ensureLegacyDescriptionBuilder(); + + legacyBuilder.description(tooltips); return this; } @@ -328,16 +341,23 @@ public final class ListOptionImpl<T> implements ListOption<T> { Validate.notNull(binding, "`binding` must not be null"); Validate.notNull(initialValue, "`initialValue` must not be null"); - MutableComponent concatenatedTooltip = Component.empty(); - boolean first = true; - for (Component line : tooltipLines) { - if (!first) concatenatedTooltip.append("\n"); - first = false; + if (description == null) { + if (ensureLegacyDescriptionBuilder()) + YACLConstants.LOGGER.warn("Using deprecated `tooltip` method in list option {}. Use `description` instead.", name.getString()); - concatenatedTooltip.append(line); + description = legacyBuilder.name(name).build(); } - return new ListOptionImpl<>(name, OptionDescription.createBuilder().name(name).description(concatenatedTooltip).build(), binding, initialValue, typeClass, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available, listeners); + return new ListOptionImpl<>(name, description, binding, initialValue, typeClass, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available, listeners); + } + + private boolean ensureLegacyDescriptionBuilder() { + if (legacyBuilder == null) { + legacyBuilder = OptionDescription.createBuilder(); + return false; + } else { + return true; + } } } } diff --git a/common/src/main/java/dev/isxander/yacl/impl/OptionGroupImpl.java b/common/src/main/java/dev/isxander/yacl/impl/OptionGroupImpl.java index a72aa71..0649f16 100644 --- a/common/src/main/java/dev/isxander/yacl/impl/OptionGroupImpl.java +++ b/common/src/main/java/dev/isxander/yacl/impl/OptionGroupImpl.java @@ -5,13 +5,11 @@ import dev.isxander.yacl.api.ListOption; import dev.isxander.yacl.api.Option; import dev.isxander.yacl.api.OptionDescription; import dev.isxander.yacl.api.OptionGroup; +import dev.isxander.yacl.impl.utils.YACLConstants; import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.ComponentContents; -import net.minecraft.network.chat.MutableComponent; import org.apache.commons.lang3.Validate; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collection; @@ -132,16 +130,21 @@ public final class OptionGroupImpl implements OptionGroup { Validate.notEmpty(options, "`options` must not be empty to build `OptionGroup`"); if (description == null) { - ensureLegacyDescriptionBuilder(); + if (ensureLegacyDescriptionBuilder()) + YACLConstants.LOGGER.warn("Using deprecated `tooltip` method in option group '{}'. Use `description` instead.", name != null ? name.getString() : "unnamed group"); + description = legacyBuilder.name(name).build(); } return new OptionGroupImpl(name, description, ImmutableList.copyOf(options), collapsed, false); } - private void ensureLegacyDescriptionBuilder() { + private boolean ensureLegacyDescriptionBuilder() { if (legacyBuilder == null) { legacyBuilder = OptionDescription.createBuilder(); + return false; + } else { + return true; } } } diff --git a/common/src/main/java/dev/isxander/yacl/impl/OptionImpl.java b/common/src/main/java/dev/isxander/yacl/impl/OptionImpl.java index 27f43b8..98dcc23 100644 --- a/common/src/main/java/dev/isxander/yacl/impl/OptionImpl.java +++ b/common/src/main/java/dev/isxander/yacl/impl/OptionImpl.java @@ -2,8 +2,8 @@ package dev.isxander.yacl.impl; import com.google.common.collect.ImmutableSet; import dev.isxander.yacl.api.*; +import dev.isxander.yacl.impl.utils.YACLConstants; import net.minecraft.ChatFormatting; -import net.minecraft.Util; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.ComponentContents; import net.minecraft.network.chat.MutableComponent; @@ -291,24 +291,27 @@ public final class OptionImpl<T> implements Option<T> { 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, Component> concatenatedTooltipGetter = value -> { - MutableComponent concatenatedTooltip = Component.empty(); - boolean first = true; - for (Function<T, Component> line : tooltipGetters) { - Component lineComponent = line.apply(value); + if (descriptionFunction == null) { + if (!tooltipGetters.isEmpty()) + YACLConstants.LOGGER.warn("Using deprecated `tooltip` method in option '{}'. Use `description` instead.", name.getString()); - if (lineComponent.getContents() == ComponentContents.EMPTY) - continue; + Function<T, Component> concatenatedTooltipGetter = value -> { + MutableComponent concatenatedTooltip = Component.empty(); + boolean first = true; + for (Function<T, Component> line : tooltipGetters) { + Component lineComponent = line.apply(value); - if (!first) concatenatedTooltip.append("\n"); - first = false; + if (lineComponent.getContents() == ComponentContents.EMPTY) + continue; - concatenatedTooltip.append(lineComponent); - } + if (!first) concatenatedTooltip.append("\n"); + first = false; - return concatenatedTooltip; - }; - if (descriptionFunction == null) { + concatenatedTooltip.append(lineComponent); + } + + return concatenatedTooltip; + }; descriptionFunction = opt -> OptionDescription.createBuilder().name(name).description(concatenatedTooltipGetter.apply(opt)).build(); } |