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

Started by NOYB on

Topic category: User side tutorials

Joined May 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • 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
Joined May 2020
Points:

User statistics:

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

Thanks Klemen and Goldorion.

Also added Fabric

Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • 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?

Joined Jan 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
It's not working to me. It…
Thu, 01/20/2022 - 05:06

It's not working to me. It always failed to build.

Executing Gradle task: runClient 
Build info: MCreator 2021.3.53117, forge-1.16.5, 64-bit, 16346 MB, Windows 7, JVM 16.0.2, JAVA_HOME: D:\Program Files\Pylo\MCreator\jdk 
  
> Task :compileJava 
R:\Users\KOR_APUcard\Documents\MCreatorWorkspaces\mymod\src\main\java\net\apucsw\mymod\block\PeridotOreBlock.java:79: error: method does not override or implement a method from a supertype 
      @Override 
      ^ 
1 error 
> Task :compileJava FAILED 
FAILURE: Build failed with an exception. 
* What went wrong: 
Execution failed for task ':compileJava'. 
> Compilation failed; see the compiler error output for details. 
* Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. 
* Get more help at https://help.gradle.org 
BUILD FAILED in 5s 
1 actionable task: 1 executed 
  
BUILD FAILED 
Task completed in 9 seconds

 

Joined Feb 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hey there, i tried this on 1…
Sat, 08/13/2022 - 17:40

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!

Joined Feb 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I managed to look for myself…
Wed, 09/07/2022 - 15:47

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?

Joined Feb 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I found it, these are the…
Wed, 09/07/2022 - 16:14

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!

import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.TranslatableComponent;