From b668795518bd16e0e1e60c302f81a20590bf5b85 Mon Sep 17 00:00:00 2001 From: coding Date: Mon, 16 Oct 2017 16:18:35 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E6=9D=83=E9=99=90=E6=B3=A8=E5=86=8C=E5=92=8C=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/modules/bukkit.js | 11 +++-- src/main/resources/modules/command.js | 62 ++++++++++++++---------- src/main/resources/modules/plugin.js | 68 +++++++++++++++++++++------ 3 files changed, 99 insertions(+), 42 deletions(-) diff --git a/src/main/resources/modules/bukkit.js b/src/main/resources/modules/bukkit.js index 259828f..0b597b3 100644 --- a/src/main/resources/modules/bukkit.js +++ b/src/main/resources/modules/bukkit.js @@ -22,9 +22,14 @@ exports.nmsCls = function (name) { /** * 获取在线玩家 */ -exports.players = function (func) { - return Bukkit.onlinePlayers.forEach(func); -}; +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}} diff --git a/src/main/resources/modules/command.js b/src/main/resources/modules/command.js index 8078864..06364b9 100644 --- a/src/main/resources/modules/command.js +++ b/src/main/resources/modules/command.js @@ -5,45 +5,55 @@ /*global Java, base, module, exports, require, __FILE__*/ var plugin = base.plugin; -var bukkit = require('./bukkit'); var ref = require('core/reflect'); -var lookupNames = ref.on(bukkit.plugin.manager).get('lookupNames').get(); -var knownCommands = ref.on(bukkit.plugin.manager).get('commandMap').get('knownCommands').get(); +var bukkit = require('./bukkit'); +var commandMap = ref.on(bukkit.plugin.manager).get('commandMap').get(); var PluginCommand = Java.type('org.bukkit.command.PluginCommand'); var Arrays = Java.type('java.util.Arrays') -function create(jsp, name) { - var cmd = ref.on(PluginCommand).create(name, plugin).get(); - register(jsp, name, cmd); - return cmd; +function init(jsp){ + var commands = jsp.description.commands; + if(commands){ + var pluginCmds = []; + for (var name in commands){ + var command = commands[name]; + if (typeof command !== 'object') continue; + var newCmd = create(jsp, name); + if (command.description) newCmd.setDescription(command.description); + if (command.usage) newCmd.setUsage(command.usage); + if (command.aliases) newCmd.setAliases(Arrays.asList(command.aliases)); + if (command.permission) newCmd.setPermission(command.permission); + if (command['permission-message']) newCmd.setPermissionMessage(command['permission-message']); + pluginCmds.push(newCmd); + log.d('插件 %s 注册命令 %s ...', jsp.description.name, name); + } + commandMap.registerAll(jsp.description.name, Arrays.asList(pluginCmds)); + } } -function register(jsp, name, cmd) { - if (name.isEmpty()) { - return; - } - knownCommands.put(name, cmd); - knownCommands.computeIfAbsent(jsp.description.name + ":" + name, function () { - return cmd; - }); - knownCommands.computeIfAbsent('ms:' + jsp.description.name + ":" + name, function () { - return cmd; - }); - lookupNames.put(name, plugin); +function get(name) { + return commandMap.getCommand(name); +} + +function create(jsp, name) { + return ref.on(PluginCommand).create(name, plugin).get(); +} + +function register(jsp, cmd){ + commandMap.register(jsp.description.name, cmd); } // var exec = { -// onCommand: function (sender, cmd, command, args) { +// cmd: function (sender, command, args) { // // }, -// onTabComplete: function (sender, cmd, command, args) { +// tab: function (sender, command, args) { // // } // }; - -exports.on = function (jsp, name, exec) { - var c = create(jsp, name); +function on(jsp, name, exec) { + var c = get(name) || create(jsp, name); if (exec.cmd) { c.setExecutor(function (sender, cmd, command, args) { return exec.cmd(sender, command, args); @@ -54,8 +64,10 @@ exports.on = function (jsp, name, exec) { return Arrays.asList(exec.tab(sender, command, args)); }); } -}; +} +exports.init = init +exports.on = on; exports.off = function () { }; \ No newline at end of file diff --git a/src/main/resources/modules/plugin.js b/src/main/resources/modules/plugin.js index fd13902..e4a9cb8 100644 --- a/src/main/resources/modules/plugin.js +++ b/src/main/resources/modules/plugin.js @@ -7,6 +7,8 @@ var fs = require('core/fs'); var yaml = require('modules/yaml'); var event = require('modules/event'); +var bukkit = require('./bukkit'); +var command = require('./command'); /** * 载入插件 @@ -18,7 +20,7 @@ function loadPlugins(path) { log.i("首次加载 创建文件夹 %s ...", path); } else { log.i("开始扫描 %s 下的插件 ...", path); - updatePlugins(path); + createUpdate(path); var files = []; fs.list(path).forEach(function (file) { files.push(file.toFile()); @@ -32,15 +34,10 @@ function loadPlugins(path) { * 更新插件 * @param path */ -function updatePlugins(path) { +function createUpdate(path) { var update = fs.file(path, "update"); if (!update.exists()) { update.mkdirs(); - } else { - fs.list(update).forEach(function (file) { - log.i('自动升级插件 %s', file); - fs.move(file, fs.file(path, file.name), true); - }) } } @@ -71,6 +68,11 @@ function loadJsPlugins(files) { } function loadPlugin(file) { + var update = fs.file(fs.file(file.parentFile, 'update'), file.name); + if (update.exists()) { + log.i('自动升级插件 %s', file.name); + fs.move(update, file, true); + } var plugin = require(file, { cache: false, // 给插件注入单独的 console @@ -115,11 +117,23 @@ function initPlugin(file, plugin){ plugin.__FILE__ = file; // 初始化 __DATA__ plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name); + // 初始化 getDataFolder() + plugin.getDataFolder = function() { + return plugin.__DATA__; + } // 初始化 getFile() plugin.getFile = function(name) { - return fs.file(plugin.__DATA__, name); + return fs.file(plugin.getDataFolder(), name); } - // 初始化 getConfig() + initPluginConfig(plugin); + initPluginCommand(plugin); + initPluginPermission(plugin); +} + +/** + * 初始化插件配置 + */ +function initPluginConfig(plugin){ /** * 获取配置文件 * @constructor @@ -137,7 +151,6 @@ function initPlugin(file, plugin){ return yaml.safeLoad(base.read(file)); } } - // 初始化 saveConfig() /** * 保存配置文件 * @constructor @@ -154,10 +167,6 @@ function initPlugin(file, plugin){ break; } } - // 初始化 getDataFolder() - plugin.getDataFolder = function() { - return plugin.__DATA__; - } // 初始化 config plugin.configFile = plugin.getFile('config.yml'); if (plugin.configFile.isFile()) { @@ -168,6 +177,37 @@ function initPlugin(file, 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 {