tutorial:tooltip
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| tutorial:tooltip [2019/07/24 12:48] – created fudge | 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 | + | Here we assume you have created your own class (see [[items]]), for example, '' |
| - | tooltip.add(new TranslatableText(" | + | |
| + | For versions 1.18.2 and before: | ||
| + | <yarncode | ||
| + | public class CustomItem extends Item { | ||
| + | // ... | ||
| + | |||
| + | | ||
| + | public void method_7851(class_1799 | ||
| + | | ||
| + | // default white text | ||
| + | | ||
| + | |||
| + | // formatted red text | ||
| + | tooltip.add(new TranslatableText(" | ||
| + | } | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | For versions since 1.19: | ||
| + | < | ||
| + | public class CustomItem extends Item { | ||
| + | // ... | ||
| + | |||
| + | @Override | ||
| + | public void method_7851(class_1799 itemStack, class_1937 world, List< | ||
| + | tooltip.add(Text.translatable(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 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(" | ||
| + | }); | ||
| </ | </ | ||
| - | For a block, do the same but override '' | + | |
| + | The example adds a custom tooltip for the item we created | ||
tutorial/tooltip.1563972519.txt.gz · Last modified: 2019/07/24 12:48 by fudge