Jump to content

1.17.1 Text on UI


e2rifia

Recommended Posts

This is my code. How can I change it to have "Hello world!" show on the screen?

@Mod(Chants2.MODID)
public class Chants2 {
    public static final String MODID = "chants2";
    public Chants2(){
        final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
        final ClientSideOnlyModEventRegistrar clientSideOnlyModEventRegistrar = new ClientSideOnlyModEventRegistrar(modEventBus);
        registerCommonEvents(modEventBus);
        DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> clientSideOnlyModEventRegistrar::registerClientOnlyEvents);
    }
    public void registerCommonEvents(IEventBus eventBus) {
        //eventBus.register(GuiHandler.class);
    }
}
public class ClientSideOnlyModEventRegistrar {
    private final IEventBus eventBus;
    public ClientSideOnlyModEventRegistrar(IEventBus eventBus) {
        this.eventBus = eventBus;
    }
    public void registerClientOnlyEvents() {
        eventBus.register(GuiHandler.class);
    }
}
public class GuiHandler {
    String text = "Hello world!";
    @SubscribeEvent
    public void onRenderGui(RenderGameOverlayEvent.Post event)
    {
        drawString(event.getMatrixStack(), Minecraft.getInstance().font, text, event.getWindow().getWidth() / 2, event.getWindow().getHeight() / 2, Integer.parseInt("33AA66", 16));
    }
}

 

Link to comment
Share on other sites

The text doesn't show. What did I do wrong this time?

public class GuiHandler {
    String text = "Hello world!";
    @SubscribeEvent
    public void onRenderGui(RenderGameOverlayEvent.Post event)
    {
        OverlayRegistry.registerOverlayTop("hello", new IIngameOverlay() {
            @Override
            public void render(ForgeIngameGui gui, PoseStack mStack, float partialTicks, int width, int height) {
                drawString(mStack, Minecraft.getInstance().font, text, width / 2, height / 2, Integer.parseInt("33AA66", 16));
            }
        });
    }
}
Link to comment
Share on other sites

"Call from net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent."

I found it. One step further!

I don't think FMLClientSetupEvent is reached. The other classes are untouched. What did I do wrong this time?

public class GuiHandler {
    String text = "Hello world!";
    @SubscribeEvent
    public void onRenderGui(FMLClientSetupEvent event)
    {System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
        OverlayRegistry.registerOverlayBottom("hello", new IIngameOverlay() {
            @Override
            public void render(ForgeIngameGui gui, PoseStack mStack, float partialTicks, int width, int height) {
                drawString(mStack, Minecraft.getInstance().font, text, width / 2, height / 2, Integer.parseInt("33AA66", 16));
            }
        });
    }
}

 

Link to comment
Share on other sites

I think I registered the GuiHandler event handler here.

public class ClientSideOnlyModEventRegistrar {
    private final IEventBus eventBus;
    public ClientSideOnlyModEventRegistrar(IEventBus eventBus) {
        this.eventBus = eventBus;
    }
    public void registerClientOnlyEvents() {
        eventBus.register(GuiHandler.class);
    }
}

I called the registrar above here.

@Mod(Chants2.MODID)
public class Chants2 {
    public static final String MODID = "chants2";
    public Chants2(){
        final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
        final ClientSideOnlyModEventRegistrar clientSideOnlyModEventRegistrar = new ClientSideOnlyModEventRegistrar(modEventBus);
        DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> clientSideOnlyModEventRegistrar::registerClientOnlyEvents);
    }
}

I guessed from reading the documentation on FMLClientSetupEvent that said to use Dist.CLIENT.

Link to comment
Share on other sites

It worked! Thank you for guiding me, diesieben07.

If I have @EventBusSubscriber for my client-only event handlers, do I still have to call them in my Chants2 constructor?

I'll paste my code here for when I have to look back.

@Mod(Chants2.MODID)
public class Chants2 {
    public static final String MODID = "chants2";
    public Chants2(){
        final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
        final ClientSideOnlyModEventRegistrar clientSideOnlyModEventRegistrar = new ClientSideOnlyModEventRegistrar(modEventBus);
        registerCommonEvents(modEventBus);
        DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> clientSideOnlyModEventRegistrar::registerClientOnlyEvents);
    }
    public void registerCommonEvents(IEventBus eventBus) {
        //eventBus.register(GuiHandler.class);
    }
}
//@Mod.EventBusSubscriber(Dist.CLIENT)
public class ClientSideOnlyModEventRegistrar {
    private final IEventBus eventBus;
    public ClientSideOnlyModEventRegistrar(IEventBus eventBus) {
        this.eventBus = eventBus;
    }
    public void registerClientOnlyEvents() {
        eventBus.register(GuiHandler.class);
    }
}
//@Mod.EventBusSubscriber(Dist.CLIENT)
public class GuiHandler {
    static String text = "Hello world!";
    @SubscribeEvent
    public static void onRenderGui(FMLClientSetupEvent event)
    {
        OverlayRegistry.registerOverlayBottom("hello", new IIngameOverlay() {
            @Override
            public void render(ForgeIngameGui gui, PoseStack mStack, float partialTicks, int width, int height) {
                drawString(mStack, Minecraft.getInstance().font, text, width / 2, height / 2, Integer.parseInt("33AA66", 16));
            }
        });
    }
}

 

Edited by e2rifia
Grammar.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.