Table of Contents
Adding a tooltip
Item tooltip
Sometimes you want to add some extra information when showing the tooltip of the item. To achieve this, you should override your own appendTooltip
like so (see lang for how to translate the tooltip).
Here we assume you have created your own class (see items), for example, CustomItem
:
For versions 1.18.2 and before:
public class CustomItem extends Item { // ... @Override public void appendTooltip(ItemStack itemStack, World world, List<Text> tooltip, TooltipContext tooltipContext) { // default white text tooltip.add(new TranslatableText("item.tutorial.custom_item.tooltip")); // formatted red text tooltip.add(new TranslatableText("item.tutorial.custom_item.tooltip").formatted(Formatting.RED)); } }
For versions since 1.19:
public class CustomItem extends Item { // ... @Override public void appendTooltip(ItemStack itemStack, World world, List<Text> tooltip, TooltipContext tooltipContext) { tooltip.add(Text.translatable("item.tutorial.custom_item.tooltip")); } }
For versions since 1.20.5:
public class CustomItem extends Item { // ... @Override public void appendTooltip(ItemStack itemStack, TooltipContext context, List<Text> tooltip, TooltipType type) { tooltip.add(Text.translatable("item.tutorial.custom_item.tooltip")); } }
The method is deprectaed since 1.21.5. To add item tooltips for 1.21.5, please use Fabric API.
Block tooltip
You can also append your tooltip in the block class by overriding a similar method:
For versions below 1.20.5:
public class CustomBlock extends Block { // ... @Override public void appendTooltip(ItemStack itemStack, BlockView world, List<Text> tooltip, TooltipContext tooltipContext) { // for versions 1.18.2 and before tooltip.add(new TranslatableText("block.tutorial.custom_block.tooltip")); // for versions since 1.19 tooltip.add(Text.translatable("block.tutorial.custom_block.tooltip")); } }
For versions since 1.20.5, before 1.21.5:
public class CustomBlock extends Block { // ... @Override public void appendTooltip(ItemStack itemStack, Item.TooltipContext context, List<Text> tooltip, TooltipType options) { tooltip.add(Text.translatable("block.tutorial.custom_block.tooltip")); } }
Adding tooltips in 1.21.5
Since 1.21.5, the implementation of item tooltips are rewritten. Most item tooltips are implemented with item components now, which may be influenced by tooltip_display
item component to decide whether to display. However, you can still use Fabric API to add custom tooltips. Add the following code in the mod initializer in your code:
ItemTooltipCallback.EVENT.register((itemStack, tooltipContext, tooltipType, list) -> { if (!itemStack.isOf(TutorialItems.CUSTOM_ITEM)) { return; } list.add(Text.translatable("item.tutorial.custom_item.tooltip")); });
The example adds a custom tooltip for the item we created in the items tutorial.