2020-05-02 16:14:47 +00:00
|
|
|
import { task, plugin } from '@ccms/api'
|
2020-09-22 10:42:39 +00:00
|
|
|
import { inject, provideSingleton } from '@ccms/container'
|
2019-09-21 07:04:25 +00:00
|
|
|
|
2020-09-22 10:42:39 +00:00
|
|
|
const Bukkit = Java.type('org.bukkit.Bukkit')
|
|
|
|
const BukkitRunnable = Java.type('org.bukkit.scheduler.BukkitRunnable')
|
2020-01-17 03:08:51 +00:00
|
|
|
const Callable = Java.type('java.util.concurrent.Callable')
|
2019-09-21 07:04:25 +00:00
|
|
|
|
2020-02-26 02:15:33 +00:00
|
|
|
@provideSingleton(task.TaskManager)
|
2020-09-22 10:42:39 +00:00
|
|
|
export class BukkitTaskManager extends task.TaskManager {
|
2019-09-21 07:04:25 +00:00
|
|
|
@inject(plugin.PluginInstance)
|
2020-09-22 10:42:39 +00:00
|
|
|
private pluginInstance: any
|
2019-09-21 07:04:25 +00:00
|
|
|
|
2020-09-22 10:42:39 +00:00
|
|
|
create0(func: Function): task.Task {
|
|
|
|
return new BukkitTask(this.pluginInstance, func)
|
2019-09-21 07:04:25 +00:00
|
|
|
}
|
2020-01-17 03:08:51 +00:00
|
|
|
callSyncMethod(func: Function): any {
|
|
|
|
return Bukkit.getScheduler().callSyncMethod(this.pluginInstance, new Callable({ call: () => func() })).get()
|
|
|
|
}
|
2020-09-22 10:42:39 +00:00
|
|
|
disable0() {
|
|
|
|
Bukkit.getScheduler().cancelTasks(this.pluginInstance)
|
2020-04-03 03:36:06 +00:00
|
|
|
}
|
2019-09-21 07:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class BukkitTask extends task.Task {
|
2020-09-22 10:42:39 +00:00
|
|
|
submit0(...args: any[]): task.Cancelable {
|
2020-05-30 02:43:33 +00:00
|
|
|
let run = new BukkitRunnable({ run: () => this.run(...args) })
|
2019-09-21 07:04:25 +00:00
|
|
|
let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}`
|
|
|
|
if (this.interval) {
|
|
|
|
return run[funcName](this.plugin, this.laterTime, this.interval)
|
|
|
|
} else {
|
2019-09-22 10:01:50 +00:00
|
|
|
return run[funcName](this.plugin, this.laterTime)
|
2019-09-21 07:04:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|