Skip to content

How to open a screen from an item in a Minecraft mod

A screen is a client-only class that extends Screen (GuiScreen on 1.12.2). init adds widgets such as a Button; render draws on top of the background (through GuiGraphics from 1.20). Open it with Minecraft.getInstance().setScreen(new MyScreen()). Call that only on the client: keep it in a small helper class so a dedicated server never loads the screen. A screen that shows items or slots needs a menu too (the server side); this one is for buttons and text only.

Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.

  1. Write a class that extends Screen; add widgets in init.
  2. Draw text or images in render, after super.render.
  3. Open it from a client-side check, such as use() when level.isClientSide().
  4. Right-click the item and press Esc to close.

The example uses these values. Change them in the code.

Setting In the example What it does
title_text My first screen The title the screen shows.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.20 to 1.20.1, Fabric 1.20 to 1.20.3

Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.20 to 1.20.1, Fabric 1.20 to 1.20.3”

Uses:

  • Screen.init
  • Screen.render
  • Button.builder
  • Minecraft.setScreen
  • GuiGraphics.drawCenteredString

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
NotesBookItemOpener.open();
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(Component.literal("My first screen"));
}
@Override
protected void init() {
addRenderableWidget(Button.builder(Component.literal("Close"), button -> onClose())
.bounds(width / 2 - 50, height / 2, 100, 20).build());
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
graphics.drawCenteredString(font, title, width / 2, height / 2 - 30, 0xFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.render
  • Button.builder
  • Minecraft.setScreen
  • GuiGraphics.drawCenteredString

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
NotesBookItemOpener.open();
}
return InteractionResult.SUCCESS;
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(Component.literal("My first screen"));
}
@Override
protected void init() {
addRenderableWidget(Button.builder(Component.literal("Close"), button -> onClose())
.bounds(width / 2 - 50, height / 2, 100, 20).build());
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
graphics.drawCenteredString(font, title, width / 2, height / 2 - 30, 0xFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.extractRenderState
  • Button.builder
  • Minecraft.setScreen
  • GuiGraphicsExtractor.centeredText

Since 26.1 a screen no longer draws with render(GuiGraphics, …): it overrides extractRenderState(GuiGraphicsExtractor, …) and draws with the extractor (centeredText, text). Colours need the alpha byte (0xFFFFFFFF).

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
NotesBookItemOpener.open();
}
return InteractionResult.SUCCESS;
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(Component.literal("My first screen"));
}
@Override
protected void init() {
addRenderableWidget(Button.builder(Component.literal("Close"), button -> onClose())
.bounds(width / 2 - 50, height / 2, 100, 20).build());
}
@Override
public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float partialTick) {
super.extractRenderState(graphics, mouseX, mouseY, partialTick);
graphics.centeredText(font, title, width / 2, height / 2 - 30, 0xFFFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.extractRenderState
  • Button.builder
  • Minecraft.setScreenAndShow
  • GuiGraphicsExtractor.centeredText

Since 26.2 Minecraft#setScreen is setScreenAndShow. Since 26.1 a screen draws in extractRenderState(GuiGraphicsExtractor, …) with centeredText/text; colours need the alpha byte (0xFFFFFFFF).

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
NotesBookItemOpener.open();
}
return InteractionResult.SUCCESS;
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(Component.literal("My first screen"));
}
@Override
protected void init() {
addRenderableWidget(Button.builder(Component.literal("Close"), button -> onClose())
.bounds(width / 2 - 50, height / 2, 100, 20).build());
}
@Override
public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float partialTick) {
super.extractRenderState(graphics, mouseX, mouseY, partialTick);
graphics.centeredText(font, title, width / 2, height / 2 - 30, 0xFFFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreenAndShow(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.render
  • Button.builder
  • Minecraft.setScreen
  • GuiComponent.drawCenteredString

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
NotesBookItemOpener.open();
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import net.minecraft.network.chat.TextComponent;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(new TextComponent("My first screen"));
}
@Override
protected void init() {
addRenderableWidget(new Button(width / 2 - 50, height / 2, 100, 20, new TextComponent("Close"), button -> onClose()));
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
drawCenteredString(poseStack, font, title, width / 2, height / 2 - 30, 0xFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.render
  • Button.builder
  • Minecraft.setScreen
  • GuiComponent.drawCenteredString

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
NotesBookItemOpener.open();
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(Component.literal("My first screen"));
}
@Override
protected void init() {
addRenderableWidget(new Button(width / 2 - 50, height / 2, 100, 20, Component.literal("Close"), button -> onClose()));
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
drawCenteredString(poseStack, font, title, width / 2, height / 2 - 30, 0xFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.render
  • Button.builder
  • Minecraft.setScreen
  • GuiComponent.drawCenteredString

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
NotesBookItemOpener.open();
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(Component.literal("My first screen"));
}
@Override
protected void init() {
addRenderableWidget(Button.builder(Component.literal("Close"), button -> onClose())
.bounds(width / 2 - 50, height / 2, 100, 20).build());
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
drawCenteredString(poseStack, font, title, width / 2, height / 2 - 30, 0xFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.render
  • Button.builder
  • Minecraft.setScreen
  • GuiGraphics.drawCenteredString

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
NotesBookItemOpener.open();
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(Component.literal("My first screen"));
}
@Override
protected void init() {
addRenderableWidget(Button.builder(Component.literal("Close"), button -> onClose())
.bounds(width / 2 - 50, height / 2, 100, 20).build());
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
drawCenteredString(poseStack, font, title, width / 2, height / 2 - 30, 0xFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.render
  • Button.builder
  • Minecraft.setScreen
  • GuiGraphics.drawCenteredString

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
NotesBookItemOpener.open();
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(Component.literal("My first screen"));
}
@Override
protected void init() {
addRenderableWidget(new Button(width / 2 - 50, height / 2, 100, 20, Component.literal("Close"), button -> onClose()));
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
drawCenteredString(poseStack, font, title, width / 2, height / 2 - 30, 0xFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.render
  • Button.builder
  • Minecraft.setScreen
  • GuiGraphics.drawCenteredString

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
NotesBookItemOpener.open();
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import net.minecraft.network.chat.TextComponent;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(new TextComponent("My first screen"));
}
@Override
protected void init() {
addRenderableWidget(new Button(width / 2 - 50, height / 2, 100, 20, new TextComponent("Close"), button -> onClose()));
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
drawCenteredString(poseStack, font, title, width / 2, height / 2 - 30, 0xFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.render
  • Button.builder
  • Minecraft.setScreen
  • GuiGraphics.drawCenteredString

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
NotesBookItemOpener.open();
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import net.minecraft.network.chat.TextComponent;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(new TextComponent("My first screen"));
}
@Override
protected void init() {
addButton(new Button(width / 2 - 50, height / 2, 100, 20, new TextComponent("Close"), button -> onClose()));
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
drawCenteredString(poseStack, font, title, width / 2, height / 2 - 30, 0xFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.render
  • Button
  • Minecraft.setScreen
  • Screen.drawCenteredString

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.util.Hand;
import net.minecraft.util.ActionResult;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
// Notes Book: right-click opens a screen. use runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
public NotesBookItem(Item.Properties properties) {
super(properties);
}
@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
if (world.isClientSide()) {
NotesBookItemOpener.open();
}
return ActionResult.sidedSuccess(player.getItemInHand(hand), world.isClientSide());
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import net.minecraft.util.text.StringTextComponent;
import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.client.gui.screen.Screen;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends Screen {
public NotesBookItemScreen() {
super(new StringTextComponent("My first screen"));
}
@Override
protected void init() {
addButton(new Button(width / 2 - 50, height / 2, 100, 20, new StringTextComponent("Close"), button -> onClose()));
}
@Override
public void render(MatrixStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
drawCenteredString(poseStack, font, title, width / 2, height / 2 - 30, 0xFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getInstance().setScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Uses:

  • Screen.init
  • Screen.render
  • Button
  • Minecraft.setScreen
  • Screen.drawCenteredString

In 1.12.2 the class is GuiScreen: widgets are added in initGui, button clicks arrive in actionPerformed, drawing is in drawScreen, and the screen opens with Minecraft.getMinecraft().displayGuiScreen(screen).

src/main/java/com/example/examplemod/item/NotesBookItem.java

package com.example.examplemod.item;
import com.example.examplemod.client.NotesBookItemOpener;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
// Notes Book: right-click opens a screen. onItemRightClick runs on both sides; only the client opens it.
public class NotesBookItem extends Item {
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
if (world.isRemote) {
NotesBookItemOpener.open();
}
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
}
}

src/main/java/com/example/examplemod/client/NotesBookItemScreen.java

package com.example.examplemod.client;
import java.io.IOException;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
// Notes Book: a plain screen with a title and a Close button. It runs in the client only.
public class NotesBookItemScreen extends GuiScreen {
@Override
public void initGui() {
buttonList.add(new GuiButton(0, width / 2 - 50, height / 2, 100, 20, "Close"));
}
@Override
protected void actionPerformed(GuiButton button) throws IOException {
if (button.id == 0) {
mc.displayGuiScreen(null);
}
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
drawDefaultBackground();
super.drawScreen(mouseX, mouseY, partialTicks);
drawCenteredString(fontRenderer, "My first screen", width / 2, height / 2 - 30, 0xFFFFFF);
}
}

src/main/java/com/example/examplemod/client/NotesBookItemOpener.java

package com.example.examplemod.client;
import net.minecraft.client.Minecraft;
// The only class that touches the client screen, so a dedicated server never loads it.
public final class NotesBookItemOpener {
private NotesBookItemOpener() {
}
public static void open() {
Minecraft.getMinecraft().displayGuiScreen(new NotesBookItemScreen());
}
}

It’s registered like any other item, as NotesBookItem: see a new item.

Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.