From e2566c4bf173d2c15d75d06f5a0a6608fc24e35f Mon Sep 17 00:00:00 2001 From: coding Date: Mon, 30 Oct 2017 03:17:42 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=BC=E5=AE=B9Sponge=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/pw/yumc/MiaoScript/MiaoScript.java | 2 +- .../pw/yumc/MiaoScript/MiaoScriptSponge.java | 18 +- .../java/pw/yumc/MiaoScript/ScriptEngine.java | 23 ++- src/main/resources/bios.js | 35 ++-- src/main/resources/core/console.js | 167 ++++++++++-------- src/main/resources/core/detect.js | 4 +- src/main/resources/core/init.js | 17 +- src/main/resources/core/require.js | 4 +- src/main/resources/modules/bukkit/command.js | 6 +- src/main/resources/modules/bukkit/console.js | 19 ++ src/main/resources/modules/bukkit/event.js | 2 +- src/main/resources/modules/bukkit/plugin.js | 5 +- src/main/resources/modules/bukkit/server.js | 4 +- src/main/resources/modules/command.js | 2 +- src/main/resources/modules/event.js | 2 +- src/main/resources/modules/mserver.js | 8 + src/main/resources/modules/permission.js | 2 +- src/main/resources/modules/plugin.js | 2 +- src/main/resources/modules/server.js | 2 +- src/main/resources/modules/sponge/command.js | 93 ++++++++++ src/main/resources/modules/sponge/console.js | 23 +++ src/main/resources/modules/sponge/server.js | 43 +++++ src/main/resources/modules/sponge/task.js | 72 ++++++++ src/main/resources/modules/task.js | 2 +- 24 files changed, 431 insertions(+), 126 deletions(-) create mode 100644 src/main/resources/modules/bukkit/console.js create mode 100644 src/main/resources/modules/mserver.js create mode 100644 src/main/resources/modules/sponge/command.js create mode 100644 src/main/resources/modules/sponge/console.js create mode 100644 src/main/resources/modules/sponge/server.js create mode 100644 src/main/resources/modules/sponge/task.js diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScript.java b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java index 8d35277..3e304c9 100644 --- a/src/main/java/pw/yumc/MiaoScript/MiaoScript.java +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java @@ -29,7 +29,7 @@ public class MiaoScript extends JavaPlugin implements Executor { @SneakyThrows public void onEnable() { new CommandSub("ms", this); - engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getClassLoader(), getLogger()); + engine = new ScriptEngine(getDataFolder().getCanonicalPath(), getLogger()); } @Cmd diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java b/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java index 5bcaac0..73cd1e5 100644 --- a/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScriptSponge.java @@ -3,10 +3,10 @@ package pw.yumc.MiaoScript; import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.util.logging.Logger; import javax.script.ScriptException; +import org.slf4j.Logger; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandResult; import org.spongepowered.api.command.args.GenericArguments; @@ -48,6 +48,7 @@ public class MiaoScriptSponge { .permission("MiaoScript.admin") .child(js(), "js") .child(file(), "file") + .child(reload(), "reload") .build(); } @@ -81,11 +82,24 @@ public class MiaoScriptSponge { .build(); } + private CommandSpec reload() { + return CommandSpec.builder() + .description(Text.of("重载 JS 引擎")) + .executor((src, args) -> { + engine.disableEngine(); + Sponge.getEventManager().unregisterPluginListeners(this); + engine.enableEngine(); + // src.sendMesssage("§6[§bMiaoScript§6]§r §bMiaoScript §eEngine §a重启完成!"); + return CommandResult.success(); + }) + .build(); + } + @Listener @SneakyThrows public void onStart(GameStartedServerEvent event) { Sponge.getServer().getConsole(); Sponge.getCommandManager().register(this, main(), "ms", "mscript", "MiaoScript"); - engine = new ScriptEngine(pluginConfigDir.getCanonicalPath(), Thread.currentThread().getContextClassLoader(), logger); + engine = new ScriptEngine(pluginConfigDir.getCanonicalPath(), logger); } } diff --git a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java index d7417a0..401249b 100644 --- a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java +++ b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java @@ -1,9 +1,14 @@ package pw.yumc.MiaoScript; +import java.lang.Thread; import java.io.InputStreamReader; import java.util.logging.Level; import java.util.logging.Logger; +import java.nio.file.Paths; +import java.nio.file.Files; +import java.nio.file.Path; + import javax.script.ScriptEngineManager; import javax.script.ScriptException; @@ -17,13 +22,11 @@ import pw.yumc.YumCore.engine.MiaoScriptEngine; */ public class ScriptEngine { private String root; - private ClassLoader classLoader; - private Logger logger; + private Object logger; private MiaoScriptEngine engine; - public ScriptEngine(String root, ClassLoader classLoader, Logger logger) { + public ScriptEngine(String root, Object logger) { this.root = root; - this.classLoader = classLoader; this.logger = logger; enableEngine(); } @@ -33,10 +36,16 @@ public class ScriptEngine { ScriptEngineManager manager = new ScriptEngineManager(null); this.engine = new MiaoScriptEngine(manager, "nashorn"); this.engine.put("base", new Base()); - this.engine.eval(new InputStreamReader(classLoader.getResourceAsStream("bios.js"))); + Path bios = Paths.get(root, "bios.js"); + // 如果存在自定义bios就加载自定义的 + if (Files.exists(bios)) { + this.engine.eval("load('" + bios.toFile().getCanonicalPath() + "')"); + } else { + this.engine.eval(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("bios.js"))); + } engine.invokeFunction("boot", root, logger); } catch (Exception e) { - logger.log(Level.SEVERE, "MiaoScript 启动失败!", e); + // logger.log(Level.SEVERE, "MiaoScript 启动失败!", e); } } @@ -44,7 +53,7 @@ public class ScriptEngine { try { engine.invokeFunction("disable"); } catch (ScriptException | NoSuchMethodException e) { - logger.log(Level.SEVERE, "MiaoScript 关闭失败!", e); + // logger.log(Level.SEVERE, "MiaoScript 关闭失败!", e); } } diff --git a/src/main/resources/bios.js b/src/main/resources/bios.js index 4e0bf64..feb21ae 100644 --- a/src/main/resources/bios.js +++ b/src/main/resources/bios.js @@ -7,23 +7,28 @@ var disable; */ (function () { boot = function (root, logger) { - log = logger; - // 开发环境下初始化 - root = root || "src/main/resources"; - // 解压文件到根目录 - release(root, "[core|modules]/.*", true); - release(root, "plugins/.*"); - load(root + '/core/init.js'); try { + log = logger; + // 开发环境下初始化 + root = root || "src/main/resources"; + // 解压文件到根目录 + release(root, "[core|modules]/.*", true); + release(root, "plugins/.*"); + load(root + '/core/init.js'); + if (__FILE__ !== "") { + logger.info('载入自定义 BIOS 文件 ' + __FILE__); + global.debug = true; + } init(root); } catch (ex) { - if(console.ex){ + if (console && console.ex) { console.ex(ex); - }else{ + } else { + ex.printStackTrace(); throw ex; } } finally { - disable = disablePlugins + disable = disablePlugins; } }; @@ -34,14 +39,12 @@ var disable; var classLoader = java.lang.Thread.currentThread().getContextClassLoader(); var url = classLoader.getResource("plugin.yml"); - if (url === null) { - return; - } + if (url === null) { return; } + var upath = url.getFile().substring(url.getFile().indexOf("/") + 1); var jarPath = java.net.URLDecoder.decode(upath.substring(0, upath.indexOf('!'))); - if (!Files.exists(Paths.get(jarPath))) { - jarPath = "/" + jarPath; - } + if (!Files.exists(Paths.get(jarPath))) { jarPath = "/" + jarPath; } + try { var jar = new java.util.jar.JarFile(jarPath); var r = new RegExp(regex);// "[core|modules]/.*" diff --git a/src/main/resources/core/console.js b/src/main/resources/core/console.js index df6ca78..ef20f7e 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -5,80 +5,99 @@ (function (global) { var Arrays = Java.type('java.util.Arrays'); var Level = Java.type('java.util.logging.Level'); - var String = Java.type('java.lang.String'); - var Console = function (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 ' : ''; - }.bind(this) - }); - this.name = name; - this.log = function () { - log.info(this.name + Array.prototype.join.call(arguments, ' ')); - }; - this.info = this.log; - 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, ' ')); - }; - 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); - }; - } - this.debug = this.log; - this.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%s => §c%s §4行%s'.format(stack.fileName, stack.methodName, stack.lineNumber)); - } else { - this.console(' §e位于 §c%s.%s(§4%s:%s§c)'.format(stack.className, stack.methodName, stack.fileName, stack.lineNumber)); + 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.ex = function (ex) { + this.console('§4' + ex); + var track = ex.getStackTrace(); + if (track.class) { + track = Arrays.asList(track) } - }.bind(this)); + track.forEach(function (stack) { + if (stack.className.startsWith('<')) { + this.console(' §e位于 §c%s => §c%s §4行%s'.format(stack.fileName, stack.methodName, stack.lineNumber)); + } else { + this.console(' §e位于 §c%s.%s(§4%s:%s§c)'.format(stack.className, stack.methodName, stack.fileName, stack.lineNumber)); + } + }.bind(this)); + } + return console; } - }; - global.Console = Console; - global.console = new 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); \ No newline at end of file diff --git a/src/main/resources/core/detect.js b/src/main/resources/core/detect.js index b52dd6f..1746275 100644 --- a/src/main/resources/core/detect.js +++ b/src/main/resources/core/detect.js @@ -3,8 +3,8 @@ */ /*global base*/ var ServerType = { - Bukkit: 1, - Sponge: 2 + Bukkit: 'bukkit', + Sponge: 'sponge' }; var MServer; var DetectServerType = ServerType.Bukkit; diff --git a/src/main/resources/core/init.js b/src/main/resources/core/init.js index 4b522be..7f2724a 100644 --- a/src/main/resources/core/init.js +++ b/src/main/resources/core/init.js @@ -4,11 +4,12 @@ var global = this; // noinspection JSUnusedLocalSymbols function init(root) { + log.info('Init MiaoScript System...'); global.root = root; loadCore(); loadRequire(); loadPatch(); - loadLib4Bukkit(); + loadServerLib(); loadPlugins(); } @@ -16,9 +17,12 @@ function init(root) { * 初始化核心 */ function loadCore() { + global.noop = function () {}; // 加载基础模块 load(root + '/core/ext.js'); + // 探测服务器类型 load(root + '/core/detect.js'); + // 加载Console load(root + '/core/console.js'); } @@ -41,8 +45,7 @@ function loadPatch() { /** * 加载Bukkit的类库 */ -function loadLib4Bukkit() { - require('modules/event'); +function loadServerLib() { var task = require('modules/task'); global.setTimeout = function (func, time, _async) { return _async ? task.laterAsync(func, time) : task.later(func, time); @@ -64,11 +67,13 @@ function loadLib4Bukkit() { function loadPlugins() { // 初始化本体插件 global.manager = require('modules/plugin'); - manager.init('plugins'); - // 只有当在正式环境运行的时候才加载 - if (manager.$) { + if (manager) { + manager.init('plugins'); + // 只有当在正式环境运行的时候才加载 manager.load(); manager.enable(); + } else { + console.console('§4当前服务器不支持使用MiaoScript插件系统!'); } } diff --git a/src/main/resources/core/require.js b/src/main/resources/core/require.js index 31e2634..8269664 100644 --- a/src/main/resources/core/require.js +++ b/src/main/resources/core/require.js @@ -20,9 +20,7 @@ * @param parent 父目录 */ function resolve(name, parent) { - if (_canonical(name)) { - name = _canonical(name); - } + name = _canonical(name) || name; // 解析本地目录 if (name.startsWith('./') || name.startsWith('../')) { return resolveAsFile(parent, name) || resolveAsDirectory(parent, name) || undefined; diff --git a/src/main/resources/modules/bukkit/command.js b/src/main/resources/modules/bukkit/command.js index c5cad95..93c545f 100644 --- a/src/main/resources/modules/bukkit/command.js +++ b/src/main/resources/modules/bukkit/command.js @@ -59,13 +59,13 @@ function register(jsp, cmd) { function on(jsp, name, exec) { var c = get(name) || create(jsp, name); - console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.fotmat(jsp.description.name, name, c)); + console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.format(jsp.description.name, name, c)); if (exec.cmd) { c.setExecutor(function (sender, cmd, command, args) { try { return exec.cmd(sender, command, args); } catch (ex) { - console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常 §4%s'.fotmat(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); + console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常 §4%s'.format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); console.ex(ex); } }); @@ -78,7 +78,7 @@ function on(jsp, name, exec) { StringUtil.copyPartialMatches(token, Arrays.asList(exec.tab(sender, command, args)), completions); return completions; } catch (ex) { - console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常 §4%s'.fotmat(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); + console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常 §4%s'.format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); console.ex(ex); } }); diff --git a/src/main/resources/modules/bukkit/console.js b/src/main/resources/modules/bukkit/console.js new file mode 100644 index 0000000..7d2d4be --- /dev/null +++ b/src/main/resources/modules/bukkit/console.js @@ -0,0 +1,19 @@ +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; + } +} +global.Console = BukkitConsole; +exports = global.Console; \ No newline at end of file diff --git a/src/main/resources/modules/bukkit/event.js b/src/main/resources/modules/bukkit/event.js index d9763e2..b43f2cf 100644 --- a/src/main/resources/modules/bukkit/event.js +++ b/src/main/resources/modules/bukkit/event.js @@ -138,7 +138,7 @@ function listen(jsp, event, exec, priority, ignoreCancel) { } listeners.push(off); // noinspection JSUnresolvedVariable - console.debug('插件 %s 注册事件 %s 方法 %s'.format(name, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name)); + console.debug('插件 %s 注册事件 %s => %s'.format(name, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name)); return off; } diff --git a/src/main/resources/modules/bukkit/plugin.js b/src/main/resources/modules/bukkit/plugin.js index 2724752..34079a4 100644 --- a/src/main/resources/modules/bukkit/plugin.js +++ b/src/main/resources/modules/bukkit/plugin.js @@ -6,7 +6,7 @@ // var zip = require("core/zip"); var fs = require('core/fs'); var yaml = require('modules/yaml'); -var event = require('modules/event'); +var event = require('./event'); var bukkit = require('./server'); var command = require('./command'); var permission = require('./permission'); @@ -99,7 +99,7 @@ function beforeLoadHook(origin) { // 处理 event 为了不影响 正常逻辑 event 还是手动require吧 // result = result + 'var event = {}; module.exports.event = event;'; // 注入 console 对象 // 给插件注入单独的 console - result = result + 'var console = new Console(); module.exports.console = console;'; + result = result + 'var console = Console.createNew(); module.exports.console = console;'; // 插件注入 self 对象 result = result + 'var self = {}; module.exports.self = self;'; return result; @@ -226,7 +226,6 @@ function checkAndGet(args) { var plugins = []; -exports.$ = undefined; exports.plugins = plugins; exports.init = function (path) { var plugin = bukkit.plugin.self; diff --git a/src/main/resources/modules/bukkit/server.js b/src/main/resources/modules/bukkit/server.js index 1d1cc04..875a75a 100644 --- a/src/main/resources/modules/bukkit/server.js +++ b/src/main/resources/modules/bukkit/server.js @@ -5,10 +5,10 @@ */ /*global Java, base, module, exports, require, __FILE__*/ -var Bukkit = Java.type("org.bukkit.Bukkit"); +var Bukkit = MServer; // noinspection JSUnresolvedVariable var Server = Bukkit.server; -var PluginManager = Bukkit.pluginManager; +var PluginManager = Server.pluginManager; exports.$ = Bukkit; /** * 获取NMS版本 diff --git a/src/main/resources/modules/command.js b/src/main/resources/modules/command.js index 90faac5..9057bd4 100644 --- a/src/main/resources/modules/command.js +++ b/src/main/resources/modules/command.js @@ -1 +1 @@ -module.exports = require('./bukkit/command'); \ No newline at end of file +module.exports = require('./mserver').command; \ No newline at end of file diff --git a/src/main/resources/modules/event.js b/src/main/resources/modules/event.js index 6929ac9..fb1bdbd 100644 --- a/src/main/resources/modules/event.js +++ b/src/main/resources/modules/event.js @@ -1 +1 @@ -module.exports = require('./bukkit/event'); \ No newline at end of file +module.exports = require('./mserver').event; \ No newline at end of file diff --git a/src/main/resources/modules/mserver.js b/src/main/resources/modules/mserver.js new file mode 100644 index 0000000..2f6d478 --- /dev/null +++ b/src/main/resources/modules/mserver.js @@ -0,0 +1,8 @@ +module.exports = { + command: require('./' + DetectServerType + '/command'), + event: require('./' + DetectServerType + '/event'), + permission: require('./' + DetectServerType + '/permission'), + plugin: require('./' + DetectServerType + '/plugin'), + server: require('./' + DetectServerType + '/server'), + task: require('./' + DetectServerType + '/task') +} \ No newline at end of file diff --git a/src/main/resources/modules/permission.js b/src/main/resources/modules/permission.js index 0d4bc0d..c112c6a 100644 --- a/src/main/resources/modules/permission.js +++ b/src/main/resources/modules/permission.js @@ -1 +1 @@ -module.exports = require('./bukkit/permission'); \ No newline at end of file +module.exports = require('./mserver').permission; \ No newline at end of file diff --git a/src/main/resources/modules/plugin.js b/src/main/resources/modules/plugin.js index b5c78b6..bcc7a02 100644 --- a/src/main/resources/modules/plugin.js +++ b/src/main/resources/modules/plugin.js @@ -1 +1 @@ -module.exports = require('./bukkit/plugin'); \ No newline at end of file +module.exports = require('./mserver').plugin; \ No newline at end of file diff --git a/src/main/resources/modules/server.js b/src/main/resources/modules/server.js index 6026f0c..de1c408 100644 --- a/src/main/resources/modules/server.js +++ b/src/main/resources/modules/server.js @@ -1 +1 @@ -module.exports = require('./bukkit/server'); \ No newline at end of file +module.exports = require('./mserver').server; \ No newline at end of file diff --git a/src/main/resources/modules/sponge/command.js b/src/main/resources/modules/sponge/command.js new file mode 100644 index 0000000..741292c --- /dev/null +++ b/src/main/resources/modules/sponge/command.js @@ -0,0 +1,93 @@ +'use strict'; +/** + * Sponge 命令相关类 + */ + +/*global Java, base, module, exports, require, __FILE__*/ +var server = require('./server'); +var plugin = server.plugin.self; + +var CommandManager = server.CommandManager; + +var CommandSpec = Java.type('org.spongepowered.api.command.spec.CommandSpec'); +var Text = Java.type('org.spongepowered.api.text.Text'); + +var ArrayList = Java.type('java.util.ArrayList'); +var Arrays = Java.type('java.util.Arrays'); + +function enable(jsp) { + var commands = jsp.description.commands; + if (commands) { + var pluginCmds = []; + for (var name in commands) { + var command = commands[name]; + if (typeof command !== 'object') continue; + var newCmd = create(jsp, name); + if (command.description) newCmd.setDescription(command.description); + if (command.usage) newCmd.setUsage(command.usage); + if (command.aliases) newCmd.setAliases(Arrays.asList(command.aliases)); + if (command.permission) newCmd.setPermission(command.permission); + if (command['permission-message']) newCmd.setPermissionMessage(command['permission-message']); + pluginCmds.push(newCmd); + console.debug('插件 %s 注册命令 %s ...'.format(jsp.description.name, name)); + } + commandMap.registerAll(jsp.description.name, Arrays.asList(pluginCmds)); + } +} + +function get(name) { + return commandMap.getCommand(name); +} + +function create(jsp, name) { + return register(jsp, ref.on(PluginCommand).create(name, plugin).get()); +} + +function register(jsp, cmd) { + commandMap.register(jsp.description.name, cmd); + return cmd; +} + +// var exec = { +// cmd: function (sender, command, args) { +// +// }, +// tab: function (sender, command, args) { +// +// } +// }; + +function on(jsp, name, exec) { + var c = get(name) || create(jsp, name); + console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.format(jsp.description.name, name, c)); + if (exec.cmd) { + c.setExecutor(function (sender, cmd, command, args) { + try { + return exec.cmd(sender, command, args); + } catch (ex) { + console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常 §4%s'.format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); + console.ex(ex); + } + }); + } + if (exec.tab) { + c.setTabCompleter(function (sender, cmd, command, args) { + try { + var completions = new ArrayList(); + var token = args[args.length - 1]; + StringUtil.copyPartialMatches(token, Arrays.asList(exec.tab(sender, command, args)), completions); + return completions; + } catch (ex) { + console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常 §4%s'.format(sender.name, jsp.description.name, command, Java.from(args).join(' '), ex)); + console.ex(ex); + } + }); + } +} + +exports.enable = enable; + +exports.on = on; +exports.off = function () { + +}; \ No newline at end of file diff --git a/src/main/resources/modules/sponge/console.js b/src/main/resources/modules/sponge/console.js new file mode 100644 index 0000000..7a39b07 --- /dev/null +++ b/src/main/resources/modules/sponge/console.js @@ -0,0 +1,23 @@ +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; + } +} +global.Console = SpongeConsole; +exports = global.Console; \ No newline at end of file diff --git a/src/main/resources/modules/sponge/server.js b/src/main/resources/modules/sponge/server.js new file mode 100644 index 0000000..292df6f --- /dev/null +++ b/src/main/resources/modules/sponge/server.js @@ -0,0 +1,43 @@ +'use strict'; +/** + * Sponge基础操作 + * Created by 蒋天蓓 on 2017/10/27 0009. + */ + +/*global Java, base, module, exports, require, __FILE__*/ +var Sponge = MServer; +// noinspection JSUnresolvedVariable +var Server = Sponge.server; +var PluginManager = Sponge.pluginManager; +exports.$ = Sponge; +/** + * 插件管理 + * @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}} + */ +exports.plugin = { + /** + * 插件管理工具 + */ + manager: PluginManager, + /** + * 获得插件实例 + * @param name 插件名称 + * @returns {*} + */ + get: function (name) { + return PluginManager.getPlugin(name); + }, + /** + * 载入插件 并且返回结果 + * @param name 插件名称 + * @returns {*} + */ + load: function (name) { + var plugin = this.get(name); + if (ext.notNull(plugin) && !plugin.isEnabled()) { + PluginManager.enablePlugin(plugin); + } + return PluginManager.isPluginEnabled(name); + }, + self: PluginManager.getPlugin('miaoscript').get() +}; \ No newline at end of file diff --git a/src/main/resources/modules/sponge/task.js b/src/main/resources/modules/sponge/task.js new file mode 100644 index 0000000..da09705 --- /dev/null +++ b/src/main/resources/modules/sponge/task.js @@ -0,0 +1,72 @@ +'use strict'; +/*global Java, base, module, exports, require, __FILE__*/ +/** + * 任务计划 + * Created by 蒋天蓓 on 2017/2/9 0009. + */ +var plugin = require('./server').plugin.self; +var Comsumer = Java.type('java.util.function.Consumer'); +var Task = Java.type("org.spongepowered.api.scheduler.Task"); +/** + * 创建任务对象 + * @param func 任务 + */ +exports.create = function (func) { + return Task.builder().execute(new Comsumer(func)); +}; +/** + * 运行任务 + * @param func 任务 + */ +exports.run = function (func) { + return exports.create(func).submit(plugin); +}; +/** + * 延时运行任务 + * @param func 任务 + * @param time 延时时间 + */ +exports.later = function (func, time) { + return exports.create(func).delayTicks(time).submit(plugin); +}; +/** + * 运行循环任务 + * @constructor (任务,执行间隔). + * @constructor (任务,首次延时,执行间隔) + */ +exports.timer = function () { + switch (arguments.length) { + case 2: + return exports.create(arguments[0]).intervalTicks(arguments[1]).submit(plugin); + case 3: + return exports.create(arguments[0]).delayTicks(arguments[1]).intervalTicks(arguments[2]).submit(plugin); + } +}; +/** + * 运行异步任务 + * @param func function 任务 + */ +exports.async = function (func) { + return exports.create(func).async().submit(plugin); +}; +/** + * 延时运行异步任务 + * @param func 任务 + * @param time 延时时间 + */ +exports.laterAsync = function (func, time) { + return exports.create(func).async().delayTicks(time).submit(plugin); +}; +/** + * 运行异步循环任务 + * @constructor (任务,执行间隔). + * @constructor (任务,首次延时,执行间隔) + */ +exports.timerAsync = function () { + switch (arguments.length) { + case 2: + return exports.create(arguments[0]).async().intervalTicks(arguments[1]).submit(plugin); + case 3: + return exports.create(arguments[0]).async().delayTicks(arguments[1]).intervalTicks(arguments[2]).submit(plugin); + } +}; \ No newline at end of file diff --git a/src/main/resources/modules/task.js b/src/main/resources/modules/task.js index 0b4a7b1..72b5928 100644 --- a/src/main/resources/modules/task.js +++ b/src/main/resources/modules/task.js @@ -1 +1 @@ -module.exports = require('./bukkit/task'); \ No newline at end of file +module.exports = require('./mserver').task; \ No newline at end of file