fix: console error

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-02-26 09:40:00 +00:00
parent 6766c3b0ce
commit 2cd6e019c6
3 changed files with 15 additions and 11 deletions

View File

@ -2,43 +2,43 @@
* 控制台输出类 * 控制台输出类
*/ */
/*global base*/ /*global base*/
(function (global) { (function(global) {
var Arrays = Java.type('java.util.Arrays'); var Arrays = Java.type('java.util.Arrays');
var Level = Java.type('java.util.logging.Level'); var Level = Java.type('java.util.logging.Level');
global.ConsoleDefault = function ConsoleDefault(name) { global.ConsoleDefault = function ConsoleDefault(name) {
Object.defineProperty(this, 'name', { Object.defineProperty(this, 'name', {
get: function () { get: function() {
return this._name; return this._name;
}.bind(this), }.bind(this),
set: function (name) { set: function(name) {
this._name = name ? '[' + name + '] ' : ''; this._name = name ? '[' + name + '] ' : '';
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
this.prefix = name ? '§6[§cMS§6][§b' + name + '§6]§r ' : '§6[§bMiaoScript§6]§r '; this.prefix = name ? '§6[§cMS§6][§b' + name + '§6]§r ' : '§6[§bMiaoScript§6]§r ';
}.bind(this) }.bind(this)
}); });
this.name = name; this.name = name;
this.log = this.info = function () { this.log = this.info = function() {
log.info(this.name + Array.prototype.join.call(arguments, ' ')); log.info(this.name + Array.prototype.join.call(arguments, ' '));
}; };
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
this.warn = function () { this.warn = function() {
log.warning(this.name + Array.prototype.join.call(arguments, ' ')); log.warning(this.name + Array.prototype.join.call(arguments, ' '));
}; };
this.error = function () { this.error = function() {
log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' ')); log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' '));
}; };
this.debug = function () { this.debug = function() {
log.info(this.name + '[DEBUG] ' + Array.prototype.join.call(arguments, ' ')); log.info(this.name + '[DEBUG] ' + Array.prototype.join.call(arguments, ' '));
}; };
this.debug = global.debug ? this.debug : global.noop; this.debug = global.debug ? this.debug : global.noop;
this.sender = this.info; this.sender = this.info;
this.console = this.info; this.console = this.info;
this.object = function (obj) { this.object = function(obj) {
for (var i in obj) { for (var i in obj) {
this.log(i, '=>', obj[i]) this.log(i, '=>', obj[i])
} }
}; };
this.ex = function (message, ex) { this.ex = function(message, ex) {
if (!ex) { if (!ex) {
this.console('§4' + message); this.console('§4' + message);
ex = message; ex = message;
@ -49,11 +49,13 @@
if (track.class) { if (track.class) {
track = Arrays.asList(track) track = Arrays.asList(track)
} }
track.forEach(function (stack) { track.forEach(function(stack) {
if (stack.className.startsWith('<')) { if (stack.className.startsWith('<')) {
this.console(' §e位于§c', stack.fileName, '=>§c', stack.methodName, '§4行', stack.lineNumber); this.console(' §e位于§c', stack.fileName, '=>§c', stack.methodName, '§4行', stack.lineNumber);
} else {// %s.%s(§4%s:%s§c) } else {// %s.%s(§4%s:%s§c)
this.console(' §e位于§c', stack.className + '.' + stack.methodName + '(§4' + stack.fileName + ':' + stack.lineNumber + '§c)'); var className = stack.className
className = className.startsWith('jdk.nashorn.internal.scripts') ? className.substr(className.lastIndexOf('$') + 1) : className
this.console(' §e位于§c', className + '.' + stack.methodName + '(§4' + stack.fileName + ':' + stack.lineNumber + '§c)');
} }
}.bind(this)); }.bind(this));
}; };

View File

@ -6,6 +6,7 @@
var sender = arguments[0]; var sender = arguments[0];
if (!(sender instanceof org.bukkit.command.CommandSender)) { if (!(sender instanceof org.bukkit.command.CommandSender)) {
this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!") this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!")
return
} }
var args = Array.prototype.slice.call(arguments, 1); var args = Array.prototype.slice.call(arguments, 1);
sender.sendMessage(console.prefix + args.join(' ')); sender.sendMessage(console.prefix + args.join(' '));

View File

@ -7,6 +7,7 @@
var sender = arguments[0]; var sender = arguments[0];
if (!(sender instanceof org.spongepowered.api.command.CommandSource)) { if (!(sender instanceof org.spongepowered.api.command.CommandSource)) {
this.error("第一个参数未实现 org.spongepowered.api.command.CommandSource 无法发送消息!") this.error("第一个参数未实现 org.spongepowered.api.command.CommandSource 无法发送消息!")
return
} }
var args = Array.prototype.slice.call(arguments, 1); var args = Array.prototype.slice.call(arguments, 1);
sender.sendMessage(Text.of(console.prefix + args.join(' '))); sender.sendMessage(Text.of(console.prefix + args.join(' ')));