From 035f8873633153398367369fa85d7cda50306584 Mon Sep 17 00:00:00 2001 From: coding Date: Wed, 3 Jan 2018 14:00:29 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4Console=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/core/console.js | 149 ++++++------------ src/main/resources/core/detect.js | 11 +- src/main/resources/internal/bukkit/console.js | 17 ++ src/main/resources/internal/sponge/console.js | 21 +++ 4 files changed, 96 insertions(+), 102 deletions(-) create mode 100644 src/main/resources/internal/bukkit/console.js create mode 100644 src/main/resources/internal/sponge/console.js diff --git a/src/main/resources/core/console.js b/src/main/resources/core/console.js index 8735882..38ddd67 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -5,109 +5,56 @@ (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', { - 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.object = function (obj) { - for (var i in obj) { - console.log(i, '=>', obj[i]) - } + global.ConsoleDefault = function ConsoleDefault(name) { + Object.defineProperty(this, 'name', { + get: function () { + return this._name; + }.bind(this), + set: function (name) { + this._name = name ? '[' + name + '] ' : ''; + this.prefix = name ? '§6[§cMS§6][§b' + name + '§6]§r ' : '§6[§bMiaoScript§6]§r '; + }.bind(this) + }); + this.name = name; + this.log = this.info = function () { + log.info(this.name + Array.prototype.join.call(arguments, ' ')); + }; + this.warn = function () { + log.warning(this.name + Array.prototype.join.call(arguments, ' ')); + }; + this.error = function () { + log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' ')); + }; + this.debug = function () { + log.info(this.name + '[DEBUG] ' + Array.prototype.join.call(arguments, ' ')); + }; + 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) { + this.log(i, '=>', obj[i]) } - console.ex = function (message, ex) { - if (!ex) { - this.console('§4' + message); - ex = message; - } else { - this.console('§4' + message + ' ' + 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)'); - } - }.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 无法发送消息!") + this.ex = function (message, ex) { + if (!ex) { + this.console('§4' + message); + ex = message; + } else { + this.console('§4' + message + ' ' + 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)'); } - 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; + }.bind(this)); + }; } - global.console = global.Console.createNew(); + global.Console = ConsoleDefault; })(global); \ No newline at end of file diff --git a/src/main/resources/core/detect.js b/src/main/resources/core/detect.js index 1746275..bce6082 100644 --- a/src/main/resources/core/detect.js +++ b/src/main/resources/core/detect.js @@ -19,4 +19,13 @@ try { DetectServerType = ServerType.Sponge; } catch (ex) { // IGNORE -} \ No newline at end of file +} +/** + * 尝试加载特殊的Console类 + */ +try { + load(root + '/internal/' + DetectServerType + '/console.js'); +} catch (ex) { + // IGNORE +} +global.console = new global.Console(); \ No newline at end of file diff --git a/src/main/resources/internal/bukkit/console.js b/src/main/resources/internal/bukkit/console.js new file mode 100644 index 0000000..e562d3c --- /dev/null +++ b/src/main/resources/internal/bukkit/console.js @@ -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); \ No newline at end of file diff --git a/src/main/resources/internal/sponge/console.js b/src/main/resources/internal/sponge/console.js new file mode 100644 index 0000000..5baf457 --- /dev/null +++ b/src/main/resources/internal/sponge/console.js @@ -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); \ No newline at end of file