Minecraft fontRenderer.drawString() disappears instantly

I tried to make a ingame GUI in minecraft with this function. (I’m using forge with MC 1.5.2).

Minecraft.getMinecraft().fontRenderer.drawString(String)

Here’s my code:

@ForgeSubscribe
public void onServerChat(ServerChatEvent event)
{
    mcObject.fontRenderer.drawStringWithShadow("Hello WOrld", 4, 4, 0xffFFFFFF);
    mcObject.ingameGUI.drawRect(2 /*X*/, 2 /*Y*/, 100 /*X1*/, 12 /*Y1*/, 0x80000000 /*COLOUR*/);
}

But when I type a chat message (just using that event for testing) the text disappears instantly and you can barely see it and certainly not read it.

I’m quite new to forge modding.

How to make a text more permanent and how you remove it again (or overwrite)?

Answer

onServerChat is an instantaneous event: it fires when the chat receives a message and immediately stops.

You will need to draw your GUI in an event that updates every frame. I have been doing it OnRenderExperienceBar, an element of Minecraft’s default GUI. Here’s an example using your code.

@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent event) {

mcObject.fontRenderer.drawStringWithShadow("Hello World", 4, 4, 0xffFFFFFF);
mcObject.ingameGUI.drawRect(2 /*X*/, 2 /*Y*/, 100 /*X1*/, 12 /*Y1*/, 0x80000000 /*COLOUR*/);

}

Leave a Reply

Your email address will not be published. Required fields are marked *