Skip to content
PRISM
Dashboard extensions

Dashboard extensions

Overview

A plugin can add pages to Prism's web dashboard: a link in the sidebar, pages drawn in the dashboard's own style from what the plugin describes, actions run from buttons and forms, pages that update live, and the plugin's own web pages in sandboxed frames. The dashboard draws everything in both themes and both languages, on phones as on screens.

Setting up

The API is io.prismmc.prism:prism-dashboard-api, built for Java 21. Its classes come with the Prism server: build against it (compileOnly, provided) and never shade it into your jar. Until Prism's Maven repository opens, install it from Prism's source code (buyers get it on Discord, on request):

./gradlew :prism-dashboard-api:publishToMavenLocal

Gradle (Kotlin):

repositories {
    mavenLocal()
    maven("https://repo.papermc.io/repository/maven-public/")
}

dependencies {
    compileOnly("io.papermc.paper:paper-api:<your Paper version>")
    compileOnly("io.prismmc.prism:prism-dashboard-api:1.1.0")
}

Maven:

<dependency>
    <groupId>io.prismmc.prism</groupId>
    <artifactId>prism-dashboard-api</artifactId>
    <version>1.1.0</version>
    <scope>provided</scope>
</dependency>

Prism threads its regions like Folia: a plugin says it is ready for it with folia-supported: true in its plugin.yml.

A plugin that also runs on other servers checks for Prism by name, and keeps everything that touches the API in a class of its own: on another server that class never loads, and nothing breaks.

public void onEnable() {
    if (onPrism()) {
        new ShopDashboard(this).register(); // the only class that imports io.prismmc.prism.dashboard
    }
}

private static boolean onPrism() {
    try {
        Class.forName("io.prismmc.prism.dashboard.Dashboard");
        return true;
    } catch (ClassNotFoundException elsewhere) {
        return false;
    }
}

Dashboard.version() gives the API version the server speaks (1.0); Dashboard.enabled() says whether the dashboard is switched on. An extension can be registered either way.

An extension and its pages

Extension extension = Dashboard.extension(plugin)   // from onEnable; goes away when the plugin is disabled
    .title("Shop").title("fr", "Boutique")          // the name in the sidebar
    .icon("cart")                                   // one of the dashboard's icons, or SVG path data
    .permission("shop.dashboard");                  // who may see it

extension.page("sales", page -> page
    .title("Sales").title("fr", "Ventes")
    .refreshEvery(Duration.ofSeconds(10))           // optional: drawn again while shown
    .render((view, viewer) -> {
        view.subtitle("Today's figures");
        view.stats(stats -> stats.add("Sales", shop.salesToday()).tone(Tone.GOOD));
    }));
  • The sidebar opens the first page; an extension with several pages shows them as tabs.
  • page.hidden() keeps a page out of the tabs: it opens from a button or after an action, as the detail of something, with parameters (viewer.param("id")).
  • page.render(...) is called for each dashboard, each time the page is drawn. It runs off the server's threads unless page.on(Execution.GLOBAL), and has 10 seconds: read what the plugin keeps in memory there, not a database.
  • The Viewer says who looks: name(), language() (en or fr, to write in it), may(permission), param(name).
  • Text is always shown as text, never read as HTML.

Icons by name: overview, regions, players, console, maintenance, config, pack, plug, server, clock, globe, send, backup, save, info, check, alert, restart, play, stop, chart, cart, shield, star, heart, chat, trash, edit, search, bell, coin, key, lock, link, list, eye, home, bolt, gift, user, flag, calendar, download, upload, arrowLeft, arrowRight, external. Any other line drawing goes as path data on a 24 by 24 grid: .icon("M4 6h16 M4 12h16 M4 18h10").

Blocks

A View is filled top to bottom. Blocks that hold others take a view of their own.

Block What it shows
title(text), subtitle(text) The page's title for this drawing, and the line under it
stats(stats -> stats.add(label, value)) Figures in tiles; a number counts up when it changes. Each takes .unit(), .sub(), .tone(), .decimals(), .spark(values)
card(title, content) A card with a title, holding blocks
columns(count, content) Blocks side by side, up to count (2 to 4) per row, stacked on phones
tabs(tabs -> tabs.tab(label, content)) Tabs; the open one is remembered
table(table -> ...) A table: .column(label), .row(), .search(), .sort(), .pages(rows), .empty(text)
chart(chart -> ...) Lines over time: .times(millis), .series(label, tone, values), .unit(), .min(), .max(), .height()
bars(bars -> bars.add(label, value)) Horizontal bars against the longest: a ranking, a share
facts(facts -> facts.add(label, value)) Pairs of a label and a value
progress(label, fraction[, tone]) A progress bar, 0 to 1
text(text[, tone]) A paragraph; Tone.NEUTRAL for a quieter one
notice(tone, text) A notice that stands out
buttons(buttons -> ...) A row of buttons
form(action, form -> ...) A form sent to an action
frame(file[, height]) One of the plugin's own web pages, among the blocks
empty(text) A quiet line saying there is nothing

