From 5db96e203f830ab53df08a5c1903fc1fbd4b6734 Mon Sep 17 00:00:00 2001 From: Nguyen Tan Phuong <37770098+Phuongaz@users.noreply.github.com> Date: Fri, 22 Sep 2023 13:01:48 +0700 Subject: [PATCH] implement AsyncForm --- composer.json | 2 + src/thebigcrafter/Hydrogen/Hydrogen.php | 1 + src/thebigcrafter/Hydrogen/form/AsyncForm.php | 71 +++++++++++++++++++ .../Hydrogen/tasks/CheckUpdatesTask.php | 1 - .../Hydrogen/utils/ArrayUtils.php | 34 +++++---- .../Hydrogen/utils/StringConverter.php | 6 +- 6 files changed, 92 insertions(+), 23 deletions(-) create mode 100644 src/thebigcrafter/Hydrogen/form/AsyncForm.php diff --git a/composer.json b/composer.json index d8a7e0f..d338533 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,8 @@ "type": "library", "require": { "php": "^8.1", + "dktapps/pmforms": "dev-master", + "sof3/await-generator": "^3.6", "pocketmine/pocketmine-mp": "^5.0" }, "require-dev": { diff --git a/src/thebigcrafter/Hydrogen/Hydrogen.php b/src/thebigcrafter/Hydrogen/Hydrogen.php index fb134d9..bc88747 100644 --- a/src/thebigcrafter/Hydrogen/Hydrogen.php +++ b/src/thebigcrafter/Hydrogen/Hydrogen.php @@ -16,6 +16,7 @@ use thebigcrafter\Hydrogen\tasks\CheckUpdatesTask; class Hydrogen { + /** * Notify if an update is available on Poggit. */ diff --git a/src/thebigcrafter/Hydrogen/form/AsyncForm.php b/src/thebigcrafter/Hydrogen/form/AsyncForm.php new file mode 100644 index 0000000..83f89da --- /dev/null +++ b/src/thebigcrafter/Hydrogen/form/AsyncForm.php @@ -0,0 +1,71 @@ + + * This source file is subject to the Apache-2.0 license that is bundled + * with this source code in the file LICENSE. + */ + +declare(strict_types=1); + +namespace thebigcrafter\Hydrogen\form; + +use dktapps\pmforms\CustomForm; +use dktapps\pmforms\CustomFormResponse; +use dktapps\pmforms\element\CustomFormElement; +use dktapps\pmforms\MenuForm; +use dktapps\pmforms\MenuOption; +use dktapps\pmforms\ModalForm; +use Generator; +use pocketmine\player\Player; +use SOFe\AwaitGenerator\Await; + +final class AsyncForm { + + /** + * @param CustomFormElement[] $elements + */ + public static function custom(Player $player, string $title, array $elements) : Generator { + $f = yield Await::RESOLVE; + $player->sendForm(new CustomForm( + $title, $elements, + function (Player $player, CustomFormResponse $result) use ($f) : void { + $f($result); + }, + function (Player $player) use ($f) : void { + $f(null); + } + )); + return yield Await::ONCE; + } + + /** + * @param MenuOption[] $options + */ + public static function menu(Player $player, string $title, string $text, array $options) : Generator { + $f = yield Await::RESOLVE; + $player->sendForm(new MenuForm( + $title, $text, $options, + function (Player $player, int $selectedOption) use ($f) : void { + $f($selectedOption); + }, + function (Player $player) use ($f) : void { + $f(null); + } + )); + return yield Await::ONCE; + } + + public static function modal(Player $player, string $title, string $text, string $yesButtonText = "gui.yes", string $noButtonText = "gui.no") : Generator { + $f = yield Await::RESOLVE; + $player->sendForm(new ModalForm( + $title, $text, + function (Player $player, bool $choice) use ($f) : void { + $f($choice); + }, + $yesButtonText, $noButtonText + )); + return yield Await::ONCE; + } +} diff --git a/src/thebigcrafter/Hydrogen/tasks/CheckUpdatesTask.php b/src/thebigcrafter/Hydrogen/tasks/CheckUpdatesTask.php index ea77039..155d82e 100644 --- a/src/thebigcrafter/Hydrogen/tasks/CheckUpdatesTask.php +++ b/src/thebigcrafter/Hydrogen/tasks/CheckUpdatesTask.php @@ -65,7 +65,6 @@ public function onCompletion() : void { if ($highestVersion !== $this->version) { $artifactUrl .= "/{$this->name}_{$highestVersion}.phar"; $logger->notice("{$this->name} v{$highestVersion} is available for download at {$artifactUrl}"); - return; } } } diff --git a/src/thebigcrafter/Hydrogen/utils/ArrayUtils.php b/src/thebigcrafter/Hydrogen/utils/ArrayUtils.php index 31f76a2..1be8345 100644 --- a/src/thebigcrafter/Hydrogen/utils/ArrayUtils.php +++ b/src/thebigcrafter/Hydrogen/utils/ArrayUtils.php @@ -1,4 +1,4 @@ - $arr - */ - public static function binarySearch(array $arr, string $target, int $left, int $right) : int - { - if ($left <= $right) { - $middle = (int) floor(($left + $right) / 2); + */ + public static function binarySearch(array $arr, string $target, int $left, int $right) : int { + if ($left <= $right) { + $middle = (int) floor(($left + $right) / 2); $result = strcmp($arr[$middle], $target); - if ($result === 0) { - return $middle; - } elseif ($result < 0) { - return self::binarySearch($arr, $target, $middle + 1, $right); - } else { - return self::binarySearch($arr, $target, $left, $middle - 1); - } + if ($result === 0) { + return $middle; + } elseif ($result < 0) { + return self::binarySearch($arr, $target, $middle + 1, $right); + } else { + return self::binarySearch($arr, $target, $left, $middle - 1); + } } - return -1; - } + return -1; + } } diff --git a/src/thebigcrafter/Hydrogen/utils/StringConverter.php b/src/thebigcrafter/Hydrogen/utils/StringConverter.php index d056c3f..543cd7a 100644 --- a/src/thebigcrafter/Hydrogen/utils/StringConverter.php +++ b/src/thebigcrafter/Hydrogen/utils/StringConverter.php @@ -21,8 +21,7 @@ class StringConverter { /** * @throws HException */ - public static function stringToItem(int $id, int $meta) : Item - { + public static function stringToItem(int $id, int $meta) : Item { $item = StringToItemParser::getInstance()->parse($id . ":" . $meta); if ($item === null) { @@ -39,8 +38,7 @@ public static function stringToItem(int $id, int $meta) : Item /** * @throws HException */ - public static function stringToBlock(int $id, int $meta) : Block - { + public static function stringToBlock(int $id, int $meta) : Block { $item = StringToItemParser::getInstance()->parse($id . ":" . $meta); if ($item === null) {