Topic category: User side tutorials
Advanced Tooltips
You need some Java knowledge for this tutorial.
What will you learn in this tutorial?
1 - Translatable tooltips
2 - Shiftable/Altable/Controllable tooltips
Before starting this tutorial keep in mind that this tutorial will require you to lock your block/item element and you can't change any of your block/item properties without coding after locking the element. So before doing anything below, make sure your block/item is done.
Also give your block/item a tooltip before locking to save some time.
For Forge 1.15.x/1.16.x (and probably 1.14.x):
First of all we need to find the override below in our block/item class.
@Override
public void addInformation(ItemStack itemstack, World world, List<ITextComponent> list, ITooltipFlag flag) {
super.addInformation(itemstack, world, list, flag);
list.add(new StringTextComponent("Line 1"));
list.add(new StringTextComponent("Line 2"));
}
1) Translatable Tooltip
This tooltip uses literal/untranslatable texts, making this translatable is very easy.
We'll just change StringTextComponent to TranslationTextComponent and put a localization key instead of the text like below:
@Override
public void addInformation(ItemStack itemstack, World world, List<ITextComponent> list, ITooltipFlag flag) {
super.addInformation(itemstack, world, list, flag);
list.add(new TranslationTextComponent("item.modid.registry_name.tooltip.line1"));
list.add(new TranslationTextComponent("item.modid.registry_name.tooltip.line2"));
}
After creating a new localization key, you will be able to see your translated tooltip.
Your localization key can be anything you want. But I used this extremely long key.
2) Shiftable/Altable/Controllable Tooltip
Minecraft allows 3 diffrent options ShiftDown, AltDownand ControlDown, you can use one or more of these at the same time. Let's now see how:
@Override
public void addInformation(ItemStack itemstack, World world, List<ITextComponent> list, ITooltipFlag flag) {
super.addInformation(itemstack, world, list, flag);
if(Screen.hasShiftDown() && Screen.hasControlDown()) {
list.add(new StringTextComponent("Shifted and Controlled Tooltip"));
} else if(Screen.hasAltDown()) {
list.add(new StringTextComponent("Alted Tooltip"));
} else {
list.add(new StringTextComponent("Normal Tooltip"));
}
list.add(new StringTextComponent("This line is always shown, no matter what!"));
}
This will first check if shift and control is both pressed, if it doesn't met, it will check if alt key is pressed, if its not pressed too, it will show the normal tooltip. If you replace && with || it will tur into a or statement and will check if one of them is pressed.
If you are going to just copy paste stuff 👿, you may need some of the imports below:
Translatable Text import:
import net.minecraft.util.text.TranslationTextComponent;
Key Pressing import:
import net.minecraft.client.gui.screen.Screen;
For Fabric 1.16.x:
For Fabric, find the override below:
@Override
@Environment(EnvType.CLIENT)
public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, TooltipContext context) {
tooltip.add(new LiteralText("Line 1"));
tooltip.add(new LiteralText("Line 2"));
}
1) Translatable Tooltip
To make the tooltip translatable in Fabric, you just need to rename LiteralText to TranslatableText and write a localization key instead of the literal text, like below:
@Override
@Environment(EnvType.CLIENT)
public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, TooltipContext context) {
tooltip.add(new TranslatableText("item.modid.registry_name.tooltip.line1"));
tooltip.add(new TranslatableText("item.modid.registry_name.tooltip.line2"));
}
Again, you can use any localization key you want.
2) Shiftable/Altable/Controllable Tooltip
Adding tooltip is very similar to Forge but checking fkeys are literally the same with Forge.
@Override
@Environment(EnvType.CLIENT)
public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, TooltipContext context) {
if(Screen.hasShiftDown() && Screen.hasControlDown()) {
tooltip.add(new LiteralText("Shifted and Controlled Tooltip"));
} else if(Screen.hasAltDown()) {
tooltip.add(new LiteralText("Alted Tooltip"));
} else {
tooltip.add(new LiteralText("Normal Tooltip"));
}
tooltip.add(new LiteralText("This line is always shown, no matter what!"));
}
This is the same thing I did above. It's just for Fabric. It will first check if shift and control is both pressed, if not, it will check if alt key is pressed, if its not pressed too, it will show the normal tooltip. Again if you replace && with || it will tur into a or statement and will check if one of them is pressed.
If you are going to just copy paste stuff 👿, you may need some of the imports below:
Translatable Text import:
import net.minecraft.text.TranslatableText;
Key Pressing import:
import net.minecraft.client.gui.screen.Screen;
Enjoy your advanced tooltips. 😄
If you come across with any issues, you can post them below. Also don't forgot to give your log and code using a website like pastebin, etc..
(Please don't just copy paste the whole log 😢☹️👿)
Nice work once again!
Good tutorial!
Thanks Klemen and Goldorion.
Also added Fabric
nice!
One Question is there a way to add variables to Tooltips?
It's not working to me. It always failed to build.
Hey there, i tried this on 1.18.2 and it seems like some methods have changed, and the system doesn't recognize those. Do you know how the function should be made in the 1.18 generator? Thanks!
I managed to look for myself and found what the import looks like now, but i cannot find the import for the traslatable text components. Any of you have any idea on how should i import it?
I found it, these are the imports i needed to include (I only tested for Forge):
This work, i can confirm that with these the traslatable tooltips and normal ones do work, i hope this helps!