presentkim / closure-form-utils
Closure based form classes for axolotl-pm/PocketMine-MP
Requires
- php: ^8.4
- pocketmine/pocketmine-mp: ^5.0
Requires (Dev)
- axolotl-pm/pocketmine-mp: ^5.51
- phpunit/phpunit: ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-02 02:48:15 UTC
README
closure-form-utils
Handle form answers with closures instead of writing a Form class per menu.
Packagist ยท Changelog ยท Report a bug ยท Request a feature
$player->sendForm(new BaseSelectForm("Menu", "Where to?", [ "Spawn" => fn(Player $player, int $index) => $player->teleport($spawn), "Shop" => fn(Player $player, int $index) => $shop->open($player), ]));
๐ Why closure-form-utils?
Every menu normally needs its own Form subclass just to route one answer. These two classes keep the handler next to
the code that opens the form.
- Closures : the handler is a plain
Closure, no subclass per menu - Three building blocks :
BaseSelectForm(a list of buttons),BaseInputForm(one text input) andBaseConfirmForm(confirm / cancel) - Safe : answers from a tampered client (wrong type, button index out of range) throw
FormValidationException, whichPlayer::onFormSubmit()logs, instead of an uncaughtTypeError/ undefined key - Tested : on PHP 8.4 / 8.5 against the real
Player::sendForm()/Player::onFormSubmit()
๐ค libasynform or closure-form-utils?
Both send the same forms; they differ in how you receive the answer.
| libasynform | closure-form-utils | |
|---|---|---|
| Style | yield from $form->send($player) inside Await::f2c() |
a Closure per button or for the input |
| Dependency | sof3/await-generator |
none |
| Forms | ModalForm, SimpleForm, CustomForm (any elements) |
BaseSelectForm (buttons), BaseInputForm (one text input), BaseConfirmForm (two buttons) |
| Fits | multi-step flows: ask, check, ask again, then act | one menu whose buttons each do one thing |
Pick libasynform when the plugin already uses await-generator or the flow has more than one form. Pick closure-form-utils for a simple menu in a plugin without await-generator. Both can live in one plugin.
๐ฆ Installation
composer require presentkim/closure-form-utils
| Requirement | Version |
|---|---|
| Server | axolotl-pm/PocketMine-MP (fork of PocketMine-MP 5) |
| PHP | 8.4 or newer |
The Poggit virion for the original PocketMine-MP 5.44 and older is kept on the
legacy/pmmp-5.44branch (no longer maintained).
๐ Quick start
use kim\present\utils\closureform\BaseConfirmForm; use kim\present\utils\closureform\BaseInputForm; use kim\present\utils\closureform\BaseSelectForm; $player->sendForm(new BaseSelectForm( "Main menu", "Pick one", [ "Spawn" => function(Player $player, int $index) : void{ /* index is 0 */ }, "Shop" => function(Player $player, int $index) : void{ /* index is 1 */ }, ], onClose: fn(Player $player) => $player->sendMessage("Bye") )); $player->sendForm(new BaseInputForm( "Rename", "New name", onSuccess: fn(Player $player, string $text) => $player->setDisplayName($text), onCancel: fn(Player $player) => $player->sendMessage("Cancelled"), placeholder: "Steve" )); $player->sendForm(BaseConfirmForm::create( "Delete island", "This cannot be undone.", "Delete", "Keep", onConfirm: fn(Player $player) => $player->sendMessage("Deleted"), onCancel: fn(Player $player) => $player->sendMessage("Cancelled"), dangerous: true // colors the confirm button red ));
๐ API reference
| Class | Description |
|---|---|
new BaseSelectForm(string $title, string $content, array $buttons, ?Closure $onClose) |
Button list. $buttons is button text => Closure |
new BaseInputForm(string $title, string $inputText, Closure $onSuccess, ?Closure $onCancel, string $placeholder, ?string $default) |
One input. $onSuccess gets the trimmed text |
BaseConfirmForm::create(string $title, string $content, string $confirmText, string $cancelText, Closure $onConfirm, ?Closure $onCancel, bool $dangerous) |
Two-button modal. $dangerous colors the confirm text red (ยงc) |
- Button handlers are called as
fn(Player $player, int $buttonIndex),$onClose/$onCancel/$onConfirmasfn(Player $player). - Closing the form calls
$onClose/$onCancelwhen given, otherwise nothing happens. ForBaseConfirmFormboth the cancel button and closing call$onCancel. - A response that does not fit the form (non-integer or out-of-range button index, non-string input, non-bool confirm answer) throws
pocketmine\form\FormValidationException. - A non-
Closurebutton handler throwsInvalidArgumentExceptionwhen the form is created.
๐งช Development
composer install --ignore-platform-req='ext-*' composer test
๐ License
Distributed under the MIT License. See LICENSE for more information.