Code Patterns
Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.
All
225
Command
60
Damage
2
Ecs
27
Entity
5
Gui
75
Interaction
2
Inventory
4
Moderation
3
Permission
2
Player
9
Plugin
4
Storage
2
Teleport
3
Ui
22
Util
5
← Back to Patterns
java
ui
ui-dynamic-values
Pattern for passing dynamic data from Java to UI elements using cmd.set(). Pass data through constructor, store as fields, then use cmd.set("#ElementId.Property", value) in build() method.
Example Code
// Pass data via constructor and display in UI
public class DashboardPage extends InteractiveCustomUIPage<DashboardPage.EventData> {
// Data passed via constructor
private final int playersOnline;
private final int questCount;
private final String uptime;
public static class EventData {
public static final BuilderCodec<EventData> CODEC =
BuilderCodec.builder(EventData.class, EventData::new).build();
}
public DashboardPage(
@Nonnull PlayerRef playerRef,
int playersOnline,
int questCount,
String uptime
) {
super(playerRef, CustomPageLifetime.CanDismissOrCloseThroughInteraction, EventData.CODEC);
this.playersOnline = playersOnline;
this.questCount = questCount;
this.uptime = uptime;
}
@Override
public void build(
@Nonnull Ref<EntityStore> ref,
@Nonnull UICommandBuilder cmd,
@Nonnull UIEventBuilder evt,
@Nonnull Store<EntityStore> store
) {
cmd.append("Pages/Dashboard.ui");
// Set dynamic values using cmd.set()
// Pattern: cmd.set("#ElementId.Property", stringValue)
// IMPORTANT: Numbers must be converted with String.valueOf()
cmd.set("#Stat1Value.Text", String.valueOf(playersOnline));
cmd.set("#Stat2Value.Text", String.valueOf(questCount));
cmd.set("#Stat3Value.Text", uptime);
evt.addEventBinding(CustomUIEventBindingType.Activating, "#CloseButton");
}
@Override
public void handleDataEvent(
@Nonnull Ref<EntityStore> ref,
@Nonnull Store<EntityStore> store,
@Nonnull EventData data
) {
Player player = store.getComponent(ref, Player.getComponentType());
player.getPageManager().setPage(ref, store, Page.None);
}
}
// Opening the page with data:
int players = server.getOnlinePlayers().size();
int quests = questManager.getActiveCount();
String uptime = formatUptime(System.currentTimeMillis() - startTime);
player.getPageManager().setPage(ref, store,
new DashboardPage(playerRef, players, quests, uptime));
Thread Safety
Data is captured at page creation time - for live updates consider periodic refresh
Common Mistakes
Passing integers directly to cmd.set() - always use String.valueOf() for numbers