1+ package com.redmagic.undefinedapi.scheduler
2+
3+ import com.redmagic.undefinedapi.UndefinedAPI
4+ import org.bukkit.scheduler.BukkitRunnable
5+ import org.bukkit.scheduler.BukkitTask
6+
7+ fun sync (runnable : BukkitRunnable .() -> Unit ): BukkitTask = createRunnable(runnable).runTask(UndefinedAPI .plugin)
8+ fun async (runnable : BukkitRunnable .() -> Unit ): BukkitTask = createRunnable(runnable).runTaskAsynchronously(UndefinedAPI .plugin)
9+
10+ fun delay (ticks : Int , unit : TimeUnit = TimeUnit .TICKS , async : Boolean = false, runnable : BukkitRunnable .() -> Unit ):BukkitTask {
11+ return if (async){
12+ createRunnable(runnable).runTaskLater(UndefinedAPI .plugin, unit.toTicks(ticks.toLong()))
13+ }else {
14+ createRunnable(runnable).runTaskLater(UndefinedAPI .plugin, unit.toTicks(ticks.toLong()))
15+ }
16+ }
17+
18+ fun delay (ticks : Int = 1, runnable : BukkitRunnable .() -> Unit ): BukkitTask = delay(ticks, TimeUnit .TICKS , false , runnable)
19+ fun delay (ticks : Int = 1, async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = delay(ticks, TimeUnit .TICKS , async, runnable)
20+
21+
22+ fun repeatingTask (delay : Int , period : Int , times : Int = -1, unit : TimeUnit = TimeUnit .TICKS , async : Boolean = false, runnable : BukkitRunnable .() -> Unit ): BukkitTask {
23+ return if (async){
24+ createRunnable(times, runnable).runTaskTimerAsynchronously(UndefinedAPI .plugin, unit.toTicks(delay.toLong()), unit.toTicks(period.toLong()))
25+ }else {
26+ createRunnable(times ,runnable).runTaskTimer(UndefinedAPI .plugin, unit.toTicks(delay.toLong()), unit.toTicks(period.toLong()))
27+ }
28+ }
29+ fun repeatingTask (ticks : Int = 1, runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(0 , ticks, - 1 , TimeUnit .TICKS , false , runnable)
30+ fun repeatingTask (ticks : Int = 1, times : Int = -1, runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(0 , ticks, times, TimeUnit .TICKS , false , runnable)
31+ fun repeatingTask (periodTicks : Int = 1, async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(periodTicks, periodTicks, - 1 , TimeUnit .TICKS , async, runnable)
32+ fun repeatingTask (periodTicks : Int = 1, async : Boolean , times : Int = -1, runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(periodTicks, periodTicks, times, TimeUnit .TICKS , async, runnable)
33+ fun repeatingTask (period : Int , unit : TimeUnit , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(period, period, - 1 , unit, false , runnable)
34+ fun repeatingTask (period : Int , unit : TimeUnit , times : Int = -1, runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(period, period, times, unit, false , runnable)
35+ fun repeatingTask (period : Int , unit : TimeUnit , async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(period, period, - 1 , unit, async, runnable)
36+ fun repeatingTask (period : Int , unit : TimeUnit , times : Int = -1, async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(period, period, times, unit, async, runnable)
37+ fun repeatingTask (delayTicks : Int , periodTicks : Int , async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(delayTicks, periodTicks, - 1 , TimeUnit .TICKS , async, runnable)
38+ fun repeatingTask (delayTicks : Int , periodTicks : Int , times : Int = -1, async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(delayTicks, periodTicks, times, TimeUnit .TICKS , async, runnable)
39+ fun repeatingTask (delay : Int , period : Int , unit : TimeUnit , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(delay, period, - 1 , unit, false , runnable)
40+ fun repeatingTask (delay : Int , period : Int , times : Int = -1, unit : TimeUnit , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(delay, period, times, unit, false , runnable)
41+
42+ private fun createRunnable (runnable : BukkitRunnable .() -> Unit ): BukkitRunnable {
43+ return object : BukkitRunnable (){
44+ override fun run () {
45+ runnable()
46+ }
47+ }
48+ }
49+
50+ private fun createRunnable (times : Int = -1, runnable : BukkitRunnable .() -> Unit ): BukkitRunnable {
51+ var amount = 0
52+ return object : BukkitRunnable (){
53+ override fun run () {
54+ runnable()
55+ if (times == - 1 ) return
56+ amount++
57+ if (amount >= times) {
58+ cancel()
59+ }
60+ }
61+ }
62+ }
0 commit comments