Search by

presentkim / libasynform

PresentKim

Provides form classes for await-generator for axolotl-pm/PocketMine-MP

Package info

github.com/presentkim-pm/libasynform

pkg:composer/presentkim/libasynform

Statistics

Installs: 76

Dependents: 0

Suggesters: 0

Stars: 4

Open Issues: 0

2.0.1 2026-12-01 15:14 UTC

This package is auto-updated.

Last update: 2026-12-01 15:16:10 UTC


README

Packagist CI Stars License


Logo

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, no Form subclass per menu
  • Typed answers : ModalForm gives bool, SimpleForm the button index, CustomForm an 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.44 branch (no longer maintained).

๐Ÿš€ Quick start

  1. Create the form with the static ::create() and fill it with the builder methods
  2. yield from $form->send($player) inside an Await::f2c() generator
  3. Handle the result. It is null when 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.