feat: 完善反射和命令处理

This commit is contained in:
coding 2017-10-11 01:21:25 +00:00
parent 3106119ff1
commit 6004590323
4 changed files with 52 additions and 20 deletions

View File

@ -6,16 +6,16 @@ var log = base.getLog().static;
var Level = Java.type('java.util.logging.Level'); var Level = Java.type('java.util.logging.Level');
var console = { var console = {
log: function () { log: function () {
log.i(arguments.join(' ')); log.i(Array.prototype.join.call(arguments, ' '));
}, },
warn: function () { warn: function () {
log.w(arguments.join(' ')); log.w(Array.prototype.join.call(arguments, ' '));
}, },
error: function () { error: function () {
log.log(Level.SEVERE, arguments.join(' ')); log.log(Level.SEVERE, Array.prototype.join.call(arguments, ' '));
}, },
debug: function () { debug: function () {
log.d(arguments.join(' ')); log.d(Array.prototype.join.call(arguments, ' '));
} }
}; };
global.console = console; global.console = console;

View File

@ -31,9 +31,9 @@ function Reflect(obj) {
var name = arguments[0]; var name = arguments[0];
var clazzs = Array.prototype.slice.call(arguments, 1); var clazzs = Array.prototype.slice.call(arguments, 1);
try { try {
return this.class.method(name, clazzs); return this.class.getMethod(name, clazzs);
} catch (ex) { } catch (ex) {
return this.class.declaredMethod(name, clazzs); return this.class.getDeclaredMethod(name, clazzs);
} }
}; };
@ -58,20 +58,21 @@ function Reflect(obj) {
}; };
this.create = function () { this.create = function () {
return on(declaredConstructor(this.class, arguments).newInstance(arguments)); var param = Array.prototype.slice.call(arguments);
return on(declaredConstructor(this.class, param).newInstance(param));
}; };
} }
/** /**
* Get an array of types for an array of objects * Get an array of types for an array of objects
*/ */
function types(values) { function types(values, def) {
if (values === null) { if (values === null) {
return []; return [];
} }
var result = []; var result = [];
values.forEach(function (t) { values.forEach(function (t) {
result.push(t === null ? Object.class : t.class) result.push((t === null || def) ? Object.class : t instanceof Class ? t : t.class)
}); });
return result; return result;
} }
@ -86,8 +87,18 @@ function accessible(accessible) {
return accessible; return accessible;
} }
function declaredConstructor() { function declaredConstructor(clazz, param) {
return accessible(arguments[0].declaredConstructor(arguments.slice(1))); var constructor;
try {
constructor = clazz.getDeclaredConstructor(types(param));
}catch(ex) {
try {
constructor = clazz.getDeclaredConstructor(types(param, true));
}catch(ex) {
constructor = clazz.getDeclaredConstructors()[0];
}
}
return accessible(constructor);
} }
function declaredField(clazz, name) { function declaredField(clazz, name) {
@ -95,12 +106,12 @@ function declaredField(clazz, name) {
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
while (clazz !== java.lang.Object.class) { while (clazz !== java.lang.Object.class) {
try { try {
field = clazz.declaredField(name); field = clazz.getDeclaredField(name);
if (field !== null) { if (field !== null) {
break; break;
} }
} catch (e) { } catch (e) {
clazz = clazz.superclass(); clazz = clazz.getSuperclass();
} }
} }
if (field === null) { if (field === null) {

View File

@ -11,9 +11,12 @@ var lookupNames = ref.on(bukkit.plugin.manager).get('lookupNames').get();
var knownCommands = ref.on(bukkit.plugin.manager).get('commandMap').get('knownCommands').get(); var knownCommands = ref.on(bukkit.plugin.manager).get('commandMap').get('knownCommands').get();
var PluginCommand = Java.type('org.bukkit.command.PluginCommand'); var PluginCommand = Java.type('org.bukkit.command.PluginCommand');
var Arrays = Java.type('java.util.Arrays')
function create(jsp, name) { function create(jsp, name) {
var cmd = ref.on(PluginCommand).create(name, plugin).get(); var cmd = ref.on(PluginCommand).create(name, plugin).get();
register(jsp, name, cmd); register(jsp, name, cmd);
return cmd;
} }
function register(jsp, name, cmd) { function register(jsp, name, cmd) {
@ -38,13 +41,21 @@ function register(jsp, name, cmd) {
// //
// } // }
// }; // };
/
exports.on = function (plugin, name, exec) { exports.on = function (jsp, name, exec) {
var c = create(plugin, name); var c = create(jsp, name);
if (exec.onCommand) { if (exec.cmd) {
c.setExecutor(exec); c.setExecutor(
function (sender, cmd, command, args) {
return exec.cmd(sender, command, args);
} }
if (exec.onTabComplete) { );
c.setTabCompleter(exec); }
if (exec.tab) {
c.setTabCompleter(
function (sender, cmd, command, args) {
return Arrays.asList(exec.tab(sender, command, args));
}
);
} }
}; };

View File

@ -5,6 +5,7 @@
/*global Java, base, module, exports, require*/ /*global Java, base, module, exports, require*/
var event = require('modules/event'); var event = require('modules/event');
var cmd = require('modules/command');
var join; var join;
var description = { var description = {
@ -26,6 +27,15 @@ function enable() {
event.player.sendMessage(require("plugins/ext/papi").$(event.player, "§a欢迎来到 §bMiaoScript §a的世界! 当前在线: %server_online%")); event.player.sendMessage(require("plugins/ext/papi").$(event.player, "§a欢迎来到 §bMiaoScript §a的世界! 当前在线: %server_online%"));
}, 10); }, 10);
}); });
cmd.on(module.exports, 'hello', {
cmd: function (sender, command, args) {
console.log(command, args);
return true;
},
tab: function (sender, command, args) {
return 'test';
}
});
} }
function disable() { function disable() {