feat: Core Framework Optimization

This commit is contained in:
MiaoWoo 2019-03-13 17:27:17 +08:00
parent 2013b3b07c
commit 478bff9599
8 changed files with 47 additions and 32 deletions

View File

@ -126,7 +126,7 @@ function beforeLoadHook(origin) {
function afterLoadHook(plugin) { function afterLoadHook(plugin) {
// plugin.event.on = event.on.bind(plugin); // plugin.event.on = event.on.bind(plugin);
// 给 console 添加插件名称 // 给 console 添加插件名称
plugin.console.name = plugin.description.name; plugin.console.name = plugin.description.prefix || plugin.description.name;
// 赋值 self // 赋值 self
Object.assign(plugin.self, plugin); Object.assign(plugin.self, plugin);
} }

View File

@ -43,6 +43,7 @@ var global = this;
throw Error("Error class loader: " + classLoader.class.name + " Please contact the author MiaoWoo!"); throw Error("Error class loader: " + classLoader.class.name + " Please contact the author MiaoWoo!");
} else { } else {
log.info("Class loader compatible: " + classLoader.class.name); log.info("Class loader compatible: " + classLoader.class.name);
log.info("Parent class loader: " + classLoader.parent.class.name);
} }
return classLoader; return classLoader;
} }

View File

@ -38,36 +38,32 @@
this.log(i, '=>', obj[i]) this.log(i, '=>', obj[i])
} }
}; };
this.ex = function(message, ex) { this.ex = function(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.stack(ex).forEach(function(line) { this.stack(ex).forEach(function(line) {
this.console(line) this.console(line)
}.bind(this)) }.bind(this))
}; };
this.stack = function(ex) { 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]; var cache = ['§4' + ex];
if (track.class) { if (stack.class) {
track = Arrays.asList(track) stack = Arrays.asList(stack)
} }
track.forEach(function(stack) { stack.forEach(function(trace) {
if (stack.className.startsWith('<')) { if (trace.className.startsWith('<')) {
var fileName = stack.fileName var fileName = trace.fileName
fileName = fileName.indexOf('runtime') > -1 ? fileName.split('runtime')[1] : 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) } 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 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; return cache;

View File

@ -1,14 +1,15 @@
/** /**
* 补丁和方法扩展 * 补丁和方法扩展
*/ */
(function () { (function() {
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
if (!Array.prototype.copyPartialMatches) { if (!Array.prototype.copyPartialMatches) {
Object.defineProperty(Array.prototype, "copyPartialMatches", { Object.defineProperty(Array.prototype, "copyPartialMatches", {
enumerable: false, enumerable: false,
value: function (token, array) { value: function(token, array) {
this.forEach(function (e) { if (!token) { return this }
if (e.toLowerCase().startsWith(token.toLowerCase())) { this.forEach(function(e) {
if (typeof e === "string" && e.toLowerCase().startsWith(token.toLowerCase())) {
array.push(e) array.push(e)
} }
}); });

View File

@ -13,7 +13,7 @@ function init() {
nmsChatSerializerClass = bukkit.nmsCls(bukkit.nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer"); nmsChatSerializerClass = bukkit.nmsCls(bukkit.nmsVersion.split("_")[1] > 7 ? "IChatBaseComponent$ChatSerializer" : "ChatSerializer");
packetTypeClass = bukkit.nmsCls("PacketPlayOutChat"); packetTypeClass = bukkit.nmsCls("PacketPlayOutChat");
var packetTypeConstructor; var packetTypeConstructor;
Java.from(packetTypeClass.class.constructors).forEach(function (c) { Java.from(packetTypeClass.class.constructors).forEach(function(c) {
if (c.parameterTypes.length === 2) { if (c.parameterTypes.length === 2) {
packetTypeConstructor = c packetTypeConstructor = c
} }

View File

@ -17,11 +17,26 @@ var Material = Java.type('org.bukkit.Material');
*/ */
item.create = function() { item.create = function() {
var idOrType = arguments[0]; var idOrType = arguments[0];
if (isNaN(Number(idOrType))) { var argType = toString.call(idOrType)
idOrType = Material[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) { if (!idOrType) {
throw Error('无效的物品ID或枚举!' + arguments[0] + ' => ' + idOrType) throw Error('无效的物品ID或枚举 ' + arguments[0] + ' => ' + idOrType)
} }
switch (arguments.length) { switch (arguments.length) {
case 1: case 1:

View File

@ -16,7 +16,8 @@ function create(func) {
try { try {
func(); func();
} catch (ex) { } catch (ex) {
console.ex('§4插件执行任务时发生错误', ex); console.console('§4插件执行任务时发生错误', ex)
console.ex(ex);
} }
}); });
}; };

View File

@ -13,11 +13,12 @@ var Task = Java.type("org.spongepowered.api.scheduler.Task");
*/ */
function create(func) { function create(func) {
if (toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); }; 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 { try {
func(); func();
} catch (ex) { } catch (ex) {
console.ex('§4插件执行任务时发生错误', ex); console.console('§4插件执行任务时发生错误', ex)
console.ex(ex);
} }
})); }));
}; };