-
Notifications
You must be signed in to change notification settings - Fork 0
Tooltip Formatting
Vanilla Minecraft rigidly enforces how enchantment tooltips are displayed: Enchantment Name + Roman Numeral Level. When building complex magic overhauls or RPG-style progression systems, this default formatting often falls short.
EnchantmentCore provides the TooltipFormatterRegistry to allow developers to completely intercept and rewrite how specific enchantments (or groups of enchantments) are displayed on items.
To manipulate a tooltip, implement the EnchantmentTooltipFormatter interface.
import johnsmith.enchantmentcore.api.tooltip.EnchantmentTooltipFormatter;
import net.minecraft.core.Holder;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.enchantment.Enchantment;
import java.util.List;
import java.util.Optional;
public class MyCustomFormatter implements EnchantmentTooltipFormatter {
@Override
public Optional<List<Component>> format(Holder<Enchantment> enchantment, int level) {
// Return Optional.empty() if this formatter should ignore the current enchantment.
if (!enchantment.is(MyModTags.REQUIRES_CUSTOM_TOOLTIP)) {
return Optional.empty();
}
// Return your custom formatted components.
return Optional.of(List.of(
Component.literal("★ " + enchantment.value().description().getString() + " ★").withStyle(ChatFormatting.GOLD),
Component.literal(" Power Level: " + level).withStyle(ChatFormatting.GRAY)
));
}
@Override
public int getPriority() {
return 100; // Higher values execute before lower values
}
}The TooltipFormatterRegistry maintains a prioritized list of all registered formatters. When the client generates an item's tooltip, it iterates through this list descending by priority.
The first formatter to return a populated Optional<List<Component>> successfully claims the tooltip, and subsequent formatters are skipped. If all formatters return Optional.empty(), the system falls back to the standard vanilla rendering string.
Register your formatter during your mod's initialization phase:
import johnsmith.enchantmentcore.api.tooltip.TooltipFormatterRegistry;
public class MyMod {
public static void init() {
TooltipFormatterRegistry.register(new MyCustomFormatter());
}
}EnchantmentCore provides two utility classes specifically designed to assist with building complex lore descriptions in your tooltips.
Automatically splits long Component strings into multiple lines based on a strict character limit, preserving chat styling and optional indentation depths.
// Wraps text to 30 characters per line, indented by 1 depth unit (" ").
List<Component> loreLines = TooltipLineWrapper.wrap(longDescriptionComponent, 30, 1);Provides fast translation between standard integers and classical Roman numerals. Supports values from 1 to 3999.
String numeral = RomanNumeralConverter.toRoman(15); // Returns "XV"
int value = RomanNumeralConverter.parse("IV"); // Returns 4Want to learn more about the configuration engine powering Enchantment Core?
Check out Config Understood, the official wiki for Config Overhauled, for a deep dive into dynamic property generation and state synchronization!