feat: 优化JS插件权限和命令模块

This commit is contained in:
coding 2017-10-17 02:54:16 +00:00
parent 5b621d3d99
commit 2788a9a8aa
4 changed files with 72 additions and 35 deletions

View File

@ -7,6 +7,7 @@
/*global Java, base, module, exports, require, __FILE__*/ /*global Java, base, module, exports, require, __FILE__*/
var Bukkit = Java.type("org.bukkit.Bukkit"); var Bukkit = Java.type("org.bukkit.Bukkit");
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
var Server = Bukkit.server;
var PluginManager = Bukkit.pluginManager; var PluginManager = Bukkit.pluginManager;
exports.$ = Bukkit; exports.$ = Bukkit;
/** /**
@ -19,6 +20,22 @@ exports.nmsVersion = Bukkit.server.class.name.split('.')[3];
exports.nmsCls = function (name) { exports.nmsCls = function (name) {
return Java.type(['net.minecraft.server', exports.nmsVersion, name].join('.')); 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]);
}
}
/** /**
* 获取在线玩家 * 获取在线玩家
*/ */

View File

@ -12,7 +12,7 @@ var PluginCommand = Java.type('org.bukkit.command.PluginCommand');
var Arrays = Java.type('java.util.Arrays') var Arrays = Java.type('java.util.Arrays')
function init(jsp){ function enable(jsp){
var commands = jsp.description.commands; var commands = jsp.description.commands;
if(commands){ if(commands){
var pluginCmds = []; var pluginCmds = [];
@ -66,7 +66,8 @@ function on(jsp, name, exec) {
} }
} }
exports.init = init exports.enable = enable
exports.on = on; exports.on = on;
exports.off = function () { exports.off = function () {

View File

@ -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

View File

@ -9,6 +9,7 @@ var yaml = require('modules/yaml');
var event = require('modules/event'); var event = require('modules/event');
var bukkit = require('./bukkit'); var bukkit = require('./bukkit');
var command = require('./command'); var command = require('./command');
var permission = require('./permission');
/** /**
* 载入插件 * 载入插件
@ -126,8 +127,9 @@ function initPlugin(file, plugin){
return fs.file(plugin.getDataFolder(), name); return fs.file(plugin.getDataFolder(), name);
} }
initPluginConfig(plugin); 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) { function runAndCatch(jsp, exec, ext) {
if (exec) { if (exec) {
try { try {