From 500384326f6b3ce31a877f88fad9f9b534e80558 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sat, 21 Sep 2019 15:04:25 +0800 Subject: [PATCH] feat: add task and optimize command Signed-off-by: MiaoWoo --- packages/bukkit/src/command.ts | 53 +++++++--------------------------- packages/bukkit/src/console.ts | 2 +- packages/bukkit/src/index.ts | 8 +++-- packages/bukkit/src/task.ts | 31 ++++++++++++++++++++ 4 files changed, 48 insertions(+), 46 deletions(-) create mode 100644 packages/bukkit/src/task.ts diff --git a/packages/bukkit/src/command.ts b/packages/bukkit/src/command.ts index a31d8197..1a704ad9 100644 --- a/packages/bukkit/src/command.ts +++ b/packages/bukkit/src/command.ts @@ -5,7 +5,6 @@ import * as reflect from '@ms/common/dist/reflect' import { injectable, postConstruct, inject } from '@ms/container' let Bukkit = org.bukkit.Bukkit; -let Arrays = Java.type('java.util.Arrays'); let TabCompleter = Java.type('org.bukkit.command.TabCompleter'); let PluginCommand = Java.type('org.bukkit.command.PluginCommand'); let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor'); @@ -21,53 +20,23 @@ export class BukkitCommand extends command.Command { this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get(); } - create(jsp, command) { - var cmd = this.commandMap.getCommand(command.name) + create(plugin: any, command: string) { + var cmd = this.commandMap.getCommand(command) if (cmd && cmd instanceof PluginCommand) { return cmd }; - cmd = reflect.on(PluginCommand).create(command.name, this.pluginInstance).get(); - this.commandMap.register(jsp.description.name, cmd); + cmd = reflect.on(PluginCommand).create(command, this.pluginInstance).get(); + this.commandMap.register(plugin.description.name, cmd); return cmd; } - - onCommand(jsp, c, cmd) { + onCommand(plugin: any, command: any, executor: Function) { // 必须指定需要实现的接口类型 否则MOD服会报错 - c.setExecutor(new CommandExecutor({ - onCommand: function(sender, _, command, args) { - try { - return cmd(sender, command, Java.from(args)); - } catch (ex) { - console.console(`§6玩家 §a${sender.name} §6执行 §b${jsp.description.name} §6插件 §d${command} ${Java.from(args).join(' ')} §6命令时发生异常 §4${ex}`); - console.ex(ex); - } - } + command.setExecutor(new CommandExecutor({ + onCommand: super.setExecutor(plugin, command, executor) })); } - - onTabComplete(jsp, c, tab) { + onTabComplete(plugin: any, command: any, tabCompleter: Function) { // 必须指定需要实现的接口类型 否则MOD服会报错 - // noinspection JSUnusedGlobalSymbols - c.setTabCompleter(new TabCompleter({ - onTabComplete: (sender: any, _, command: string, args: string[]) => { - try { - var token = args[args.length - 1]; - var complete = tab(sender, command, Java.from(args)) || []; - return this.copyPartialMatches(complete, token); - } catch (ex) { - console.console(`§6玩家 §a${sender.name} §6执行 §b${jsp.description.name} §6插件 §d${command} ${Java.from(args).join(' ')} §6补全时发生异常 §4${ex}`); - console.ex(ex); - return []; - } - } + command.setTabCompleter(new TabCompleter({ + onTabComplete: super.setTabCompleter(plugin, command, tabCompleter) })); } - - copyPartialMatches(complete: string[], token: string, array: string[] = []): string[] { - if (!token) { return complete } - complete.forEach(function(e) { - if (typeof e === "string" && e.toLowerCase().startsWith(token.toLowerCase())) { - array.push(e) - } - }); - return array - } -} \ No newline at end of file +} diff --git a/packages/bukkit/src/console.ts b/packages/bukkit/src/console.ts index 559dc695..8c8735ef 100644 --- a/packages/bukkit/src/console.ts +++ b/packages/bukkit/src/console.ts @@ -8,7 +8,7 @@ export class BukkitConsole extends MiaoScriptConsole { this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!") return; } - if (''.toString.call(args[0]) === "[object Array]") { + if (Object.prototype.toString.call(args[0]) === "[object Array]") { args[0].forEach(line => sender.sendMessage(this.prefix + line)) } else { sender.sendMessage(this.prefix + args.join(' ')); diff --git a/packages/bukkit/src/index.ts b/packages/bukkit/src/index.ts index f2a080c0..3dfb87b2 100644 --- a/packages/bukkit/src/index.ts +++ b/packages/bukkit/src/index.ts @@ -1,11 +1,12 @@ import './typings' -import { server, plugin, command, event } from '@ms/api' +import { server, plugin, command, event, task } from '@ms/api' import { DefaultContainer as container } from '@ms/container' -import { BukkitEvent } from './event'; import { BukkitConsole } from './console'; +import { BukkitEvent } from './event'; import { BukkitCommand } from './command'; +import { BukkitTaskManager } from './task'; let BukkitServerType = 'bukkit'; let Bukkit = Java.type("org.bukkit.Bukkit"); @@ -16,5 +17,6 @@ container.bind(plugin.PluginInstance).toConstantValue(Bukkit.pluginManager.getPl container.bind(event.Event).to(BukkitEvent).inSingletonScope(); container.bind(command.Command).to(BukkitCommand).inSingletonScope(); +container.bind(task.TaskManager).to(BukkitTaskManager).inSingletonScope(); -console.debug(`Detect Sponge Compatible set ServerType to ${BukkitServerType} ...`) +console.debug(`Detect Bukkit Compatible set ServerType to ${BukkitServerType} ...`) diff --git a/packages/bukkit/src/task.ts b/packages/bukkit/src/task.ts new file mode 100644 index 00000000..2e771878 --- /dev/null +++ b/packages/bukkit/src/task.ts @@ -0,0 +1,31 @@ +import { task, plugin } from '@ms/api' +import { injectable, inject } from '@ms/container'; + +let BukkitRunnable = Java.type('org.bukkit.scheduler.BukkitRunnable'); + +@injectable() +export class BukkitTaskManager 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 BukkitTask(this.pluginInstance, func); + } +} + +export class BukkitTask extends task.Task { + submit(): task.Cancelable { + let run = new BukkitRunnable({ + run: () => this.run() + }) + let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}` + if (this.interval) { + return run[funcName](this.plugin, this.laterTime, this.interval) + } else if (this.laterTime) { + return run[funcName](this.plugin, this.laterTime) + } else { + return run[funcName](this.plugin) + } + } +}