Search by

presentkim / closure-form-utils

PresentKim

Closure based form classes for axolotl-pm/PocketMine-MP

Package info

github.com/presentkim-pm/closure-form-utils

pkg:composer/presentkim/closure-form-utils

Statistics

Installs: 102

Dependents: 0

Suggesters: 0

Stars: 2

Open Issues: 0

2.1.0 2026-12-02 02:44 UTC

This package is auto-updated.

Last update: 2026-12-02 02:48:15 UTC


README

Packagist CI Stars License


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) and BaseConfirmForm (confirm / cancel)
  • Safe : answers from a tampered client (wrong type, button index out of range) throw FormValidationException, which Player::onFormSubmit() logs, instead of an uncaught TypeError / 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.44 branch (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 / $onConfirm as fn(Player $player).
  • Closing the form calls $onClose / $onCancel when given, otherwise nothing happens. For BaseConfirmForm both 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-Closure button handler throws InvalidArgumentException when the form is created.

๐Ÿงช Development

composer install --ignore-platform-req='ext-*'
composer test

๐Ÿ“„ License

Distributed under the MIT License. See LICENSE for more information.