A row of a table takes one cell per call: .text(text[, tone]), .number(value[, decimals]), .badge(text, tone), .player(uuid, name) (with their head), .time(millis) ("3 min ago"), .date(millis), .buttons(...). A column of numbers is aligned to the right.

Buttons: .action(label, action) runs an action, .open(label, page) opens a page of the extension, .link(label, url) opens an http(s) address in a new tab. Each takes .with(key, value) (sent with the action, or a parameter of the page opened), .tone(tone), .icon(icon), .confirm(question).

Fields of a form: .text, .number, .select (with .option(value, label)), .toggle, .range(name, label, min, max), .textarea, .player (suggests the players online), and .hidden(name, value). Each takes .value(), .placeholder(), .help(), .required(), .min(), .max(), .step(), .suggest(...), .length(max). A form ends with .submit(label[, tone]), and .confirm(question) asks first.

Tones: NEUTRAL, ACCENT (Prism's violet), GOOD, WARN, BAD.

When a page is drawn again, the new drawing morphs into the one on screen: what is being typed in a form stays, the open tab stays, a frame keeps its page, numbers count to their new value and new rows slide in.

Actions

extension.action("restock", context -> {
    String item = context.string("item", "");
    long amount = context.integer("amount", 1);
    if (!shop.has(item)) {
        return Result.error("No such item");
    }
    shop.restock(item, (int) amount);
    return Result.ok("Restocked " + amount + " " + item);
}).permission("shop.dashboard.restock");
  • An action runs on the global region thread, like a command from the console, unless .on(Execution.ASYNC). It has 30 seconds. The console logs who ran which action.

  • To change an entity or blocks, go to their region's thread: asyncAction returns a future, and the context gives entity(entity, task), region(location, task) and global(task):

    extension.asyncAction("heal", context -> {
        Player player = context.player("player");
        if (player == null) {
            return CompletableFuture.completedFuture(Result.error("Not online"));
        }
        return context.entity(player, () -> {
            player.setHealth(20);
            return Result.ok(player.getName() + " healed");
        });
    });
  • The values come from the form's fields, the button's .with(), or the view's script: string(name), integer(name, fallback), decimal(name, fallback), bool(name), has(name), input() for all of them, and player(name) for a player online by name or id. They come from a browser: check them as any input.

  • A Result says what follows: Result.ok(), Result.ok(message), Result.error(message) (the page stays), .refresh(false) (the page is drawn again by default), .navigate(page[, params]), and .data(object) for a view's script.

Live updates

  • extension.refresh(page) draws that page again in every dashboard showing it: call it where the change happens (an event, a command, another dashboard's action). Calls close together are drawn once. extension.refresh() does it for every page.
  • page.refreshEvery(duration) draws a page again at that pace while it is shown.
  • extension.push(channel, data) sends data to the plugin's own views listening on that channel, at once, without drawing anything again: chat lines, positions, a counter. Anything Gson writes as JSON, 256 KB at most. It reaches only the dashboards showing a page of the extension that their owner may see, and whose views listen on that channel: the server keeps who listens to what, not the browser.
  • extension.channel(channel, node) (1.1.0) asks a permission to receive a channel's data, on top of the extension's and the page's.
  • extension.viewers() says how many dashboards show the extension now: nothing to compute when it is 0.

Updates go through a WebSocket the dashboard keeps open, which comes back by itself when it drops.

The plugin's own web pages

For what blocks cannot draw, a page can be the plugin's own HTML, CSS and JavaScript:

extension.assets("dashboard");                       // the folder of the jar holding them (the default)
extension.page("map", page -> page.title("Live map").view("map.html"));  // a whole page
view.frame("widget.html");                           // or among the blocks, as tall as its content
view.frame("widget.html", 240);                      // or of a given height

While working on them, extension.assets(Path.of("/path/to/src/main/resources/dashboard")) serves them from disk, read again at each request: a change shows on the next reload.

Prism gives each HTML file the dashboard's styles and its script, before the file's own: the classes of the dashboard (card, card-head, stack, grid, btn, btn-primary, btn-quiet, btn-danger, input, select, textarea, field, field-label, field-help, table, badge with data-tone, muted, label, chips, chip...) and its colours (var(--text), var(--text-2), var(--surface), var(--line), var(--accent), var(--good), var(--warn), var(--bad)...) follow the theme by themselves. Relative addresses stay in the folder; module scripts work.

The page runs sandboxed, in an origin of its own: it cannot read the dashboard's session nor call its API. It talks to the dashboard through window.prism:

Call What it does
prism.ready A promise of {lang, theme, viewer, extension, page, params}
prism.call(action, input) Runs one of the plugin's actions; a promise of {ok, message, data}. A message shows as for a button
prism.on(channel, listener) Data the plugin pushes on a channel; returns how to stop listening
prism.toast(text, tone) A short message: good, warn or bad
prism.confirm(question, {text, confirm, danger}) A promise of true or false
prism.navigate(page, params) Opens another page of the extension
prism.refresh() The page around a frame draws again
prism.storage.get(key), prism.storage.set(key, value) Kept in this browser, for this extension
prism.onTheme(listener) The theme changed: dark or light
prism.lang, prism.theme, prism.viewer, prism.params What ready gave, kept up to date
prism.format.number(value, decimals), prism.format.date(millis) Written the way of the dashboard's language
<!doctype html>
<html>
<head><meta charset="utf-8"><title>Sales</title></head>
<body>
  <section class="card">
    <div class="card-head"><h2>Sales, live</h2><span class="badge" data-tone="good" id="count">0</span></div>
    <ol id="feed"></ol>
  </section>
  <script>
    prism.ready.then(async () => {
      const recent = await prism.call("recent-sales");
      recent.data.forEach(show);
      prism.on("sale", show);
    });
    function show(sale) {
      const line = document.createElement("li");
      line.textContent = sale.player + " bought " + sale.item;
      document.getElementById("feed").append(line);
      document.getElementById("count").textContent = prism.format.number(document.getElementById("feed").children.length);
    }
  </script>
</body>
</html>

A frame among blocks takes the height of its content; a whole page fills the screen. Neither is loaded again when the page around it is drawn again.

Permissions

  • extension.permission(node) is needed to see the extension at all. Without one, the extension is for those who may manage the server (prism.dashboard.manage).
  • page.permission(node) and action.permission(node) are needed on top of it. The dashboard shows an action's buttons to everyone who sees the page, and refuses the action to those without its permission: hide the buttons with viewer.may(node) when that reads better.
  • A page's permission covers what it shows, not the actions its buttons call: an action is called by its name, from any page. Tie an action to its page with action.page(id) (1.1.0): only those who see that page run it, and context.page() is always that page. Or give the action its own permission.
  • While the owner of a dashboard session is online, the server answers for their permissions, so a change shows at once. Otherwise the session answers with the permissions they had when they asked for their link, among those the extensions declare: the extension's, its pages', its actions', and those given to extension.permissions(...) for viewer.may checks.
  • A link asked from the console may do everything.

Security

  • Blocks are data: the dashboard writes their text as text, so a player's name or a chat line cannot run anything in it.
  • The plugin's web pages run sandboxed in an origin of their own, even opened on their own: they never see the session's token. Their messages reach only their own plugin's actions, with the session's permissions. Their address holds a key, secret and new at each start, which only signed-in dashboards get.
  • Actions only come from signed-in sessions with the permission; each is logged with who ran it. A failure of the plugin's code is logged at most once a minute for each page and action, and the dashboard says the plugin failed, without its details.
  • What protects a page protects its blocks only. For what a page triggers or receives live, use action.page(id) or action.permission(node), and extension.channel(channel, node): the request of an action names its page itself, and is refused when its session may not see that page.
  • A plugin's web pages may keep 100 keys and 512 KB in the browser (prism.storage), 100 KB per value.

Limits

What At most
Drawing a page 10 seconds
An action 30 seconds
Blocks in a page 2,000, nested 6 deep
Table cells in a page 100,000; 10,000 rows per table
Points of a chart or a sparkline 5,000
A piece of text 20,000 characters
Data pushed at once 256 KB
Parameters of a page 20, of 200 characters

The example plugin

examples/dashboard-example is a plugin that uses all of it: figures with sparklines, a broadcast form, a chart, a frame that beats with the chat, a table of players with actions run on each player's thread, bars, notes written from a form with a page for each, and a live chat page of its own fed by a channel. Build it with ./gradlew :prism-dashboard-example:jar and drop the jar in plugins/.