Skip to content

TextComponentUtils

Fulminazzo edited this page Oct 2, 2023 · 1 revision

Since Minecraft 1.16, creating TextComponent HoverEvents has become easier, but at the same time incompatible with previous versions. TextComponentUtils aims to correct this, by providing a simple function to create an HoverEvent to display a message to the players:

/**
 * Returns a HoverEvent displaying the given text as message.
 * @param text: the message.
 * @return the HoverEvent.
 */
public static HoverEvent getTextHoverEvent(String text);

It uses reflections and VersionsUtils to check what version the server is running on and create a HoverEvent accordingly.
The code could be reinterpreted like this:

public static HoverEvent getTextHoverEvent(String text) {
    if (VersionsUtils.is1_16())
        return new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(text));
    else
        return new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent(text)});
}