Skip to content

Commit

Permalink
Update AsyncTaskTimer.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
PreviousW authored Jul 6, 2024
1 parent 4b7a9fe commit 213c3bf
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions AsyncTaskTimer.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package io.github.FlagFan34272.pcutills

import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.scheduler.BukkitTask
Expand All @@ -11,33 +9,31 @@ class AsyncTaskTimer(private val plugin: JavaPlugin) {

private var bukkitTask: BukkitTask? = null

private val tasks: MutableSet<Pair<BukkitTask, Int>> = mutableSetOf()

fun delay(delay: Long) {
fun delay(delay: Long = 0L) {
this.delay = delay
}

fun period(period: Long) {
this.period = period
}

fun timeToTick(time: Long): Long {
return time * 20
}

fun run(action: () -> Unit) {
this.task = action
}

fun exec() {
fun execute() {
requireNotNull(task) { "태스크 블럭이 정의되지 않았어요!" }
bukkitTask = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, task!!, delay, period)
tasks.add(Pair(bukkitTask!!, bukkitTask!!.taskId))
}

fun stop() {
requireNotNull(task) { "태스크 블럭이 정의되지 않았어요!" }
if (tasks.firstOrNull { it.first == task } != null) {
val tk = tasks.firstOrNull { it.first == task }
Bukkit.getScheduler().cancelTask(tk!!.second)
tasks.remove(tk)
}
val taskId = bukkitTask?.taskId ?: return
plugin.server.scheduler.cancelTask(taskId)
}

fun cancelTaskIf(condition: () -> Boolean) {
Expand All @@ -48,9 +44,13 @@ class AsyncTaskTimer(private val plugin: JavaPlugin) {
}


fun asyncTaskTimer(init: AsyncTaskTimer.() -> Unit): AsyncTaskTimer {
fun runAsync(init: AsyncTaskTimer.() -> Unit): AsyncTaskTimer {
val taskTimer = AsyncTaskTimer(this.plugin)
taskTimer.init()
return taskTimer
}
}

fun Plugin.tasKT(init: AsyncTaskTimer.() -> Unit): AsyncTaskTimer {
return AsyncTaskTimer(this).init()
}

0 comments on commit 213c3bf

Please sign in to comment.