diff --git a/src/main/resources/api/server.js b/src/main/resources/api/server.js index 27e7564..288c132 100644 --- a/src/main/resources/api/server.js +++ b/src/main/resources/api/server.js @@ -1,2 +1,18 @@ /*global Java, base, module, exports, require*/ -module.exports = requireInternal('server'); \ No newline at end of file +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; diff --git a/src/main/resources/internal/bukkit/server.js b/src/main/resources/internal/bukkit/server.js index 92fe0f8..b074ccd 100644 --- a/src/main/resources/internal/bukkit/server.js +++ b/src/main/resources/internal/bukkit/server.js @@ -6,54 +6,28 @@ /*global Java, base, module, exports, require, __FILE__*/ var Bukkit = MServer; -var Server = MServer.server; -exports.$ = Bukkit; /** * 获取NMS版本 */ -exports.nmsVersion = Bukkit.server.class.name.split('.')[3]; +var nmsVersion = Bukkit.class.name.split('.')[3]; /** * 获取NMS类 */ -exports.nmsCls = function (name) { - return Java.type(['net.minecraft.server', exports.nmsVersion, name].join('.')); +function nmsCls(name) { + return Java.type(['net.minecraft.server', nmsVersion, name].join('.')); }; /** * 获取OBC类 */ -exports.obcCls = function (name) { - return Java.type(['org.bukkit.craftbukkit', exports.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; - } +function obcCls(name) { + return Java.type(['org.bukkit.craftbukkit', nmsVersion, name].join('.')); }; /** * 插件管理 * @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}} */ var PluginManager = Bukkit.pluginManager; -exports.plugin = { +var plugin = { /** * 插件管理工具 */ @@ -85,7 +59,7 @@ exports.plugin = { * @type {{manager: *, get: exports.plugin.get, load: exports.plugin.load}} */ var ServicesManager = Bukkit.servicesManager; -exports.service = { +var service = { /** * 服务管理工具 */ @@ -100,11 +74,29 @@ exports.service = { 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 消息 */ -exports.broadcast = function (message) { +function broadcast(message) { Bukkit.broadcastMessage(message); }; /** @@ -112,26 +104,26 @@ exports.broadcast = function (message) { * @param player 玩家 * @param command 命令 */ -exports.command = function (player, command) { +function command(player, command) { Bukkit.dispatchCommand(player, command); }; /** * 执行控制台命令 * @param command 命令 */ -exports.console = function (command) { - exports.command(Bukkit.getConsoleSender(), command); +function console(command) { + command(Bukkit.consoleSender, command); }; /** * 玩家以OP权限执行命令 * @param player * @param command */ -exports.opcommand = function (player, command) { +function opcommand(player, command) { var origin = player.isOp(); player.setOp(true); try { - exports.command(player, command); + command(player, command); } finally { player.setOp(origin); } @@ -140,13 +132,24 @@ exports.opcommand = function (player, command) { * 关闭引擎时执行的操作 */ 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(); - } + Bukkit.scheduler.cancelTasks(plugin.self); + Bukkit.servicesManager.unregisterAll(plugin.self); + org.bukkit.event.HandlerList.unregisterAll(plugin.self); + Bukkit.messenger.unregisterIncomingPluginChannel(plugin.self); + Bukkit.messenger.unregisterOutgoingPluginChannel(plugin.self); +} + +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 } \ 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 644e399..7d61165 100644 --- a/src/main/resources/internal/sponge/server.js +++ b/src/main/resources/internal/sponge/server.js @@ -3,8 +3,9 @@ * Sponge基础操作 * Created by 蒋天蓓 on 2017/10/27 0009. */ - /*global Java, base, module, exports, require, __FILE__*/ +var Text = Java.type('org.spongepowered.api.text.Text'); + var Sponge = MServer; var Server = MServer.server; /** @@ -23,7 +24,7 @@ var plugin = { * @returns {*} */ get: function (name) { - return PluginManager.getPlugin(name).orElse(undefined); + return PluginManager.getPlugin(name).orElse(null); }, /** * 载入插件 并且返回结果 @@ -33,7 +34,7 @@ var plugin = { load: function (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() { - switch (arguments.length) { - case 0: - return undefined; - case 1: - return Server.getPlayer(arguments[0]).orElse(undefined); - default: - return Server.getPlayer(arguments[0]).orElse(undefined); - } + if (!arguments[0]) { throw TypeError("player name can't be null!") } + return Server.getPlayer(arguments[0]).orElse(null); }; /** * 获取在线玩家 */ -function players() { - switch (arguments.length) { - case 1: - // 此处的forEach是Collection接口的 - return Server.onlinePlayers.forEach(arguments[0]); - default: - // 此处会转换为JS原生的Array - return Java.from(Server.onlinePlayers.toArray()); +function onlinePlayers() { + return Server.onlinePlayers; +}; +/** + * 公告 + * @param message 消息 + */ +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, service: service, player: player, - players: players, + onlinePlayers: onlinePlayers, + broadcast: broadcast, + command: command, + console: console, + opcommand: opcommand, shutdown: shutdown } \ No newline at end of file