feat: 完善反射和命令处理
This commit is contained in:
parent
3106119ff1
commit
6004590323
@ -6,16 +6,16 @@ var log = base.getLog().static;
|
||||
var Level = Java.type('java.util.logging.Level');
|
||||
var console = {
|
||||
log: function () {
|
||||
log.i(arguments.join(' '));
|
||||
log.i(Array.prototype.join.call(arguments, ' '));
|
||||
},
|
||||
warn: function () {
|
||||
log.w(arguments.join(' '));
|
||||
log.w(Array.prototype.join.call(arguments, ' '));
|
||||
},
|
||||
error: function () {
|
||||
log.log(Level.SEVERE, arguments.join(' '));
|
||||
log.log(Level.SEVERE, Array.prototype.join.call(arguments, ' '));
|
||||
},
|
||||
debug: function () {
|
||||
log.d(arguments.join(' '));
|
||||
log.d(Array.prototype.join.call(arguments, ' '));
|
||||
}
|
||||
};
|
||||
global.console = console;
|
@ -31,9 +31,9 @@ function Reflect(obj) {
|
||||
var name = arguments[0];
|
||||
var clazzs = Array.prototype.slice.call(arguments, 1);
|
||||
try {
|
||||
return this.class.method(name, clazzs);
|
||||
return this.class.getMethod(name, clazzs);
|
||||
} catch (ex) {
|
||||
return this.class.declaredMethod(name, clazzs);
|
||||
return this.class.getDeclaredMethod(name, clazzs);
|
||||
}
|
||||
};
|
||||
|
||||
@ -58,20 +58,21 @@ function Reflect(obj) {
|
||||
};
|
||||
|
||||
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
|
||||
*/
|
||||
function types(values) {
|
||||
function types(values, def) {
|
||||
if (values === null) {
|
||||
return [];
|
||||
}
|
||||
var result = [];
|
||||
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;
|
||||
}
|
||||
@ -86,8 +87,18 @@ function accessible(accessible) {
|
||||
return accessible;
|
||||
}
|
||||
|
||||
function declaredConstructor() {
|
||||
return accessible(arguments[0].declaredConstructor(arguments.slice(1)));
|
||||
function declaredConstructor(clazz, param) {
|
||||
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) {
|
||||
@ -95,12 +106,12 @@ function declaredField(clazz, name) {
|
||||
// noinspection JSUnresolvedVariable
|
||||
while (clazz !== java.lang.Object.class) {
|
||||
try {
|
||||
field = clazz.declaredField(name);
|
||||
field = clazz.getDeclaredField(name);
|
||||
if (field !== null) {
|
||||
break;
|
||||
}
|
||||
} catch (e) {
|
||||
clazz = clazz.superclass();
|
||||
clazz = clazz.getSuperclass();
|
||||
}
|
||||
}
|
||||
if (field === null) {
|
||||
|
@ -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 PluginCommand = Java.type('org.bukkit.command.PluginCommand');
|
||||
|
||||
var Arrays = Java.type('java.util.Arrays')
|
||||
|
||||
function create(jsp, name) {
|
||||
var cmd = ref.on(PluginCommand).create(name, plugin).get();
|
||||
register(jsp, name, cmd);
|
||||
return cmd;
|
||||
}
|
||||
|
||||
function register(jsp, name, cmd) {
|
||||
@ -38,13 +41,21 @@ function register(jsp, name, cmd) {
|
||||
//
|
||||
// }
|
||||
// };
|
||||
|
||||
exports.on = function (plugin, name, exec) {
|
||||
var c = create(plugin, name);
|
||||
if (exec.onCommand) {
|
||||
c.setExecutor(exec);
|
||||
/
|
||||
exports.on = function (jsp, name, exec) {
|
||||
var c = create(jsp, name);
|
||||
if (exec.cmd) {
|
||||
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));
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
@ -5,6 +5,7 @@
|
||||
/*global Java, base, module, exports, require*/
|
||||
|
||||
var event = require('modules/event');
|
||||
var cmd = require('modules/command');
|
||||
var join;
|
||||
|
||||
var description = {
|
||||
@ -26,6 +27,15 @@ function enable() {
|
||||
event.player.sendMessage(require("plugins/ext/papi").$(event.player, "§a欢迎来到 §bMiaoScript §a的世界! 当前在线: %server_online%"));
|
||||
}, 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() {
|
||||
|
Loading…
Reference in New Issue
Block a user