Skip to main content

Draw Text The Fabric Mod Loader

·2 mins

I haven’t really seen a lot of specific tutorials for certain aspects of fabric. So, I decided to make one for drawing on the screen

Basic Setup #

This tutorial assumes you already have basic knowledge of fabric and java. If you do not there are plenty of tutorials which you can do to get caught up with the information in this.
To start setup a working fabric mod environment for 1.21

When to Render #

You should render every time render is called in the client. This would usually require a mixin, however, fabric has an event specifically for this use

Listening for The Event #

To listen to the event simply add this listener to your code

HudRenderCallback.EVENT.register(((drawContext, tickCounter) -> {
    // TODO: Use the event
}

DrawContext #

Normally to draw anything on the string you need a lot of parameters so that everything works correctly. DrawContext eliminates all the boilerplate parameters allowing you to actually do the rendering.

TickCounter #

TickCounter can be used if you need to check anything with rendering ticks. It can be useful in some situations but generally is less common than using DrawContext.

Drawing the Text #

To draw the text there are a handful of parameters

TextRenderer #

This is what actually renders the text for this example I will be using the default Minecraft Text Renderer

Text #

This can either be a string "Hello World!" or Minecraft Text Text.literal("Hello World")

X and Y #

The next 2 parameters are the x and y values on the screen. To make the text easy to see starting at x and y 2 is a good choice.

Color #

Color is represented in the format

AA RR GG BB
Alpha Red Green Blue
The opacity 0-255

Each number is from 0-F where FF is 255 and 00 is 0
For this example we will use 0xFFFFFFFF which is (255, 255, 255, 255) or white at 255 opacity
This is a good converter if you do not want to color it manually

Shadow #

If you want text shadow, for this example we will not use it

Final Code #

HudRenderCallback.EVENT.register(((drawContext, tickCounter) -> {
    // Draw Hello World in full opacity white (255, 255, 255, 255) with no shadow
    drawContext.drawText(MinecraftClient.getInstance().textRenderer, "Hello World!", 2, 2, 0xFFFFFFFF, false);
}));