feat: 调整Console初始化逻辑

This commit is contained in:
coding
2018-01-03 14:00:29 +00:00
parent 2fcb7f1a1d
commit 035f887363
4 changed files with 96 additions and 102 deletions

View File

@ -5,40 +5,38 @@
(function (global) {
var Arrays = Java.type('java.util.Arrays');
var Level = Java.type('java.util.logging.Level');
var Console = {
createNew: function (name) {
var console = {};
Object.defineProperty(console, 'name', {
global.ConsoleDefault = function ConsoleDefault(name) {
Object.defineProperty(this, 'name', {
get: function () {
return this._name;
}.bind(console),
}.bind(this),
set: function (name) {
this._name = name ? '[' + name + '] ' : '';
this.prefix = name ? '§6[§cMS§6][§b' + name + '§6]§r ' : '§6[§bMiaoScript§6]§r ';
}.bind(console)
}.bind(this)
});
console.name = name;
console.log = console.info = function () {
this.name = name;
this.log = this.info = function () {
log.info(this.name + Array.prototype.join.call(arguments, ' '));
};
console.warn = function () {
this.warn = function () {
log.warning(this.name + Array.prototype.join.call(arguments, ' '));
};
console.error = function () {
this.error = function () {
log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' '));
};
console.debug = function () {
this.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.object = function (obj) {
this.debug = global.debug ? this.debug : global.noop;
this.sender = this.info;
this.console = this.info;
this.object = function (obj) {
for (var i in obj) {
console.log(i, '=>', obj[i])
this.log(i, '=>', obj[i])
}
}
console.ex = function (message, ex) {
this.ex = function (message, ex) {
if (!ex) {
this.console('§4' + message);
ex = message;
@ -57,57 +55,6 @@
}
}.bind(this));
};
return console;
}
};
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;
}
};
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 无法发送消息!")
}
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;
}
};
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.Console = ConsoleDefault;
})(global);

View File

@ -20,3 +20,12 @@ try {
} catch (ex) {
// IGNORE
}
/**
* 尝试加载特殊的Console类
*/
try {
load(root + '/internal/' + DetectServerType + '/console.js');
} catch (ex) {
// IGNORE
}
global.console = new global.Console();

View File

@ -0,0 +1,17 @@
/*global Java, base, module, exports, require, __FILE__*/
(function (global) {
global.Console = function BukkitConsole() {
ConsoleDefault.call(this);
this.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(' '));
};
this.console = function () {
this.sender(MServer.consoleSender, Array.prototype.join.call(arguments, ' '));
};
};
})(global);

View File

@ -0,0 +1,21 @@
/*global Java, base, module, exports, require, __FILE__*/
(function (global) {
global.Console = function SpongeConsole() {
ConsoleDefault.call(this);
this.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 无法发送消息!")
}
var args = Array.prototype.slice.call(arguments, 1);
sender.sendMessage(Text.of(console.prefix + args.join(' ')));
};
this.console = function () {
this.sender(MServer.server.console, Array.prototype.join.call(arguments, ' '));
};
this.warn = function () {
log.warn(this.name + Array.prototype.join.call(arguments, ' '));
};
};
})(global);