Skip to content

Instantly share code, notes, and snippets.

@JackNoordhuis
Created February 15, 2019 12:24
Show Gist options
  • Save JackNoordhuis/aede93478c29e2f28d57b0a437dc5b76 to your computer and use it in GitHub Desktop.
Save JackNoordhuis/aede93478c29e2f28d57b0a437dc5b76 to your computer and use it in GitHub Desktop.
Some PocketMine-MP task examples
<?php
namespace jacknoordhuis\example;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\ClosureTask;
class RepeatingTaskExample extends PluginBase{
public function onEnable(){
$this->getScheduler()->scheduleRepeatingTask(new class extends \pocketmine\scheduler\Task{
public function onRun(int $currentTick) : void{
//some code here to repeat every 20 ticks (1 second)
}
}, 20);
}
}
class RepeatingClosureTaskExample extends PluginBase{
public function onEnable(){
$this->getScheduler()->scheduleRepeatingTask(new ClosureTask(
function(int $currentTick){
//some code here to repeat every 20 ticks (1 second)
}
), 20);
}
}
class DelayedTaskExample extends PluginBase{
public function onEnable(){
$this->getScheduler()->scheduleDelayedTask(new class extends \pocketmine\scheduler\Task{
public function onRun(int $currentTick) : void{
//some code here to run after 20 ticks (1 second)
}
}, 20);
}
}
class DelayedClosureTaskExample extends PluginBase{
public function onEnable(){
$this->getScheduler()->scheduleDelayedTask(new ClosureTask(
function(int $currentTick){
//some code here to run after 20 ticks (1 second)
}
), 20);
}
}
class InstantTaskExample extends PluginBase{
public function onEnable(){
$this->getScheduler()->scheduleTask(new class extends \pocketmine\scheduler\Task{
public function onRun(int $currentTick) : void{
//some code here to run on the next scheduler heartbeat (this tick or the next tick)
}
});
}
}
class InstantClosureTaskExample extends PluginBase{
public function onEnable(){
$this->getScheduler()->scheduleTask(new ClosureTask(
function(int $currentTick){
//some code here to run on the next scheduler heartbeat (this tick or the next tick)
}
));
}
}
class DelayedRepeatingTaskExample extends PluginBase{
public function onEnable(){
$this->getScheduler()->scheduleDelayedRepeatingTask(new class extends \pocketmine\scheduler\Task{
public function onRun(int $currentTick) : void{
//some code here to run after 20 ticks (1 second) have passed, every 20 ticks (1 second)
}
}, 20,20);
}
}
class DelayedRepeatingClosureTaskExample extends PluginBase{
public function onEnable(){
$this->getScheduler()->scheduleDelayedRepeatingTask(new ClosureTask(
function(int $currentTick){
//some code here to run after 20 ticks (1 second) have passed, every 20 ticks (1 second)
}
), 20, 20);
}
}
@xigma0512
Copy link

thx dude, this is so great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment