feat: 抽离command类库

This commit is contained in:
coding 2018-05-30 13:10:28 +00:00
parent 33fecc7372
commit c955670658
3 changed files with 75 additions and 81 deletions

View File

@ -1,2 +1,12 @@
/*global Java, base, module, exports, require*/ /*global Java, base, module, exports, require*/
module.exports = requireInternal('command'); 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;

View File

@ -47,59 +47,50 @@ function disable(jsp) {
} }
} }
function get(name) { function create(jsp, command) {
return commandMap.getCommand(name); var cmd = commandMap.getCommand(command.name)
} if (cmd) { return cmd };
cmd = ref.on(PluginCommand).create(command.name, plugin).get();
function create(jsp, name) { register(jsp, cmd);
return register(jsp, ref.on(PluginCommand).create(name, plugin).get());
}
function register(jsp, cmd) {
commandMap.register(jsp.description.name, cmd);
return cmd; return cmd;
} }
function on(jsp, name, exec) { function onCommand(jsp, c, cmd) {
var c = get(name) || create(jsp, name);
console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.format(jsp.description.name, name, c));
if (exec.cmd) {
// 必须指定需要实现的接口类型 否则MOD服会报错 // 必须指定需要实现的接口类型 否则MOD服会报错
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
c.setExecutor(new org.bukkit.command.CommandExecutor({ c.setExecutor(new org.bukkit.command.CommandExecutor({
onCommand: function (sender, cmd, command, args) { onCommand: function (sender, cmd, command, args) {
try { try {
return exec.cmd(sender, command, args); return cmd(sender, command, args);
} catch (ex) { } catch (ex) {
console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常 §4%s' 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));
.format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex));
console.ex(ex); console.ex(ex);
} }
} }
})); }));
} }
if (exec.tab) {
function onTabComplete(jsp, c, tab) {
// 必须指定需要实现的接口类型 否则MOD服会报错 // 必须指定需要实现的接口类型 否则MOD服会报错
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
c.setTabCompleter(new org.bukkit.command.TabCompleter({ c.setTabCompleter(new org.bukkit.command.TabCompleter({
onTabComplete: function (sender, cmd, command, args) { onTabComplete: function (sender, cmd, command, args) {
try { try {
var token = args[args.length - 1]; var token = args[args.length - 1];
var complete = exec.tab(sender, command, args) || []; var complete = tab(sender, command, args) || [];
return Arrays.asList(complete.copyPartialMatches(token, [])); return Arrays.asList(complete.copyPartialMatches(token, []));
} catch (ex) { } catch (ex) {
console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常 §4%s' 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));
.format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex));
console.ex(ex); console.ex(ex);
} }
} }
})); }));
}
} }
exports.enable = enable; exports = module.exports = {
enable: enable,
exports.on = on; create: create,
exports.off = function () { onCommand: onCommand,
onTabComplete: onTabComplete,
off: noop
}; };

View File

@ -79,10 +79,6 @@ function enable(jsp) {
} }
} }
// noinspection JSUnusedLocalSymbols
function get(name) {
}
function create(jsp, command) { function create(jsp, command) {
var commandKey = jsp.description.name.toLowerCase() + ":" + command.name; var commandKey = jsp.description.name.toLowerCase() + ":" + command.name;
if (!commandMap[commandKey]) { if (!commandMap[commandKey]) {
@ -92,37 +88,34 @@ function create(jsp, command) {
return commandMap[commandKey]; return commandMap[commandKey];
} }
function on(jsp, name, exec) { function onCommand(jsp, c, cmd) {
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) { c.setExecutor(function execCmd(sender, command, args) {
try { try {
return exec.cmd(sender, command, args); return cmd(sender, command, args);
} catch (ex) { } catch (ex) {
console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常' console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常'.format(sender.name, jsp.description.name, command, args.join(' ')));
.format(sender.name, jsp.description.name, command, args.join(' ')));
console.ex(ex); console.ex(ex);
} }
}); });
} }
if (exec.tab) {
function onTabComplete(jsp, c, tab) {
c.setTabCompleter(function execTab(sender, command, args) { c.setTabCompleter(function execTab(sender, command, args) {
try { try {
var token = args[args.length - 1]; var token = args[args.length - 1];
var complete = exec.tab(sender, command, args) || []; var complete = tab(sender, command, args) || [];
return Arrays.asList(complete.copyPartialMatches(token, [])); return Arrays.asList(complete.copyPartialMatches(token, []));
} catch (ex) { } catch (ex) {
console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常' console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常'.format(sender.name, jsp.description.name, command, args.join(' ')));
.format(sender.name, jsp.description.name, command, args.join(' ')));
console.ex(ex); console.ex(ex);
} }
}); });
}
} }
exports = module.exports = { exports = module.exports = {
enable: enable, enable: enable,
on: on, create: create,
onCommand: onCommand,
onTabComplete: onTabComplete,
off: noop off: noop
}; };