Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Created June 15, 2020 11:33
Show Gist options
  • Select an option

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

Select an option

Save PJZ9n/9d12bd6a1cb86dac7a3ca58ea2c15cb2 to your computer and use it in GitHub Desktop.
<?php
/**
* @name ClosureTaskTest
* @version 1.0.0
* @main PJZ9n\ClosureTaskTest\ClosureTaskTest
* @api 3.12.0
*/
declare(strict_types=1);
namespace PJZ9n\ClosureTaskTest;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\ClosureTask;
use pocketmine\scheduler\TaskHandler;
class ClosureTaskTest extends PluginBase
{
public function onEnable(): void
{
echo $this->getName() . " Please select test (1-3) " . PHP_EOL;
$input = trim(fgets(STDIN));
switch ($input) {
case "1":
//Test 1: Backward compatibility
$this->getScheduler()->scheduleDelayedTask(new ClosureTask(function (int $currentTick): void {
$this->getLogger()->info("Test 1!");
}), 20);
break;
case "2":
//Test 2: New type
$this->getScheduler()->scheduleDelayedRepeatingTask(new ClosureTask(function (int $currentTick, TaskHandler $handler): void {
$this->getLogger()->info("Test 2!");
$handler->cancel();
}), 40, 20);
break;
case "3":
//Test 3: Pass an invalid closure
$this->getScheduler()->scheduleDelayedTask(new ClosureTask(function (string $currentTick): void {
$this->getLogger()->info("Test 3!");
}), 60);
break;
default:
echo "Unknown type." . PHP_EOL;
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment