PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
TaskHandler.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\scheduler;
25
28
33 protected int $nextRun;
34
35 protected bool $cancelled = false;
36
37 private TimingsHandler $timings;
38
39 private string $taskName;
40 private string $ownerName;
41
45 public function __construct(
46 protected Task $task,
47 protected int $delay = -1,
48 protected int $period = -1,
49 ?string $ownerName = null
50 ){
51 if($task->getHandler() !== null){
52 throw new \InvalidArgumentException("Cannot assign multiple handlers to the same task");
53 }
54 $this->taskName = $task->getName();
55 $this->ownerName = $ownerName ?? "Unknown";
56 $this->timings = Timings::getScheduledTaskTimings($this, $period);
57 $this->task->setHandler($this);
58 }
59
60 public function isCancelled() : bool{
61 return $this->cancelled;
62 }
63
64 public function getNextRun() : int{
65 return $this->nextRun;
66 }
67
71 public function setNextRun(int $ticks) : void{
72 $this->nextRun = $ticks;
73 }
74
78 public function getTask() : Task{
79 return $this->task;
80 }
81
82 public function getDelay() : int{
83 return $this->delay;
84 }
85
86 public function isDelayed() : bool{
87 return $this->delay > 0;
88 }
89
90 public function isRepeating() : bool{
91 return $this->period > 0;
92 }
93
94 public function getPeriod() : int{
95 return $this->period;
96 }
97
98 public function cancel() : void{
99 try{
100 if(!$this->isCancelled()){
101 $this->task->onCancel();
102 }
103 }finally{
104 $this->remove();
105 }
106 }
107
111 public function remove() : void{
112 $this->cancelled = true;
113 $this->task->setHandler(null);
114 }
115
119 public function run() : void{
120 $this->timings->startTiming();
121 try{
122 $this->task->onRun();
123 }catch(CancelTaskException $e){
124 $this->cancel();
125 }finally{
126 $this->timings->stopTiming();
127 }
128 }
129
130 public function getTaskName() : string{
131 return $this->taskName;
132 }
133
134 public function getOwnerName() : string{
135 return $this->ownerName;
136 }
137}
__construct(protected Task $task, protected int $delay=-1, protected int $period=-1, ?string $ownerName=null)