Skip to content

Commit f84bc94

Browse files
committed
tag all builtin tooltips
Note that the tags are not guaranteed to be stable as API classes do. This is more of workaround to allow plugins to override builtin tooltips.
1 parent 0c9c888 commit f84bc94

File tree

22 files changed

+116
-64
lines changed

22 files changed

+116
-64
lines changed

src/api/java/mcp/mobius/waila/api/WailaConstants.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public class WailaConstants {
5050
*/
5151
public static final ResourceLocation MOD_NAME_TAG = id("mod_name");
5252

53+
/**
54+
* Tooltip tag for errors.
55+
*
56+
* @see ITooltip#setLine
57+
*/
58+
public static final ResourceLocation ERROR_TAG = id("error");
59+
5360
/**
5461
* Whether Waila should show tooltip for blocks.
5562
* <p>

src/main/java/mcp/mobius/waila/util/ExceptionUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Set;
55

66
import mcp.mobius.waila.api.ITooltip;
7+
import mcp.mobius.waila.api.WailaConstants;
78
import net.minecraft.ChatFormatting;
89
import net.minecraft.network.chat.Component;
910
import org.apache.commons.lang3.exception.ExceptionUtils;
@@ -23,8 +24,9 @@ public static boolean dump(Throwable e, String errorName, @Nullable ITooltip too
2324
}
2425

2526
if (tooltip != null) {
26-
tooltip.addLine(Component.literal("Error on " + errorName).withStyle(ChatFormatting.RED));
27-
tooltip.addLine(Component.literal("See logs for more info").withStyle(ChatFormatting.RED));
27+
tooltip.setLine(WailaConstants.ERROR_TAG, Component
28+
.literal("Error on " + errorName + "\nSee logs for more info")
29+
.withStyle(ChatFormatting.RED));
2830
}
2931

3032
return log;

src/pluginExtra/java/mcp/mobius/waila/plugin/extra/provider/FluidProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import mcp.mobius.waila.api.data.FluidData;
1313
import mcp.mobius.waila.api.util.WNumbers;
1414
import mcp.mobius.waila.plugin.extra.data.FluidDataImpl;
15+
import net.minecraft.core.registries.BuiltInRegistries;
1516
import net.minecraft.network.chat.Component;
1617
import net.minecraft.resources.ResourceLocation;
1718
import net.minecraft.world.level.block.Block;
@@ -64,7 +65,7 @@ private void addFluidTooltip(ITooltip tooltip, FluidDataImpl data, IPluginConfig
6465
text += " " + displayUnit.symbol;
6566

6667
var sprite = desc.sprite();
67-
tooltip.addLine(new PairComponent(
68+
tooltip.setLine(FluidData.ID.withSuffix("." + BuiltInRegistries.FLUID.getKey(entry.fluid()).toLanguageKey()), new PairComponent(
6869
new WrappedComponent(desc.name().getString()),
6970
new SpriteBarComponent(ratio, sprite, 16, 16, desc.tint(), Component.literal(text))));
7071
}

src/pluginHarvest/java/mcp/mobius/waila/plugin/harvest/provider/HarvestProvider.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import net.minecraft.client.resources.language.I18n;
2727
import net.minecraft.network.chat.Component;
2828
import net.minecraft.network.chat.MutableComponent;
29+
import net.minecraft.resources.ResourceLocation;
2930
import net.minecraft.world.item.ItemStack;
3031
import net.minecraft.world.level.block.state.BlockState;
3132
import org.jetbrains.annotations.NotNull;
@@ -34,6 +35,12 @@ public enum HarvestProvider implements IBlockComponentProvider, IEventListener {
3435

3536
INSTANCE;
3637

38+
private static final ResourceLocation CLASSIC_HARVESTABLE = Options.rl("classic.harvestable");
39+
private static final ResourceLocation CLASSIC_EFFECTIVE_TOOL = Options.rl("classic.effective_tool");
40+
private static final ResourceLocation CLASSIC_LEVEL = Options.rl("classic.level");
41+
42+
private static final ResourceLocation CLASSIC_MINIMAL = Options.rl("classic.minimal");
43+
3744
private static final ToolType UNBREAKABLE = new ToolType();
3845

3946
public final Map<BlockState, List<ToolType>> toolsCache = new Reference2ObjectOpenHashMap<>();
@@ -94,16 +101,16 @@ public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig
94101
var heldStack = accessor.getPlayer().getInventory().getSelected();
95102

96103
if (displayMode == HarvestDisplayMode.CLASSIC) {
97-
tooltip.addLine(Component.empty()
104+
tooltip.setLine(CLASSIC_HARVESTABLE, Component.empty()
98105
.append(getHarvestableSymbol(accessor, unbreakable))
99106
.append(" ")
100107
.append(Component.translatable(Tl.Tooltip.Harvest.HARVESTABLE)));
101108

102-
if (!tools.isEmpty() && !unbreakable) tooltip.addLine(new PairComponent(
109+
if (!tools.isEmpty() && !unbreakable) tooltip.setLine(CLASSIC_EFFECTIVE_TOOL, new PairComponent(
103110
Component.translatable(Tl.Tooltip.Harvest.EFFECTIVE_TOOL),
104111
getToolText(tools, heldStack)));
105112

106-
if (highestTier != ToolTier.NONE) tooltip.addLine(new PairComponent(
113+
if (highestTier != ToolTier.NONE) tooltip.setLine(CLASSIC_LEVEL, new PairComponent(
107114
Component.translatable(Tl.Tooltip.Harvest.LEVEL),
108115
getTierText(highestTier, heldStack)));
109116
} else if (displayMode == HarvestDisplayMode.CLASSIC_MINIMAL) {
@@ -121,7 +128,7 @@ public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig
121128
text.append(getTierText(highestTier, heldStack));
122129
}
123130

124-
tooltip.addLine(text);
131+
tooltip.setLine(CLASSIC_MINIMAL, text);
125132
}
126133
}
127134

src/pluginVanilla/java/mcp/mobius/waila/plugin/vanilla/provider/BeaconProvider.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import mcp.mobius.waila.plugin.vanilla.config.Options;
88
import mcp.mobius.waila.plugin.vanilla.provider.data.BeaconDataProvider;
99
import net.minecraft.core.Holder;
10+
import net.minecraft.network.chat.CommonComponents;
1011
import net.minecraft.network.chat.MutableComponent;
1112
import net.minecraft.world.effect.MobEffect;
1213

@@ -23,17 +24,16 @@ public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig
2324
if (!config.getBoolean(Options.EFFECT_BEACON)) return;
2425

2526
var data = accessor.getData().get(BeaconDataProvider.DATA);
26-
if (data == null) return;
27+
if (data == null || data.primary() == null) return;
2728

28-
if (data.primary() != null) {
29-
var text = getText(data.primary());
30-
if (data.primary() == data.secondary()) text.append(" II");
31-
tooltip.addLine(text);
32-
}
29+
var text = getText(data.primary());
30+
if (data.primary() == data.secondary()) text.append(" II");
3331

3432
if (data.secondary() != null && data.primary() != data.secondary()) {
35-
tooltip.addLine(getText(data.secondary()));
33+
text.append(CommonComponents.NEW_LINE).append(getText(data.secondary()));
3634
}
35+
36+
tooltip.setLine(Options.EFFECT_BEACON, text);
3737
}
3838

3939
}

src/pluginVanilla/java/mcp/mobius/waila/plugin/vanilla/provider/BeeProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public enum BeeProvider implements IEntityComponentProvider {
2020
public void appendBody(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig config) {
2121
var hivePos = accessor.getData().get(BeeDataProvider.HIVE_POS);
2222
if (hivePos != null && config.getBoolean(Options.BEE_HIVE_POS)) {
23-
tooltip.addLine(new PairComponent(
23+
tooltip.setLine(Options.BEE_HIVE_POS, new PairComponent(
2424
new WrappedComponent(Component.translatable(Tl.Tooltip.Bee.HIVE)),
2525
new PositionComponent(hivePos.pos())));
2626
}

src/pluginVanilla/java/mcp/mobius/waila/plugin/vanilla/provider/BeehiveProvider.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import mcp.mobius.waila.buildconst.Tl;
1010
import mcp.mobius.waila.plugin.vanilla.config.Options;
1111
import mcp.mobius.waila.plugin.vanilla.provider.data.BeehiveDataProvider;
12+
import net.minecraft.network.chat.CommonComponents;
1213
import net.minecraft.network.chat.Component;
1314
import net.minecraft.world.level.block.BeehiveBlock;
1415

@@ -33,17 +34,24 @@ public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig
3334
names.put(name, names.getOrDefault(name, 0) + 1);
3435
}
3536

36-
for (var entry : names.object2IntEntrySet()) {
37-
var name = entry.getKey();
38-
var count = entry.getIntValue();
39-
if (count > 1) tooltip.addLine(Component.literal(count + " " + name));
40-
else tooltip.addLine(Component.literal(name));
37+
if (!names.isEmpty()) {
38+
var component = Component.empty();
39+
40+
for (var entry : names.object2IntEntrySet()) {
41+
if (!component.getSiblings().isEmpty()) component.append(CommonComponents.NEW_LINE);
42+
var name = entry.getKey();
43+
var count = entry.getIntValue();
44+
if (count > 1) component.append(Component.literal(count + " " + name));
45+
else component.append(Component.literal(name));
46+
}
47+
48+
tooltip.setLine(Options.BEE_HIVE_OCCUPANTS, component);
4149
}
4250
}
4351

4452
if (config.getBoolean(Options.BEE_HIVE_HONEY_LEVEL)) {
4553
var state = accessor.getBlockState();
46-
tooltip.addLine(new PairComponent(
54+
tooltip.setLine(Options.BEE_HIVE_HONEY_LEVEL, new PairComponent(
4755
Component.translatable(Tl.Tooltip.HONEY_LEVEL),
4856
Component.literal(state.getValue(BeehiveBlock.HONEY_LEVEL).toString())));
4957
}

src/pluginVanilla/java/mcp/mobius/waila/plugin/vanilla/provider/BlockAttributesProvider.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ public enum BlockAttributesProvider implements IBlockComponentProvider {
1818
@Override
1919
public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig config) {
2020
if (config.getBoolean(Options.BLOCK_POSITION)) {
21-
tooltip.addLine(new PositionComponent(accessor.getPosition()));
21+
tooltip.setLine(Options.BLOCK_POSITION, new PositionComponent(accessor.getPosition()));
2222
}
2323

2424
if (config.getBoolean(Options.BLOCK_STATE)) {
2525
var state = accessor.getBlockState();
26-
state.getProperties().forEach(property -> {
27-
Comparable<?> value = state.getValue(property);
26+
for (var property : state.getProperties()) {
27+
var value = state.getValue(property);
2828
var valueText = Component.literal(value.toString());
2929
if (property instanceof BooleanProperty) {
3030
valueText.withStyle(value == Boolean.TRUE ? ChatFormatting.GREEN : ChatFormatting.RED);
3131
}
32-
tooltip.addLine(new PairComponent(Component.literal(property.getName()), valueText));
33-
});
32+
var name = property.getName();
33+
tooltip.setLine(Options.BLOCK_STATE.withSuffix("." + name), new PairComponent(Component.literal(name), valueText));
34+
}
3435
}
3536
}
3637

src/pluginVanilla/java/mcp/mobius/waila/plugin/vanilla/provider/BreakProgressProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void onAfterTooltipRender(GuiGraphics ctx, Rectangle rect, ICommonAccesso
4747
var progressChangeAmount = progressDiff * dt;
4848
var actualProgress = Mth.clamp(lastProgress + progressChangeAmount, 0f, 1f);
4949

50-
final float[] lineLength = new float[1];
50+
final var lineLength = new float[1];
5151

5252
if (config.getBoolean(Options.BREAKING_PROGRESS_BOTTOM_ONLY)) {
5353
lineLength[0] = (rect.width - 2) * actualProgress;

src/pluginVanilla/java/mcp/mobius/waila/plugin/vanilla/provider/ComposterProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public enum ComposterProvider implements IBlockComponentProvider {
1818
public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig config) {
1919
if (config.getBoolean(Options.LEVEL_COMPOSTER)) {
2020
var state = accessor.getBlockState();
21-
tooltip.addLine(new PairComponent(
21+
tooltip.setLine(Options.LEVEL_COMPOSTER, new PairComponent(
2222
Component.translatable(Tl.Tooltip.COMPOST_LEVEL),
2323
Component.literal(state.getValue(ComposterBlock.LEVEL).toString())));
2424
}

0 commit comments

Comments
 (0)