From c955670658fb40fae589ef02fa58e770fa079a09 Mon Sep 17 00:00:00 2001 From: coding Date: Wed, 30 May 2018 13:10:28 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8A=BD=E7=A6=BBcommand=E7=B1=BB?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/api/command.js | 12 ++- src/main/resources/internal/bukkit/command.js | 87 +++++++++---------- src/main/resources/internal/sponge/command.js | 57 ++++++------ 3 files changed, 75 insertions(+), 81 deletions(-) diff --git a/src/main/resources/api/command.js b/src/main/resources/api/command.js index 6548312..b542081 100644 --- a/src/main/resources/api/command.js +++ b/src/main/resources/api/command.js @@ -1,2 +1,12 @@ /*global Java, base, module, exports, require*/ -module.exports = requireInternal('command'); \ No newline at end of file +function CommandHandlerDefault() { + this.on = function (jsp, name, exec) { + var cmd = this.create(jsp, { name: name }); + console.debug('插件 %s 设置命令 %s 执行器(%s) ...'.format(jsp.description.name, name, cmd)); + if (exec.cmd) { this.onCommand(jsp, cmd, exec.cmd) } + if (exec.tab) { this.onTabComplete(jsp, cmd, exec.tab) } + } +} +var CommandHandler = Object.assign(new CommandHandlerDefault(), requireInternal('command')); + +exports = module.exports = CommandHandler; diff --git a/src/main/resources/internal/bukkit/command.js b/src/main/resources/internal/bukkit/command.js index 760cf12..6fe3445 100644 --- a/src/main/resources/internal/bukkit/command.js +++ b/src/main/resources/internal/bukkit/command.js @@ -47,59 +47,50 @@ function disable(jsp) { } } -function get(name) { - return commandMap.getCommand(name); -} - -function create(jsp, name) { - return register(jsp, ref.on(PluginCommand).create(name, plugin).get()); -} - -function register(jsp, cmd) { - commandMap.register(jsp.description.name, cmd); +function create(jsp, command) { + var cmd = commandMap.getCommand(command.name) + if (cmd) { return cmd }; + cmd = ref.on(PluginCommand).create(command.name, plugin).get(); + register(jsp, cmd); return cmd; } -function on(jsp, name, exec) { - var c = get(name) || create(jsp, name); - console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.format(jsp.description.name, name, c)); - if (exec.cmd) { - // 必须指定需要实现的接口类型 否则MOD服会报错 - // noinspection JSUnusedGlobalSymbols - c.setExecutor(new org.bukkit.command.CommandExecutor({ - onCommand: function (sender, cmd, command, args) { - try { - return exec.cmd(sender, command, args); - } catch (ex) { - console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常 §4%s' - .format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); - console.ex(ex); - } +function onCommand(jsp, c, cmd) { + // 必须指定需要实现的接口类型 否则MOD服会报错 + // noinspection JSUnusedGlobalSymbols + c.setExecutor(new org.bukkit.command.CommandExecutor({ + onCommand: function (sender, cmd, command, args) { + try { + return cmd(sender, command, args); + } catch (ex) { + console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常 §4%s'.format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); + console.ex(ex); } - })); - } - if (exec.tab) { - // 必须指定需要实现的接口类型 否则MOD服会报错 - // noinspection JSUnusedGlobalSymbols - c.setTabCompleter(new org.bukkit.command.TabCompleter({ - onTabComplete: function (sender, cmd, command, args) { - try { - var token = args[args.length - 1]; - var complete = exec.tab(sender, command, args) || []; - return Arrays.asList(complete.copyPartialMatches(token, [])); - } catch (ex) { - console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常 §4%s' - .format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); - console.ex(ex); - } - } - })); - } + } + })); } -exports.enable = enable; - -exports.on = on; -exports.off = function () { +function onTabComplete(jsp, c, tab) { + // 必须指定需要实现的接口类型 否则MOD服会报错 + // noinspection JSUnusedGlobalSymbols + c.setTabCompleter(new org.bukkit.command.TabCompleter({ + onTabComplete: function (sender, cmd, command, args) { + try { + var token = args[args.length - 1]; + var complete = tab(sender, command, args) || []; + return Arrays.asList(complete.copyPartialMatches(token, [])); + } catch (ex) { + console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常 §4%s'.format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); + console.ex(ex); + } + } + })); +} +exports = module.exports = { + enable: enable, + create: create, + onCommand: onCommand, + onTabComplete: onTabComplete, + off: noop }; \ No newline at end of file diff --git a/src/main/resources/internal/sponge/command.js b/src/main/resources/internal/sponge/command.js index a5820fd..988fa53 100644 --- a/src/main/resources/internal/sponge/command.js +++ b/src/main/resources/internal/sponge/command.js @@ -79,10 +79,6 @@ function enable(jsp) { } } -// noinspection JSUnusedLocalSymbols -function get(name) { -} - function create(jsp, command) { var commandKey = jsp.description.name.toLowerCase() + ":" + command.name; if (!commandMap[commandKey]) { @@ -92,37 +88,34 @@ function create(jsp, command) { return commandMap[commandKey]; } -function on(jsp, name, exec) { - var c = create(jsp, {name: name}); - console.debug('插件 %s 设置命令 %s 执行器 ...'.format(jsp.description.name, name)); - if (exec.cmd) { - c.setExecutor(function execCmd(sender, command, args) { - try { - return exec.cmd(sender, command, args); - } catch (ex) { - console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常' - .format(sender.name, jsp.description.name, command, args.join(' '))); - console.ex(ex); - } - }); - } - if (exec.tab) { - c.setTabCompleter(function execTab(sender, command, args) { - try { - var token = args[args.length - 1]; - var complete = exec.tab(sender, command, args) || []; - return Arrays.asList(complete.copyPartialMatches(token, [])); - } catch (ex) { - console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常' - .format(sender.name, jsp.description.name, command, args.join(' '))); - console.ex(ex); - } - }); - } +function onCommand(jsp, c, cmd) { + c.setExecutor(function execCmd(sender, command, args) { + try { + return cmd(sender, command, args); + } catch (ex) { + console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常'.format(sender.name, jsp.description.name, command, args.join(' '))); + console.ex(ex); + } + }); +} + +function onTabComplete(jsp, c, tab) { + c.setTabCompleter(function execTab(sender, command, args) { + try { + var token = args[args.length - 1]; + var complete = tab(sender, command, args) || []; + return Arrays.asList(complete.copyPartialMatches(token, [])); + } catch (ex) { + console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常'.format(sender.name, jsp.description.name, command, args.join(' '))); + console.ex(ex); + } + }); } exports = module.exports = { enable: enable, - on: on, + create: create, + onCommand: onCommand, + onTabComplete: onTabComplete, off: noop }; \ No newline at end of file