diff --git a/src/main/resources/modules/bukkit.js b/src/main/resources/modules/bukkit.js index 0b597b3..55af460 100644 --- a/src/main/resources/modules/bukkit.js +++ b/src/main/resources/modules/bukkit.js @@ -7,6 +7,7 @@ /*global Java, base, module, exports, require, __FILE__*/ var Bukkit = Java.type("org.bukkit.Bukkit"); // noinspection JSUnresolvedVariable +var Server = Bukkit.server; var PluginManager = Bukkit.pluginManager; exports.$ = Bukkit; /** @@ -19,6 +20,22 @@ exports.nmsVersion = Bukkit.server.class.name.split('.')[3]; exports.nmsCls = function (name) { return Java.type(['net.minecraft.server', exports.nmsVersion, name].join('.')); } +exports.command = function (name) { + return Server.getPluginCommand(name); +} +/** + * 获取玩家 + */ +exports.player = function(){ + switch (arguments.length) { + case 0: + return undefined; + case 1: + return Bukkit.getPlayer(arguments[0]); + default: + return Bukkit.getPlayerExtra(arguments[0]); + } +} /** * 获取在线玩家 */ diff --git a/src/main/resources/modules/command.js b/src/main/resources/modules/command.js index 06364b9..21f9391 100644 --- a/src/main/resources/modules/command.js +++ b/src/main/resources/modules/command.js @@ -12,7 +12,7 @@ var PluginCommand = Java.type('org.bukkit.command.PluginCommand'); var Arrays = Java.type('java.util.Arrays') -function init(jsp){ +function enable(jsp){ var commands = jsp.description.commands; if(commands){ var pluginCmds = []; @@ -66,7 +66,8 @@ function on(jsp, name, exec) { } } -exports.init = init +exports.enable = enable + exports.on = on; exports.off = function () { diff --git a/src/main/resources/modules/permission.js b/src/main/resources/modules/permission.js new file mode 100644 index 0000000..3eca344 --- /dev/null +++ b/src/main/resources/modules/permission.js @@ -0,0 +1,48 @@ +'use strict'; +/** + * Bukkit 权限相关类 + */ + +/*global Java, base, module, exports, require, __FILE__*/ +var manager = require('./bukkit').plugin.manager; + +/** + * Permission(String name, String description) + */ +var Permission = Java.type("org.bukkit.permissions.Permission"); +var PermissionDefault = Java.type('org.bukkit.permissions.PermissionDefault'); + +function enable (plugin) { + var permissions = plugin.description.permissions; + if(permissions){ + for (var name in permissions){ + var permission = permissions[name]; + if (typeof permission !== 'object') continue; + var desc = permission.description; + var def = permission.default || 'OP'; + try { + manager.addPermission(new Permission(name, desc, PermissionDefault.getByName(def))); + } catch (ex) { + // ignore eg: java.lang.IllegalArgumentException: The permission xxxxxx.default is already defined! + } + log.d('插件 %s 注册权限 %s Default %s ...', plugin.description.name, name, def); + } + } +} + +function disable (plugin) { + var permissions = plugin.description.permissions; + if(permissions){ + for (var name in permissions){ + try { + manager.removePermission(name); + } catch (ex) { + // ignore eg: java.lang.IllegalArgumentException: The permission xxxxxx.default is already defined! + } + log.d('插件 %s 注销权限 %s ...', plugin.description.name, name); + } + } +} + +exports.enable = enable +exports.disable = disable \ No newline at end of file diff --git a/src/main/resources/modules/plugin.js b/src/main/resources/modules/plugin.js index e4a9cb8..fde3630 100644 --- a/src/main/resources/modules/plugin.js +++ b/src/main/resources/modules/plugin.js @@ -9,6 +9,7 @@ var yaml = require('modules/yaml'); var event = require('modules/event'); var bukkit = require('./bukkit'); var command = require('./command'); +var permission = require('./permission'); /** * 载入插件 @@ -126,8 +127,9 @@ function initPlugin(file, plugin){ return fs.file(plugin.getDataFolder(), name); } initPluginConfig(plugin); - initPluginCommand(plugin); - initPluginPermission(plugin); + + command.enable(plugin); + permission.enable(plugin); } /** @@ -177,37 +179,6 @@ function initPluginConfig(plugin){ } } -/* - * 初始化插件命令 - */ -function initPluginCommand(plugin) { - command.init(plugin); -} - - -/** - * Permission(String name, String description) - */ -var Permission = Java.type("org.bukkit.permissions.Permission"); -var PermissionDefault = Java.type('org.bukkit.permissions.PermissionDefault'); -/* - * 初始化插件命令 - */ -function initPluginPermission(plugin) { - var permissions = plugin.description.permissions; - var manager = bukkit.plugin.manager; - if(permissions){ - for (var name in permissions){ - var permission = permissions[name]; - if (typeof permission !== 'object') continue; - var desc = permission.description; - var def = permission.default || 'OP'; - manager.addPermission(new Permission(name, desc, PermissionDefault.getByName(def))); - log.d('插件 %s 注册权限 %s Default %s ...', plugin.description.name, name, def); - } - } -} - function runAndCatch(jsp, exec, ext) { if (exec) { try {