@@ -4,9 +4,30 @@ import com.redmagic.undefinedapi.UndefinedAPI
44import org.bukkit.scheduler.BukkitRunnable
55import org.bukkit.scheduler.BukkitTask
66
7+ /* *
8+ * Schedules a synchronous task that will be executed by the Bukkit server.
9+ *
10+ * @param runnable the code to be executed synchronously
11+ * @return the BukkitTask that represents the scheduled task
12+ */
713fun sync (runnable : BukkitRunnable .() -> Unit ): BukkitTask = createRunnable(runnable).runTask(UndefinedAPI .plugin)
14+ /* *
15+ * Executes the given [runnable] asynchronously on a separate thread.
16+ *
17+ * @param runnable the code to be executed asynchronously.
18+ * @return the [BukkitTask] associated with the scheduled task.
19+ */
820fun async (runnable : BukkitRunnable .() -> Unit ): BukkitTask = createRunnable(runnable).runTaskAsynchronously(UndefinedAPI .plugin)
921
22+ /* *
23+ * Delays the execution of a given runnable task for a specified number of ticks.
24+ *
25+ * @param ticks The number of ticks to delay the execution of the task.
26+ * @param unit The time unit to use for the delay (default is TimeUnit.TICKS).
27+ * @param async Determines whether the task should be executed asynchronously (default is false).
28+ * @param runnable The runnable task to be executed after the delay.
29+ * @return The BukkitTask representing the delayed task.
30+ */
1031fun delay (ticks : Int , unit : TimeUnit = TimeUnit .TICKS , async : Boolean = false, runnable : BukkitRunnable .() -> Unit ):BukkitTask {
1132 return if (async){
1233 createRunnable(runnable).runTaskLater(UndefinedAPI .plugin, unit.toTicks(ticks.toLong()))
@@ -15,30 +36,177 @@ fun delay(ticks: Int, unit: TimeUnit = TimeUnit.TICKS, async: Boolean = false, r
1536 }
1637}
1738
39+ /* *
40+ * Delays the execution of the specified [runnable] by the specified number of [ticks].
41+ *
42+ * @param ticks The number of ticks to delay the execution by. Default is 1.
43+ * @param runnable The lambda function representing the code to be executed after the delay.
44+ * @return The scheduled BukkitTask that can be used to cancel the delayed execution if needed.
45+ */
1846fun delay (ticks : Int = 1, runnable : BukkitRunnable .() -> Unit ): BukkitTask = delay(ticks, TimeUnit .TICKS , false , runnable)
47+ /* *
48+ * Delays the execution of the specified function by the given number of ticks.
49+ *
50+ * @param ticks The number of ticks to delay the execution by (default is 1).
51+ * @param async Whether to execute the function asynchronously (default is false).
52+ * @param runnable The function to delay.
53+ * @return The Bukkit task representing the delayed execution.
54+ */
1955fun delay (ticks : Int = 1, async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = delay(ticks, TimeUnit .TICKS , async, runnable)
2056
2157
58+ /* *
59+ * Schedules a repeating task with the given delay, period, and other optional parameters.
60+ *
61+ * @param delay The delay before the task starts, in the specified time unit.
62+ * @param period The time between each execution of the task, in the specified time unit.
63+ * @param times The number of times the task should repeat. Use -1 for indefinitely. Defaults to -1.
64+ * @param unit The time unit used for the delay and period. Defaults to TimeUnit.TICKS.
65+ * @param async Specifies whether to run the task asynchronously or synchronously. Defaults to false.
66+ * @param runnable The code to be executed by the task.
67+ *
68+ * @return The BukkitTask representing the scheduled repeating task.
69+ */
2270fun repeatingTask (delay : Int , period : Int , times : Int = -1, unit : TimeUnit = TimeUnit .TICKS , async : Boolean = false, runnable : BukkitRunnable .() -> Unit ): BukkitTask {
2371 return if (async){
2472 createRunnable(times, runnable).runTaskTimerAsynchronously(UndefinedAPI .plugin, unit.toTicks(delay.toLong()), unit.toTicks(period.toLong()))
2573 }else {
2674 createRunnable(times ,runnable).runTaskTimer(UndefinedAPI .plugin, unit.toTicks(delay.toLong()), unit.toTicks(period.toLong()))
2775 }
2876}
77+ /* *
78+ * Executes a repeating task with the given ticks interval.
79+ *
80+ * @param ticks the number of ticks between each execution of the task (default is 1 tick)
81+ * @param runnable the code to be executed by the task
82+ * @return the BukkitTask representing the scheduled repeating task
83+ */
2984fun repeatingTask (ticks : Int = 1, runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(0 , ticks, - 1 , TimeUnit .TICKS , false , runnable)
85+ /* *
86+ * Schedules a repeating task with the specified options.
87+ *
88+ * @param ticks the delay before the task starts, in ticks (default is 1)
89+ * @param times the number of times the task should repeat (-1 for indefinitely, default is -1)
90+ * @param runnable the code to be executed repeatedly
91+ * @return the BukkitTask representing the scheduled repeating task
92+ */
3093fun repeatingTask (ticks : Int = 1, times : Int = -1, runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(0 , ticks, times, TimeUnit .TICKS , false , runnable)
94+ /* *
95+ * Schedules a repeating task to be executed with a specified delay between each execution.
96+ *
97+ * @param periodTicks The delay between each execution in ticks (default is 1).
98+ * @param async Specifies whether to run the task asynchronously or synchronously.
99+ * @param runnable The code to be executed in the task, defined as an extension function on [BukkitRunnable].
100+ * @return The scheduled BukkitTask instance representing the task.
101+ */
31102fun repeatingTask (periodTicks : Int = 1, async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(periodTicks, periodTicks, - 1 , TimeUnit .TICKS , async, runnable)
103+ /* *
104+ * Executes a repeating task with the specified parameters.
105+ *
106+ * @param periodTicks The number of ticks between each execution.
107+ * @param async Whether the task should be executed asynchronously.
108+ * @param times The number of times the task should be executed. Use -1 for unlimited executions.
109+ * @param runnable The code to be executed by the task.
110+ *
111+ * @return The BukkitTask representing the scheduled repeating task.
112+ */
32113fun repeatingTask (periodTicks : Int = 1, async : Boolean , times : Int = -1, runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(periodTicks, periodTicks, times, TimeUnit .TICKS , async, runnable)
114+ /* *
115+ * Schedules a repeating task with the specified period and unit.
116+ *
117+ * @param period the time between each execution of the task
118+ * @param unit the unit of time used for the period
119+ * @param runnable the code to be executed by the task
120+ * @return the BukkitTask representing the scheduled task
121+ */
33122fun repeatingTask (period : Int , unit : TimeUnit , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(period, period, - 1 , unit, false , runnable)
123+ /* *
124+ * Executes a repeating task with the specified period and times.
125+ *
126+ * @param period The time between each execution of the task.
127+ * @param unit The time unit of the period.
128+ * @param times The number of times the task should repeat. Defaults to -1 which means the task will repeat indefinitely.
129+ * @param runnable The code to be executed by the task.
130+ *
131+ * @return The BukkitTask representing the repeating task.
132+ */
34133fun repeatingTask (period : Int , unit : TimeUnit , times : Int = -1, runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(period, period, times, unit, false , runnable)
134+ /* *
135+ * Schedule a repeating task with the specified period and unit.
136+ *
137+ * @param period the time between each execution of the task
138+ * @param unit the time unit of the period value
139+ * @param async whether to run the task asynchronously or not
140+ * @param runnable the code to be executed by the task
141+ * @return the BukkitTask representing the scheduled task
142+ */
35143fun repeatingTask (period : Int , unit : TimeUnit , async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(period, period, - 1 , unit, async, runnable)
144+ /* *
145+ * Executes a repeating task asynchronously or synchronously in a Bukkit server.
146+ *
147+ * @param period The period between each task execution.
148+ * @param unit The time unit of the period.
149+ * @param times The number of times the task should be executed.
150+ * If set to -1, the task will repeat indefinitely until cancelled.
151+ * Default value is -1.
152+ * @param async Specifies whether the task should be executed asynchronously or synchronously.
153+ * If set to true, the task will be executed asynchronously.
154+ * If set to false, the task will be executed synchronously.
155+ * @param runnable The code to be executed for each task execution.
156+ *
157+ * @return The BukkitTask representing the repeating task.
158+ */
36159fun repeatingTask (period : Int , unit : TimeUnit , times : Int = -1, async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(period, period, times, unit, async, runnable)
160+ /* *
161+ * Schedules a repeating task on the Bukkit scheduler.
162+ *
163+ * @param delayTicks The number of ticks to wait before the task is first executed.
164+ * @param periodTicks The number of ticks between each execution of the task.
165+ * @param async Whether the task should be executed asynchronously.
166+ * @param runnable The code to be executed by the task. This code is specified as a lambda expression
167+ * that takes a [BukkitRunnable] object as its receiver.
168+ * @return The Bukkit task that has been scheduled.
169+ */
37170fun repeatingTask (delayTicks : Int , periodTicks : Int , async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(delayTicks, periodTicks, - 1 , TimeUnit .TICKS , async, runnable)
171+ /* *
172+ * Schedules a repeating task with the specified delay, period, and number of times to repeat.
173+ *
174+ * @param delayTicks the delay in ticks before the task should start running
175+ * @param periodTicks the delay in ticks between each execution of the task
176+ * @param times the number of times the task should repeat (-1 for infinite)
177+ * @param async if true, the task will run asynchronously; otherwise, it will run synchronously
178+ * @param runnable the code to be executed by the task
179+ * @return a BukkitTask representing the scheduled task
180+ */
38181fun repeatingTask (delayTicks : Int , periodTicks : Int , times : Int = -1, async : Boolean , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(delayTicks, periodTicks, times, TimeUnit .TICKS , async, runnable)
182+ /* *
183+ * Schedules a repeating task with the given delay, period, time unit, and runnable function.
184+ *
185+ * @param delay the initial delay before the task is executed, in the specified time unit
186+ * @param period the time between consecutive executions of the task, in the specified time unit
187+ * @param unit the time unit of the delay and period parameters
188+ * @param runnable the function to be run repeatedly
189+ * @return the BukkitTask representing the scheduled task
190+ */
39191fun repeatingTask (delay : Int , period : Int , unit : TimeUnit , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(delay, period, - 1 , unit, false , runnable)
192+ /* *
193+ * Schedules a repeating task with the given delay, period, times, unit, and runnable.
194+ *
195+ * @param delay The delay in time units before the task is executed for the first time.
196+ * @param period The time period in time units between subsequent task executions.
197+ * @param times The number of times the task should be repeated. A value of -1 means the task will repeat indefinitely.
198+ * @param unit The time unit for the delay and period parameters.
199+ * @param runnable The code to be executed as part of the task. It is defined as a lambda expression with the BukkitRunnable as the receiver.
200+ * @return The BukkitTask object representing the scheduled repeating task.
201+ */
40202fun repeatingTask (delay : Int , period : Int , times : Int = -1, unit : TimeUnit , runnable : BukkitRunnable .() -> Unit ): BukkitTask = repeatingTask(delay, period, times, unit, false , runnable)
41203
204+ /* *
205+ * Creates a new instance of [BukkitRunnable] using the given lambda expression as the run method.
206+ *
207+ * @param runnable the lambda expression to be executed when the [BukkitRunnable] runs.
208+ * @return a new instance of [BukkitRunnable] with the specified run method.
209+ */
42210private fun createRunnable (runnable : BukkitRunnable .() -> Unit ): BukkitRunnable {
43211 return object : BukkitRunnable (){
44212 override fun run () {
@@ -47,6 +215,13 @@ private fun createRunnable(runnable: BukkitRunnable.() -> Unit): BukkitRunnable{
47215 }
48216}
49217
218+ /* *
219+ * Creates a `BukkitRunnable` with the specified number of times to run and a lambda expression to execute.
220+ *
221+ * @param times The number of times to run the `BukkitRunnable`. Defaults to -1, which means the `BukkitRunnable` will run indefinitely.
222+ * @param runnable The lambda expression to execute when the `BukkitRunnable` is run.
223+ * @return The created `BukkitRunnable`.
224+ */
50225private fun createRunnable (times : Int = -1, runnable : BukkitRunnable .() -> Unit ): BukkitRunnable {
51226 var amount = 0
52227 return object : BukkitRunnable (){
0 commit comments