TextFieldWidget not working with FabricMC (Java)

0

Recently I've been trying to learn some stuff about Minecraft Modding. I've chosen to use Fabric Mod Loader to make/test mods I'm making.

In doing so, I've working on a recent project (more like test) and it has to do with screens. So enough about that and on to the problem.

So I've made a text field and for some reason the text in it isn't showing up when I type..
Video of the problem: https://streamable.com/ik7j79
The Code: https://pastebin.com/8XtSGpWj

package battledash2.clientcommands.screens;

import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.*;
import net.minecraft.util.Formatting;

import java.util.logging.LogManager;

public class GuiScreen extends Screen {
    public GuiScreen(Text title) {
        super(title);
    }

    protected void init() {
        // init

        System.out.println("Loading Screen");
    }

    public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
        matrices.push();
        renderBackground(matrices);

        /*
         * this.addButton(new ButtonWidget(this.width / 2 - 102, this.height / 4 + 24 + -16, 204, 20, new TranslatableText("menu.returnToGame"), (buttonWidgetx) -> {
         *          this.client.openScreen((Screen)null);
         *          this.client.mouse.lockCursor();
         *       }));
         */

        TextFieldWidget textfield = new TextFieldWidget(textRenderer, this.width / 2 - 102, this.height / 4 + 24 + -16, 204, 20, new LiteralText("Chat message").setStyle(Style.EMPTY.withColor(Formatting.WHITE)));

        this.addButton(textfield);

        ButtonWidget submit = new ButtonWidget(this.width / 2 - 102, (this.height / 4 + 24 + -16)+(this.height / 4 + 24 + -16)+10, 204, 20, new LiteralText("Send chat message"), button->{
            String message = textfield.getText();

            MinecraftClient.getInstance().openScreen((Screen) null);
            MinecraftClient.getInstance().mouse.lockCursor();

            MinecraftClient.getInstance().player.sendChatMessage(message);
        });
        this.addButton(submit);

        // drawTextWithShadow(matrices, textRenderer, new LiteralText("Chat message").setStyle(Style.EMPTY.withColor(Formatting.WHITE)), 10, 10, 0xFFFFFFFF);

        matrices.pop();
        super.render(matrices, mouseX, mouseY, delta);
    }
}
java
minecraft
minecraft-fabric
asked on Stack Overflow Jan 21, 2021 by Battledash2 • edited Jan 21, 2021 by Vadim Kotov

1 Answer

0

Okay so I found out the problem. It was because I was putting the widgets in the "render" method. I created a new method called "initWidgets" and ran initWidgets() in the main method and then moved the widgets to the "initWidgets" method and it worked.

answered on Stack Overflow Jan 21, 2021 by Battledash2

User contributions licensed under CC BY-SA 3.0