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'
|
2020-02-23 20:01:39 +00:00
|
|
|
|
2020-09-22 10:42:39 +00:00
|
|
|
const NukkitRunnable = Java.type('cn.nukkit.scheduler.NukkitRunnable')
|
2020-02-23 20:01:39 +00:00
|
|
|
|
2020-02-26 02:15:33 +00:00
|
|
|
@provideSingleton(task.TaskManager)
|
2020-09-22 10:42:39 +00:00
|
|
|
export class NukkitTaskManager extends task.TaskManager {
|
2020-02-23 20:01:39 +00:00
|
|
|
@inject(plugin.PluginInstance)
|
2020-09-22 10:42:39 +00:00
|
|
|
private pluginInstance: any
|
2020-02-23 20:01:39 +00:00
|
|
|
|
2020-09-22 10:42:39 +00:00
|
|
|
create0(func: Function): task.Task {
|
|
|
|
return new NukkitTask(this.pluginInstance, func)
|
2020-02-23 20:01:39 +00:00
|
|
|
}
|
|
|
|
callSyncMethod(func: Function): any {
|
|
|
|
return func()
|
|
|
|
}
|
2020-09-22 10:42:39 +00:00
|
|
|
disable0() {
|
2020-04-03 03:36:06 +00:00
|
|
|
base.getInstance().getServer().getScheduler().cancelTask(this.pluginInstance)
|
|
|
|
}
|
2020-02-23 20:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class NukkitTask 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 NukkitRunnable({ run: () => this.run(...args) })
|
2020-02-23 20:01:39 +00:00
|
|
|
let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}`
|
|
|
|
if (this.interval) {
|
2020-09-22 10:42:39 +00:00
|
|
|
return run[funcName](this.plugin, this.laterTime, this.interval)
|
2020-02-23 20:01:39 +00:00
|
|
|
} else {
|
2020-09-22 10:42:39 +00:00
|
|
|
return run[funcName](this.plugin, this.laterTime)
|
2020-02-23 20:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|