[TUTORIAL] Advanced Tooltips (1.14/1.15/1.16) (Forge/Fabric)

Started by NOYB on Fri, 08/28/2020 - 11:13

Topic category: User side tutorials

Last seen on 06:40, 15. Feb 2021
Joined May 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
[TUTORIAL] Advanced Tooltips (1.14/1.15/1.16) (Forge/Fabric)
Wed, 09/02/2020 - 10:17 (edited)

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 😢☹️👿)

Edited by NOYB on Wed, 09/02/2020 - 10:17
Last seen on 06:40, 15. Feb 2021
Joined May 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Thanks Klemen and Goldorion…
Fri, 08/28/2020 - 13:42

Thanks Klemen and Goldorion.

Also added Fabric

Last seen on 02:05, 20. Sep 2021
Joined Sep 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
nice!
Fri, 08/28/2020 - 17:40

nice!

Last seen on 08:58, 15. Aug 2021
Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
One Question is there a way…
Fri, 02/19/2021 - 07:06

One Question is there a way to add variables to Tooltips?