diff --git a/src/main/resources/core/init.js b/src/main/resources/core/init.js index fe37188..e38f4f4 100644 --- a/src/main/resources/core/init.js +++ b/src/main/resources/core/init.js @@ -7,6 +7,7 @@ global.root = root; global.noop = function () { }; + var startTime = new Date().getTime(); loadCore(); loadPatch(); loadRequire(); @@ -17,6 +18,7 @@ console.console("§4初始化插件基础系统库错误:§c", ex); console.ex(ex); } + console.log('MiaoScript Engine Load Complete... Done (' + (new Date().getTime() - startTime) / 1000 + 's)!'); }; /** @@ -100,8 +102,17 @@ * 关闭插件Hook */ global.engineDisable = function disable() { - if (global.manager && global.manager.$) { - global.manager.disable(); + try { + if (global.manager && global.manager.$) { + global.manager.disable(); + } + var server = require('api/server'); + if (server.shutdown) { + server.shutdown(); + } + } catch (ex) { + console.console("§3MiaoScript Engine §aShutDown §4Error... ERR: ", ex); + console.ex(ex); } } })(global); diff --git a/src/main/resources/internal/bukkit/server.js b/src/main/resources/internal/bukkit/server.js index ccf9e1c..92fe0f8 100644 --- a/src/main/resources/internal/bukkit/server.js +++ b/src/main/resources/internal/bukkit/server.js @@ -135,4 +135,18 @@ exports.opcommand = function (player, command) { } finally { player.setOp(origin); } -}; \ No newline at end of file +}; +/** + * 关闭引擎时执行的操作 + */ +function shutdown () { + try { + Bukkit.getScheduler().cancelTasks(plugin.self); + Bukkit.getServicesManager().unregisterAll(plugin.self); + org.bukkit.event.HandlerList.unregisterAll(plugin.self); + Bukkit.getMessenger().unregisterIncomingPluginChannel(plugin.self); + Bukkit.getMessenger().unregisterOutgoingPluginChannel(plugin.self); + } catch (ex) { + console.console(); + } +} \ No newline at end of file diff --git a/src/main/resources/internal/sponge/server.js b/src/main/resources/internal/sponge/server.js index b32d516..644e399 100644 --- a/src/main/resources/internal/sponge/server.js +++ b/src/main/resources/internal/sponge/server.js @@ -7,13 +7,12 @@ /*global Java, base, module, exports, require, __FILE__*/ var Sponge = MServer; var Server = MServer.server; -exports.$ = Sponge; /** * 插件管理 * @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}} */ var PluginManager = Sponge.pluginManager; -exports.plugin = { +var plugin = { /** * 插件管理工具 */ @@ -41,7 +40,7 @@ exports.plugin = { * @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}} */ var ServicesManager = Sponge.serviceManager; -exports.service = { +var service = { /* * 服务管理工具 */ @@ -58,7 +57,7 @@ exports.service = { /** * 获取玩家 */ -exports.player = function () { +function player() { switch (arguments.length) { case 0: return undefined; @@ -71,7 +70,7 @@ exports.player = function () { /** * 获取在线玩家 */ -exports.players = function () { +function players() { switch (arguments.length) { case 1: // 此处的forEach是Collection接口的 @@ -80,4 +79,25 @@ exports.players = function () { // 此处会转换为JS原生的Array return Java.from(Server.onlinePlayers.toArray()); } -}; \ No newline at end of file +}; +/** + * 关闭引擎时执行的操作 + */ +function shutdown() { + Sponge.eventManager.unregisterPluginListeners(plugin.self); + Sponge.scheduler.getScheduledTasks(plugin.self).forEach(function (task) { + task.cancel(); + }); + Sponge.commandManager.getOwnedBy(plugin.self).forEach(function (commandMapping) { + Sponge.commandManager.removeMapping(commandMapping); + }); +} + +exports = module.exports = { + $: Sponge, + plugin: plugin, + service: service, + player: player, + players: players, + shutdown: shutdown +} \ No newline at end of file