From 8cf8dccbe4c535f45acb230f6a19da94aa565c27 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Mon, 25 Mar 2019 17:13:54 +0800 Subject: [PATCH] feat: Optimizing Framework Code Signed-off-by: MiaoWoo --- src/main/resources/api/item.js | 2 +- src/main/resources/bios.js | 4 +++- src/main/resources/core/console.js | 19 +++++++++++-------- src/main/resources/core/require.js | 2 +- src/main/resources/internal/bukkit/server.js | 20 ++++++++++++++------ 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/main/resources/api/item.js b/src/main/resources/api/item.js index d38ad11..c59785e 100644 --- a/src/main/resources/api/item.js +++ b/src/main/resources/api/item.js @@ -6,4 +6,4 @@ /*global Java, base, module, exports, require*/ function ItemHandlerDefault() { } -exports = module.exports = Object.assign(new ItemHandlerDefault(), requireInternal('item'));; +exports = module.exports = Object.assign(new ItemHandlerDefault(), requireInternal('item', true));; diff --git a/src/main/resources/bios.js b/src/main/resources/bios.js index 0d6dc17..666bfa2 100644 --- a/src/main/resources/bios.js +++ b/src/main/resources/bios.js @@ -43,7 +43,9 @@ 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); + if (classLoader.parent) { + 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 c4a85a3..0306356 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -5,6 +5,7 @@ (function(global) { var Arrays = Java.type('java.util.Arrays'); var Level = Java.type('java.util.logging.Level'); + var ignoreLogPrefix = ['java.', 'net.minecraft.', 'org.bukkit.', 'jdk.nashorn.']; global.ConsoleDefault = function ConsoleDefault(name) { Object.defineProperty(this, 'name', { get: function() { @@ -44,12 +45,6 @@ }.bind(this)) }; this.stack = function(ex) { - // 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 (stack.class) { @@ -61,8 +56,16 @@ fileName = fileName.indexOf('runtime') > -1 ? fileName.split('runtime')[1] : fileName; cache.push(' §e->§c %s => §4%s:%s'.format(fileName, trace.methodName, trace.lineNumber)) } else {// %s.%s(§4%s:%s§c) - var className = trace.className - className = className.startsWith('jdk.nashorn.internal.scripts') ? className.substr(className.lastIndexOf('$') + 1) : className + var className = trace.className; + if (className.startsWith('jdk.nashorn.internal.scripts')) { + className = className.substr(className.lastIndexOf('$') + 1) + } else { + for (var prefix in ignoreLogPrefix) { + if (className.startsWith(ignoreLogPrefix[prefix])) { + return; + } + } + } cache.push(' §e->§c %s.%s(§4%s:%s§c)'.format(className, trace.methodName, trace.fileName, trace.lineNumber)); } }); diff --git a/src/main/resources/core/require.js b/src/main/resources/core/require.js index c7041cd..896ad0c 100644 --- a/src/main/resources/core/require.js +++ b/src/main/resources/core/require.js @@ -240,7 +240,7 @@ file = _isFile(file) ? file : resolve(name, path); optional = Object.assign({ cache: true }, optional); if (file === undefined) { - throw Error("Can't found module" + name + 'in directory' + path) + throw Error("Can't found module " + name + " in directory " + path) } // 重定向文件名称和类型 return getCacheModule(_canonical(file), file.name.split(".")[0], file, optional); diff --git a/src/main/resources/internal/bukkit/server.js b/src/main/resources/internal/bukkit/server.js index 35e7f29..07475d6 100644 --- a/src/main/resources/internal/bukkit/server.js +++ b/src/main/resources/internal/bukkit/server.js @@ -14,13 +14,21 @@ var nmsVersion = Bukkit.server.class.name.split('.')[3]; * 获取NMS类 */ function nmsCls(name) { - return Java.type(['net.minecraft.server', nmsVersion, name].join('.')); + try { + return Java.type(['net.minecraft.server', nmsVersion, name].join('.')); + } catch (e) { + return base.getClass(['net.minecraft.server', nmsVersion, name].join('.')) + } }; /** * 获取OBC类 */ function obcCls(name) { - return Java.type(['org.bukkit.craftbukkit', nmsVersion, name].join('.')); + try { + return Java.type(['org.bukkit.craftbukkit', nmsVersion, name].join('.')); + } catch (e) { + return base.getClass(['org.bukkit.craftbukkit', nmsVersion, name].join('.')) + } }; /** * 插件管理 @@ -37,7 +45,7 @@ var plugin = { * @param name 插件名称 * @returns {*} */ - get: function (name) { + get: function(name) { return PluginManager.getPlugin(name); }, /** @@ -45,7 +53,7 @@ var plugin = { * @param name 插件名称 * @returns {*} */ - load: function (name) { + load: function(name) { var plugin = this.get(name); if (ext.notNull(plugin) && !plugin.isEnabled()) { PluginManager.enablePlugin(plugin); @@ -69,7 +77,7 @@ var service = { * @param name 插件名称 * @returns {*} */ - get: function (name) { + get: function(name) { var reg = ServicesManager.getRegistration(base.getClass(name)); return reg && reg.provider || null; } @@ -131,7 +139,7 @@ function opcommand(player, command) { /** * 关闭引擎时执行的操作 */ -function shutdown () { +function shutdown() { Bukkit.scheduler.cancelTasks(plugin.self); Bukkit.servicesManager.unregisterAll(plugin.self); org.bukkit.event.HandlerList.unregisterAll(plugin.self);