Skip to content

Commit 7a66935

Browse files
committed
Fix occasionally creating invalid tooltips that then cause crashes (#8058)
1 parent 2792ad0 commit 7a66935

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

src/main/java/mekanism/client/gui/element/GuiDropdown.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import net.minecraft.client.gui.GuiGraphics;
1414
import net.minecraft.client.gui.components.Tooltip;
1515
import net.minecraft.client.gui.navigation.ScreenRectangle;
16-
import net.minecraft.network.chat.Component;
1716
import net.minecraft.resources.ResourceLocation;
1817
import org.jetbrains.annotations.NotNull;
1918
import org.jetbrains.annotations.Nullable;
@@ -111,10 +110,7 @@ protected ScreenRectangle getTooltipRectangle(int mouseX, int mouseY) {
111110
public void updateTooltip(int mouseX, int mouseY) {
112111
int index = getHoveredIndex(mouseX, mouseY);
113112
if (index != -1) {
114-
Tooltip text = typeTooltips.computeIfAbsent(options[index], t -> {
115-
Component tooltip = t.getTooltip();
116-
return tooltip == null ? null : TooltipUtils.create(tooltip);
117-
});
113+
Tooltip text = typeTooltips.computeIfAbsent(options[index], t -> TooltipUtils.create(t.getTooltip()));
118114
cachedTooltipRect = new ScreenRectangle(getX() + 1, getY() + 12 + index * 10, width - 2, 10);
119115
setTooltip(text);
120116
} else {

src/main/java/mekanism/client/gui/element/bar/GuiBar.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mekanism.client.gui.element.bar;
22

3+
import java.util.Objects;
34
import java.util.function.BooleanSupplier;
45
import mekanism.client.gui.IGuiWrapper;
56
import mekanism.client.gui.element.GuiTexturedElement;
@@ -84,9 +85,7 @@ void drawContentsChecked(@NotNull GuiGraphics guiGraphics, int mouseX, int mouse
8485
@Override
8586
public void updateTooltip(int mouseX, int mouseY) {
8687
Component tooltip = handler.getTooltip();
87-
if (tooltip == null) {
88-
lastTooltip = null;
89-
} else if (!tooltip.equals(lastInfo)) {
88+
if (!Objects.equals(tooltip, lastInfo)) {
9089
lastTooltip = TooltipUtils.create(tooltip);
9190
}
9291
lastInfo = tooltip;

src/main/java/mekanism/client/gui/element/button/ToggleButton.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.minecraft.network.chat.Component;
1010
import net.minecraft.resources.ResourceLocation;
1111
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
1213

1314
public class ToggleButton extends MekanismImageButton {
1415

@@ -17,7 +18,9 @@ public class ToggleButton extends MekanismImageButton {
1718

1819
private final ResourceLocation flipped;
1920
private final BooleanSupplier toggled;
21+
@Nullable
2022
private final Tooltip yes;
23+
@Nullable
2124
private final Tooltip no;
2225

2326
public ToggleButton(IGuiWrapper gui, int x, int y, BooleanSupplier toggled, @NotNull IClickable onPress) {
@@ -29,12 +32,12 @@ public ToggleButton(IGuiWrapper gui, int x, int y, int size, BooleanSupplier tog
2932
}
3033

3134
public ToggleButton(IGuiWrapper gui, int x, int y, int size, int textureSize, ResourceLocation toggle, ResourceLocation flipped, BooleanSupplier toggled,
32-
@NotNull IClickable onPress, Component yes, Component no) {
35+
@NotNull IClickable onPress, @Nullable Component yes, @Nullable Component no) {
3336
this(gui, x, y, size, size, textureSize, textureSize, toggle, flipped, toggled, onPress, yes, no);
3437
}
3538

3639
public ToggleButton(IGuiWrapper gui, int x, int y, int width, int height, int textureWidth, int textureHeight, ResourceLocation toggle, ResourceLocation flipped,
37-
BooleanSupplier toggled, @NotNull IClickable onPress, Component yes, Component no) {
40+
BooleanSupplier toggled, @NotNull IClickable onPress, @Nullable Component yes, @Nullable Component no) {
3841
super(gui, x, y, width, height, textureWidth, textureHeight, toggle, onPress);
3942
this.toggled = toggled;
4043
this.flipped = flipped;

src/main/java/mekanism/client/gui/tooltip/TooltipUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import mekanism.common.MekanismLang;
77
import net.minecraft.client.gui.components.Tooltip;
88
import net.minecraft.network.chat.Component;
9+
import org.jetbrains.annotations.Contract;
910
import org.jetbrains.annotations.Nullable;
1011

1112
public class TooltipUtils {
@@ -19,8 +20,12 @@ public static Tooltip create(ILangEntry langEntry) {
1920
return create(langEntry.translate());
2021
}
2122

22-
public static Tooltip create(Component messages) {
23-
Tooltip tooltip = Tooltip.create(messages);
23+
@Contract("null -> null")
24+
public static Tooltip create(@Nullable Component message) {
25+
if (message == null) {
26+
return null;
27+
}
28+
Tooltip tooltip = Tooltip.create(message);
2429
//Set the delay to -1 so that it appears immediately instead of after a single millisecond
2530
tooltip.setDelay(-1);
2631
return tooltip;

0 commit comments

Comments
 (0)