tutorial:tooltip
Table of Contents
This page has been moved, the new location is https://docs.fabricmc.net/develop/items/first-item#custom-tooltips.
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")); } }
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:
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")); } }
tutorial/tooltip.txt · Last modified: 2024/08/23 13:10 by solidblock