From 478bff9599a488e5c77995879adef350e4b1c25d Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Wed, 13 Mar 2019 17:27:17 +0800 Subject: [PATCH] feat: Core Framework Optimization --- src/main/resources/api/plugin.js | 2 +- src/main/resources/bios.js | 1 + src/main/resources/core/console.js | 36 ++++++++++------------ src/main/resources/core/patch/Array.js | 9 +++--- src/main/resources/internal/bukkit/chat.js | 2 +- src/main/resources/internal/bukkit/item.js | 21 +++++++++++-- src/main/resources/internal/bukkit/task.js | 3 +- src/main/resources/internal/sponge/task.js | 5 +-- 8 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/main/resources/api/plugin.js b/src/main/resources/api/plugin.js index 1917f8a..247d074 100644 --- a/src/main/resources/api/plugin.js +++ b/src/main/resources/api/plugin.js @@ -126,7 +126,7 @@ function beforeLoadHook(origin) { function afterLoadHook(plugin) { // plugin.event.on = event.on.bind(plugin); // 给 console 添加插件名称 - plugin.console.name = plugin.description.name; + plugin.console.name = plugin.description.prefix || plugin.description.name; // 赋值 self Object.assign(plugin.self, plugin); } diff --git a/src/main/resources/bios.js b/src/main/resources/bios.js index 790f019..0d6dc17 100644 --- a/src/main/resources/bios.js +++ b/src/main/resources/bios.js @@ -43,6 +43,7 @@ var global = this; throw Error("Error class loader: " + classLoader.class.name + " Please contact the author MiaoWoo!"); } else { log.info("Class loader compatible: " + classLoader.class.name); + log.info("Parent class loader: " + classLoader.parent.class.name); } return classLoader; } diff --git a/src/main/resources/core/console.js b/src/main/resources/core/console.js index ad009be..c4a85a3 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -38,36 +38,32 @@ this.log(i, '=>', obj[i]) } }; - this.ex = function(message, ex) { - switch (toString.call(message)) { - case "[object String]": - message = message + ' ' //message = 'xxxx' ex =Error - break - case "[object Error]": - ex = message // message = Error ex=null - message = '' - break - } - this.console('§4' + message + ex) + this.ex = function(ex) { this.stack(ex).forEach(function(line) { this.console(line) }.bind(this)) }; this.stack = function(ex) { - var track = ex ? ex.getStackTrace() : new Error().getStackTrace(); + // var exType = toString.call(ex) + // if (exType !== "[object Error]") { + // console.console('§6[WARN] Unknown Type', exType, ex) + // ex.printStackTrace(); + // return [] + // } + var stack = ex.getStackTrace(); var cache = ['§4' + ex]; - if (track.class) { - track = Arrays.asList(track) + if (stack.class) { + stack = Arrays.asList(stack) } - track.forEach(function(stack) { - if (stack.className.startsWith('<')) { - var fileName = stack.fileName + stack.forEach(function(trace) { + if (trace.className.startsWith('<')) { + var fileName = trace.fileName fileName = fileName.indexOf('runtime') > -1 ? fileName.split('runtime')[1] : fileName; - cache.push(' §e->§c %s => §4%s:%s'.format(fileName, stack.methodName, stack.lineNumber)) + cache.push(' §e->§c %s => §4%s:%s'.format(fileName, trace.methodName, trace.lineNumber)) } else {// %s.%s(§4%s:%s§c) - var className = stack.className + var className = trace.className className = className.startsWith('jdk.nashorn.internal.scripts') ? className.substr(className.lastIndexOf('$') + 1) : className - cache.push(' §e->§c %s.%s(§4%s:%s§c)'.format(className, stack.methodName, stack.fileName, stack.lineNumber)); + cache.push(' §e->§c %s.%s(§4%s:%s§c)'.format(className, trace.methodName, trace.fileName, trace.lineNumber)); } }); return cache; diff --git a/src/main/resources/core/patch/Array.js b/src/main/resources/core/patch/Array.js index c1aebaa..7bb899a 100644 --- a/src/main/resources/core/patch/Array.js +++ b/src/main/resources/core/patch/Array.js @@ -1,14 +1,15 @@ /** * 补丁和方法扩展 */ -(function () { +(function() { // noinspection JSUnresolvedVariable if (!Array.prototype.copyPartialMatches) { Object.defineProperty(Array.prototype, "copyPartialMatches", { enumerable: false, - value: function (token, array) { - this.forEach(function (e) { - if (e.toLowerCase().startsWith(token.toLowerCase())) { + value: function(token, array) { + if (!token) { return this } + this.forEach(function(e) { + if (typeof e === "string" && e.toLowerCase().startsWith(token.toLowerCase())) { array.push(e) } }); diff --git a/src/main/resources/internal/bukkit/chat.js b/src/main/resources/internal/bukkit/chat.js index c96813a..2c20b89 100644 --- a/src/main/resources/internal/bukkit/chat.js +++ b/src/main/resources/internal/bukkit/chat.js @@ -13,7 +13,7 @@ function init() { nmsChatSerializerClass = bukkit.nmsCls(bukkit.nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer"); packetTypeClass = bukkit.nmsCls("PacketPlayOutChat"); var packetTypeConstructor; - Java.from(packetTypeClass.class.constructors).forEach(function (c) { + Java.from(packetTypeClass.class.constructors).forEach(function(c) { if (c.parameterTypes.length === 2) { packetTypeConstructor = c } diff --git a/src/main/resources/internal/bukkit/item.js b/src/main/resources/internal/bukkit/item.js index 9d84d03..82ec2ce 100644 --- a/src/main/resources/internal/bukkit/item.js +++ b/src/main/resources/internal/bukkit/item.js @@ -17,11 +17,26 @@ var Material = Java.type('org.bukkit.Material'); */ item.create = function() { var idOrType = arguments[0]; - if (isNaN(Number(idOrType))) { - idOrType = Material[idOrType]; + var argType = toString.call(idOrType) + switch (argType) { + case "[object Number]": + break; + case "[object String]": + idOrType = Material[idOrType]; + break; + case "[object Array]": + idOrType.forEach(function(type) { + var temp = Material[idOrType]; + if (temp) { + idOrType = temp; + return; + } + }) + default: + throw Error("Unsupport argument type " + argType + " value " + idOrType) } if (!idOrType) { - throw Error('无效的物品ID或枚举!' + arguments[0] + ' => ' + idOrType) + throw Error('无效的物品ID或枚举 ' + arguments[0] + ' => ' + idOrType) } switch (arguments.length) { case 1: diff --git a/src/main/resources/internal/bukkit/task.js b/src/main/resources/internal/bukkit/task.js index 8b0f37a..50c9296 100644 --- a/src/main/resources/internal/bukkit/task.js +++ b/src/main/resources/internal/bukkit/task.js @@ -16,7 +16,8 @@ function create(func) { try { func(); } catch (ex) { - console.ex('§4插件执行任务时发生错误', ex); + console.console('§4插件执行任务时发生错误', ex) + console.ex(ex); } }); }; diff --git a/src/main/resources/internal/sponge/task.js b/src/main/resources/internal/sponge/task.js index 521946a..22e9588 100644 --- a/src/main/resources/internal/sponge/task.js +++ b/src/main/resources/internal/sponge/task.js @@ -13,11 +13,12 @@ var Task = Java.type("org.spongepowered.api.scheduler.Task"); */ function create(func) { if (toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); }; - return Task.builder().execute(new Consumer(function () { + return Task.builder().execute(new Consumer(function() { try { func(); } catch (ex) { - console.ex('§4插件执行任务时发生错误', ex); + console.console('§4插件执行任务时发生错误', ex) + console.ex(ex); } })); };