Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Created June 3, 2020 13:07
Show Gist options
  • Save PJZ9n/c00b8bfb92e5fb422a0841e0b1c2d46c to your computer and use it in GitHub Desktop.
Save PJZ9n/c00b8bfb92e5fb422a0841e0b1c2d46c to your computer and use it in GitHub Desktop.
<?php
/**
* @license GNU Lesser General Public License v2.1
* @author FlowyProject(https://github.com/FlowyProject/)
*/
declare(strict_types=1);
namespace PJZ9n\Example;
use pocketmine\event\Event;
use pocketmine\scheduler\Task;
use pocketmine\scheduler\TaskScheduler;
use function flowy\listen;
final class Delay
{
public static function load(): void
{
//NOOP
}
}
class DelayCallbackEvent extends Event
{
/** @var int */
protected $id;
public function __construct(int $id)
{
$this->id = $id;
}
public function getDelayId(): int
{
return $this->id;
}
}
class DelayTask extends Task
{
protected static $currentId = 0;
/** @var int */
protected $id;
protected function __construct(int $id)
{
$this->id = $id;
}
public function getDelayId(): int
{
return $this->id;
}
public function onRun(int $currentTick)
{
(new DelayCallbackEvent($this->id))->call();
}
public static function create(): DelayTask
{
return new DelayTask(self::$currentId++);
}
}
function delay(TaskScheduler $scheduler, int $tick)
{
$task = DelayTask::create();
$scheduler->scheduleDelayedTask($task, $tick);
yield listen(DelayCallbackEvent::class)->filter(function ($ev) use ($task) {
/** @var DelayCallbackEvent $ev */
return $ev->getDelayId() === $task->getDelayId();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment