Code Patterns

Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.

← Back to Patterns
ui

ui-page-lifetime

CustomPageLifetime options control when and how a UI page can be closed. Choose based on whether the UI should be dismissible by ESC key, clicking outside, or only through explicit code.

Example Code

java
// CustomPageLifetime options:

// 1. CanDismissOrCloseThroughInteraction (most common)
// - Player can press ESC to close
// - Can be closed by button clicks (after handling event)
// - Good for: dialogs, settings, info panels
new MyPage(playerRef, CustomPageLifetime.CanDismissOrCloseThroughInteraction);

// 2. CanDismiss
// - Player can press ESC to close
// - Page stays open after interactions
// - Good for: persistent UI that should allow escape

// 3. CanCloseThroughInteraction
// - ESC does NOT close the page
// - Only closed through button click handlers
// - Good for: important confirmations, tutorials

// 4. None (or CannotDismissOrClose)
// - Cannot be closed by player at all
// - Must be closed programmatically
// - Good for: loading screens, forced dialogs

// Opening pages with different lifetimes:
public class ConfirmDialog extends InteractiveCustomUIPage<ConfirmDialog.Data> {

    public ConfirmDialog(@Nonnull PlayerRef playerRef) {
        // Cannot ESC out - must click a button
        super(playerRef, CustomPageLifetime.CanCloseThroughInteraction, Data.CODEC);
    }
}

public class SettingsPage extends InteractiveCustomUIPage<SettingsPage.Data> {

    public SettingsPage(@Nonnull PlayerRef playerRef) {
        // Can ESC or click to close
        super(playerRef, CustomPageLifetime.CanDismissOrCloseThroughInteraction, Data.CODEC);
    }
}

// Closing a page programmatically (works with any lifetime):
Player player = store.getComponent(ref, Player.getComponentType());
player.getPageManager().setPage(ref, store, Page.None);

Common Mistakes

Using CannotDismissOrClose without providing a way to close - player gets stuck in UI