2017-10-10 13:01:43 +00:00
|
|
|
/**
|
|
|
|
* 控制台输出类
|
|
|
|
*/
|
|
|
|
/*global base*/
|
2017-10-26 13:01:24 +00:00
|
|
|
(function (global) {
|
2017-10-24 10:41:57 +00:00
|
|
|
var Arrays = Java.type('java.util.Arrays');
|
2017-10-17 02:53:51 +00:00
|
|
|
var Level = Java.type('java.util.logging.Level');
|
2017-10-26 13:01:24 +00:00
|
|
|
var String = Java.type('java.lang.String');
|
2017-10-17 02:53:51 +00:00
|
|
|
var Console = function (name) {
|
|
|
|
Object.defineProperty(this, 'name', {
|
|
|
|
get: function () {
|
|
|
|
return this._name;
|
|
|
|
}.bind(this),
|
|
|
|
set: function (name) {
|
|
|
|
this._name = name ? '[' + name + '] ' : '';
|
2017-10-25 11:24:05 +00:00
|
|
|
this.prefix = name ? '§6[§cMS§6][§b' + name + '§6]§r ' : '';
|
2017-10-17 02:53:51 +00:00
|
|
|
}.bind(this)
|
|
|
|
});
|
|
|
|
this.name = name;
|
|
|
|
this.log = function () {
|
2017-10-26 13:01:24 +00:00
|
|
|
log.info(this.name + Array.prototype.join.call(arguments, ' '));
|
|
|
|
};
|
2017-10-26 16:40:01 +00:00
|
|
|
this.info = this.log;
|
2017-10-17 02:53:51 +00:00
|
|
|
this.warn = function () {
|
2017-10-26 13:01:24 +00:00
|
|
|
log.warning(this.name + Array.prototype.join.call(arguments, ' '));
|
|
|
|
};
|
2017-10-17 02:53:51 +00:00
|
|
|
this.error = function () {
|
|
|
|
log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' '));
|
2017-10-26 13:01:24 +00:00
|
|
|
};
|
|
|
|
switch (DetectServerType) {
|
|
|
|
case ServerType.Bukkit:
|
|
|
|
this.sender = function () {
|
|
|
|
var sender = arguments[0];
|
|
|
|
if (!(sender instanceof org.bukkit.command.CommandSender)) {
|
|
|
|
console.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!")
|
|
|
|
}
|
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
sender.sendMessage(this.prefix + args.join(' '));
|
|
|
|
};
|
|
|
|
this.console = function () {
|
|
|
|
this.sender(MServer.consoleSender, Array.prototype.join.call(arguments, ' '));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case ServerType.Sponge:
|
|
|
|
this.sender = function () {
|
|
|
|
var Text = Java.type("org.spongepowered.api.text.Text");
|
|
|
|
var sender = arguments[0];
|
|
|
|
if (!(sender instanceof org.spongepowered.api.command.CommandSource)) {
|
|
|
|
console.error("第一个参数未实现 org.spongepowered.api.command.CommandSource 无法发送消息!")
|
|
|
|
}
|
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
sender.sendMessage(Text.of(this.prefix + args.join(' ')));
|
|
|
|
};
|
|
|
|
this.console = function () {
|
|
|
|
this.sender(MServer.server.console, Array.prototype.join.call(arguments, ' '));
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.sender = function () {
|
|
|
|
throw Error("console.sender 不支持的服务器类型: " + DetectServerType);
|
|
|
|
};
|
|
|
|
this.console = function () {
|
|
|
|
throw Error("console.console 不支持的服务器类型: " + DetectServerType);
|
|
|
|
};
|
2017-10-24 10:41:57 +00:00
|
|
|
}
|
2017-10-26 13:01:24 +00:00
|
|
|
this.debug = this.log;
|
2017-10-20 09:14:42 +00:00
|
|
|
this.ex = function (ex) {
|
2017-10-26 13:01:24 +00:00
|
|
|
this.console('§4' + ex);
|
2017-10-24 10:41:57 +00:00
|
|
|
var track = ex.getStackTrace();
|
2017-10-26 13:01:24 +00:00
|
|
|
if (track.class) {
|
|
|
|
track = Arrays.asList(track)
|
2017-10-24 10:41:57 +00:00
|
|
|
}
|
2017-10-26 13:01:24 +00:00
|
|
|
track.forEach(function (stack) {
|
|
|
|
if (stack.className.startsWith('<')) {
|
2017-10-26 16:40:01 +00:00
|
|
|
this.console(' §e位于 §c%s => §c%s §4行%s'.format(stack.fileName, stack.methodName, stack.lineNumber));
|
2017-10-26 13:01:24 +00:00
|
|
|
} else {
|
2017-10-26 16:40:01 +00:00
|
|
|
this.console(' §e位于 §c%s.%s(§4%s:%s§c)'.format(stack.className, stack.methodName, stack.fileName, stack.lineNumber));
|
2017-10-26 13:01:24 +00:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2017-10-20 09:14:42 +00:00
|
|
|
}
|
2017-10-26 13:01:24 +00:00
|
|
|
};
|
2017-10-17 02:53:51 +00:00
|
|
|
global.Console = Console;
|
|
|
|
global.console = new Console();
|
2017-10-26 13:01:24 +00:00
|
|
|
})(global);
|