FormAPI
FormAPI copied to clipboard
SimpleForm: Allow mixed value on labels
Introduction
This pull request will remove the limit for plugin developers to only set strings as labels.
Justification
In some cases, for example when the button references an object, it is necessary to make hacky or inefficient code to achieve the goal.
Example: A teleport form
Before:
$form = new SimpleForm(function (Player $player, ?string $result = null) {
if ($result !== null) {
$pl = $player->getServer()->getPlayerExact($result);
if ($pl !== null) {
$player->teleport($pl->getPosition());
}
}
});
$form->setTitle("Teleporter");
$form->setContent("Teleport you to another player position");
foreach ($player->getWorld()->getPlayers() as $pl) {
$form->addButton($pl->getName(), -1, "", $pl->getName());
}
$form->sendToPlayer($player);
After:
$form = new SimpleForm(function (Player $player, ?Player $result = null) {
if ($result !== null) {
$player->teleport($result->getPosition());
}
});
$form->setTitle("Teleporter");
$form->setContent("Teleport you to another player position");
foreach ($player->getWorld()->getPlayers() as $pl) {
$form->addButton($pl->getName(), -1, "", $pl);
}
$form->sendToPlayer($player);
Backwards compatibility
This is technically BC Break as it requires at least PHP8, but since PM4 already requires PHP8 as a minimum there should be no problems.