Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Last active April 22, 2020 23:30
Show Gist options
  • Save PJZ9n/d4304ba983563a74a91ebd29af142c9f to your computer and use it in GitHub Desktop.
Save PJZ9n/d4304ba983563a74a91ebd29af142c9f to your computer and use it in GitHub Desktop.
PocketMineでFormを扱うサンプル。動作未確認。
<?php
declare(strict_types=1);
class selectForm implements \pocketmine\form\Form
{
public function handleResponse(\pocketmine\Player $player, $data): void
{
if ($data === null) {
$player->sendMessage("フォームが閉じられました");
return;
}
switch ($data) {
case 0:
//名前の設定が押された
$player->sendForm(new nameSettingForm());
return;
}
//何も該当しなかった場合
throw new \pocketmine\form\FormValidationException();
}
public function jsonSerialize()
{
return [
"type" => "form",
"title" => "選択フォーム",
"content" => "選択してください",
"buttons" => [
[
"text" => "名前の設定",
],
],
];
}
}
class nameSettingForm implements \pocketmine\form\Form
{
public function handleResponse(\pocketmine\Player $player, $data): void
{
//保存したりする処理を書く
$player->sendForm(new successForm(new selectForm(), "名前の設定が完了しました!"));
}
public function jsonSerialize()
{
return [
"type" => "custom_form",
"title" => "名前設定フォーム",
"content" => [
[
"type" => "input",
"text" => "名前を入力",
],
],
];
}
}
class successForm implements \pocketmine\form\Form
{
/** @var \pocketmine\form\Form */
private $back;
/** @var string */
private $message;
public function __construct(\pocketmine\form\Form $back, string $message)
{
$this->back = $back;
$this->message = $message;
}
public function handleResponse(\pocketmine\Player $player, $data): void
{
if ($data === null) {
$player->sendMessage("フォームが閉じられました");
return;
}
if ($data === false) {
//戻るボタンが押された
$player->sendForm($this->back);
return;
} else if ($data === true) {
//閉じるボタンが押された
return;
}
throw new \pocketmine\form\FormValidationException();
}
public function jsonSerialize()
{
return [
"type" => "modal",
"title" => "完了しました",
"content" => $this->message,
"button1" => "戻る",
"button2" => "閉じる",
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment