How to make an item with a tooltip in a Minecraft mod
The lines under an item’s name come from a method in its class: appendHoverText, called addInformation on 1.12.2. Add components to the list it gives you (from 1.21.5 you pass each line to a consumer instead). Use a translatable component with a key in the lang file so the text can be translated, and withStyle(ChatFormatting.GRAY) for the usual colour.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Make a class that extends Item and override the tooltip method (appendHoverText).
- Add a translatable component with the gray style to the list.
- Write the text for the key in en_us.json.
- Register the item with your class.
What you can change
Section titled “What you can change”The example uses these values. Change them in the code.
| Setting | In the example | What it does |
|---|---|---|
| text | Found in a shipwreck. |
The tooltip’s English text. |
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric and NeoForge 1.20.1 to 1.20.4, Fabric 1.19 to 1.20
Section titled “Fabric and NeoForge 1.20.1 to 1.20.4, Fabric 1.19 to 1.20”Uses:
Item.appendHoverTextComponent.translatableChatFormatting.GRAY
src/main/java/com/example/examplemod/item/OldMapItem.java
package com.example.examplemod.item;
import java.util.List;import net.minecraft.ChatFormatting;import net.minecraft.network.chat.Component;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.TooltipFlag;import net.minecraft.world.level.Level;
// Old Map: one grey line under its name, from en_us.json's item.examplemod.old_map.tooltip.public class OldMapItem extends Item { public OldMapItem(Item.Properties properties) { super(properties); }
@Override public void appendHoverText(ItemStack stack, Level level, List<Component> tooltip, TooltipFlag flag) { tooltip.add(Component.translatable("item.examplemod.old_map.tooltip").withStyle(ChatFormatting.GRAY)); super.appendHoverText(stack, level, tooltip, flag); }}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "item.examplemod.old_map.tooltip": "Found in a shipwreck."}It’s registered like any other item, as OldMapItem: see a new item.
Fabric and NeoForge 1.20.5 to 1.21.1, Fabric and NeoForge 1.21.2 to 1.21.4
Section titled “Fabric and NeoForge 1.20.5 to 1.21.1, Fabric and NeoForge 1.21.2 to 1.21.4”Uses:
Item.appendHoverTextComponent.translatableChatFormatting.GRAY
src/main/java/com/example/examplemod/item/OldMapItem.java
package com.example.examplemod.item;
import java.util.List;import net.minecraft.ChatFormatting;import net.minecraft.network.chat.Component;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.TooltipFlag;
// Old Map: one grey line under its name, from en_us.json's item.examplemod.old_map.tooltip.public class OldMapItem extends Item { public OldMapItem(Item.Properties properties) { super(properties); }
@Override public void appendHoverText(ItemStack stack, Item.TooltipContext context, List<Component> tooltip, TooltipFlag flag) { tooltip.add(Component.translatable("item.examplemod.old_map.tooltip").withStyle(ChatFormatting.GRAY)); super.appendHoverText(stack, context, tooltip, flag); }}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "item.examplemod.old_map.tooltip": "Found in a shipwreck."}It’s registered like any other item, as OldMapItem: see a new item.
Fabric and NeoForge 1.21.5 to 26.3
Section titled “Fabric and NeoForge 1.21.5 to 26.3”Uses:
Item.appendHoverTextTooltipDisplayComponent.translatableChatFormatting.GRAY
Since 1.21.5 appendHoverText takes a TooltipDisplay and a Consumer<Component> instead of a List; call tooltip.accept(line).
src/main/java/com/example/examplemod/item/OldMapItem.java
package com.example.examplemod.item;
import java.util.function.Consumer;import net.minecraft.ChatFormatting;import net.minecraft.network.chat.Component;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.TooltipFlag;import net.minecraft.world.item.component.TooltipDisplay;
// Old Map: one grey line under its name, from en_us.json's item.examplemod.old_map.tooltip.public class OldMapItem extends Item { public OldMapItem(Item.Properties properties) { super(properties); }
@Override public void appendHoverText(ItemStack stack, Item.TooltipContext context, TooltipDisplay display, Consumer<Component> tooltip, TooltipFlag flag) { tooltip.accept(Component.translatable("item.examplemod.old_map.tooltip").withStyle(ChatFormatting.GRAY)); super.appendHoverText(stack, context, display, tooltip, flag); }}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "item.examplemod.old_map.tooltip": "Found in a shipwreck."}It’s registered like any other item, as OldMapItem: see a new item.
Forge 1.17.1 to 1.18.2
Section titled “Forge 1.17.1 to 1.18.2”Uses:
Item.appendHoverTextComponent.translatableChatFormatting.GRAY
src/main/java/com/example/examplemod/item/OldMapItem.java
package com.example.examplemod.item;
import net.minecraft.network.chat.TranslatableComponent;import java.util.List;import javax.annotation.Nullable;import net.minecraft.ChatFormatting;import net.minecraft.network.chat.Component;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.TooltipFlag;import net.minecraft.world.level.Level;
// Old Map: one grey line under its name, from en_us.json's item.examplemod.old_map.tooltip.public class OldMapItem extends Item { public OldMapItem(Item.Properties properties) { super(properties); }
@Override public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> tooltip, TooltipFlag flag) { tooltip.add(new TranslatableComponent("item.examplemod.old_map.tooltip").withStyle(ChatFormatting.GRAY)); super.appendHoverText(stack, level, tooltip, flag); }}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "item.examplemod.old_map.tooltip": "Found in a shipwreck."}It’s registered like any other item, as OldMapItem: see a new item.
Forge 1.19 to 1.20.1
Section titled “Forge 1.19 to 1.20.1”Uses:
Item.appendHoverTextComponent.translatableChatFormatting.GRAY
src/main/java/com/example/examplemod/item/OldMapItem.java
package com.example.examplemod.item;
import java.util.List;import javax.annotation.Nullable;import net.minecraft.ChatFormatting;import net.minecraft.network.chat.Component;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.TooltipFlag;import net.minecraft.world.level.Level;
// Old Map: one grey line under its name, from en_us.json's item.examplemod.old_map.tooltip.public class OldMapItem extends Item { public OldMapItem(Item.Properties properties) { super(properties); }
@Override public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> tooltip, TooltipFlag flag) { tooltip.add(Component.translatable("item.examplemod.old_map.tooltip").withStyle(ChatFormatting.GRAY)); super.appendHoverText(stack, level, tooltip, flag); }}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "item.examplemod.old_map.tooltip": "Found in a shipwreck."}It’s registered like any other item, as OldMapItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
Item.appendHoverTextTranslationTextComponentTextFormatting.GRAY
src/main/java/com/example/examplemod/item/OldMapItem.java
package com.example.examplemod.item;
import net.minecraft.util.text.TranslationTextComponent;
import java.util.List;import javax.annotation.Nullable;import net.minecraft.util.text.TextFormatting;import net.minecraft.util.text.ITextComponent;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.client.util.ITooltipFlag;import net.minecraft.world.World;
// Old Map: one grey line under its name, from en_us.json's item.examplemod.old_map.tooltip.public class OldMapItem extends Item { public OldMapItem(Item.Properties properties) { super(properties); }
@Override public void appendHoverText(ItemStack stack, @Nullable World world, List<ITextComponent> tooltip, ITooltipFlag flag) { tooltip.add(new TranslationTextComponent("item.examplemod.old_map.tooltip").withStyle(TextFormatting.GRAY)); super.appendHoverText(stack, world, tooltip, flag); }}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "item.examplemod.old_map.tooltip": "Found in a shipwreck."}It’s registered like any other item, as OldMapItem: see a new item.
Fabric 1.16.5 to 1.18.2
Section titled “Fabric 1.16.5 to 1.18.2”Uses:
Item.appendHoverTextComponent.translatableChatFormatting.GRAY
src/main/java/com/example/examplemod/item/OldMapItem.java
package com.example.examplemod.item;
import net.minecraft.network.chat.TranslatableComponent;import java.util.List;import net.minecraft.ChatFormatting;import net.minecraft.network.chat.Component;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.TooltipFlag;import net.minecraft.world.level.Level;
// Old Map: one grey line under its name, from en_us.json's item.examplemod.old_map.tooltip.public class OldMapItem extends Item { public OldMapItem(Item.Properties properties) { super(properties); }
@Override public void appendHoverText(ItemStack stack, Level level, List<Component> tooltip, TooltipFlag flag) { tooltip.add(new TranslatableComponent("item.examplemod.old_map.tooltip").withStyle(ChatFormatting.GRAY)); super.appendHoverText(stack, level, tooltip, flag); }}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "item.examplemod.old_map.tooltip": "Found in a shipwreck."}It’s registered like any other item, as OldMapItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
Item.appendHoverTextTextComponentTranslationTextFormatting.GRAY
In 1.12.2 the method is addInformation(stack, world, tooltip, flag) and the tooltip is a list of plain Strings, not components.
src/main/java/com/example/examplemod/item/OldMapItem.java
package com.example.examplemod.item;
import java.util.List;import javax.annotation.Nullable;import net.minecraft.client.resources.I18n;import net.minecraft.client.util.ITooltipFlag;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.util.text.TextFormatting;import net.minecraft.world.World;import net.minecraftforge.fml.relauncher.Side;import net.minecraftforge.fml.relauncher.SideOnly;
// Old Map: one grey line under its name, from the item.examplemod.old_map.tooltip line of the .lang file.public class OldMapItem extends Item { @Override @SideOnly(Side.CLIENT) public void addInformation(ItemStack stack, @Nullable World world, List<String> tooltip, ITooltipFlag flag) { tooltip.add(TextFormatting.GRAY + I18n.format("item.examplemod.old_map.tooltip")); }}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "item.examplemod.old_map.tooltip": "Found in a shipwreck."}It’s registered like any other item, as OldMapItem: see a new item.
Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/items/first-item
- https://docs.neoforged.net/docs/1.21.1/resources/client/i18n
Related how-tos
Section titled “Related how-tos”- A new item
- A sword that sets targets on fire
- An item that does something when used
- An item that gives a potion effect
- A block that does something when clicked
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.