Skip to content

Commit

Permalink
implement AsyncForm
Browse files Browse the repository at this point in the history
  • Loading branch information
Phuongaz committed Sep 22, 2023
1 parent 36c9b3a commit 5db96e2
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 23 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions src/thebigcrafter/Hydrogen/Hydrogen.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use thebigcrafter\Hydrogen\tasks\CheckUpdatesTask;

class Hydrogen {

/**
* Notify if an update is available on Poggit.
*/
Expand Down
71 changes: 71 additions & 0 deletions src/thebigcrafter/Hydrogen/form/AsyncForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of Hydrogen.
* (c) thebigcrafter <[email protected]>
* 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;
}
}
1 change: 0 additions & 1 deletion src/thebigcrafter/Hydrogen/tasks/CheckUpdatesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
34 changes: 16 additions & 18 deletions src/thebigcrafter/Hydrogen/utils/ArrayUtils.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php

/*
* This file is part of Hydrogen.
Expand All @@ -11,29 +11,27 @@

namespace thebigcrafter\Hydrogen\utils;

use function floor;
use function floor;
use function strcmp;

class ArrayUtils
{
class ArrayUtils {
/**
* @param array<string> $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;
}
}
6 changes: 2 additions & 4 deletions src/thebigcrafter/Hydrogen/utils/StringConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 5db96e2

Please sign in to comment.