Posted April 21, 20205 yr 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 April 21, 20205 yr by lolgeny
April 21, 20205 yr Author I've also tried with Objects.requireNonNull(ITextComponent.Serializer.fromJson("{\"text\": \"MyGUI\"}")) same thing happened. I got the idea of null from https://forum.islandearth.net/d/10-forge-modding-tutorial-1-14-creating-custom-guis-1-3 but to be fair I haven't been completely following that.
April 21, 20205 yr Author 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
April 21, 20205 yr Author 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}
April 21, 20205 yr Author Thank you! It now loads the GUI, but there's still a problem... Have I created the button wrongly?
April 21, 20205 yr Author 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.