Form API
New Form API plugin for PocketMine-MP +4.0.0
Report Bug
About this project
There are many Form API plugins available for PocketMine-MP, however, I didn't find one that really suit my needs! so I created this one, Which you can make your UI forms with easy / new methods (I hope so), There is something that you should know before using this API :
- This FormAPI uses Muqsit's FormImageFix, So if you have this plugin in your server, You need to delete it first!
- This plugin has a special method of use and it's not like most other published Form API plugins, So please read Usage carefully.
Installation
You need to download PHAR of the plugin in here, After downloading put .PHAR plugin to your plugins folder and start your server, Done!
Usage
Basically before using this API, You should know php, You need to create your own php class, I made this one for myself :
declare(strict_types=1);
namespace TestForm;
class MyClass
{
}
Now follow your ui type example from below table for your next steps :
Quick Access Table
Modal Form
Follow these steps to make a UI modal form:
- extend your class to form\ModalForm :
class MyClass extends form\ModalForm
{
}
- You need to define title and content functions (If you don't, You'll get Internal server error) :
class MyClass extends form\ModalForm
{
/* This will be the title of your form */
protected function title(): string
{
return 'The title of your form';
}
/* This will be the content of your form */
protected function content(): string
{
return '+ The content of your form';
}
}
- Now you need to define first and second button of your form. (Yes/No buttons)
class MyClass extends form\ModalForm
{
/* This will be the title of your form */
protected function title(): string
{
return 'Title of your form';
}
/* This will be the content of your form */
protected function content(): string
{
return '+ The content of your form';
}
/* The first button of your form */
protected function firstButton(): string
{
return 'Yes';
}
/* The second button of your form */
protected function secondButton(): string
{
return 'No';
}
}
- Now you need to define these two functions for when player click yes / no buttons :
class MyClass extends form\ModalForm
{
/* This will be the title of your form */
protected function title(): string
{
return 'The title of form';
}
/* This will be the content of your form */
protected function content(): string
{
return '+ This is content of your form.';
}
/* 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
{
$player->sendMessage('You clicked on Yes button.');
}
/* When player clicks the second button OR closes the form, this function will be executed */
protected function onClickSecondButton(Player $player): void
{
$player->sendMessage('You clicked on No button.');
}
}
Now you're done! Your form is ready for sending to the player with :
$player->sendForm(new MyClass());
Simple Form
- This will work like ModalForms But instead of 2 buttons, You can define as many buttons as you want, So in this form, there is three new functions, buttons() for registering form buttons and onClickButtons() for when player clicks on form buttons and onClose() for when player closes the form.
class MyClass extends form\SimpleForm
{
/* This will be the title of your form */
protected function title(): string
{
return 'The title of form';
}
/* This will be the content of your form */
protected function content(): string
{
return '+ This is content of your form.';
}
/* With this function you can register your form buttons */
protected function buttons(): array
{
return [
'This is first button', # This button does not have image
'This is second button', # This button does not have image
'Url image button' => 'https://www.freeiconspng.com/uploads/poop-emoji-png---images-free-download-6.png', # This button have image, The image of this button is set with a URL, Your image Url must start with https or http
'Path image button' => 'textures/ui/checkboxFilledYellow', # This button have image, The image of this button is set with minecraft path, You can find paths in (https://github.com/KygekDev/default-textures/tree/master/textures)
'Not exist image' => 'textures/ui/steveJobsFace' # This is a wrong image that is not exist in minecraft path!
];
}
/* When player clicks on buttons, this function will be executed */
protected function onClickButton(Player $player, int $button): void
{
switch ($button) {
case 0:
$player->sendMessage('You clicked on button 1');
break;
case 1:
$player->sendMessage('You clicked on second button');
break;
case 2:
$player->sendMessage('This is button with url image');
break;
case 3:
$player->sendMessage('This button have path image');
break;
case 4:
$player->sendMessage('This image is not exist, You must write the correct path of your image!');
break;
}
}
/* When player closes the form, this function will be executed */
protected function onClose(Player $player): void
{
$player->sendMessage('You closed the form');
}
}
Now you're done! Your form is ready for sending to the player with :
$player->sendForm(new MyClass());
Custom Form
- This is like SimpleForms but with a little bit diffrent, In content() of these forms you need to add form entries (Like addLabel(), addDropdown(), addInput(), addSlider() and addStepSlider()) and Also Instead of onClickButton() you need onSubmit() function, Look at the code :
class MyClass extends form\SimpleForm
{
/* This will be the title of your form */
protected function title(): string
{
return 'The title of form';
}
/* Just add content in this method (Content like: Dropdown, Label, Input, Slider, StepSlider) */
protected function content(): void
{
$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);
}
/* When player closes the form, this function will be executed */
protected function onClose(Player $player): void
{
$player->sendMessage('You closed the form');
}
}
Now you're done! Your form is ready for sending to the player with :
$player->sendForm(new MyClass());