Skip to content

Instantly share code, notes, and snippets.

@Frago9876543210
Last active March 30, 2018 06:31
Show Gist options
  • Save Frago9876543210/6c75aac1ee565a4dd12b67e6926a9762 to your computer and use it in GitHub Desktop.
Save Frago9876543210/6c75aac1ee565a4dd12b67e6926a9762 to your computer and use it in GitHub Desktop.
simple form
<?php
namespace ui;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\item\Item;
use pocketmine\network\protocol\v120\ModalFormResponsePacket;
use pocketmine\network\protocol\v120\ShowModalFormPacket;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\TextFormat;
class main extends PluginBase implements Listener
{
public function onEnable()
{
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
public function onJoin(PlayerJoinEvent $e)
{
$i = $e->getPlayer()->getInventory();
$i->setItem(
0, Item::get(331, 0, 1)->setCustomName(TextFormat::AQUA . "Sign in")
);
$i->setItem(
1, Item::get(345, 0, 1)->setCustomName(TextFormat::GREEN . "Menu")
);
$i->setItem(
2, Item::get(347, 0, 1)->setCustomName(
TextFormat::GREEN . "Modal"
)
);
}
public function onTap(PlayerInteractEvent $e)
{
$player = $e->getPlayer();
switch ($e->getItem()->getId()) {
case 331:
$data = [
'type' => 'custom_form',
'title' => 'Sign in',
'content' => [
[
"type" => "label",
"text" => "Sign in to your account."
],
[
"type" => "input",
"text" => "login",
"placeholder" => "login",
"default" => ""
],
[
"type" => "input",
"text" => "password",
"placeholder" => "password",
"default" => ""
]
]
];
$this->createWindow($player, $data, 1);
break;
case 345:
$data = [
'type' => 'form',
'title' => 'menu',
'content' => 'text',
'buttons' => [
['text' => 'button #1'],
['text' => 'button #2']
]
];
$this->createWindow($player, $data, 2);
break;
case 347:
$data = [
'type' => 'modal',
'title' => "some question",
'content' => "Do you drink milk? XD",
'button1' => "yes",
'button2' => "no",
];
$this->createWindow($player, $data, 3);
break;
}
}
public function onPickItem(DataPacketReceiveEvent $e)
{
$p = $e->getPlayer();
$pk = $e->getPacket();
if ($pk instanceof ModalFormResponsePacket) {
$id = $pk->formId;
$data = $pk->data;
switch ($id) {
case 1:
$login = "admin";
$pass = "12345";
if ($data == '[null,"' . $login . '","' . $pass . '"]' . "\n") {
$p->sendMessage("success!");
} else {
$p->sendMessage("wrong login or password error!");
}
break;
case 2:
switch ($data) {
case 0:
$p->sendMessage("You clicked on button #1");
break;
case 1:
$p->sendMessage("You clicked on button #2");
break;
}
break;
case 3:
switch ($data) {
case "true\n":
$p->sendMessage("You chose \"yes\"");
break;
case "false\n":
$p->sendMessage("You chose \"no\"");
break;
}
break;
}
}
}
public function createWindow(Player $player, $data, int $id)
{
$pk = new ShowModalFormPacket();
$pk->formId = $id;
$pk->data = json_encode(
$data,
JSON_PRETTY_PRINT | JSON_BIGINT_AS_STRING | JSON_UNESCAPED_UNICODE
);
$player->dataPacket($pk);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment