presentkim / toast-utils
Show toast notifications in the top right corner of the screen to players, with an action bar fallback, for axolotl-pm/PocketMine-MP
Requires
- php: ^8.4
- pocketmine/pocketmine-mp: ^5.0
- presentkim/resourcepack-register: ^2.2
Requires (Dev)
- axolotl-pm/pocketmine-mp: ^5.51
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
toast-utils
Show a toast (icon, title and body) in the top right corner of a player's screen, with an action bar fallback.
Packagist · 한국어 · Changelog · Report a bug · Request a feature
protected function onEnable() : void{ ToastUtils::register($this, required: true); } Toast::send($player, "Quest complete", "Slay 10 zombies", ToastIcon::texture("textures/items/diamond_sword"));
🌟 Why toast-utils?
- One line :
Toast::send($player, $title, $body, $icon) - No files to copy : the resource pack comes with the library and is registered from your plugin (with resourcepack-register)
- Works with shaded copies : plugins bundle libraries under their own namespace, so a server can have many copies of this one. The pack is identified by its UUID and registered once, whichever copy calls first
- Falls back : players without the pack read
title: bodyon the action bar instead of a mangled title - Does not touch other packs :
hud_screen.jsonis changed withmodificationsonly, and every control and binding of the pack is namedpktoast... - Tested : the payload, the registration and the pack files (against the payload layout) on PHP 8.4 / 8.5, and checked with PHPStan level 9
📦 Installation
composer require presentkim/toast-utils
| Requirement | Version |
|---|---|
| Server | axolotl-pm/PocketMine-MP (fork of PocketMine-MP 5) |
| PHP | 8.4 or newer (with ext-zip) |
The pack lives in resources/pack/ of this library, so your build must keep that folder next to src/
(vendor/presentkim/toast-utils/resources/pack). If your build moves it, pass its path to ToastUtils::register().
🚀 Quick start
use kim\present\utils\toast\Toast; use kim\present\utils\toast\ToastIcon; use kim\present\utils\toast\ToastUtils; final class Main extends PluginBase{ protected function onEnable() : void{ // Registers the pack until the plugin is disabled, `required` makes players accept it to join ToastUtils::register($this, required: true); } public function onQuestComplete(Player $player) : void{ Toast::send($player, "Quest complete", "Slay 10 zombies", ToastIcon::texture("textures/items/diamond_sword")); Toast::send($player, "Welcome", "Have a nice stay"); // no icon } }
The toast slides in from the right, stays about 3 seconds and slides out. Register in onEnable(), before players
join: the pack is sent to the players that join afterwards.
🧭 Toast or action bar?
PocketMine has no way to ask whether a client applied a resource pack. A toast without the pack would show the raw
title text (PKTOAST:n~~~~...), so Toast::send() chooses for all players at once, with ToastMode:
| Mode | Toast when... | Otherwise |
|---|---|---|
ToastMode::AUTO |
the pack is registered and the server requires its packs | Action bar |
ToastMode::TOAST |
the pack is registered | Action bar |
ToastMode::ACTION_BAR |
never | Action bar |
AUTO(default) relies on the required flag of the server (ToastUtils::register($this, required: true), orforce_resources: trueinresource_packs.yml): a player must accept the packs to join, so every player has the toast pack. If the packs are optional, nobody can be trusted to have it and the action bar is used.TOASTis for servers that want toasts even though the packs are optional: players that declined the pack see the raw title text. Use it only if you accept that.- The action bar text is
title: bodyin one line, the icon is left out. Toast::setMode()sets the mode of the copy of the library your plugin bundles.Toast::isToastEnabled()tells whatsend()does now.Toast::sendToast()andToast::sendActionBar()skip the choice.- If the pack is not registered at all (the plugin is disabled, or
register()was never called),AUTOandTOASTuse the action bar.
📚 Examples
Choose the mode by config
Toast::setMode($this->getConfig()->get("toast", true) ? ToastMode::AUTO : ToastMode::ACTION_BAR);
Icons
An icon is the texture path of a resource pack the player has, without the extension. The vanilla ones work:
ToastIcon::texture("textures/items/diamond"); ToastIcon::texture("textures/ui/check"); Toast::send($player, "Title", "Body", "textures/items/apple"); // a string is accepted too
Texture paths use up to 64 letters, digits and _ - . /, anything else throws InvalidArgumentException. An icon
that no pack has is drawn as an empty box. Item textures are in textures/items/, block textures in textures/blocks/.
Several plugins, several copies
Every plugin calls ToastUtils::register() in its own onEnable(). The first call registers the pack and returns
true; the others find the pack by its UUID (ToastUtils::PACK_UUID), return false, and only turn required on if
they asked for it. If the plugin that registered the pack is disabled, the pack goes with it and toasts fall back to
the action bar until a plugin registers it again.
If two copies are different versions, the pack of the one that registered first is used.
Register the pack yourself
$pack = ToastUtils::createPack(); // built from resources/pack, not registered ResourcePackRegister::bind($this, $pack, required: true);
📖 API reference
| Method | Description |
|---|---|
Toast::send(Player $player, string $title, string $body, ToastIcon|string|null $icon = null) |
Toast or action bar, by the mode |
Toast::sendToast(...) / Toast::sendActionBar(Player, string $title, string $body) |
Force one of the two |
Toast::setMode(ToastMode) / getMode() |
AUTO (default), TOAST, ACTION_BAR |
Toast::isToastEnabled(?ResourcePackManager $manager = null) : bool |
Whether send() shows a toast now |
ToastIcon::texture(string $path) : ToastIcon |
An icon from a texture path |
ToastUtils::register(Plugin $plugin, bool $required = false, ?string $packDirectory = null) : bool |
Register the pack once (true if this call did), until the plugin is disabled |
ToastUtils::isRegistered() : bool |
Whether any plugin registered the pack |
ToastUtils::createPack(?string $packDirectory = null) : ResourcePack |
Build the pack without registering it |
🧩 How it works
The pack draws the toast from the title of the client. Toast::send() sends a title with fixed timings
(0 / 90 / 0 ticks) that starts with PKTOAST::
PKTOAST: i|n <icon, 64 bytes> <title, 96 bytes> <body>
i or n tells whether there is an icon; the icon and the title are padded with ~. Titles are cut to 96 bytes
(… is added) and the body to 200 bytes; a ~ in a title is replaced with ~.
resources/pack/
├─ manifest.json uuid f8ca3469-538f-457e-bac5-977cb85f949b, version 1.0.0
└─ ui/
├─ _ui_defs.json registers ui/pktoast.json
├─ hud_screen.json modifications only
└─ pktoast.json the toast (namespace pktoast) and its animation
hud_screen.jsonadds a binding to the vanillahud_title_textthat hides it when the title starts withPKTOAST:(so the vanilla title shows nothing), and a secondhud_title_text_factoryinroot_panelthat creates the toast. A title without the prefix only creates an invisible toast.- Every title creates a new control, so the animation starts from the beginning each time and the control destroys itself at the end.
- Every property name of the pack starts with
#pktoast_, so it does not react to the bindings of other packs.
Limits
- The title channel is shared: a new toast replaces the one that is showing, and a title sent by another plugin hides a toast. There is no queue, send the next toast later if you need them one after another.
- The toast sets the title timings of the player to 0 / 90 / 0 ticks. Titles that other plugins send without
timings (the default of
Player::sendTitle()) are shown with those until they set theirs. - Hiding the HUD (F1) hides the toast too.
🧪 Development
composer install --ignore-platform-req='ext-*' composer analyse composer test
📄 License
Distributed under the MIT License. See LICENSE for more information.