Repeating tasks must have a positive period (greater than 0).Incorrect:
// Period of 0 is invalid$scheduler->scheduleRepeatingTask($task, 0);// Negative period is invalid$scheduler->scheduleRepeatingTask($task, -10);
Correct:
// Repeat every second (20 ticks)$scheduler->scheduleRepeatingTask($task, 20);// Repeat every 5 seconds (100 ticks)$scheduler->scheduleRepeatingTask($task, 100);
Repeating tasks with periods less than 20 ticks (1 second) may impact performance.
This is an informational warning. Consider if such high frequency is necessary.
Potentially problematic:
// Runs every tick - very frequent!$scheduler->scheduleRepeatingTask($task, 1);// Every 5 ticks - still very frequent$scheduler->scheduleRepeatingTask($task, 5);
Better approach:
// Every second (20 ticks) - reasonable$scheduler->scheduleRepeatingTask($task, 20);// Every 3 seconds (60 ticks)$scheduler->scheduleRepeatingTask($task, 60);
class Main extends PluginBase { private ?TaskHandler $taskHandler = null; public function onEnable(): void { $task = new MyTask($this); $this->taskHandler = $this->getScheduler()->scheduleRepeatingTask($task, 20); } public function stopTask(): void { if ($this->taskHandler !== null) { $this->taskHandler->cancel(); } } public function onDisable(): void { // Cancel all tasks automatically on plugin disable }}
class AutoSaveTask extends Task { private Main $plugin; public function __construct(Main $plugin) { $this->plugin = $plugin; } public function onRun(): void { // Save player data $this->plugin->saveAllData(); // Log the save $this->plugin->getLogger()->info("Auto-saved player data"); }}// Start auto-save every 10 minutes$this->getScheduler()->scheduleRepeatingTask( new AutoSaveTask($this), 20 * 60 * 10);
class TeleportCountdownTask extends Task { private Player $player; private Position $destination; private int $countdown; public function __construct(Player $player, Position $dest, int $seconds) { $this->player = $player; $this->destination = $dest; $this->countdown = $seconds; } public function onRun(): void { if (!$this->player->isOnline()) { $this->getHandler()->cancel(); return; } $this->countdown--; if ($this->countdown <= 0) { $this->player->teleport($this->destination); $this->player->sendMessage("Teleported!"); $this->getHandler()->cancel(); } else { $this->player->sendMessage("Teleporting in {$this->countdown}..."); } }}// Usage: Teleport after 5 second countdown$task = new TeleportCountdownTask($player, $destination, 5);$this->getScheduler()->scheduleRepeatingTask($task, 20); // Every second