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) {
// 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);
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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)
}
});

View File

@ -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
}

View File

@ -17,11 +17,26 @@ var Material = Java.type('org.bukkit.Material');
*/
item.create = function() {
var idOrType = arguments[0];
if (isNaN(Number(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:

View File

@ -16,7 +16,8 @@ function create(func) {
try {
func();
} 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) {
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);
}
}));
};