Skip to content
HighestDreams edited this page Apr 16, 2022 · 2 revisions

How to send forms to the player

$player->sendForm(new YourFormClass());

How to add images to the forms

You can only add images to the simple form buttons, The image can be url OR path like below:

protected function buttons(): array
    {
        return [
            'Button with no image',
            'Url image button' => 'https://www.freeiconspng.com/uploads/poop-emoji-png---images-free-download-6.png',
            'Path image button' => 'textures/ui/checkboxFilledYellow' # Other paths: https://github.com/KygekDev/default-textures/tree/master/textures
        ];
    }

Button images are not working?

If the images are not loading, just use below code in your onEnable() or onLoad() functions in your plugin.

FormHandler::fixImages($this); # To fix images

Also don't forget to use:

use form\FormHandler;

Modal Form Template

Functions (title(), content(), firstButton(), secondButton()) are so important, Which means if you do not define them in your FormClass, You'll get an error.

<?php

declare(strict_types=1);

namespace YourName\Space;

use form\ModalForm;
use pocketmine\player\Player;

class YourClass extends ModalForm
{
    /* This will be the title of your form */
    protected function title(): string
    {
        return 'The title';
    }

    /* This will be the content of your form */
    protected function content(): string
    {
        return '+ The content.';
    }

    /* The first button of your form */
    protected function firstButton(): string
    {
        return 'Yes';
    }

    /* The second button of your form */
    protected function secondButton(): string
    {
        return 'No';
    }

    /* When player clicks the first button, this function will be executed */
    protected function onClickFirstButton(Player $player): void
    {
        // Your code goes here
    }

    /* When player clicks the second button OR closes the form, this function will be executed */
    protected function onClickSecondButton(Player $player): void
    {
        // Your code goes here
    }
}

Simple Form Template

Functions (title(), content(), buttons()) are so important, Which means if you do not define them in your FormClass, You'll get an error.

<?php

declare(strict_types=1);

namespace YourName\Space;

use form\SimpleForm;
use pocketmine\player\Player;

class YourClass extends SimpleForm
{
    /* This will be the title of your form */
    protected function title(): string
    {
        return 'The title';
    }

    /* This will be the content of your form */
    protected function content(): string
    {
        return '+ The content';
    }

    /* With this function you can register your form buttons */
    protected function buttons(): array
    {
        return ['Button 1', 'Button2'];
    }

    /* When player clicks on buttons, this function will be executed */
    protected function onClickButton(Player $player, int $button): void
    {
        switch ($button) {
            case 0:
                // You code goes here....
                break;
            case 1:
                // You code goes here...
                break;
        }
    }

    /* When player closes the form, this function will be executed */
    protected function onClose(Player $player): void
    {
        // Your code goes here...
    }
}

Custom Form Template

Functions (title(), content()) are so important, Which means if you do not define them in your FormClass, You'll get an error.

<?php

declare(strict_types=1);

namespace YourName\Space;

use form\CustomForm;
use pocketmine\player\Player;

class YourClass extends CustomForm
{
    protected function title(): string
    {
        return 'The title';
    }

    /* Add content with this method (Content like: Dropdown, Label, Input, Slider, StepSlider) */
    protected function content(): void
    {
        // Like: 
        // $this->addLabel('+ Did you just learbed ?');
        // $this->addInput('Your answer :', 'Write your answer here...', 'Yes, Tysm :).');
        // You can also define Dropdown, StepSlider, Slider here!
    }

    /* When player clicks the submit button, this function will be executed */
    protected function onSubmit(Player $player, array $data): void
    {
        /* $data[0] is the first thing we added to our content (Here is label) So the second thing we added is input, We can get result of our input with below variable : */
        // $input = $data[1];
        // $player->sendMessage('Your answer is : ' . $input);
    }

    protected function onClose(Player $player): void
    {
        $player->sendMessage('You closed the form');
    }
}