-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96cba44
There was a problem hiding this comment.
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.