Jump to content

Recommended Posts

Posted (edited)

Hello, I'm trying to make a GUI using Screen.

So far I've got 

public class MyGUI extends Screen {
    public MyGUI() {
        super(null);
    }

    @Override
    public void init(Minecraft p_init_1_, int p_init_2_, int p_init_3_) {
        this.addButton(new Button(this.width/2-100, this.height/2-50,200, 100, "Click me!", (button) -> this.onClose()));
    }

    @Override
    public void render(int p_render_1_, int p_render_2_, float p_render_3_) {
        this.renderBackground();
        super.render(p_render_1_, p_render_2_, p_render_3_);
        this.drawCenteredString(this.font, "Click the button!", this.width/2, this.height/2 + 70, 0xFFFFFF);
    }
}

It opens the Screen when I right-click on my block. Right now it draws the background (i.e the screen darkens) but not the buttons/text, then proceeds to freeze the game after a few seconds / pressing Esc.

 

Thanks in advance!

Edited by lolgeny
Posted

On my block, I have

@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult p_225533_6_) {
    Minecraft.getInstance().displayGuiScreen(new MyGUI());
    return ActionResultType.SUCCESS;
}

The log shows nothing abnormal, doesn't mention GUI/crashing and only has an error that I force close the game.

 

How do you construct it normally? That was really the only method I could find to create one from a string, sorry if I'm overlooking something here

Posted

Thank you for your response! I've changed some things:

on the GUI, I now use 

super(new StringTextComponent("My GUI"));

and to launch it, I use

@Override
    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult p_225533_6_) {
        if (worldIn.isRemote()) {
            DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> Minecraft.getInstance().displayGuiScreen(new MyGUI()));
        }
        return ActionResultType.SUCCESS;
    }

Now it crashes with a NullPointerException, not freezing this time: it just goes to 'Saving World' and crashes on the renderBackground method

java.lang.NullPointerException: Rendering screen
	at net.minecraft.client.gui.screen.Screen.renderBackground(Screen.java:367) ~[forge-1.15.2-31.1.46_mapped_snapshot_20200225-1.15.1-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	

 

Posted
public class MyGUI extends Screen {
    public MyGUI() {
        super(new StringTextComponent("My GUI"));
    }

    @Override
    public void init() {
        this.addButton(new Button(this.width/2-100, this.height/2-50,200, 100, "Click me!", (button) -> this.onClose()));
    }

    @Override
    public void render(int p_render_1_, int p_render_2_, float p_render_3_) {
        this.renderBackground();
        super.render(p_render_1_, p_render_2_, p_render_3_);
        this.drawCenteredString(this.font, "Yes, click it!", this.width/2, this.height/2 + 70, 0xFFFFFF);
    }
}

 

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.