├── .poggit.yml ├── LICENSE ├── README.md ├── ScheduleAPI ├── plugin.yml └── src │ └── skh6075 │ └── ScheduleAPI │ └── ScheduleAPI.php └── poggit.yml /.poggit.yml: -------------------------------------------------------------------------------- 1 | --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/GodVas/ScheduleAPI 2 | build-by-default: true 3 | branches: 4 | - master 5 | projects: 6 | ScheduleAPI: 7 | path: ScheduleAPI/ 8 | ... 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Song ki ho 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ScheduleAPI 2 | PocketMine-MP Make it easy for developers to use scheduler task 3 | 4 | ### How use? 5 | 6 | ```php 7 | use skh6075\ScheduleAPI\ScheduleAPI; 8 | ``` 9 | 10 | ### ScheduleAPI DelayedTask 11 | ```php 12 | $main = $this; 13 | 14 | ScheduleAPI::delayedTask (function () use ($main) { 15 | $main->getLogger ()->info ('Alert in 2 seconds'); 16 | }, 20 * 2); 17 | ``` 18 | 19 | ### ScheduleAPI RepeatingTask 20 | ```php 21 | $main = $this; 22 | 23 | ScheduleAPI::repeatingTask (function () use ($main) { 24 | $main->getLogger ()->info ('two-second repetition'); 25 | }, 20 * 2); 26 | ``` 27 | 28 | ### ScheduleAPI DelayedRepeatingTask 29 | ```php 30 | $main = $this; 31 | $delayTicks = 20 * 2; 32 | $ticks = 20 * 2; 33 | 34 | ScheduleAPI::delayedRepeatingTask (function () use ($main) { 35 | $main->getLogger ()->info ('repeat delayed two-second'); 36 | }, $delayTicks, $ticks); 37 | ``` 38 | 39 | ### ScheduleAPI Function cancelTask 40 | ```php 41 | $taskHandler = ScheduleAPI::repeatingTask (function () { 42 | var_dump ('hello world'); 43 | }, 20 * 2); 44 | ScheduleAPI::cancelTask ($taskHandler->getTaskId ()); 45 | ``` 46 | -------------------------------------------------------------------------------- /ScheduleAPI/plugin.yml: -------------------------------------------------------------------------------- 1 | name: ScheduleAPI 2 | main: skh6075\ScheduleAPI\ScheduleAPI 3 | author: AvasKr 4 | version: 1.0.0 5 | api: 3.16.0 6 | website: https://github.com/GodVas 7 | load: STARTUP -------------------------------------------------------------------------------- /ScheduleAPI/src/skh6075/ScheduleAPI/ScheduleAPI.php: -------------------------------------------------------------------------------- 1 | callback = $callback; 34 | } 35 | 36 | public function onRun (int $currentTick): void{ 37 | $callback = $this->callback; 38 | $callback(); 39 | } 40 | }; 41 | } 42 | 43 | /** 44 | * @param callable $callback 45 | * @param int $ticks 46 | * @return TaskHandler 47 | */ 48 | public static function delayedTask (callable $callback, int $ticks): TaskHandler{ 49 | $handler = self::$i->getScheduler ()->scheduleDelayedTask (($task = self::taskHandler ($callback)), $ticks); 50 | self::$tasks [$task->getTaskId ()] = $handler; 51 | return $handler; 52 | } 53 | 54 | /** 55 | * @param callable $callback 56 | * @param int $ticks 57 | * @return TaskHandler 58 | */ 59 | public static function repeatingTask (callable $callback, int $ticks): TaskHandler{ 60 | $handler = self::$i->getScheduler ()->scheduleRepeatingTask (($task = self::taskHandler ($callback)), $ticks); 61 | self::$tasks [$task->getTaskId ()] = $handler; 62 | return $handler; 63 | } 64 | 65 | /** 66 | * @param callable $callback 67 | * @param int $delay 68 | * @param int $ticks 69 | * @return TaskHandler 70 | */ 71 | public static function delayedRepeatingTask (callable $callback, int $delay = 0, int $ticks): TaskHandler{ 72 | $handler = self::$i->getScheduler ()->scheduleDelayedRepeatingTask (($task = self::taskHandler ($callback)), $delay, $ticks); 73 | self::$tasks [$task->getTaskId ()] = $handler; 74 | return $handler; 75 | } 76 | 77 | /** 78 | * @param int $taskId 79 | * @return TaskHandler|null 80 | */ 81 | public static function getTaskHandler (int $taskId): ?TaskHandler{ 82 | return self::$tasks [$taskId] ?? null; 83 | } 84 | 85 | /** 86 | * @param int $taskId 87 | */ 88 | public static function cancelTask (int $taskId): void{ 89 | if (($handler = self::getTaskHandler ($taskId)) instanceof TaskHandler) { 90 | self::$i->getScheduler ()->cancelTask ($handler->getTaskId ()); 91 | } 92 | } 93 | 94 | } -------------------------------------------------------------------------------- /poggit.yml: -------------------------------------------------------------------------------- 1 | --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/GodVas/ScheduleAPI 2 | build-by-default: true 3 | branches: 4 | - master 5 | projects: 6 | ScheduleAPI: 7 | path: ScheduleAPI/ 8 | ... 9 | --------------------------------------------------------------------------------