feat: 调整server类库

This commit is contained in:
coding 2018-05-30 13:09:46 +00:00
parent b33826bbe9
commit 33fecc7372
3 changed files with 116 additions and 69 deletions

View File

@ -1,2 +1,18 @@
/*global Java, base, module, exports, require*/ /*global Java, base, module, exports, require*/
module.exports = requireInternal('server'); function ServerHandlerDefault() {
/**
* 获取在线玩家
*/
this.players = function () {
switch (arguments.length) {
case 1:
if (toString.call(arguments[0]) !== "[object Function]") { throw TypeError('first argument must be a function!') }
return this.onlinePlayers().forEach(arguments[0]);
default:
return this.onlinePlayers();
}
};
}
var ServerHandler = Object.assign(new ServerHandlerDefault(), requireInternal('server'));
exports = module.exports = ServerHandler;

View File

@ -6,54 +6,28 @@
/*global Java, base, module, exports, require, __FILE__*/ /*global Java, base, module, exports, require, __FILE__*/
var Bukkit = MServer; var Bukkit = MServer;
var Server = MServer.server;
exports.$ = Bukkit;
/** /**
* 获取NMS版本 * 获取NMS版本
*/ */
exports.nmsVersion = Bukkit.server.class.name.split('.')[3]; var nmsVersion = Bukkit.class.name.split('.')[3];
/** /**
* 获取NMS类 * 获取NMS类
*/ */
exports.nmsCls = function (name) { function nmsCls(name) {
return Java.type(['net.minecraft.server', exports.nmsVersion, name].join('.')); return Java.type(['net.minecraft.server', nmsVersion, name].join('.'));
}; };
/** /**
* 获取OBC类 * 获取OBC类
*/ */
exports.obcCls = function (name) { function obcCls(name) {
return Java.type(['org.bukkit.craftbukkit', exports.nmsVersion, name].join('.')); return Java.type(['org.bukkit.craftbukkit', nmsVersion, name].join('.'));
};
/**
* 获取玩家
*/
exports.player = function () {
switch (arguments.length) {
case 0:
return undefined;
case 1:
return Server.getPlayer(arguments[0]);
default:
return Server.getPlayerExtra(arguments[0]);
}
};
/**
* 获取在线玩家
*/
exports.players = function () {
switch (arguments.length) {
case 1:
return Bukkit.onlinePlayers.forEach(arguments[0]);
default:
return Bukkit.onlinePlayers;
}
}; };
/** /**
* 插件管理 * 插件管理
* @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}} * @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}}
*/ */
var PluginManager = Bukkit.pluginManager; var PluginManager = Bukkit.pluginManager;
exports.plugin = { var plugin = {
/** /**
* 插件管理工具 * 插件管理工具
*/ */
@ -85,7 +59,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 = Bukkit.servicesManager; var ServicesManager = Bukkit.servicesManager;
exports.service = { var service = {
/** /**
* 服务管理工具 * 服务管理工具
*/ */
@ -100,11 +74,29 @@ exports.service = {
return reg && reg.provider || null; return reg && reg.provider || null;
} }
}; };
/**
* 获取玩家
*/
function player() {
if (!arguments[0]) { throw TypeError("player name can't be null!") }
switch (arguments.length) {
case 1:
return Bukkit.getPlayer(arguments[0]);
default:
return Bukkit.getPlayerExtra(arguments[0]);
}
};
/**
* 获取在线玩家
*/
function onlinePlayers() {
return Bukkit.onlinePlayers;
};
/** /**
* 公告 * 公告
* @param message 消息 * @param message 消息
*/ */
exports.broadcast = function (message) { function broadcast(message) {
Bukkit.broadcastMessage(message); Bukkit.broadcastMessage(message);
}; };
/** /**
@ -112,26 +104,26 @@ exports.broadcast = function (message) {
* @param player 玩家 * @param player 玩家
* @param command 命令 * @param command 命令
*/ */
exports.command = function (player, command) { function command(player, command) {
Bukkit.dispatchCommand(player, command); Bukkit.dispatchCommand(player, command);
}; };
/** /**
* 执行控制台命令 * 执行控制台命令
* @param command 命令 * @param command 命令
*/ */
exports.console = function (command) { function console(command) {
exports.command(Bukkit.getConsoleSender(), command); command(Bukkit.consoleSender, command);
}; };
/** /**
* 玩家以OP权限执行命令 * 玩家以OP权限执行命令
* @param player * @param player
* @param command * @param command
*/ */
exports.opcommand = function (player, command) { function opcommand(player, command) {
var origin = player.isOp(); var origin = player.isOp();
player.setOp(true); player.setOp(true);
try { try {
exports.command(player, command); command(player, command);
} finally { } finally {
player.setOp(origin); player.setOp(origin);
} }
@ -140,13 +132,24 @@ exports.opcommand = function (player, command) {
* 关闭引擎时执行的操作 * 关闭引擎时执行的操作
*/ */
function shutdown () { function shutdown () {
try { Bukkit.scheduler.cancelTasks(plugin.self);
Bukkit.getScheduler().cancelTasks(plugin.self); Bukkit.servicesManager.unregisterAll(plugin.self);
Bukkit.getServicesManager().unregisterAll(plugin.self);
org.bukkit.event.HandlerList.unregisterAll(plugin.self); org.bukkit.event.HandlerList.unregisterAll(plugin.self);
Bukkit.getMessenger().unregisterIncomingPluginChannel(plugin.self); Bukkit.messenger.unregisterIncomingPluginChannel(plugin.self);
Bukkit.getMessenger().unregisterOutgoingPluginChannel(plugin.self); Bukkit.messenger.unregisterOutgoingPluginChannel(plugin.self);
} catch (ex) {
console.console();
} }
exports = module.exports = {
$: Bukkit,
nmsCls: nmsCls,
obcCls: obcCls,
plugin: plugin,
service: service,
player: player,
onlinePlayers: onlinePlayers,
broadcast: broadcast,
command: command,
console: console,
opcommand: opcommand,
shutdown: shutdown
} }

View File

@ -3,8 +3,9 @@
* Sponge基础操作 * Sponge基础操作
* Created by 蒋天蓓 on 2017/10/27 0009. * Created by 蒋天蓓 on 2017/10/27 0009.
*/ */
/*global Java, base, module, exports, require, __FILE__*/ /*global Java, base, module, exports, require, __FILE__*/
var Text = Java.type('org.spongepowered.api.text.Text');
var Sponge = MServer; var Sponge = MServer;
var Server = MServer.server; var Server = MServer.server;
/** /**
@ -23,7 +24,7 @@ var plugin = {
* @returns {*} * @returns {*}
*/ */
get: function (name) { get: function (name) {
return PluginManager.getPlugin(name).orElse(undefined); return PluginManager.getPlugin(name).orElse(null);
}, },
/** /**
* 载入插件 并且返回结果 * 载入插件 并且返回结果
@ -33,7 +34,7 @@ var plugin = {
load: function (name) { load: function (name) {
return PluginManager.isLoaded(name); return PluginManager.isLoaded(name);
}, },
self: PluginManager.getPlugin('miaoscript').orElse(undefined) self: PluginManager.getPlugin('miaoscript').orElse(null)
}; };
/** /**
* 服务管理 * 服务管理
@ -58,26 +59,49 @@ var service = {
* 获取玩家 * 获取玩家
*/ */
function player() { function player() {
switch (arguments.length) { if (!arguments[0]) { throw TypeError("player name can't be null!") }
case 0: return Server.getPlayer(arguments[0]).orElse(null);
return undefined;
case 1:
return Server.getPlayer(arguments[0]).orElse(undefined);
default:
return Server.getPlayer(arguments[0]).orElse(undefined);
}
}; };
/** /**
* 获取在线玩家 * 获取在线玩家
*/ */
function players() { function onlinePlayers() {
switch (arguments.length) { return Server.onlinePlayers;
case 1: };
// 此处的forEach是Collection接口的 /**
return Server.onlinePlayers.forEach(arguments[0]); * 公告
default: * @param message 消息
// 此处会转换为JS原生的Array */
return Java.from(Server.onlinePlayers.toArray()); function broadcast(message) {
Server.getBroadcastChannel().send(Text.of(message));
};
/**
* 执行名称
* @param player 玩家
* @param command 命令
*/
function command(player, command) {
Sponge.commandManager.process(player, command)
};
/**
* 执行控制台命令
* @param command 命令
*/
function console(command) {
command(Server.console, command);
};
/**
* 玩家以OP权限执行命令
* @param player
* @param command
*/
function opcommand(player, command) {
var origin = player.isOp();
player.setOp(true);
try {
command(player, command);
} finally {
player.setOp(origin);
} }
}; };
/** /**
@ -98,6 +122,10 @@ exports = module.exports = {
plugin: plugin, plugin: plugin,
service: service, service: service,
player: player, player: player,
players: players, onlinePlayers: onlinePlayers,
broadcast: broadcast,
command: command,
console: console,
opcommand: opcommand,
shutdown: shutdown shutdown: shutdown
} }