Skip to main content

Per-player display

The minigame lobby has a "Ready" button. After Alice clicks it, her button says "Ready", while Bob's still says "Click to ready".

You could build a new button for every menu open and capture that viewer, but then the button cannot be shared between windows. A shared button has to know, at render time, who is looking.

What binding by viewer does​

dependsOn(signal, keyOf) takes a KeyedSignal and a function that picks a key. The function receives the render context of the display position and returns the partition key to bind there.

For per-player partitions, return context.player().getUniqueId() and every viewer binds their own partition. When Alice's partition changes, only the button Alice sees refreshes.

A shared ready button​

ready keeps each player's ready state by UUID; see Player partitions. In the demo, the top row is what Alice sees and the bottom row is what Bob sees. Both are the same readyButton.

Step 1 / 6

Every player starts not ready, and partitions are cleaned up when they quit.

MutableKeyedSignal<UUID, Boolean> ready = KeyedSignal.of(playerId -> false);
Signals.evictOnQuit(ready);

// create the button once and put it in every player's lobby menu
Item readyButton = Item.builder()
.dependsOn(ready, context -> context.player().getUniqueId())
.setItemProvider(context -> {
boolean isReady = ready.get(context.player().getUniqueId());
return isReady
? named(Material.LIME_DYE, "Ready")
: named(Material.GRAY_DYE, "Click to ready");
})
.addClickHandler(click -> {
ready.update(click.player().getUniqueId(), value -> !value);
})
.build();

Pane pane = Pane.builder("####R####")
.addIngredient('R', readyButton)
.build();
Window.builder(pane).setTitle("Minigame lobby").open(viewer);
Minigame lobby
Alice sees: Click to ready
Bob sees: Click to ready
ready[alice]false
ready[bob]false

Binding by a business key​

The key does not have to come from the player. An arena entrance button shows the desert arena's player count, and every viewer binds the same partition, "desert".

MutableKeyedSignal<String, Integer> arenaPlayers = KeyedSignal.of(arena -> 0);

Item desertButton = Item.builder()
.dependsOn(arenaPlayers, context -> "desert") // every viewer binds the desert partition
.setItemProvider(context -> {
String name = "Desert arena: " + arenaPlayers.get("desert") + " players";
return named(Material.SAND, name);
})
.build();

After game code calls arenaPlayers.update("desert", count -> count + 1), everyone looking at this button sees the new count; forest arena changes leave it alone.

Caveats​

Do not capture the viewer from menu creation

In shared Providers and click handlers, use context.player() for the viewer and click.player() for the clicker. Capturing a viewer from menu creation makes everyone see that one player's data.

keyOf runs at mount time

The key function runs once when the item is mounted in a slot. If a player switches teams mid-session, the function would now return another key, but slots already mounted do not rebind. To follow a selection, use switching.

private static ItemStack named(Material material, String name) {
ItemStack stack = new ItemStack(material);
stack.setData(DataComponentTypes.CUSTOM_NAME,
Component.text(name).decoration(TextDecoration.ITALIC, false)
);
return stack;
}

It uses Paper's DataComponentTypes with Adventure's Component and TextDecoration, matching Item rendering.

NextList contentsMap a list that grows and shrinks onto a group of slots.