Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Created May 27, 2020 20:26
Show Gist options
  • Select an option

  • Save PJZ9n/f32a1b3696e4a5cba7adb5b6f0ab62a4 to your computer and use it in GitHub Desktop.

Select an option

Save PJZ9n/f32a1b3696e4a5cba7adb5b6f0ab62a4 to your computer and use it in GitHub Desktop.
ClosureTaskとGeneratorを使用したタイマー
<?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