presentkim / libasynform
Provides form classes for await-generator for axolotl-pm/PocketMine-MP
Requires
- php: ^8.4
- pocketmine/pocketmine-mp: ^5.0
- sof3/await-generator: ^3.6.1
Requires (Dev)
- axolotl-pm/pocketmine-mp: ^5.51
- phpunit/phpunit: ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
libasynform
Form classes for await-generator: yield from the player's answer.
Packagist ยท Changelog ยท Report a bug ยท Request a feature
Await::f2c(function() use ($player) : \Generator{ $answer = yield from ModalForm::create("Reset?", "Really?", "Yes", "No")->send($player); if($answer === true){ /* ... */ } });
๐ Why libasynform?
Forms are callback based. A menu with a few steps turns into nested closures. With libasynform the whole flow is straight-line code.
- Linear :
$answer = yield from $form->send($player), no callbacks, noFormsubclass per menu - Typed answers :
ModalFormgivesbool,SimpleFormthe button index,CustomForman array keyed by your labels - Validated : a response that does not fit the form rejects the generator with
FormValidationException - Safe : sending to a disconnected player rejects instead of hanging, a player who quits resolves with
null, a form cannot be answered twice - 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) |
| 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/libasynform
This also installs sof3/await-generator.
| 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
- Create the form with the static
::create()and fill it with the builder methods yield from $form->send($player)inside anAwait::f2c()generator- Handle the result. It is
nullwhen the player closes the form
Don't call
Player::sendForm()with these forms yourself,send()is what connects the answer to your generator.
use kim\present\libasynform\CustomForm; use SOFe\AwaitGenerator\Await; Await::f2c(function() use ($player) : \Generator{ $receive = yield from CustomForm::create("OpenMenu") ->addToggle("I want open menu", true, "open") ->addToggle("I want close menu", false, "close") ->addDropdown("I want ", ["close menu", "open menu"], 1, "select") ->addInput("I want ", "what do you want", "open menu", "want") ->send($player); // $receive is null when the player closed the form, otherwise // ["open" => true, "close" => false, "select" => 1, "want" => "open menu"] });
๐ Examples
ModalForm : yes / no
Await::f2c(function() use ($player) : \Generator{ $yes = yield from ModalForm::create("Delete home", "Delete it for good?", "Delete", "Keep")->send($player); if($yes === true){ // button1 was pressed }elseif($yes === false){ // button2 was pressed }else{ // the form was closed } });
SimpleForm : a chain of menus
Await::f2c(function() use ($player) : \Generator{ $choice = yield from SimpleForm::create("Main menu", "Where to?") ->addButton("Spawn") ->addButton("Shop", SimpleForm::IMAGE_TYPE_PATH, "textures/items/emerald") ->send($player); if($choice === 1){ $amount = yield from CustomForm::create("Shop") ->addSlider("Amount", 1, 64, 1, 1, "amount") ->send($player); // $amount["amount"] ... } });
Labels, headers and dividers
Read-only elements (addLabel(), addHeader(), addDivider()) are not part of the answer.
Value elements are keyed by the label you give them, or by their 0-based index among the value elements.
$receive = yield from CustomForm::create("Profile") ->addHeader("Account") ->addInput("Name", "Steve", null, "name") ->addDivider() ->addToggle("Public", true) // key 1 ->send($player);
Server settings page
send($player, true) sends the form as a server settings response (the page in the client's settings menu)
instead of a normal form. Answer the client's ServerSettingsRequestPacket with it.
Handling errors and quitting players
try{ $answer = yield from $form->send($player); }catch(FormValidationException $e){ // invalid response, form already open for this player, or the form could not be sent }
When the player quits while the form is open, send() resolves with null like a closed form, so code waiting on
the answer always finishes. Check $player->isConnected() after a null answer if you need to tell the two apart.
๐ API reference
| Method | Description |
|---|---|
BaseForm::send(Player $player, bool $isServerSetting = false) : Generator |
Send the form, resolves with the processed answer or null |
ModalForm::create(title, content, button1, button2) |
Yes / no form, answer is bool |
SimpleForm::create(title, content) / addButton(text, ?imageType, imagePath) |
Button list, answer is the button index |
CustomForm::create(title) |
Form with elements, answer is an array keyed by label |
CustomForm::addToggle addSlider addStepSlider addDropdown addInput |
Value elements, the last argument is the label of the answer key |
CustomForm::addLabel addHeader addDivider |
Read-only elements, not part of the answer |
CustomForm::addContent(array $content, Closure $validator, ?string $label) |
Custom value element with your own validator |
CustomForm::setIcon(string $type, string $data) |
Icon shown next to the title |
๐งช Development
composer install --ignore-platform-req='ext-*' composer test
๐ License
Distributed under the MIT License. See LICENSE for more information.