From 4a4a5291f26f6798f5e048b649f05ad682efd5f9 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Sun, 18 Oct 2020 16:29:13 +0300 Subject: Fix #86 --- .../cottonmc/cotton/gui/widget/WTabPanel.java | 34 +++++++++++++--------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java index 039f2bd..d185d09 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTabPanel.java @@ -19,6 +19,7 @@ import net.minecraft.util.Identifier; import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.function.Consumer; @@ -106,6 +107,7 @@ public class WTabPanel extends WPanel { @Nullable private final Icon icon; private final WWidget widget; + @Nullable private final Consumer tooltip; /** @@ -116,9 +118,11 @@ public class WTabPanel extends WPanel { * @param widget the widget contained in the tab * @param tooltip the tab tooltip * @throws IllegalArgumentException if both the title and the icon are null - * @throws NullPointerException if either the widget or the tooltip is null + * @throws NullPointerException if the widget is null + * @deprecated Use {@link Builder} instead. */ - public Tab(@Nullable Text title, @Nullable Icon icon, WWidget widget, Consumer tooltip) { + @Deprecated + public Tab(@Nullable Text title, @Nullable Icon icon, WWidget widget, @Nullable Consumer tooltip) { if (title == null && icon == null) { throw new IllegalArgumentException("A tab must have a title or an icon"); } @@ -126,7 +130,7 @@ public class WTabPanel extends WPanel { this.title = title; this.icon = icon; this.widget = Objects.requireNonNull(widget, "widget"); - this.tooltip = Objects.requireNonNull(tooltip, "tooltip"); + this.tooltip = tooltip; } /** @@ -163,8 +167,11 @@ public class WTabPanel extends WPanel { * * @param tooltip the tooltip builder */ + @Environment(EnvType.CLIENT) public void addTooltip(TooltipBuilder tooltip) { - this.tooltip.accept(tooltip); + if (this.tooltip != null) { + this.tooltip.accept(tooltip); + } } /** @@ -176,8 +183,7 @@ public class WTabPanel extends WPanel { @Nullable private Icon icon; private final WWidget widget; - private final List> tooltip = new ArrayList<>(); - private static final Consumer DEFAULT_TOOLTIP = builder -> {}; + private final List tooltip = new ArrayList<>(); /** * Constructs a new tab data builder. @@ -222,7 +228,7 @@ public class WTabPanel extends WPanel { */ public Builder tooltip(Text... lines) { Objects.requireNonNull(lines, "lines"); - tooltip.add(builder -> builder.add(lines)); + Collections.addAll(tooltip, lines); return this; } @@ -236,7 +242,7 @@ public class WTabPanel extends WPanel { */ public Builder tooltip(Collection lines) { Objects.requireNonNull(lines, "lines"); - tooltip.add(builder -> builder.add(lines.toArray(new Text[0]))); + tooltip.addAll(lines); return this; } @@ -244,15 +250,17 @@ public class WTabPanel extends WPanel { * Builds a tab from this builder. * * @return the built tab - * @see Tab#Tab(Text, Icon, WWidget, Consumer) */ public Tab build() { - Consumer tooltip = DEFAULT_TOOLTIP; + Consumer tooltip = null; if (!this.tooltip.isEmpty()) { - tooltip = builder -> { - for (Consumer entry : this.tooltip) { - entry.accept(builder); + //noinspection Convert2Lambda + tooltip = new Consumer() { + @Environment(EnvType.CLIENT) + @Override + public void accept(TooltipBuilder builder) { + builder.add(Tab.Builder.this.tooltip.toArray(new Text[0])); } }; } -- cgit