2020-02-23 20:01:39 +00:00
|
|
|
import { task, plugin } from '@ms/api'
|
2020-02-26 02:15:33 +00:00
|
|
|
import { inject, provideSingleton } from '@ms/container';
|
2020-02-23 20:01:39 +00:00
|
|
|
|
|
|
|
const NukkitRunnable = Java.type('cn.nukkit.scheduler.NukkitRunnable');
|
|
|
|
|
2020-02-26 02:15:33 +00:00
|
|
|
@provideSingleton(task.TaskManager)
|
2020-02-23 20:01:39 +00:00
|
|
|
export class NukkitTaskManager implements task.TaskManager {
|
|
|
|
@inject(plugin.PluginInstance)
|
|
|
|
private pluginInstance: any;
|
|
|
|
|
|
|
|
create(func: Function): task.Task {
|
|
|
|
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
|
|
|
|
return new NukkitTask(this.pluginInstance, func);
|
|
|
|
}
|
|
|
|
callSyncMethod(func: Function): any {
|
|
|
|
return func()
|
|
|
|
}
|
2020-04-03 03:36:06 +00:00
|
|
|
disable() {
|
|
|
|
base.getInstance().getServer().getScheduler().cancelTask(this.pluginInstance)
|
|
|
|
}
|
2020-02-23 20:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class NukkitTask extends task.Task {
|
|
|
|
submit(): task.Cancelable {
|
|
|
|
let run = new NukkitRunnable({
|
|
|
|
run: () => this.run()
|
|
|
|
})
|
|
|
|
let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}`
|
|
|
|
if (this.interval) {
|
|
|
|
run[funcName](this.plugin, this.laterTime, this.interval);
|
|
|
|
} else {
|
|
|
|
run[funcName](this.plugin, this.laterTime);
|
|
|
|
}
|
|
|
|
return run;
|
|
|
|
}
|
|
|
|
}
|