Created
May 27, 2020 20:26
-
-
Save PJZ9n/f32a1b3696e4a5cba7adb5b6f0ab62a4 to your computer and use it in GitHub Desktop.
ClosureTaskとGeneratorを使用したタイマー
This file contains hidden or 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
| <?php | |
| declare(strict_types=1); | |
| use pocketmine\plugin\Plugin; | |
| use pocketmine\scheduler\ClosureTask; | |
| use pocketmine\scheduler\TaskHandler; | |
| use Generator; | |
| $generator = (function (int $second): Generator { | |
| for ($i = $second; $i >= 0; $i--) { | |
| yield; | |
| if ($i > 0) { | |
| $this->getLogger()->info("残り " . $i . " 秒"); | |
| } else { | |
| $this->getLogger()->info("カウント終了!"); | |
| } | |
| } | |
| })(5); | |
| $task = new ClosureTask(function (int $currentTick) use (&$handler, $generator): void { | |
| /** @var TaskHandler $handler */ | |
| /** @var Generator $generator */ | |
| if ($generator->valid()) { | |
| $generator->next(); | |
| } else { | |
| $handler->cancel(); | |
| } | |
| }); | |
| /** @var Plugin $this */ | |
| $handler = $this->getScheduler()->scheduleRepeatingTask($task, 20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment