feat: 新增插件权限注册和命令注册
This commit is contained in:
parent
f6aebaa656
commit
b668795518
@ -22,9 +22,14 @@ exports.nmsCls = function (name) {
|
|||||||
/**
|
/**
|
||||||
* 获取在线玩家
|
* 获取在线玩家
|
||||||
*/
|
*/
|
||||||
exports.players = function (func) {
|
exports.players = function(){
|
||||||
return Bukkit.onlinePlayers.forEach(func);
|
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}}
|
||||||
|
@ -5,45 +5,55 @@
|
|||||||
|
|
||||||
/*global Java, base, module, exports, require, __FILE__*/
|
/*global Java, base, module, exports, require, __FILE__*/
|
||||||
var plugin = base.plugin;
|
var plugin = base.plugin;
|
||||||
var bukkit = require('./bukkit');
|
|
||||||
var ref = require('core/reflect');
|
var ref = require('core/reflect');
|
||||||
var lookupNames = ref.on(bukkit.plugin.manager).get('lookupNames').get();
|
var bukkit = require('./bukkit');
|
||||||
var knownCommands = ref.on(bukkit.plugin.manager).get('commandMap').get('knownCommands').get();
|
var commandMap = ref.on(bukkit.plugin.manager).get('commandMap').get();
|
||||||
var PluginCommand = Java.type('org.bukkit.command.PluginCommand');
|
var PluginCommand = Java.type('org.bukkit.command.PluginCommand');
|
||||||
|
|
||||||
var Arrays = Java.type('java.util.Arrays')
|
var Arrays = Java.type('java.util.Arrays')
|
||||||
|
|
||||||
function create(jsp, name) {
|
function init(jsp){
|
||||||
var cmd = ref.on(PluginCommand).create(name, plugin).get();
|
var commands = jsp.description.commands;
|
||||||
register(jsp, name, cmd);
|
if(commands){
|
||||||
return cmd;
|
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) {
|
function get(name) {
|
||||||
if (name.isEmpty()) {
|
return commandMap.getCommand(name);
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
knownCommands.put(name, cmd);
|
function create(jsp, name) {
|
||||||
knownCommands.computeIfAbsent(jsp.description.name + ":" + name, function () {
|
return ref.on(PluginCommand).create(name, plugin).get();
|
||||||
return cmd;
|
}
|
||||||
});
|
|
||||||
knownCommands.computeIfAbsent('ms:' + jsp.description.name + ":" + name, function () {
|
function register(jsp, cmd){
|
||||||
return cmd;
|
commandMap.register(jsp.description.name, cmd);
|
||||||
});
|
|
||||||
lookupNames.put(name, plugin);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// var exec = {
|
// var exec = {
|
||||||
// onCommand: function (sender, cmd, command, args) {
|
// cmd: function (sender, command, args) {
|
||||||
//
|
//
|
||||||
// },
|
// },
|
||||||
// onTabComplete: function (sender, cmd, command, args) {
|
// tab: function (sender, command, args) {
|
||||||
//
|
//
|
||||||
// }
|
// }
|
||||||
// };
|
// };
|
||||||
|
function on(jsp, name, exec) {
|
||||||
exports.on = function (jsp, name, exec) {
|
var c = get(name) || create(jsp, name);
|
||||||
var c = create(jsp, name);
|
|
||||||
if (exec.cmd) {
|
if (exec.cmd) {
|
||||||
c.setExecutor(function (sender, cmd, command, args) {
|
c.setExecutor(function (sender, cmd, command, args) {
|
||||||
return exec.cmd(sender, 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));
|
return Arrays.asList(exec.tab(sender, command, args));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
exports.init = init
|
||||||
|
exports.on = on;
|
||||||
exports.off = function () {
|
exports.off = function () {
|
||||||
|
|
||||||
};
|
};
|
@ -7,6 +7,8 @@
|
|||||||
var fs = require('core/fs');
|
var fs = require('core/fs');
|
||||||
var yaml = require('modules/yaml');
|
var yaml = require('modules/yaml');
|
||||||
var event = require('modules/event');
|
var event = require('modules/event');
|
||||||
|
var bukkit = require('./bukkit');
|
||||||
|
var command = require('./command');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 载入插件
|
* 载入插件
|
||||||
@ -18,7 +20,7 @@ function loadPlugins(path) {
|
|||||||
log.i("首次加载 创建文件夹 %s ...", path);
|
log.i("首次加载 创建文件夹 %s ...", path);
|
||||||
} else {
|
} else {
|
||||||
log.i("开始扫描 %s 下的插件 ...", path);
|
log.i("开始扫描 %s 下的插件 ...", path);
|
||||||
updatePlugins(path);
|
createUpdate(path);
|
||||||
var files = [];
|
var files = [];
|
||||||
fs.list(path).forEach(function (file) {
|
fs.list(path).forEach(function (file) {
|
||||||
files.push(file.toFile());
|
files.push(file.toFile());
|
||||||
@ -32,15 +34,10 @@ function loadPlugins(path) {
|
|||||||
* 更新插件
|
* 更新插件
|
||||||
* @param path
|
* @param path
|
||||||
*/
|
*/
|
||||||
function updatePlugins(path) {
|
function createUpdate(path) {
|
||||||
var update = fs.file(path, "update");
|
var update = fs.file(path, "update");
|
||||||
if (!update.exists()) {
|
if (!update.exists()) {
|
||||||
update.mkdirs();
|
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) {
|
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, {
|
var plugin = require(file, {
|
||||||
cache: false,
|
cache: false,
|
||||||
// 给插件注入单独的 console
|
// 给插件注入单独的 console
|
||||||
@ -115,11 +117,23 @@ function initPlugin(file, plugin){
|
|||||||
plugin.__FILE__ = file;
|
plugin.__FILE__ = file;
|
||||||
// 初始化 __DATA__
|
// 初始化 __DATA__
|
||||||
plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name);
|
plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name);
|
||||||
|
// 初始化 getDataFolder()
|
||||||
|
plugin.getDataFolder = function() {
|
||||||
|
return plugin.__DATA__;
|
||||||
|
}
|
||||||
// 初始化 getFile()
|
// 初始化 getFile()
|
||||||
plugin.getFile = function(name) {
|
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
|
* @constructor
|
||||||
@ -137,7 +151,6 @@ function initPlugin(file, plugin){
|
|||||||
return yaml.safeLoad(base.read(file));
|
return yaml.safeLoad(base.read(file));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 初始化 saveConfig()
|
|
||||||
/**
|
/**
|
||||||
* 保存配置文件
|
* 保存配置文件
|
||||||
* @constructor
|
* @constructor
|
||||||
@ -154,10 +167,6 @@ function initPlugin(file, plugin){
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 初始化 getDataFolder()
|
|
||||||
plugin.getDataFolder = function() {
|
|
||||||
return plugin.__DATA__;
|
|
||||||
}
|
|
||||||
// 初始化 config
|
// 初始化 config
|
||||||
plugin.configFile = plugin.getFile('config.yml');
|
plugin.configFile = plugin.getFile('config.yml');
|
||||||
if (plugin.configFile.isFile()) {
|
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) {
|
function runAndCatch(jsp, exec, ext) {
|
||||||
if (exec) {
|
if (exec) {
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user