feat: 新增插件权限注册和命令注册
This commit is contained in:
parent
f6aebaa656
commit
b668795518
@ -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}}
|
||||
|
@ -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 () {
|
||||
|
||||
};
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user