diff options
Diffstat (limited to 'common/src/main/java/dev/isxander/yacl/impl')
3 files changed, 55 insertions, 28 deletions
diff --git a/common/src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java b/common/src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java index 2d39eb9..ed73174 100644 --- a/common/src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java +++ b/common/src/main/java/dev/isxander/yacl/impl/ConfigCategoryImpl.java @@ -1,10 +1,7 @@ package dev.isxander.yacl.impl; import com.google.common.collect.ImmutableList; -import dev.isxander.yacl.api.ConfigCategory; -import dev.isxander.yacl.api.ListOption; -import dev.isxander.yacl.api.Option; -import dev.isxander.yacl.api.OptionGroup; +import dev.isxander.yacl.api.*; import dev.isxander.yacl.impl.utils.YACLConstants; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.ComponentContents; @@ -114,7 +111,7 @@ public final class ConfigCategoryImpl implements ConfigCategory { Validate.notNull(name, "`name` must not be null to build `ConfigCategory`"); List<OptionGroup> combinedGroups = new ArrayList<>(); - combinedGroups.add(new OptionGroupImpl(Component.empty(), Component.empty(), ImmutableList.copyOf(rootOptions), false, true)); + combinedGroups.add(new OptionGroupImpl(Component.empty(), OptionDescription.createBuilder().name(Component.literal("Root")).build(), ImmutableList.copyOf(rootOptions), false, true)); combinedGroups.addAll(groups); Validate.notEmpty(combinedGroups, "at least one option must be added to build `ConfigCategory`"); diff --git a/common/src/main/java/dev/isxander/yacl/impl/OptionDescriptionImpl.java b/common/src/main/java/dev/isxander/yacl/impl/OptionDescriptionImpl.java index c866b43..5d09828 100644 --- a/common/src/main/java/dev/isxander/yacl/impl/OptionDescriptionImpl.java +++ b/common/src/main/java/dev/isxander/yacl/impl/OptionDescriptionImpl.java @@ -4,19 +4,20 @@ import dev.isxander.yacl.api.OptionDescription; import dev.isxander.yacl.gui.ImageRenderer; import net.minecraft.ChatFormatting; 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.Optional; +import java.util.*; import java.util.concurrent.CompletableFuture; public record OptionDescriptionImpl(Component descriptiveName, Component description, CompletableFuture<Optional<ImageRenderer>> image) implements OptionDescription { public static class BuilderImpl implements Builder { private Component name; - private Component description; + private final List<Component> descriptionLines = new ArrayList<>(); private CompletableFuture<Optional<ImageRenderer>> image = CompletableFuture.completedFuture(Optional.empty()); private boolean imageUnset = true; @@ -27,8 +28,14 @@ public record OptionDescriptionImpl(Component descriptiveName, Component descrip } @Override - public Builder description(Component description) { - this.description = description; + public Builder description(Component... description) { + this.descriptionLines.addAll(Arrays.asList(description)); + return this; + } + + @Override + public Builder description(Collection<? extends Component> lines) { + this.descriptionLines.addAll(lines); return this; } @@ -115,10 +122,14 @@ public record OptionDescriptionImpl(Component descriptiveName, Component descrip public OptionDescription build() { Validate.notNull(name, "Name must be set!"); - if (description == null) - description = Component.empty(); + MutableComponent concatenatedDescription = Component.empty(); + Iterator<Component> iter = descriptionLines.iterator(); + while (iter.hasNext()) { + concatenatedDescription.append(iter.next()); + if (iter.hasNext()) concatenatedDescription.append("\n"); + } - return new OptionDescriptionImpl(name.copy().withStyle(ChatFormatting.BOLD), description, image); + return new OptionDescriptionImpl(name.copy().withStyle(ChatFormatting.BOLD), concatenatedDescription, image); } } } 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 113aefc..a72aa71 100644 --- a/common/src/main/java/dev/isxander/yacl/impl/OptionGroupImpl.java +++ b/common/src/main/java/dev/isxander/yacl/impl/OptionGroupImpl.java @@ -3,6 +3,7 @@ package dev.isxander.yacl.impl; import com.google.common.collect.ImmutableList; 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 net.minecraft.network.chat.Component; import net.minecraft.network.chat.ComponentContents; @@ -10,6 +11,7 @@ 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; @@ -18,14 +20,14 @@ import java.util.List; @ApiStatus.Internal public final class OptionGroupImpl implements OptionGroup { private final @NotNull Component name; - private final @NotNull Component tooltip; + private final @NotNull OptionDescription description; private final ImmutableList<? extends Option<?>> options; private final boolean collapsed; private final boolean isRoot; - public OptionGroupImpl(@NotNull Component name, @NotNull Component tooltip, ImmutableList<? extends Option<?>> options, boolean collapsed, boolean isRoot) { + public OptionGroupImpl(@NotNull Component name, @NotNull OptionDescription description, ImmutableList<? extends Option<?>> options, boolean collapsed, boolean isRoot) { this.name = name; - this.tooltip = tooltip; + this.description = description; this.options = options; this.collapsed = collapsed; this.isRoot = isRoot; @@ -37,8 +39,13 @@ public final class OptionGroupImpl implements OptionGroup { } @Override + public OptionDescription description() { + return description; + } + + @Override public @NotNull Component tooltip() { - return tooltip; + return description.description(); } @Override @@ -59,7 +66,8 @@ public final class OptionGroupImpl implements OptionGroup { @ApiStatus.Internal public static final class BuilderImpl implements Builder { private Component name = Component.empty(); - private final List<Component> tooltipLines = new ArrayList<>(); + private OptionDescription description = null; + private OptionDescription.Builder legacyBuilder = null; private final List<Option<?>> options = new ArrayList<>(); private boolean collapsed = false; @@ -72,10 +80,22 @@ public final class OptionGroupImpl implements OptionGroup { } @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; } @@ -111,19 +131,18 @@ public final class OptionGroupImpl implements OptionGroup { public OptionGroup build() { Validate.notEmpty(options, "`options` must not be empty to build `OptionGroup`"); - MutableComponent concatenatedTooltip = Component.empty(); - boolean first = true; - for (Component line : tooltipLines) { - if (line.getContents() == ComponentContents.EMPTY) - continue; + if (description == null) { + ensureLegacyDescriptionBuilder(); + description = legacyBuilder.name(name).build(); + } - if (!first) concatenatedTooltip.append("\n"); - first = false; + return new OptionGroupImpl(name, description, ImmutableList.copyOf(options), collapsed, false); + } - concatenatedTooltip.append(line); + private void ensureLegacyDescriptionBuilder() { + if (legacyBuilder == null) { + legacyBuilder = OptionDescription.createBuilder(); } - - return new OptionGroupImpl(name, concatenatedTooltip, ImmutableList.copyOf(options), collapsed, false); } } } |