Skip to content

How to make a menu with slots and a screen in a Minecraft mod

A screen with slots is two classes joined by a MenuType. The menu (AbstractContainerMenu, Container on 1.16.5) lives on the server and the client; it owns the slots and the shift-click logic (quickMoveStack). The screen (AbstractContainerScreen) lives only in the client and draws the background. Register the MenuType, register the screen for it in the client (MenuScreens.register on Fabric and Forge, RegisterMenuScreensEvent on NeoForge 1.20.4 and newer), and open the menu on the server with player.openMenu. This example is a temporary 3 by 3 bag: anything left in it goes back to the player. For storage that stays, back the menu with a block entity’s container.

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. Each example was compiled against its loader and version before it shipped in Linen.

  1. Write the menu: slots for your container, then the player’s inventory and hotbar, and quickMoveStack.
  2. Register a MenuType for it.
  3. Write the screen and register it for the menu type in the client.
  4. Open the menu from the server with player.openMenu(new SimpleMenuProvider(…)).

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

Setting In the example What it does
title_text Bag The title shown on the screen.

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(TEXTURE, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = Registry.register(BuiltInRegistries.MENU, ExampleMod.id("menu_bag"),
new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

MenuScreens.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import net.minecraft.network.chat.TextComponent;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), new TextComponent("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(PoseStack poseStack, float partialTick, int mouseX, int mouseY) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, TEXTURE);
blit(poseStack, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
renderTooltip(poseStack, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.fmllegacy.RegistryObject;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(ForgeRegistries.CONTAINERS, ExampleMod.MOD_ID);
public static final RegistryObject<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new));
private MenuBagItemMenus() {
}
// Called from the mod's constructor: puts the menu type on the mod event bus.
public static void register() {
MENUS.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
// Connects the menu type to its screen (Forge: FMLClientSetupEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(MenuBagItemClientSetup::onClientSetup);
}
private static void onClientSetup(FMLClientSetupEvent event) {
event.enqueueWork(() -> MenuScreens.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new));
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register();
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import net.minecraft.network.chat.TextComponent;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), new TextComponent("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(PoseStack poseStack, float partialTick, int mouseX, int mouseY) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, TEXTURE);
blit(poseStack, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
renderTooltip(poseStack, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(ForgeRegistries.CONTAINERS, ExampleMod.MOD_ID);
public static final RegistryObject<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new));
private MenuBagItemMenus() {
}
// Called from the mod's constructor: puts the menu type on the mod event bus.
public static void register() {
MENUS.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
// Connects the menu type to its screen (Forge: FMLClientSetupEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(MenuBagItemClientSetup::onClientSetup);
}
private static void onClientSetup(FMLClientSetupEvent event) {
event.enqueueWork(() -> MenuScreens.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new));
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register();
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(PoseStack poseStack, float partialTick, int mouseX, int mouseY) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, TEXTURE);
blit(poseStack, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
renderTooltip(poseStack, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(ForgeRegistries.MENU_TYPES, ExampleMod.MOD_ID);
public static final RegistryObject<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new));
private MenuBagItemMenus() {
}
// Called from the mod's constructor: puts the menu type on the mod event bus.
public static void register() {
MENUS.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
// Connects the menu type to its screen (Forge: FMLClientSetupEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(MenuBagItemClientSetup::onClientSetup);
}
private static void onClientSetup(FMLClientSetupEvent event) {
event.enqueueWork(() -> MenuScreens.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new));
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register();
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(PoseStack poseStack, float partialTick, int mouseX, int mouseY) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, TEXTURE);
blit(poseStack, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
renderTooltip(poseStack, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(ForgeRegistries.MENU_TYPES, ExampleMod.MOD_ID);
public static final RegistryObject<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's constructor: puts the menu type on the mod event bus.
public static void register() {
MENUS.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
// Connects the menu type to its screen (Forge: FMLClientSetupEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(MenuBagItemClientSetup::onClientSetup);
}
private static void onClientSetup(FMLClientSetupEvent event) {
event.enqueueWork(() -> MenuScreens.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new));
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register();
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(TEXTURE, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(ForgeRegistries.MENU_TYPES, ExampleMod.MOD_ID);
public static final RegistryObject<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's constructor: puts the menu type on the mod event bus.
public static void register() {
MENUS.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
// Connects the menu type to its screen (Forge: FMLClientSetupEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(MenuBagItemClientSetup::onClientSetup);
}
private static void onClientSetup(FMLClientSetupEvent event) {
event.enqueueWork(() -> MenuScreens.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new));
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register();
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(TEXTURE, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(ForgeRegistries.MENU_TYPES, ExampleMod.MOD_ID);
public static final RegistryObject<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's constructor: puts the menu type on the mod event bus.
public static void register() {
MENUS.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
// Connects the menu type to its screen (NeoForge: FMLClientSetupEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(MenuBagItemClientSetup::onClientSetup);
}
private static void onClientSetup(FMLClientSetupEvent event) {
event.enqueueWork(() -> MenuScreens.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new));
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register();
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(TEXTURE, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = Registry.register(BuiltInRegistries.MENU, ExampleMod.id("menu_bag"),
new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

MenuScreens.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • RegisterMenuScreensEvent.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(TEXTURE, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import java.util.function.Supplier;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredRegister;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(Registries.MENU, ExampleMod.MOD_ID);
public static final Supplier<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
MENUS.register(modBus);
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
// Connects the menu type to its screen (NeoForge: RegisterMenuScreensEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register(IEventBus modBus) {
modBus.addListener(MenuBagItemClientSetup::onRegister);
}
private static void onRegister(RegisterMenuScreensEvent event) {
event.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new);
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register(modEventBus);
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • RegisterMenuScreensEvent.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(TEXTURE, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import java.util.function.Supplier;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredRegister;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(Registries.MENU, ExampleMod.MOD_ID);
public static final Supplier<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
MENUS.register(modBus);
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
// Connects the menu type to its screen (NeoForge: RegisterMenuScreensEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register(IEventBus modBus) {
modBus.addListener(MenuBagItemClientSetup::onRegister);
}
private static void onRegister(RegisterMenuScreensEvent event) {
event.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new);
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register(modEventBus);
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(TEXTURE, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import java.util.function.Supplier;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredRegister;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(Registries.MENU, ExampleMod.MOD_ID);
public static final Supplier<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
MENUS.register(modBus);
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.client.gui.screens.MenuScreens;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
// Connects the menu type to its screen (NeoForge: FMLClientSetupEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register(IEventBus modBus) {
modBus.addListener(MenuBagItemClientSetup::onClientSetup);
}
private static void onClientSetup(FMLClientSetupEvent event) {
event.enqueueWork(() -> MenuScreens.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new));
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register(modEventBus);
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.client;
import net.minecraft.client.renderer.RenderType;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(RenderType::guiTextured, TEXTURE, leftPos, topPos, 0.0F, 0.0F, imageWidth, imageHeight, 256, 256);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = Registry.register(BuiltInRegistries.MENU, ExampleMod.id("menu_bag"),
new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

MenuScreens.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • RegisterMenuScreensEvent.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(RenderType::guiTextured, TEXTURE, leftPos, topPos, 0.0F, 0.0F, imageWidth, imageHeight, 256, 256);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import java.util.function.Supplier;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredRegister;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(Registries.MENU, ExampleMod.MOD_ID);
public static final Supplier<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
MENUS.register(modBus);
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
// Connects the menu type to its screen (NeoForge: RegisterMenuScreensEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register(IEventBus modBus) {
modBus.addListener(MenuBagItemClientSetup::onRegister);
}
private static void onRegister(RegisterMenuScreensEvent event) {
event.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new);
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register(modEventBus);
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • RegisterMenuScreensEvent.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(RenderPipelines.GUI_TEXTURED, TEXTURE, leftPos, topPos, 0.0F, 0.0F, imageWidth, imageHeight, 256, 256);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import java.util.function.Supplier;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredRegister;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(Registries.MENU, ExampleMod.MOD_ID);
public static final Supplier<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
MENUS.register(modBus);
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
// Connects the menu type to its screen (NeoForge: RegisterMenuScreensEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register(IEventBus modBus) {
modBus.addListener(MenuBagItemClientSetup::onRegister);
}
private static void onRegister(RegisterMenuScreensEvent event) {
event.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new);
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register(modEventBus);
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.client;
import net.minecraft.client.renderer.RenderPipelines;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(RenderPipelines.GUI_TEXTURED, TEXTURE, leftPos, topPos, 0.0F, 0.0F, imageWidth, imageHeight, 256, 256);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = Registry.register(BuiltInRegistries.MENU, ExampleMod.id("menu_bag"),
new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

MenuScreens.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • RegisterMenuScreensEvent.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(RenderPipelines.GUI_TEXTURED, TEXTURE, leftPos, topPos, 0.0F, 0.0F, imageWidth, imageHeight, 256, 256);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import java.util.function.Supplier;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredRegister;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(Registries.MENU, ExampleMod.MOD_ID);
public static final Supplier<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
MENUS.register(modBus);
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
// Connects the menu type to its screen (NeoForge: RegisterMenuScreensEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register(IEventBus modBus) {
modBus.addListener(MenuBagItemClientSetup::onRegister);
}
private static void onRegister(RegisterMenuScreensEvent event) {
event.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new);
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.getDist() == Dist.CLIENT) {
MenuBagItemClientSetup.register(modEventBus);
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • RegisterMenuScreensEvent.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final Identifier TEXTURE = Identifier.withDefaultNamespace("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(RenderPipelines.GUI_TEXTURED, TEXTURE, leftPos, topPos, 0.0F, 0.0F, imageWidth, imageHeight, 256, 256);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import java.util.function.Supplier;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredRegister;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(Registries.MENU, ExampleMod.MOD_ID);
public static final Supplier<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
MENUS.register(modBus);
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
// Connects the menu type to its screen (NeoForge: RegisterMenuScreensEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register(IEventBus modBus) {
modBus.addListener(MenuBagItemClientSetup::onRegister);
}
private static void onRegister(RegisterMenuScreensEvent event) {
event.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new);
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.getDist() == Dist.CLIENT) {
MenuBagItemClientSetup.register(modEventBus);
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.client;
import net.minecraft.client.renderer.RenderPipelines;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final Identifier TEXTURE = Identifier.withDefaultNamespace("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(GuiGraphics graphics, float partialTick, int mouseX, int mouseY) {
graphics.blit(RenderPipelines.GUI_TEXTURED, TEXTURE, leftPos, topPos, 0.0F, 0.0F, imageWidth, imageHeight, 256, 256);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.render(graphics, mouseX, mouseY, partialTick);
renderTooltip(graphics, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = Registry.register(BuiltInRegistries.MENU, ExampleMod.id("menu_bag"),
new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

MenuScreens.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • RegisterMenuScreensEvent.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final Identifier TEXTURE = Identifier.withDefaultNamespace("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
// Since 26.1 a screen "extracts" what it draws; the container's picture goes in extractBackground.
@Override
public void extractBackground(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float partialTick) {
super.extractBackground(graphics, mouseX, mouseY, partialTick);
graphics.blit(RenderPipelines.GUI_TEXTURED, TEXTURE, leftPos, topPos, 0.0F, 0.0F, imageWidth, imageHeight, 256, 256);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import java.util.function.Supplier;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredRegister;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<MenuType<?>> MENUS = DeferredRegister.create(Registries.MENU, ExampleMod.MOD_ID);
public static final Supplier<MenuType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
MENUS.register(modBus);
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
// Connects the menu type to its screen (NeoForge: RegisterMenuScreensEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register(IEventBus modBus) {
modBus.addListener(MenuBagItemClientSetup::onRegister);
}
private static void onRegister(RegisterMenuScreensEvent event) {
event.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new);
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.getDist() == Dist.CLIENT) {
MenuBagItemClientSetup.register(modEventBus);
}

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.client;
import net.minecraft.client.renderer.RenderPipelines;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final Identifier TEXTURE = Identifier.withDefaultNamespace("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
// Since 26.1 a screen draws in extract methods that fill a render state; the background picture goes in extractBackground.
@Override
public void extractBackground(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float partialTick) {
super.extractBackground(graphics, mouseX, mouseY, partialTick);
graphics.blit(RenderPipelines.GUI_TEXTURED, TEXTURE, leftPos, topPos, 0.0F, 0.0F, imageWidth, imageHeight, 256, 256);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = Registry.register(BuiltInRegistries.MENU, ExampleMod.id("menu_bag"),
new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

MenuScreens.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(PoseStack poseStack, float partialTick, int mouseX, int mouseY) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, TEXTURE);
blit(poseStack, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
renderTooltip(poseStack, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = Registry.register(BuiltInRegistries.MENU, ExampleMod.id("menu_bag"),
new MenuType<>(MenuBagItemMenu::new, FeatureFlags.DEFAULT_FLAGS));
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

MenuScreens.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(PoseStack poseStack, float partialTick, int mouseX, int mouseY) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, TEXTURE);
blit(poseStack, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
renderTooltip(poseStack, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = Registry.register(BuiltInRegistries.MENU, ExampleMod.id("menu_bag"),
new MenuType<>(MenuBagItemMenu::new));
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

MenuScreens.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), Component.literal("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(PoseStack poseStack, float partialTick, int mouseX, int mouseY) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, TEXTURE);
blit(poseStack, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
renderTooltip(poseStack, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.core.Registry;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = Registry.register(Registry.MENU, ExampleMod.id("menu_bag"),
new MenuType<>(MenuBagItemMenu::new));
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

MenuScreens.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • MenuScreens.register

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

package com.example.examplemod.item;
import net.minecraft.network.chat.TextComponent;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), new TextComponent("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(PoseStack poseStack, float partialTick, int mouseX, int mouseY) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, TEXTURE);
blit(poseStack, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
renderTooltip(poseStack, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.core.Registry;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = Registry.register(Registry.MENU, ExampleMod.id("menu_bag"),
new MenuType<>(MenuBagItemMenu::new));
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

MenuScreens.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • ScreenRegistry.register

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

package com.example.examplemod.item;
import net.minecraft.network.chat.TextComponent;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), new TextComponent("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(PoseStack poseStack, float partialTick, int mouseX, int mouseY) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, TEXTURE);
blit(poseStack, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
renderTooltip(poseStack, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = ScreenHandlerRegistry.registerSimple(ExampleMod.id("menu_bag"), MenuBagItemMenu::new);
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

ScreenRegistry.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • AbstractContainerMenu
  • AbstractContainerMenu.quickMoveStack
  • Slot
  • MenuType
  • AbstractContainerScreen
  • Player.openMenu
  • SimpleMenuProvider
  • ScreenRegistry.register

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

package com.example.examplemod.item;
import net.minecraft.network.chat.TextComponent;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.SimpleMenuProvider;
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;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.openMenu(new SimpleMenuProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), new TextComponent("Bag")));
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends AbstractContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, Inventory inventory, Component title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(PoseStack poseStack, float partialTick, int mouseX, int mouseY) {
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
minecraft.getTextureManager().bind(TEXTURE);
blit(poseStack, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
renderTooltip(poseStack, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends AbstractContainerMenu {
private final Container container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9));
}
public MenuBagItemMenu(int id, Inventory inventory, Container container) {
super(MenuBagItemMenus.TYPE, id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(Player player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(Player player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(Player player) {
super.removed(player);
clearContainer(player, player.level, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.minecraft.world.inventory.MenuType;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
public static final MenuType<MenuBagItemMenu> TYPE = ScreenHandlerRegistry.registerSimple(ExampleMod.id("menu_bag"), MenuBagItemMenu::new);
private MenuBagItemMenus() {
}
// Called from the mod's onInitialize; loading this class registers the type above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

MenuBagItemMenus.register();

In your client setup class, inside onInitializeClient:

ScreenRegistry.register(MenuBagItemMenus.TYPE, MenuBagItemScreen::new);

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

Uses:

  • Container
  • ContainerType
  • ContainerScreen
  • ScreenManager.register
  • INamedContainerProvider

In 1.16.5 the classes are Container, ContainerType and ContainerScreen, the screen is registered with ScreenManager.register, and the menu is opened with an INamedContainerProvider.

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

package com.example.examplemod.item;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.container.SimpleNamedContainerProvider;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;
// Menu Bag: right-click opens a nine-slot menu. The server opens it; the client then opens the screen that goes with it.
public class MenuBagItem extends Item {
public MenuBagItem(Item.Properties properties) {
super(properties);
}
@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
if (!world.isClientSide()) {
player.openMenu(new SimpleNamedContainerProvider((id, inventory, p) -> new MenuBagItemMenu(id, inventory), new StringTextComponent("Bag")));
}
return ActionResult.sidedSuccess(player.getItemInHand(hand), world.isClientSide());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.menu.MenuBagItemMenu;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
// The screen of Menu Bag. It reuses the dispenser's picture, which has the same 3 by 3 layout. It runs in the client only.
public class MenuBagItemScreen extends ContainerScreen<MenuBagItemMenu> {
private static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/container/dispenser.png");
public MenuBagItemScreen(MenuBagItemMenu menu, PlayerInventory inventory, ITextComponent title) {
super(menu, inventory, title);
}
@Override
protected void renderBg(MatrixStack poseStack, float partialTick, int mouseX, int mouseY) {
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
minecraft.getTextureManager().bind(TEXTURE);
blit(poseStack, leftPos, topPos, 0, 0, imageWidth, imageHeight);
}
@Override
public void render(MatrixStack poseStack, int mouseX, int mouseY, float partialTick) {
renderBackground(poseStack);
super.render(poseStack, mouseX, mouseY, partialTick);
renderTooltip(poseStack, mouseX, mouseY);
}
}

src/main/java/com/example/examplemod/menu/MenuBagItemMenu.java

package com.example.examplemod.menu;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
// The menu of Menu Bag: nine slots in a 3 by 3 grid, then the player's inventory and hotbar. The menu runs on the server and in the client.
public class MenuBagItemMenu extends Container {
private final IInventory container;
// The client makes the menu with an empty container; the server sends the slot contents.
public MenuBagItemMenu(int id, PlayerInventory inventory) {
this(id, inventory, new Inventory(9));
}
public MenuBagItemMenu(int id, PlayerInventory inventory, IInventory container) {
super(MenuBagItemMenus.TYPE.get(), id);
checkContainerSize(container, 9);
this.container = container;
container.startOpen(inventory.player);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
addSlot(new Slot(container, col + row * 3, 62 + col * 18, 17 + row * 18));
}
}
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col + row * 9 + 9, 8 + col * 18, 84 + row * 18));
}
}
for (int col = 0; col < 9; col++) {
addSlot(new Slot(inventory, col, 8 + col * 18, 142));
}
}
// Shift-click: move a stack between the nine slots (0-8) and the player's slots (9-44).
@Override
public ItemStack quickMoveStack(PlayerEntity player, int index) {
ItemStack result = ItemStack.EMPTY;
Slot slot = slots.get(index);
if (slot.hasItem()) {
ItemStack stack = slot.getItem();
result = stack.copy();
if (index < 9) {
if (!moveItemStackTo(stack, 9, 45, true)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(stack, 0, 9, false)) {
return ItemStack.EMPTY;
}
if (stack.isEmpty()) {
slot.set(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return result;
}
@Override
public boolean stillValid(PlayerEntity player) {
return container.stillValid(player);
}
// This menu has no storage of its own: what is left in the slots goes back to the player.
@Override
public void removed(PlayerEntity player) {
super.removed(player);
clearContainer(player, player.level, container);
}
}

src/main/java/com/example/examplemod/registry/MenuBagItemMenus.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.menu.MenuBagItemMenu;
import net.minecraft.inventory.container.ContainerType;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
// The menu type of Menu Bag: it connects the menu on the server with its screen in the client.
public final class MenuBagItemMenus {
private static final DeferredRegister<ContainerType<?>> MENUS = DeferredRegister.create(ForgeRegistries.CONTAINERS, ExampleMod.MOD_ID);
public static final RegistryObject<ContainerType<MenuBagItemMenu>> TYPE = MENUS.register("menu_bag",
() -> new ContainerType<>(MenuBagItemMenu::new));
private MenuBagItemMenus() {
}
// Called from the mod's constructor: puts the menu type on the mod event bus.
public static void register() {
MENUS.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}

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

package com.example.examplemod.client;
import com.example.examplemod.registry.MenuBagItemMenus;
import net.minecraft.client.gui.ScreenManager;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
// Connects the menu type to its screen (Forge: FMLClientSetupEvent on the mod bus).
public final class MenuBagItemClientSetup {
private MenuBagItemClientSetup() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
FMLJavaModLoadingContext.get().getModEventBus().addListener(MenuBagItemClientSetup::onClientSetup);
}
private static void onClientSetup(FMLClientSetupEvent event) {
event.enqueueWork(() -> ScreenManager.register(MenuBagItemMenus.TYPE.get(), MenuBagItemScreen::new));
}
}

In your main mod class, inside <init>:

MenuBagItemMenus.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
MenuBagItemClientSetup.register();
}

It’s registered like any other item, as MenuBagItem: 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.