feat: cancel all task when disable engine
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
		@@ -11,6 +11,10 @@ export namespace task {
 | 
			
		||||
         * @param func 执行内容
 | 
			
		||||
         */
 | 
			
		||||
        callSyncMethod(func: Function): any;
 | 
			
		||||
        /**
 | 
			
		||||
         * 关闭任务管理器
 | 
			
		||||
         */
 | 
			
		||||
        disable();
 | 
			
		||||
    }
 | 
			
		||||
    /**
 | 
			
		||||
     * 任务抽象
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,9 @@ export class BukkitTaskManager implements task.TaskManager {
 | 
			
		||||
    callSyncMethod(func: Function): any {
 | 
			
		||||
        return Bukkit.getScheduler().callSyncMethod(this.pluginInstance, new Callable({ call: () => func() })).get()
 | 
			
		||||
    }
 | 
			
		||||
    disable() {
 | 
			
		||||
        Bukkit.getScheduler().cancelTasks(this.pluginInstance);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class BukkitTask extends task.Task {
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,9 @@ export class BungeeTaskManager implements task.TaskManager {
 | 
			
		||||
    callSyncMethod(func: Function): any {
 | 
			
		||||
        return func();
 | 
			
		||||
    }
 | 
			
		||||
    disable() {
 | 
			
		||||
        this.pluginInstance.getProxy().getScheduler().cancel(this.pluginInstance)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class BungeeTask extends task.Task {
 | 
			
		||||
 
 | 
			
		||||
@@ -48,6 +48,7 @@ class MiaoScriptCore {
 | 
			
		||||
    disable() {
 | 
			
		||||
        console.i18n("ms.core.engine.disable")
 | 
			
		||||
        this.pluginManager.disable();
 | 
			
		||||
        this.taskManager.disable();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,9 @@ export class NukkitTaskManager implements task.TaskManager {
 | 
			
		||||
    callSyncMethod(func: Function): any {
 | 
			
		||||
        return func()
 | 
			
		||||
    }
 | 
			
		||||
    disable() {
 | 
			
		||||
        base.getInstance().getServer().getScheduler().cancelTask(this.pluginInstance)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class NukkitTask extends task.Task {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
import { task, plugin } from '@ms/api'
 | 
			
		||||
import { inject, provideSingleton } from '@ms/container';
 | 
			
		||||
import { inject, provideSingleton, postConstruct } from '@ms/container';
 | 
			
		||||
 | 
			
		||||
const Sponge = Java.type("org.spongepowered.api.Sponge");
 | 
			
		||||
const Task = Java.type("org.spongepowered.api.scheduler.Task");
 | 
			
		||||
@@ -11,16 +11,23 @@ const TimeUnit = Java.type('java.util.concurrent.TimeUnit');
 | 
			
		||||
export class SpongeTaskManager implements task.TaskManager {
 | 
			
		||||
    @inject(plugin.PluginInstance)
 | 
			
		||||
    private pluginInstance: any;
 | 
			
		||||
    private syncExecutor: any;
 | 
			
		||||
 | 
			
		||||
    @postConstruct()
 | 
			
		||||
    initialize() {
 | 
			
		||||
        this.syncExecutor = Sponge.getScheduler().createSyncExecutor(this.pluginInstance)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    create(func: Function): task.Task {
 | 
			
		||||
        if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
 | 
			
		||||
        return new SpongeTask(this.pluginInstance, func);
 | 
			
		||||
    }
 | 
			
		||||
    callSyncMethod(func: Function): any {
 | 
			
		||||
        return Sponge.getScheduler()
 | 
			
		||||
            .createSyncExecutor(this.pluginInstance)
 | 
			
		||||
        // @ts-ignore
 | 
			
		||||
            .schedule(new Callable({ call: () => func() }), java.lang.Long.valueOf(0), TimeUnit.NANOSECONDS).get()
 | 
			
		||||
        return this.syncExecutor.schedule(new Callable({ call: () => func() }), java.lang.Long.valueOf(0), TimeUnit.NANOSECONDS).get()
 | 
			
		||||
    }
 | 
			
		||||
    disable() {
 | 
			
		||||
        Sponge.getScheduler().getScheduledTasks(this.pluginInstance).forEach((task: task.Cancelable) => task.cancel())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user