FormAPI icon indicating copy to clipboard operation
FormAPI copied to clipboard

SimpleForm: Allow mixed value on labels

Open IvanCraft623 opened this issue 3 years ago • 0 comments

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.

IvanCraft623 avatar Jul 13 '22 17:07 IvanCraft623