User Tools

Site Tools


tutorial:tooltip

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:tooltip [2024/04/15 01:31] solidblocktutorial:tooltip [2025/04/01 08:15] (current) – (not moved totally because it's not a separate page on docs and it does not include old versions) solidblock
Line 1: Line 1:
 ====== Adding a tooltip ====== ====== Adding a tooltip ======
-In your [[tutorial:items|item]] class, override ''<yarn method_7851>'' like so (see [[tutorial:lang]] for how to translate the tooltip).+===== Item tooltip ===== 
 +Sometimes you want to add some extra information when showing the tooltip of the [[items|item]]. To achieve thisyou should override your own ''<yarn method_7851>'' like so (see [[tutorial: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: For versions 1.18.2 and before:
 <yarncode java> <yarncode java>
-@Override +public class CustomItem extends Item 
-public void method_7851(class_1799 itemStack, class_1937 world, List<class_2561> tooltip, class_1836 tooltipContext) { +    // ...
- +
-    // default white text +
-    tooltip.add(new TranslatableText("item.tutorial.custom_item.tooltip"));+
  
-    // formatted red text +    @Override 
-    tooltip.add(new TranslatableText("item.tutorial.custom_item.tooltip").method_27692(class_124.field_1061));+    public void method_7851(class_1799 itemStack, class_1937 world, List<class_2561> tooltip, class_1836 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").method_27692(class_124.field_1061)); 
 +    }
 } }
 </yarncode> </yarncode>
Line 17: Line 24:
 For versions since 1.19: For versions since 1.19:
 <yarncode java> <yarncode java>
-@Override +public class CustomItem extends Item { 
-public void method_7851(class_1799 itemStack, class_1937 world, List<class_2561> tooltip, class_1836 tooltipContext) { +    // ... 
-    tooltip.add(Text.translatable("item.tutorial.custom_item.tooltip"));+ 
 +    @Override 
 +    public void method_7851(class_1799 itemStack, class_1937 world, List<class_2561> tooltip, class_1836 tooltipContext) { 
 +        tooltip.add(Text.translatable("item.tutorial.custom_item.tooltip")); 
 +    }
 } }
 </yarncode> </yarncode>
Line 25: Line 36:
 For versions since 1.20.5: For versions since 1.20.5:
 <yarncode java> <yarncode java>
-@Override +public class CustomItem extends Item { 
-public void method_7851(class_1799 itemStack, TooltipContext context, List<class_2561> tooltip, TooltipType type) { +    // ... 
-    tooltip.add(Text.translatable("item.tutorial.custom_item.tooltip"));+ 
 +    @Override 
 +    public void method_7851(class_1799 itemStack, TooltipContext context, List<class_2561> tooltip, TooltipType type) { 
 +        tooltip.add(Text.translatable("item.tutorial.custom_item.tooltip")); 
 +    }
 } }
 </yarncode> </yarncode>
 +> :!: The method is deprectaed since 1.21.5. To add item tooltips for 1.21.5, please use Fabric API.
  
-You can also append your tooltip in the [[tutorial:blocks|block]] class by overriding a similar method:+===== Block tooltip ===== 
 +You can also append your tooltip in the [[blocks|block]] class by overriding a similar method:
  
 For versions below 1.20.5: For versions below 1.20.5:
 <yarncode java> <yarncode java>
-@Override +public class CustomBlock extends Block { 
-public void method_9568(class_1799 itemStack, class_1922 world, List<class_2561> tooltip, class_1836 tooltipContext) { +    // ... 
-    // for versions 1.18.2 and before + 
-    tooltip.add(new TranslatableText("block.tutorial.custom_block.tooltip"));+    @Override 
 +    public void method_9568(class_1799 itemStack, class_1922 world, List<class_2561> tooltip, class_1836 tooltipContext) { 
 +        // for versions 1.18.2 and before 
 +        tooltip.add(new TranslatableText("block.tutorial.custom_block.tooltip"));
          
-    // for versions since 1.19 +        // for versions since 1.19 
-    tooltip.add(Text.translatable("block.tutorial.custom_block.tooltip"));+        tooltip.add(Text.translatable("block.tutorial.custom_block.tooltip")); 
 +    }
 } }
 </yarncode> </yarncode>
  
-For versions since 1.20.5:+For versions since 1.20.5, before 1.21.5:
 <yarncode java> <yarncode java>
-@Override +public class CustomBlock extends Block { 
-public void method_9568(class_1799 itemStack, Item.TooltipContext context, List<class_2561> tooltip, TooltipType options) { +    // ... 
-    tooltip.add(Text.translatable("block.tutorial.custom_block.tooltip"));+ 
 +    @Override 
 +    public void method_9568(class_1799 itemStack, Item.TooltipContext context, List<class_2561> tooltip, TooltipType options) { 
 +        tooltip.add(Text.translatable("block.tutorial.custom_block.tooltip")); 
 +    }
 } }
 </yarncode> </yarncode>
 +
 +===== 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:
 +<code java>
 +    ItemTooltipCallback.EVENT.register((itemStack, tooltipContext, tooltipType, list) -> {
 +      if (!itemStack.isOf(TutorialItems.CUSTOM_ITEM)) {
 +        return;
 +      }
 +      list.add(Text.translatable("item.tutorial.custom_item.tooltip"));
 +    });
 +</code>
 +
 +The example adds a custom tooltip for the item we created in the [[items]] tutorial.
tutorial/tooltip.1713144696.txt.gz · Last modified: 2024/04/15 01:31 by solidblock