Toasts, sounds, chat, placeholders and game assets
Ways for a page to reach into the game itself: sounds and hiding the vanilla HUD were added in WebGUI 2.6.0, the rest in 2.1.0. Against an older mod each of them is a no-op (or a rejected promise), so a page can use them and still run on a server that has not updated.
The examples use @webgui/client 1.10.0. Every function is also on window.webgui for pages without the library.
Toasts
A toast in the corner of the screen, drawn like an advancement: the same frame, an item for an icon, a title and a line below it.
import { showToast } from '@webgui/client'
showToast({
title: 'Purchase complete',
description: 'Diamond sword x1',
icon: 'minecraft:diamond_sword',
})| Field | Type | Notes |
|---|---|---|
title | string | Required. Yellow, one line. |
description | string | Optional second line, white. |
icon | string | An item id. Unknown ids show no icon. |
duration | number | Milliseconds, 1000–30000. Default 5000, as long as an advancement. |
A toast belongs to the game, not to the page: it stays up after the page closes, so a shop can close on a purchase and still confirm it. A page may show five every ten seconds; the rest are dropped.
The server can show one without any page open:
WebviewApi.showToast(player, "Quest completed", "Talk to the blacksmith", "minecraft:emerald");/webgui toast @a "Server restart" in 5 minutesIt travels as the page event webgui:toast, so no new network channel is involved. A client older than 2.1.0 does not show it; it passes it to an open page as an ordinary event.
Sounds
One of the game's own sounds, played the way the game plays its menu sounds: not positional, and under the player's Master volume, which an <audio> element ignores. No sound files to ship. Mod 2.6.0 and newer.
import { playSound } from '@webgui/client'
button.onclick = () => playSound('minecraft:ui.button.click', { volume: 0.25 })
playSound('minecraft:entity.player.levelup')| Option | Type | Notes |
|---|---|---|
volume | number | 0–1, default 1. The game's own buttons click at 0.25. |
pitch | number | 0.5–2, default 1. |
The id is a sound event id, as /playsound takes it. Any sound the game knows works, including those a server resource pack adds; an unknown id is ignored. A page may play twenty a second; the rest are dropped. The sound is played by the client and never reaches the server. React and Vue also export usePlaySound().
Hiding the vanilla HUD
A page that draws its own hotbar, health bar, boss bar or sidebar can hide the vanilla one it replaces. Mod 2.6.0 and newer.
import { hideVanilla } from '@webgui/client'
hideVanilla(['hotbar', 'health', 'effects'])
hideVanilla([]) // everything back| Element | What it hides |
|---|---|
hotbar | The nine slots and the offhand slot. |
health | Hearts, armor, food and air. |
effects | The effect icons in the top right. |
crosshair | The crosshair. |
bossbar | Every boss bar. |
sidebar | The scoreboard sidebar. |
title | The title and subtitle. Not on 1.20.1. |
actionbar | The action bar text above the hotbar. Not on 1.20.1. |
Each call replaces the last. The elements stay hidden only while the page that asked is on screen: a HUD page's choice while the HUD is shown, a GUI's while it is open. When the page closes, navigates or reloads they come back on their own, so a player is never left without a hotbar because of a page that is gone. Unknown names are ignored. The experience bar is not in the list.
React and Vue have useHideVanilla(elements), which hides them while the component is mounted. The data to draw them yourself is in the client state.
Chat
Every chat message the player receives, and a way to say something as the player.
import { onChat, sendChat } from '@webgui/client'
const off = onChat((message) => {
console.log(message.type, message.sender?.name, message.text)
})
sendChat('gg')Each message is a WebGUIChatMessage:
| Field | Type | Notes |
|---|---|---|
text | string | The message as plain text, e.g. <Steve> hi. |
json | object | The same message in the game's JSON text format, with its colours and formatting. |
type | 'chat' | 'system' | chat for a player's message, system for everything else, /say from the console included. |
sender | { uuid, name? } | Present for a player's message. |
The action bar is not chat and is not delivered.
Trusted origins only
Both directions work only for pages from an origin in trustedCommandOrigins. A player's chat includes their private messages, and it is not for just any page. From another origin no webgui:chat events arrive and sendChat is refused.
sendChat goes through the game's ordinary path, so the message is signed like one the player typed. It refuses anything starting with / — commands go through runCommand and its own check — and allows five messages every ten seconds.
Placeholders
When the server runs Text Placeholder API (Fabric), any %namespace:name% in a page URL is filled in for the player the page opens for, and URL-encoded:
/webgui gui @p https://shop.example/?rank=%luckperms:prefix%&balance=%economy:balance%That applies to every URL the server opens: /webgui gui, /webgui hud, bound entities, and the URLs in the config. Percent-encoding already in a URL (%20) is left alone. Without the mod, URLs are used as they are.
A page can also ask for values:
import { resolvePlaceholders } from '@webgui/client'
const values = await resolvePlaceholders(['player:playtime', 'server:online'])
// { 'player:playtime': '3h 12m', 'server:online': null }The server answers only for ids it lists in pagePlaceholders; any other id comes back null. The promise rejects when the server has no Text Placeholder API or does not answer within five seconds.
On Paper
The Paper plugin does the same through PlaceholderAPI, with its own placeholders: %player_name% rather than %namespace:name%, in URLs and in resolvePlaceholders alike.
Game assets
The game's own resources — item and block textures, GUI sprites, language files — as the player's resource packs have them.
import { gameAsset } from '@webgui/client'
img.src = gameAsset('minecraft:textures/item/diamond.png')gameAsset returns null outside the mod. Without the library, append namespace/path to window.webgui.gameAssets.
- Served by the mod over loopback, behind a token made once per game run, so pages in the main menu can use them too.
- Read-only, and only what a resource id can name: lower-case letters, digits and
/._-. - A resource pack the player switches to changes what comes back; nothing is cached.
Item icons here are the flat sprites. Blocks drawn in 3D in the inventory (chests, stairs) have no single image to serve.
Game font
The game's own font, as a font the page can use. No font file to ship, and no licence to worry about.
import { loadGameFont } from '@webgui/client'
const family = await loadGameFont() // 'Minecraft', or null outside the mod
document.body.style.fontFamily = family ? `${family}, sans-serif` : 'sans-serif'Without the library, link the stylesheet the mod provides and use the family it declares:
<link rel="stylesheet" id="game-font">
<script>document.getElementById('game-font').href = window.webgui.gameFont</script>
<style>body { font-family: Minecraft, sans-serif; }</style>- Built from the game itself. The mod reads the same bitmap sheets the game draws text from, through the player's resource packs, and turns every pixel into a square of a TrueType font. A pack that redraws the font changes this one too.
- Crisp at multiples of 8px. A pixel of the font is an eighth of the font size, so at
16px,24pxor32pxevery square lands on whole pixels. Other sizes work, softer. - The game's spacing. Each character advances exactly as far as it does in game.
- Latin, accented Latin, Cyrillic, Greek and the rest of the game's bitmap sheets. Characters the game draws with its fallback unifont (CJK and the like) come from the browser's fallback font instead.
- There is one weight.
font-weight: boldmakes the browser thicken the glyphs, which is not quite how the game draws bold.