tutorial:tooltip
                Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:tooltip [2021/02/22 00:32] – Fix another fomatting error proudmuslim | tutorial:tooltip [2025/07/09 01:55] (current) – typo fix cassiancc | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Adding a tooltip ====== | ====== Adding a tooltip ====== | ||
| - | In your [[tutorial:items|item]] | + | ===== Item tooltip ===== | 
| - | <code java> | + | Sometimes you want to add some extra information when showing the tooltip of the [[items|item]]. To achieve this, you should | 
| - | @Override | + | |
| - | public void appendTooltip(ItemStack itemStack, World world, List< | + | |
| - | // default white text | + | Here we assume you have created your own class (see [[items]]), for example, '' | 
| - |  | + | |
| + | For versions 1.18.2 and before: | ||
| + | < | ||
| + | public class CustomItem extends Item { | ||
| + | // ... | ||
| + | |||
| + | @Override | ||
| + | public void method_7851(class_1799 itemStack, class_1937 world, List< | ||
|  |  | ||
| - |  | + | // default white text | 
| - | tooltip.add( new TranslatableText(" | + | tooltip.add(new TranslatableText(" | 
| + | |||
| + |  | ||
| + | tooltip.add(new TranslatableText(" | ||
| + | } | ||
| } | } | ||
| - | </code> | + | </yarncode> | 
| - | You can also append your tooltip in the [[tutorial: | + | |
| - | <code java> | + | For versions since 1.19: | 
| - | @Override | + | <yarncode | 
| - | public void appendTooltip(ItemStack | + | public class CustomItem extends Item { | 
| - | tooltip.add( | + | // ... | 
| + | |||
| + |  | ||
| + | public void method_7851(class_1799 | ||
| + | tooltip.add(Text.translatable("item.tutorial.custom_item.tooltip" | ||
| + | } | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | For versions since 1.20.5: | ||
| + | < | ||
| + | public class CustomItem extends Item { | ||
| + | // ... | ||
| + | |||
| + | @Override | ||
| + | public void method_7851(class_1799 itemStack, TooltipContext context, List< | ||
| + | tooltip.add(Text.translatable(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | > :!: The method is deprecated 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 [[blocks|block]] class by overriding a similar method: | ||
| + | |||
| + | For versions below 1.20.5: | ||
| + | < | ||
| + | public class CustomBlock extends Block { | ||
| + | // ... | ||
| + | |||
| + | @Override | ||
| + | public void method_9568(class_1799 itemStack, class_1922 world, List< | ||
| + | // for versions 1.18.2 and before | ||
| + | tooltip.add(new TranslatableText(" | ||
| + |  | ||
| + | // for versions since 1.19 | ||
| + | tooltip.add(Text.translatable(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | For versions since 1.20.5, before 1.21.5: | ||
| + | < | ||
| + | public class CustomBlock extends Block { | ||
| + | // ... | ||
| + | |||
| + | @Override | ||
| + | public void method_9568(class_1799 itemStack, Item.TooltipContext context, List< | ||
| + | tooltip.add(Text.translatable(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== 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 '' | ||
| + | <code java> | ||
| + | ItemTooltipCallback.EVENT.register((itemStack, | ||
| + | if (!itemStack.isOf(TutorialItems.CUSTOM_ITEM)) { | ||
| + | return; | ||
| + | } | ||
| + | list.add(Text.translatable(" | ||
| + | }); | ||
| </ | </ | ||
| + | |||
| + | The example adds a custom tooltip for the item we created in the [[items]] tutorial. | ||
tutorial/tooltip.1613953946.txt.gz · Last modified: 2021/02/22 00:32 by proudmuslim
                
                