MiaoScript/src/main/resources/core/console.js

103 lines
4.3 KiB
JavaScript
Raw Normal View History

/**
* 控制台输出类
*/
/*global base*/
(function (global) {
var Arrays = Java.type('java.util.Arrays');
var Level = Java.type('java.util.logging.Level');
2017-10-30 03:17:42 +00:00
var Console = {
createNew: function (name) {
2017-10-30 03:17:42 +00:00
var console = {};
Object.defineProperty(console, 'name', {
get: function () {
return this._name;
}.bind(console),
set: function (name) {
this._name = name ? '[' + name + '] ' : '';
this.prefix = name ? '§6[§cMS§6][§b' + name + '§6]§r ' : '§6[§bMiaoScript§6]§r ';
}.bind(console)
});
console.name = name;
console.log = console.info = function () {
log.info(this.name + Array.prototype.join.call(arguments, ' '));
};
console.warn = function () {
log.warning(this.name + Array.prototype.join.call(arguments, ' '));
};
console.error = function () {
log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' '));
};
console.debug = function () {
log.info(this.name + '[DEBUG] ' + Array.prototype.join.call(arguments, ' '));
};
console.debug = global.debug ? console.debug : global.noop;
console.sender = console.info;
console.console = console.info;
console.ex = function (ex) {
this.console('§4' + ex);
var track = ex.getStackTrace();
if (track.class) {
track = Arrays.asList(track)
}
track.forEach(function (stack) {
if (stack.className.startsWith('<')) {
this.console(' §e位于§c', stack.fileName, '=>§c', stack.methodName, '§4行', stack.lineNumber);
} else {// %s.%s(§4%s:%s§c)
this.console(' §e位于§c', stack.className + '.' + stack.methodName + '(§4' + stack.fileName + ':' + stack.lineNumber + '§c)');
}
2017-10-30 03:17:42 +00:00
}.bind(this));
};
2017-10-30 03:17:42 +00:00
return console;
}
};
2017-10-30 03:17:42 +00:00
var BukkitConsole = {
createNew: function () {
var console = Console.createNew();
console.sender = function () {
var sender = arguments[0];
if (!(sender instanceof org.bukkit.command.CommandSender)) {
this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!")
}
var args = Array.prototype.slice.call(arguments, 1);
sender.sendMessage(console.prefix + args.join(' '));
};
console.console = function () {
this.sender(MServer.consoleSender, Array.prototype.join.call(arguments, ' '));
};
return console;
}
};
2017-10-30 03:17:42 +00:00
var SpongeConsole = {
createNew: function () {
var console = Console.createNew();
console.sender = function () {
var Text = Java.type("org.spongepowered.api.text.Text");
var sender = arguments[0];
if (!(sender instanceof org.spongepowered.api.command.CommandSource)) {
this.error("第一个参数未实现 org.spongepowered.api.command.CommandSource 无法发送消息!")
}
2017-10-30 03:17:42 +00:00
var args = Array.prototype.slice.call(arguments, 1);
sender.sendMessage(Text.of(console.prefix + args.join(' ')));
};
console.console = function () {
this.sender(MServer.server.console, Array.prototype.join.call(arguments, ' '));
};
console.warn = function () {
log.warn(this.name + Array.prototype.join.call(arguments, ' '));
};
return console;
2017-10-20 09:14:42 +00:00
}
};
2017-10-30 03:17:42 +00:00
switch (DetectServerType) {
case ServerType.Bukkit:
global.Console = BukkitConsole;
break;
case ServerType.Sponge:
global.Console = SpongeConsole;
break;
default:
global.Console = Console;
break;
}
global.console = global.Console.createNew();
})(global);