ms/packages/nukkit/src/task.ts

33 lines
1.1 KiB
TypeScript
Raw Normal View History

import { task, plugin } from '@ccms/api'
import { inject, provideSingleton } from '@ccms/container'
2020-02-23 20:01:39 +00:00
const NukkitRunnable = Java.type('cn.nukkit.scheduler.NukkitRunnable')
2020-02-23 20:01:39 +00:00
@provideSingleton(task.TaskManager)
export class NukkitTaskManager extends task.TaskManager {
2020-02-23 20:01:39 +00:00
@inject(plugin.PluginInstance)
private pluginInstance: any
2020-02-23 20:01: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()
}
disable0() {
base.getInstance().getServer().getScheduler().cancelTask(this.pluginInstance)
}
2020-02-23 20:01:39 +00:00
}
export class NukkitTask extends task.Task {
submit0(...args: any[]): task.Cancelable {
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) {
return run[funcName](this.plugin, this.laterTime, this.interval)
2020-02-23 20:01:39 +00:00
} else {
return run[funcName](this.plugin, this.laterTime)
2020-02-23 20:01:39 +00:00
}
}
}