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); // 必须指定需要实现的接口类型 否则MOD服会报错
console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.format(jsp.description.name, name, c)); // noinspection JSUnusedGlobalSymbols
if (exec.cmd) { c.setExecutor(new org.bukkit.command.CommandExecutor({
// 必须指定需要实现的接口类型 否则MOD服会报错 onCommand: function (sender, cmd, command, args) {
// noinspection JSUnusedGlobalSymbols try {
c.setExecutor(new org.bukkit.command.CommandExecutor({ return cmd(sender, command, args);
onCommand: function (sender, cmd, command, args) { } catch (ex) {
try { 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));
return exec.cmd(sender, command, args); console.ex(ex);
} 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; function onTabComplete(jsp, c, tab) {
// 必须指定需要实现的接口类型 否则MOD服会报错
exports.on = on; // noinspection JSUnusedGlobalSymbols
exports.off = function () { 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
}; };

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}); c.setExecutor(function execCmd(sender, command, args) {
console.debug('插件 %s 设置命令 %s 执行器 ...'.format(jsp.description.name, name)); try {
if (exec.cmd) { return cmd(sender, command, args);
c.setExecutor(function execCmd(sender, command, args) { } catch (ex) {
try { console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常'.format(sender.name, jsp.description.name, command, args.join(' ')));
return exec.cmd(sender, command, args); console.ex(ex);
} 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 {
if (exec.tab) { var token = args[args.length - 1];
c.setTabCompleter(function execTab(sender, command, args) { var complete = tab(sender, command, args) || [];
try { return Arrays.asList(complete.copyPartialMatches(token, []));
var token = args[args.length - 1]; } catch (ex) {
var complete = exec.tab(sender, command, args) || []; console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常'.format(sender.name, jsp.description.name, command, args.join(' ')));
return Arrays.asList(complete.copyPartialMatches(token, [])); console.ex(ex);
} 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 = { exports = module.exports = {
enable: enable, enable: enable,
on: on, create: create,
onCommand: onCommand,
onTabComplete: onTabComplete,
off: noop off: noop
}; };