Skip to content

Commit

Permalink
tag all builtin tooltips
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
deirn committed Jan 5, 2025
1 parent 0c9c888 commit f84bc94
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 64 deletions.
7 changes: 7 additions & 0 deletions src/api/java/mcp/mobius/waila/api/WailaConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public class WailaConstants {
*/
public static final ResourceLocation MOD_NAME_TAG = id("mod_name");

/**
* Tooltip tag for errors.
*
* @see ITooltip#setLine
*/
public static final ResourceLocation ERROR_TAG = id("error");

/**
* Whether Waila should show tooltip for blocks.
* <p>
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/mcp/mobius/waila/util/ExceptionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Set;

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

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

return log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import mcp.mobius.waila.api.data.FluidData;
import mcp.mobius.waila.api.util.WNumbers;
import mcp.mobius.waila.plugin.extra.data.FluidDataImpl;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
Expand Down Expand Up @@ -64,7 +65,7 @@ private void addFluidTooltip(ITooltip tooltip, FluidDataImpl data, IPluginConfig
text += " " + displayUnit.symbol;

var sprite = desc.sprite();
tooltip.addLine(new PairComponent(
tooltip.setLine(FluidData.ID.withSuffix("." + BuiltInRegistries.FLUID.getKey(entry.fluid()).toLanguageKey()), new PairComponent(
new WrappedComponent(desc.name().getString()),
new SpriteBarComponent(ratio, sprite, 16, 16, desc.tint(), Component.literal(text))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
Expand All @@ -34,6 +35,12 @@ public enum HarvestProvider implements IBlockComponentProvider, IEventListener {

INSTANCE;

private static final ResourceLocation CLASSIC_HARVESTABLE = Options.rl("classic.harvestable");
private static final ResourceLocation CLASSIC_EFFECTIVE_TOOL = Options.rl("classic.effective_tool");
private static final ResourceLocation CLASSIC_LEVEL = Options.rl("classic.level");

private static final ResourceLocation CLASSIC_MINIMAL = Options.rl("classic.minimal");

private static final ToolType UNBREAKABLE = new ToolType();

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

if (displayMode == HarvestDisplayMode.CLASSIC) {
tooltip.addLine(Component.empty()
tooltip.setLine(CLASSIC_HARVESTABLE, Component.empty()
.append(getHarvestableSymbol(accessor, unbreakable))
.append(" ")
.append(Component.translatable(Tl.Tooltip.Harvest.HARVESTABLE)));

if (!tools.isEmpty() && !unbreakable) tooltip.addLine(new PairComponent(
if (!tools.isEmpty() && !unbreakable) tooltip.setLine(CLASSIC_EFFECTIVE_TOOL, new PairComponent(
Component.translatable(Tl.Tooltip.Harvest.EFFECTIVE_TOOL),
getToolText(tools, heldStack)));

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

tooltip.addLine(text);
tooltip.setLine(CLASSIC_MINIMAL, text);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import mcp.mobius.waila.plugin.vanilla.config.Options;
import mcp.mobius.waila.plugin.vanilla.provider.data.BeaconDataProvider;
import net.minecraft.core.Holder;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.world.effect.MobEffect;

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

var data = accessor.getData().get(BeaconDataProvider.DATA);
if (data == null) return;
if (data == null || data.primary() == null) return;

if (data.primary() != null) {
var text = getText(data.primary());
if (data.primary() == data.secondary()) text.append(" II");
tooltip.addLine(text);
}
var text = getText(data.primary());
if (data.primary() == data.secondary()) text.append(" II");

if (data.secondary() != null && data.primary() != data.secondary()) {
tooltip.addLine(getText(data.secondary()));
text.append(CommonComponents.NEW_LINE).append(getText(data.secondary()));
}

tooltip.setLine(Options.EFFECT_BEACON, text);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum BeeProvider implements IEntityComponentProvider {
public void appendBody(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig config) {
var hivePos = accessor.getData().get(BeeDataProvider.HIVE_POS);
if (hivePos != null && config.getBoolean(Options.BEE_HIVE_POS)) {
tooltip.addLine(new PairComponent(
tooltip.setLine(Options.BEE_HIVE_POS, new PairComponent(
new WrappedComponent(Component.translatable(Tl.Tooltip.Bee.HIVE)),
new PositionComponent(hivePos.pos())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import mcp.mobius.waila.buildconst.Tl;
import mcp.mobius.waila.plugin.vanilla.config.Options;
import mcp.mobius.waila.plugin.vanilla.provider.data.BeehiveDataProvider;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.block.BeehiveBlock;

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

for (var entry : names.object2IntEntrySet()) {
var name = entry.getKey();
var count = entry.getIntValue();
if (count > 1) tooltip.addLine(Component.literal(count + " " + name));
else tooltip.addLine(Component.literal(name));
if (!names.isEmpty()) {
var component = Component.empty();

for (var entry : names.object2IntEntrySet()) {
if (!component.getSiblings().isEmpty()) component.append(CommonComponents.NEW_LINE);
var name = entry.getKey();
var count = entry.getIntValue();
if (count > 1) component.append(Component.literal(count + " " + name));
else component.append(Component.literal(name));
}

tooltip.setLine(Options.BEE_HIVE_OCCUPANTS, component);
}
}

if (config.getBoolean(Options.BEE_HIVE_HONEY_LEVEL)) {
var state = accessor.getBlockState();
tooltip.addLine(new PairComponent(
tooltip.setLine(Options.BEE_HIVE_HONEY_LEVEL, new PairComponent(
Component.translatable(Tl.Tooltip.HONEY_LEVEL),
Component.literal(state.getValue(BeehiveBlock.HONEY_LEVEL).toString())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ public enum BlockAttributesProvider implements IBlockComponentProvider {
@Override
public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig config) {
if (config.getBoolean(Options.BLOCK_POSITION)) {
tooltip.addLine(new PositionComponent(accessor.getPosition()));
tooltip.setLine(Options.BLOCK_POSITION, new PositionComponent(accessor.getPosition()));
}

if (config.getBoolean(Options.BLOCK_STATE)) {
var state = accessor.getBlockState();
state.getProperties().forEach(property -> {
Comparable<?> value = state.getValue(property);
for (var property : state.getProperties()) {
var value = state.getValue(property);
var valueText = Component.literal(value.toString());
if (property instanceof BooleanProperty) {
valueText.withStyle(value == Boolean.TRUE ? ChatFormatting.GREEN : ChatFormatting.RED);
}
tooltip.addLine(new PairComponent(Component.literal(property.getName()), valueText));
});
var name = property.getName();
tooltip.setLine(Options.BLOCK_STATE.withSuffix("." + name), new PairComponent(Component.literal(name), valueText));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void onAfterTooltipRender(GuiGraphics ctx, Rectangle rect, ICommonAccesso
var progressChangeAmount = progressDiff * dt;
var actualProgress = Mth.clamp(lastProgress + progressChangeAmount, 0f, 1f);

final float[] lineLength = new float[1];
final var lineLength = new float[1];

if (config.getBoolean(Options.BREAKING_PROGRESS_BOTTOM_ONLY)) {
lineLength[0] = (rect.width - 2) * actualProgress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum ComposterProvider implements IBlockComponentProvider {
public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig config) {
if (config.getBoolean(Options.LEVEL_COMPOSTER)) {
var state = accessor.getBlockState();
tooltip.addLine(new PairComponent(
tooltip.setLine(Options.LEVEL_COMPOSTER, new PairComponent(
Component.translatable(Tl.Tooltip.COMPOST_LEVEL),
Component.literal(state.getValue(ComposterBlock.LEVEL).toString())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void appendHead(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig
var data = accessor.getData().raw();

if (compact) {
var line = tooltip.addLine();
var line = tooltip.setLine(Options.ENTITY_COMPACT);
var i = 0;

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

if (showHealth) {
var line = tooltip.setLine(Options.ENTITY_HEALTH);
var absorption = data.contains("abs") ? data.getFloat("abs") : 0f;
if (entity.getMaxHealth() + absorption > config.getInt(Options.ENTITY_LONG_HEALTH_MAX)) {
addHealth(tooltip.addLine(), entity, data, showAbsorption);
addHealth(line, entity, data, showAbsorption);
} else {
tooltip.addLine(new HealthComponent(entity.getHealth(), entity.getMaxHealth(), maxPerLine, false));
line.with(new HealthComponent(entity.getHealth(), entity.getMaxHealth(), maxPerLine, false));
if (showAbsorption && absorption > 0) {
tooltip.addLine(new HealthComponent(absorption, 0, maxPerLine, true));
line.with(new HealthComponent(absorption, 0, maxPerLine, true));
}
}
}

if (showArmor) {
var line = tooltip.setLine(Options.ENTITY_ARMOR);
if (entity.getArmorValue() > config.getInt(Options.ENTITY_LONG_ARMOR_MAX)) {
addArmor(tooltip.addLine(), entity);
addArmor(line, entity);
} else {
tooltip.addLine(new ArmorComponent(entity.getArmorValue(), maxPerLine));
line.with(new ArmorComponent(entity.getArmorValue(), maxPerLine));
}
}
}
Expand All @@ -107,7 +109,7 @@ public void appendHead(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig
@Override
public void appendBody(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig config) {
if (config.getBoolean(Options.ENTITY_POSITION)) {
tooltip.addLine(new PositionComponent(accessor.getEntity().position()));
tooltip.setLine(Options.ENTITY_POSITION, new PositionComponent(accessor.getEntity().position()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ else if (jumpHeight > 4.0f)
else
format = ChatFormatting.RESET;

tooltip.addLine(new PairComponent(JUMP_KEY,
tooltip.setLine(Options.HORSE_JUMP_HEIGHT, new PairComponent(JUMP_KEY,
Component.translatable(Tl.Tooltip.Horse.Jump.VALUE, FORMAT.format(jumpHeight)).withStyle(format)));
}

Expand All @@ -54,7 +54,7 @@ else if (speed > 11.0f)
else
format = ChatFormatting.RESET;

tooltip.addLine(new PairComponent(SPEED_KEY,
tooltip.setLine(Options.HORSE_SPEED, new PairComponent(SPEED_KEY,
Component.translatable(Tl.Tooltip.Horse.Speed.VALUE, FORMAT.format(speed)).withStyle(format)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import mcp.mobius.waila.plugin.vanilla.config.Options;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.EnchantmentTags;
import net.minecraft.util.StringUtil;
import net.minecraft.world.entity.Entity;
Expand All @@ -32,6 +34,9 @@ public enum ItemEntityProvider implements IEntityComponentProvider {

INSTANCE;

private static final ResourceLocation AUTHOR = Options.BOOK_WRITTEN.withSuffix(".author");
private static final ResourceLocation GENERATION = Options.BOOK_WRITTEN.withSuffix(".generation");

private static long lastEnchantmentTime = 0;
private static int enchantmentIndex = 0;
private static int curseIndex = 0;
Expand Down Expand Up @@ -104,17 +109,24 @@ public static void appendBookProperties(ITooltip tooltip, ItemStack stack, IPlug
if (curseIndex > (curses.size() - 1)) curseIndex = 0;
}

Component text = null;

if (!enchantments.isEmpty()) {
var instance = enchantments.get(enchantmentIndex);
tooltip.addLine(Enchantment.getFullname(instance.enchantment, instance.level));
text = Enchantment.getFullname(instance.enchantment, instance.level);
}

if (!curses.isEmpty()) {
var instance = curses.get(curseIndex);
tooltip.addLine(Enchantment.getFullname(instance.enchantment, instance.level));
var curse = Enchantment.getFullname(instance.enchantment, instance.level);
if (text == null) text = curse;
else text = text.copy().append(CommonComponents.NEW_LINE).append(curse);
}

if (text != null) tooltip.setLine(Options.BOOK_ENCHANTMENT_DISPLAY_MODE, text);
} else {
var enchantments = stack.getOrDefault(DataComponents.STORED_ENCHANTMENTS, ItemEnchantments.EMPTY);
MutableComponent text = null;

if (mode == EnchantmentDisplayMode.COMBINED) {
MutableComponent enchantmentLine = null;
Expand All @@ -140,13 +152,20 @@ public static void appendBookProperties(ITooltip tooltip, ItemStack stack, IPlug
}
}

if (enchantmentLine != null) tooltip.addLine(enchantmentLine);
if (curseLine != null) tooltip.addLine(curseLine);
if (enchantmentLine != null) text = enchantmentLine;
if (curseLine != null) {
if (text == null) text = curseLine;
else text.append(CommonComponents.NEW_LINE).append(curseLine);
}
} else {
for (var entry : enchantments.entrySet()) {
tooltip.addLine(Enchantment.getFullname(entry.getKey(), entry.getIntValue()));
var name = Enchantment.getFullname(entry.getKey(), entry.getIntValue());
if (text == null) text = Component.empty().append(name);
else text.append(CommonComponents.NEW_LINE).append(name);
}
}

if (text != null) tooltip.setLine(Options.BOOK_ENCHANTMENT_DISPLAY_MODE, text);
}
} else if (stack.is(Items.WRITTEN_BOOK)) {
if (!config.getBoolean(Options.BOOK_WRITTEN)) return;
Expand All @@ -155,11 +174,10 @@ public static void appendBookProperties(ITooltip tooltip, ItemStack stack, IPlug
if (tag == null) return;

if (!StringUtil.isNullOrEmpty(tag.author())) {
tooltip.addLine(Component.translatable("book.byAuthor", tag.author()));
tooltip.setLine(AUTHOR, Component.translatable("book.byAuthor", tag.author()));
}

tooltip.addLine(Component.translatable("book.generation." + tag.generation()));

tooltip.setLine(GENERATION, Component.translatable("book.generation." + tag.generation()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum JukeboxProvider implements IBlockComponentProvider {
public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig config) {
if (config.getBoolean(Options.JUKEBOX_RECORD) && accessor.getData().raw().contains("record")) {
var component = Component.Serializer.fromJson(accessor.getData().raw().getString("record"), accessor.getWorld().registryAccess());
if (component != null) tooltip.addLine(component);
if (component != null) tooltip.setLine(Options.JUKEBOX_RECORD, component);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void appendBody(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig

if (it.getEffect().value().getCategory() == MobEffectCategory.HARMFUL) text.withStyle(ChatFormatting.RED);

tooltip.addLine(text);
tooltip.setLine(Options.EFFECT_MOB, text);
});
}

Expand Down
Loading

0 comments on commit f84bc94

Please sign in to comment.