8
0

More than 3 years have passed since last update.

お前らのFormの作り方は間違っている

Last updated at Posted at 2019-09-25

こちらで作成例を公開しているので、こちらを見るだけで十分だと思います。
https://github.com/deceitya/FormExample
読んでも分からない方は、以下の申し訳程度の解説をご覧になってください。


ほんへ

function onReceive(DataPacketReceiveEvent $event) {
    // なんやかんや
    switch ($formId) {
        case 23487:
            // IDが23487のフォームの処理
            break;
        case 437923:
            // IDが437923のフォームの処理
            break;
        case 82694:
            // IDが82964のフォームの処理
            break;
        // この先もひたすら続く...
    }
}

僕はもうこんな感じのフォームの処理の書き方にうんざりしているんです。予め断っておくと、僕は見なければならない状況にありました。もう見たくないのでスッキリする書き方の例を紹介します。初心者の方は日を改めた方が良いかもしれません。

メリット・デメリット

メリットだらけです。

  • フォームをIDで管理しなくてよくなる
  • コードの見た目がスッキリする

デメリットは無いです。恐らく。

ModalForm

ModalForm.php
<?php

use pocketmine\form\Form;
use pocketmine\Player;

class ModalForm implements Form
{
    public function handleResponse(Player $player, $data): void
    {
        // ここにフォームの処理を書きます。

        $player->sendMessage($data ?
            '§e真 §fを選びました。' :
            '§e偽 §fを選びました。');
    }

    public function jsonSerialize()
    {
        // 表示するフォームを配列で書き、返り値として返します。

        return [
            'type' => 'modal',
            'title' => 'ModalForm',
            'content' => 'この文(の真偽)は偽である',
            'button1' => '真',
            'button2' => '偽'
        ];
    }
}

handleResponseの引数の$dataにはboolが入っています。button1を選ぶとtrue、button2を選ぶとfalseとなります。
確認は取れていませんが、スマホで1個戻るボタン(?)を押してFormをキャンセルとnullが入るそうです。(2020/04/12 追記)

SimpleForm

SimpleForm.php
<?php

use pocketmine\form\Form;
use pocketmine\Player;

class SimpleForm implements Form
{
    public function handleResponse(Player $player, $data): void
    {
        if ($data === null) {
            return;
        }

        $buttons = ['みかん', 'りんご', '暗黒物質'];
        $player->sendMessage("§e{$buttons[$data]} §fを選びました。");
    }

    public function jsonSerialize()
    {
        return [
            'type' => 'form',
            'title' => 'SimpleForm',
            'content' => '好きなものを選んでね',
            'buttons' => [
                [
                    'text' => 'みかん',
                    'image' => [
                        'type' => 'url',
                        'data' => 'https://4.bp.blogspot.com/-PYqNTVOP9hs/UgSMR6zIbdI/AAAAAAAAXAk/tDJMCfemfwk/s800/fruit_orange.png'
                    ]
                ],
                [
                    'text' => 'りんご',
                    'image' => [
                        'type' => 'url',
                        'data' => 'https://2.bp.blogspot.com/-oTqVMb3zbQ4/UgSMNNLY2wI/AAAAAAAAW-o/4nxDWnz7YsQ/s800/fruit_apple.png'
                    ]
                ],
                [
                    'text' => '暗黒物質'
                ]
            ]
        ];
    }
}

$dataには整数型(int)が入ってます。みかんなら0、りんごなら1、暗黒物質なら2といった具合で、選んだもののキーとなります。

CustomForm

CustomForm.php
<?php

use pocketmine\form\Form;
use pocketmine\Player;

class CustomForm implements Form
{
    public function handleResponse(Player $player, $data): void
    {
        if ($data === null) {
            return;
        }

        $shobon = ['(´・ω・`)', '(`・ω・´)', '|ω・`)'];
        $steps = ['1', '10', '100', '千', '万'];
        $bool = $data[5] ? "オン" : "オフ";
        $player->sendMessage(
            "選んだ顔は§e {$shobon[$data[0]]} §fです。\n".
            "入力した文字は§e {$data[1]} §fです。\n".
            "なめらかで指定した数値は§e {$data[3]} §fです。\n".
            "区切ったやつで指定したのは§e {$steps[$data[4]]} §fです。\n".
            "スイッチを§e {$bool} §fにしました。"
        );
    }

    public function jsonSerialize()
    {
        return [
            'type' => 'custom_form',
            'title' => 'CustomForm',
            'content' => [
                [
                    'type' => 'dropdown',
                    'text' => '選択肢的な',
                    'options' => [
                        '(´・ω・`)',
                        '(`・ω・´)',
                        '|ω・`)'
                    ],
                    'default' => 1
                ],
                [
                    'type' => 'input',
                    'text' => '入力するやつ',
                    'placeholder' => '後ろにうっすらと表示される文字',
                    'default' => '最初から入力されてる文字'
                ],
                [
                    'type' => 'label',
                    'text' => '文字を表示するだけ'
                ],
                [
                    'type' => 'slider',
                    'text' => 'なめらかに動くやつ(語彙力来い)',
                    'min' => 0,
                    'max' => 100,
                    'default' => 50
                ],
                [
                    'type' => 'step_slider',
                    'text' => '上の区切られたverみたいな',
                    'steps' => ['1', '10', '100', '千', '万'],
                    'default' => 2
                ],
                [
                    'type' => 'toggle',
                    'text' => 'onかoffかみたいな',
                    'default' => true
                ]
            ]
        ];
    }
}

$dataはCustomFormにあるそれぞれの要素(?)の値が配列にまとめられたものとなっています。
var_dumpして自分で確かめたほうが分かると思います。

Formの出し方

// use文
use pocketmine\Player;
use pocketmine\form\Form;

Player::sendForm(Form);

これで終わりです。簡単な例で確認してみましょう。

Main.php
<?php

use pocketmine\plugin\PluginBase;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\Player;

class Main extends PluginBase
{
    public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool
    {
        if ($label !== 'formex') {
            return false;
        }

        if (!($sender instanceof Player)) {
            return true;
        }

        $sender->sendForm(new SimpleForm());
        return true;
    }
}

/formexと打つと先ほどのSimpleFormが表示されます。

あとがき

フォームはこれで作って下さい。お願いします。

8
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
0