From 6004590323a2f5c49b90609fcae980c7f69562ab Mon Sep 17 00:00:00 2001 From: coding Date: Wed, 11 Oct 2017 01:21:25 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E5=8F=8D=E5=B0=84?= =?UTF-8?q?=E5=92=8C=E5=91=BD=E4=BB=A4=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/core/console.js | 8 ++++---- src/main/resources/kit/reflect.js | 29 ++++++++++++++++++--------- src/main/resources/modules/command.js | 25 ++++++++++++++++------- src/main/resources/plugins/hello.js | 10 +++++++++ 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/main/resources/core/console.js b/src/main/resources/core/console.js index 5d30d86..84280e0 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -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; \ No newline at end of file diff --git a/src/main/resources/kit/reflect.js b/src/main/resources/kit/reflect.js index 573dfff..e3d10ad 100644 --- a/src/main/resources/kit/reflect.js +++ b/src/main/resources/kit/reflect.js @@ -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) { diff --git a/src/main/resources/modules/command.js b/src/main/resources/modules/command.js index 760d4d3..56d23b6 100644 --- a/src/main/resources/modules/command.js +++ b/src/main/resources/modules/command.js @@ -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)); + } + ); } }; \ No newline at end of file diff --git a/src/main/resources/plugins/hello.js b/src/main/resources/plugins/hello.js index 2bc1940..22b585f 100644 --- a/src/main/resources/plugins/hello.js +++ b/src/main/resources/plugins/hello.js @@ -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() {