feat: 新增引擎Shutdown方法 关闭资源

This commit is contained in:
coding 2018-05-17 12:34:35 +00:00
parent 88d71abb2a
commit 202dc9f00c
3 changed files with 54 additions and 9 deletions

View File

@ -7,6 +7,7 @@
global.root = root; global.root = root;
global.noop = function () { global.noop = function () {
}; };
var startTime = new Date().getTime();
loadCore(); loadCore();
loadPatch(); loadPatch();
loadRequire(); loadRequire();
@ -17,6 +18,7 @@
console.console("§4初始化插件基础系统库错误:§c", ex); console.console("§4初始化插件基础系统库错误:§c", ex);
console.ex(ex); console.ex(ex);
} }
console.log('MiaoScript Engine Load Complete... Done (' + (new Date().getTime() - startTime) / 1000 + 's)!');
}; };
/** /**
@ -100,8 +102,17 @@
* 关闭插件Hook * 关闭插件Hook
*/ */
global.engineDisable = function disable() { global.engineDisable = function disable() {
try {
if (global.manager && global.manager.$) { if (global.manager && global.manager.$) {
global.manager.disable(); 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); })(global);

View File

@ -136,3 +136,17 @@ exports.opcommand = function (player, command) {
player.setOp(origin); player.setOp(origin);
} }
}; };
/**
* 关闭引擎时执行的操作
*/
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();
}
}

View File

@ -7,13 +7,12 @@
/*global Java, base, module, exports, require, __FILE__*/ /*global Java, base, module, exports, require, __FILE__*/
var Sponge = MServer; var Sponge = MServer;
var Server = MServer.server; var Server = MServer.server;
exports.$ = Sponge;
/** /**
* 插件管理 * 插件管理
* @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}} * @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}}
*/ */
var PluginManager = Sponge.pluginManager; var PluginManager = Sponge.pluginManager;
exports.plugin = { var plugin = {
/** /**
* 插件管理工具 * 插件管理工具
*/ */
@ -41,7 +40,7 @@ exports.plugin = {
* @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}} * @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}}
*/ */
var ServicesManager = Sponge.serviceManager; var ServicesManager = Sponge.serviceManager;
exports.service = { var service = {
/* /*
* 服务管理工具 * 服务管理工具
*/ */
@ -58,7 +57,7 @@ exports.service = {
/** /**
* 获取玩家 * 获取玩家
*/ */
exports.player = function () { function player() {
switch (arguments.length) { switch (arguments.length) {
case 0: case 0:
return undefined; return undefined;
@ -71,7 +70,7 @@ exports.player = function () {
/** /**
* 获取在线玩家 * 获取在线玩家
*/ */
exports.players = function () { function players() {
switch (arguments.length) { switch (arguments.length) {
case 1: case 1:
// 此处的forEach是Collection接口的 // 此处的forEach是Collection接口的
@ -81,3 +80,24 @@ exports.players = function () {
return Java.from(Server.onlinePlayers.toArray()); return Java.from(Server.onlinePlayers.toArray());
} }
}; };
/**
* 关闭引擎时执行的操作
*/
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
}