Skip to main content

Quick start

A simple welcome menu​

Let's build a welcome menu: a three-row chest, a ring of decorative background, and two buttons in the middle. One greets the player, the other closes the menu.

Hover over the items to see their names and lore:

Welcome Menu
#
#
#
#
#
#
#
#
#
#
#
#
Say hi
#
Close
#
#
#
#
#
#
#
#
#
#
#
#

Define the layout​

The layout is declared as three strings, one character per slot:

Pane.builder(
"#########",
"###G#C###",
"#########"
)

The ring of # is decoration players cannot click; G and C are the two buttons. Where each character lands:

#
#
#
#
#
#
#
#
#
#
#
#
G
#
C
#
#
#
#
#
#
#
#
#
#
#
#
  • #background25
  • Ggreeting1
  • Cclose menu1

Walk through the code​

The whole menu is the single class below. Step through the stepper to see what each part does; unselected lines dim, and the code stays the same.

Three strings describe the shape of the menu: one character per slot. Three rows of nine makes a 27-slot chest. Pick any characters you like; they are just labels for naming which slots hold what in the next step.

#
#
#
#
#
#
#
#
#
#
#
#
G
#
C
#
#
#
#
#
#
#
#
#
#
#
#
  • #unassigned25
  • Gunassigned1
  • Cunassigned1
import io.papermc.paper.datacomponent.DataComponentTypes;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.momirealms.sparrow.ui.item.Item;
import net.momirealms.sparrow.ui.pane.NormalPane;
import net.momirealms.sparrow.ui.pane.Pane;
import net.momirealms.sparrow.ui.window.Window;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public final class WelcomeMenu {

private WelcomeMenu() {
}

/** Opens the welcome menu for one player. */
public static void open(Player viewer) {
Window.builder(buildPane())
.setTitle(Component.text("Welcome Menu"))
.open(viewer);
}

/** The menu layout: a ring of background and two buttons in the middle. */
private static NormalPane buildPane() {
return Pane.builder(
"#########",
"###G#C###",
"#########"
)
.setBackground(background())
.addIngredient('G', greetButton())
.addIngredient('C', closeButton())
.build();
}

/** Greets the player when clicked. */
private static Item greetButton() {
ItemStack itemStack = new ItemStack(Material.LIME_DYE);
itemStack.setData(DataComponentTypes.CUSTOM_NAME,
Component.text("Say hi", NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, false));

return Item.builder()
.setItemProviderConstant(itemStack)
.addClickHandler(click -> click.player().sendMessage(
Component.text("Hi, " + click.player().getName() + "!", NamedTextColor.GREEN)))
.build();
}

/** Closes the menu when clicked. */
private static Item closeButton() {
ItemStack itemStack = new ItemStack(Material.BARRIER);
itemStack.setData(DataComponentTypes.CUSTOM_NAME,
Component.text("Close", NamedTextColor.RED).decoration(TextDecoration.ITALIC, false));

return Item.builder()
.setItemProviderConstant(itemStack)
.addClickHandler(click -> click.window().close())
.build();
}

/** The decorative item shown in empty slots. */
private static ItemStack background() {
ItemStack itemStack = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
itemStack.setData(DataComponentTypes.CUSTOM_NAME, Component.empty());
return itemStack;
}
}

Add a command to open it​

The menu class is done; it just needs a caller. Register a /welcome command with Paper's Brigadier API. Join the server, type /welcome, and the menu appears.

// In your JavaPlugin#onEnable
this.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, event ->
event.registrar().register(
Commands.literal("welcome")
.executes(context -> {
if (context.getSource().getExecutor() instanceof Player player) {
WelcomeMenu.open(player);
}
return Command.SINGLE_SUCCESS;
})
.build(),
"Open the welcome menu"));
build() and open() may run off the main thread

build(viewer) and open() can both be called from any thread any thread; building a menu does not have to squeeze onto the main thread.

What you do have to own is the gap between starting the build and the menu actually opening, because state can change in between. Say a player right-clicks a shulker box item and you read its contents asynchronously to build the menu. By the time the menu opens, the item in their hand may no longer be that shulker box.

The return value of open()

open() returns a CompletableFuture<Window.OpenResult> with three outcomes: OPENED, ALREADY_OPEN, and VIEWER_UNAVAILABLE. The example above ignores the result, so it just drops the future.

NextCore concepts and layersHow Window, Pane, and Item actually relate