Skip to content

Commit

Permalink
Some base work for forms API
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Oct 6, 2017
1 parent d294d5a commit 96cba44
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/pocketmine/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
use pocketmine\event\TextContainer;
use pocketmine\event\Timings;
use pocketmine\event\TranslationContainer;
use pocketmine\forms\BaseForm;
use pocketmine\inventory\BigCraftingGrid;
use pocketmine\inventory\CraftingGrid;
use pocketmine\inventory\Inventory;
Expand Down Expand Up @@ -119,6 +120,7 @@
use pocketmine\network\mcpe\protocol\LoginPacket;
use pocketmine\network\mcpe\protocol\MapInfoRequestPacket;
use pocketmine\network\mcpe\protocol\MobEquipmentPacket;
use pocketmine\network\mcpe\protocol\ModalFormRequestPacket;
use pocketmine\network\mcpe\protocol\MovePlayerPacket;
use pocketmine\network\mcpe\protocol\PlayerActionPacket;
use pocketmine\network\mcpe\protocol\PlayerHotbarPacket;
Expand Down Expand Up @@ -296,6 +298,11 @@ public static function isValidUserName(string $name) : bool{
/** @var int|null */
protected $lineHeight = null;

/** @var int */
protected $formIdCounter = 0;
/** @var BaseForm[] */
protected $forms = [];

/**
* @return TranslationContainer|string
*/
Expand Down Expand Up @@ -3271,6 +3278,35 @@ public function sendWhisper(string $sender, string $message){
$this->dataPacket($pk);
}

public function sendForm(BaseForm $form) : void{
$id = $this->formIdCounter++;

$pk = new ModalFormRequestPacket();
$pk->formId = $id;
$pk->formData = json_encode($form);
$this->dataPacket($pk);

$this->forms[$id] = $form;
}

public function onFormSubmit(int $formId, $responseData) : bool{
$form = $this->forms[$formId] ?? null;

if($form === null){
return false;
}

try{
$form->handleResponse($this, $responseData);
}catch(\Throwable $e){
$this->server->getLogger()->logException($e);
}

unset($this->forms[$formId]);

return true;
}

/**
* Note for plugin developers: use kick() with the isAdmin
* flag set to kick without the "Kicked by admin" part instead of this method.
Expand Down
69 changes: 69 additions & 0 deletions src/pocketmine/forms/BaseForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\forms;

use pocketmine\network\mcpe\protocol\ModalFormRequestPacket;
use pocketmine\network\mcpe\protocol\ModalFormResponsePacket;
use pocketmine\Player;

abstract class BaseForm implements \JsonSerializable{

const TYPE_MODAL = "modal";
const TYPE_FORM = "form";
const TYPE_CUSTOM_FORM = "custom_form";

/**
* Returns the type used to show this form to clients
* @return string
*/
abstract public function getType() : string;

/**
* Handles a modal form response from a player
*
* @param Player $player
* @param mixed $packet
*/
abstract public function handleResponse(Player $player, $data) : void;

/**
* Serializes the form to JSON for sending to clients.
*
* @return array
*/
final public function jsonSerialize() : array{
$jsonBase = [
"type" => $this->getType()
];

return array_merge($jsonBase, $this->serializeFormData());
}

/**
* Serializes additional data needed to show this form to clients.
* @return array
*/
abstract protected function serializeFormData() : array;

}
86 changes: 86 additions & 0 deletions src/pocketmine/forms/ModalForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\forms;

use pocketmine\network\mcpe\protocol\ModalFormResponsePacket;
use pocketmine\Player;

/**
* This form type present a simple "yes/no" dialog with two buttons.
*/
abstract class ModalForm extends BaseForm{

/** @var string */
private $title;
/** @var string */
private $content;
/** @var string */
private $button1;
/** @var string */
private $button2;

/**
* @param string $title Text to put on the title of the dialog.
* @param string $text Text to put in the body.
* @param string $button1Text Text to show on the "Yes" button. Defaults to client-translated "Yes" string.
* @param string $button2Text Text to show on the "No" button. Defaults to client-translated "No" string.
*/
public function __construct(string $title, string $text, string $button1Text = "gui.yes", string $button2Text = "gui.no"){
$this->title = $title;
$this->content = $text;
$this->button1 = $button1Text;
$this->button2 = $button2Text;
}

public function getType() : string{
return self::TYPE_MODAL;
}

final public function handleResponse(Player $player, $data) : void{
if(!is_bool($data)){
throw new \UnexpectedValueException("Expected bool, got " . gettype($data));
}

$this->onSubmit($player, $data);
}

/**
* Called when a player submits this form. Plugins should extend the class and implement this method to handle form
* responses.
*
* @param Player $player The player who submitted the form.
* @param bool $responseValue True if the player clicked button1, false if button2.
*/
abstract public function onSubmit(Player $player, bool $responseValue) : void;

public function serializeFormData() : array{
return [
"title" => $this->title,
"content" => $this->content,
"button1" => $this->button1,
"button2" => $this->button2
];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function handlePlayerSkin(PlayerSkinPacket $packet) : bool{
}

public function handleModalFormResponse(ModalFormResponsePacket $packet) : bool{
return false; //TODO: GUI stuff
return $this->player->onFormSubmit($packet->formId, json_decode($packet->formData, true));
}

public function handleServerSettingsRequest(ServerSettingsRequestPacket $packet) : bool{
Expand Down

1 comment on commit 96cba44

@inxomnyaa
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the opportinity of static, cached forms again.

Please sign in to comment.