From aba5085302c6ad8eec1aa9ed25a9a86758d95d34 Mon Sep 17 00:00:00 2001 From: isXander Date: Tue, 23 May 2023 23:17:12 +0100 Subject: add list option descriptions + warn with deprecated tooltips --- .../java/dev/isxander/yacl/api/ListOption.java | 3 ++ .../isxander/yacl/gui/ScrollableNavigationBar.java | 4 +++ .../dev/isxander/yacl/impl/ListOptionImpl.java | 40 ++++++++++++++++------ .../dev/isxander/yacl/impl/OptionGroupImpl.java | 13 ++++--- .../java/dev/isxander/yacl/impl/OptionImpl.java | 33 ++++++++++-------- 5 files changed, 63 insertions(+), 30 deletions(-) (limited to 'common/src/main/java/dev') 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 extends OptionGroup, Option> { */ Builder name(@NotNull Component name); + Builder 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 extends OptionGroup, Option> { * * @param tooltips text lines - merged with a new-line on {@link dev.isxander.yacl.api.ListOption.Builder#build()}. */ + @Deprecated Builder 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 implements ListOption { @ApiStatus.Internal public static final class BuilderImpl implements Builder { private Component name = Component.empty(); - private final List tooltipLines = new ArrayList<>(); + private OptionDescription description = null; + private OptionDescription.Builder legacyBuilder = null; private Function, Controller> controllerFunction; private Binding> binding = null; private final Set flags = new HashSet<>(); @@ -240,11 +241,23 @@ public final class ListOptionImpl implements ListOption { return this; } + @Override + public Builder 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 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 implements ListOption { 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 implements 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 concatenatedTooltipGetter = value -> { - MutableComponent concatenatedTooltip = Component.empty(); - boolean first = true; - for (Function 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 concatenatedTooltipGetter = value -> { + MutableComponent concatenatedTooltip = Component.empty(); + boolean first = true; + for (Function 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(); } -- cgit