Skip to content

Commit d7f447d

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. (cherry picked from commit f84bc94)
1 parent 3218bab commit d7f447d

File tree

21 files changed

+115
-63
lines changed

21 files changed

+115
-63
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.component.WrappedComponent;
1313
import mcp.mobius.waila.api.data.FluidData;
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.item.TieredItem;
3132
import net.minecraft.world.level.block.state.BlockState;
@@ -35,6 +36,12 @@ public enum HarvestProvider implements IBlockComponentProvider, IEventListener {
3536

3637
INSTANCE;
3738

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

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

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

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

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

125-
tooltip.addLine(text);
132+
tooltip.setLine(CLASSIC_MINIMAL, text);
126133
}
127134
}
128135

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/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
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void appendHead(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig
6565
var data = accessor.getData().raw();
6666

6767
if (compact) {
68-
var line = tooltip.addLine();
68+
var line = tooltip.setLine(Options.ENTITY_COMPACT);
6969
var i = 0;
7070

7171
if (showHealth) {
@@ -83,22 +83,24 @@ public void appendHead(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig
8383
var maxPerLine = config.getInt(Options.ENTITY_ICON_PER_LINE);
8484

8585
if (showHealth) {
86+
var line = tooltip.setLine(Options.ENTITY_HEALTH);
8687
var absorption = data.contains("abs") ? data.getFloat("abs") : 0f;
8788
if (entity.getMaxHealth() + absorption > config.getInt(Options.ENTITY_LONG_HEALTH_MAX)) {
88-
addHealth(tooltip.addLine(), entity, data, showAbsorption);
89+
addHealth(line, entity, data, showAbsorption);
8990
} else {
90-
tooltip.addLine(new HealthComponent(entity.getHealth(), entity.getMaxHealth(), maxPerLine, false));
91+
line.with(new HealthComponent(entity.getHealth(), entity.getMaxHealth(), maxPerLine, false));
9192
if (showAbsorption && absorption > 0) {
92-
tooltip.addLine(new HealthComponent(absorption, 0, maxPerLine, true));
93+
line.with(new HealthComponent(absorption, 0, maxPerLine, true));
9394
}
9495
}
9596
}
9697

9798
if (showArmor) {
99+
var line = tooltip.setLine(Options.ENTITY_ARMOR);
98100
if (entity.getArmorValue() > config.getInt(Options.ENTITY_LONG_ARMOR_MAX)) {
99-
addArmor(tooltip.addLine(), entity);
101+
addArmor(line, entity);
100102
} else {
101-
tooltip.addLine(new ArmorComponent(entity.getArmorValue(), maxPerLine));
103+
line.with(new ArmorComponent(entity.getArmorValue(), maxPerLine));
102104
}
103105
}
104106
}
@@ -107,7 +109,7 @@ public void appendHead(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig
107109
@Override
108110
public void appendBody(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig config) {
109111
if (config.getBoolean(Options.ENTITY_POSITION)) {
110-
tooltip.addLine(new PositionComponent(accessor.getEntity().position()));
112+
tooltip.setLine(Options.ENTITY_POSITION, new PositionComponent(accessor.getEntity().position()));
111113
}
112114
}
113115

0 commit comments

Comments
 (0)