feat: 抽离command类库

merge/3/MERGE
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*/
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) {
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
};

View File

@ -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
};