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.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);

View File

@ -135,4 +135,18 @@ exports.opcommand = function (player, command) {
} finally {
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__*/
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());
}
};
};
/**
* 关闭引擎时执行的操作
*/
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
}