@ -19,14 +19,14 @@ 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(){
|
||||
exports.player = function () {
|
||||
switch (arguments.length) {
|
||||
case 0:
|
||||
return undefined;
|
||||
@ -35,18 +35,18 @@ exports.player = function(){
|
||||
default:
|
||||
return Bukkit.getPlayerExtra(arguments[0]);
|
||||
}
|
||||
}
|
||||
};
|
||||
/**
|
||||
* 获取在线玩家
|
||||
*/
|
||||
exports.players = function(){
|
||||
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}}
|
||||
|
@ -13,13 +13,13 @@ var PluginCommand = Java.type('org.bukkit.command.PluginCommand');
|
||||
var StringUtil = Java.type('org.bukkit.util.StringUtil');
|
||||
|
||||
var ArrayList = Java.type('java.util.ArrayList');
|
||||
var Arrays = Java.type('java.util.Arrays')
|
||||
var Arrays = Java.type('java.util.Arrays');
|
||||
|
||||
function enable(jsp){
|
||||
function enable(jsp) {
|
||||
var commands = jsp.description.commands;
|
||||
if(commands){
|
||||
if (commands) {
|
||||
var pluginCmds = [];
|
||||
for (var name in commands){
|
||||
for (var name in commands) {
|
||||
var command = commands[name];
|
||||
if (typeof command !== 'object') continue;
|
||||
var newCmd = create(jsp, name);
|
||||
@ -29,7 +29,7 @@ function enable(jsp){
|
||||
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);
|
||||
console.debug('插件 %s 注册命令 %s ...', jsp.description.name, name);
|
||||
}
|
||||
commandMap.registerAll(jsp.description.name, Arrays.asList(pluginCmds));
|
||||
}
|
||||
@ -43,7 +43,7 @@ function create(jsp, name) {
|
||||
return register(jsp, ref.on(PluginCommand).create(name, plugin).get());
|
||||
}
|
||||
|
||||
function register(jsp, cmd){
|
||||
function register(jsp, cmd) {
|
||||
commandMap.register(jsp.description.name, cmd);
|
||||
return cmd;
|
||||
}
|
||||
@ -59,13 +59,13 @@ function register(jsp, cmd){
|
||||
|
||||
function on(jsp, name, exec) {
|
||||
var c = get(name) || create(jsp, name);
|
||||
log.d('插件 %s 设置命令 %s(%s) 执行器 ...', jsp.description.name, name, c);
|
||||
console.debug('插件 %s 设置命令 %s(%s) 执行器 ...', jsp.description.name, name, c);
|
||||
if (exec.cmd) {
|
||||
c.setExecutor(function (sender, cmd, command, args) {
|
||||
try {
|
||||
return exec.cmd(sender, command, args);
|
||||
} catch (ex) {
|
||||
log.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常 §4%s', sender.name, jsp.description.name, command, Java.from(args).join(' '), ex);
|
||||
console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6命令时发生异常 §4%s', sender.name, jsp.description.name, command, Java.from(args).join(' '), ex);
|
||||
console.ex(ex);
|
||||
}
|
||||
});
|
||||
@ -78,14 +78,14 @@ function on(jsp, name, exec) {
|
||||
StringUtil.copyPartialMatches(token, Arrays.asList(exec.tab(sender, command, args)), completions);
|
||||
return completions;
|
||||
} catch (ex) {
|
||||
log.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常 §4%s',sender.name ,jsp.description.name, command, Java.from(args).join(' '), ex);
|
||||
console.console('§6玩家 §a%s §6执行 §b%s §6插件 §d%s %s §6补全时发生异常 §4%s', sender.name, jsp.description.name, command, Java.from(args).join(' '), ex);
|
||||
console.ex(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
exports.enable = enable
|
||||
exports.enable = enable;
|
||||
|
||||
exports.on = on;
|
||||
exports.off = function () {
|
||||
|
@ -46,7 +46,7 @@ function mapEventName() {
|
||||
if (isVaildEvent(clz)) {
|
||||
// noinspection JSUnresolvedVariable
|
||||
var simpleName = clz.simpleName.toLowerCase();
|
||||
log.fd("Mapping Event [%s] => %s", clz.name, simpleName);
|
||||
console.debug("Mapping Event [%s] => %s", clz.name, simpleName);
|
||||
mapEvent[simpleName] = clz;
|
||||
count++;
|
||||
}
|
||||
@ -85,12 +85,12 @@ function isVaildEvent(clz) {
|
||||
function listen(jsp, event, exec, priority, ignoreCancel) {
|
||||
var name = jsp.description.name;
|
||||
if (ext.isNull(name)) throw new TypeError('插件名称为空 请检查传入参数!');
|
||||
var eventCls = mapEvent[event] || mapEvent[event.toLowerCase()] || mapEvent[event +'Event'] || mapEvent[event.toLowerCase() + 'event'];
|
||||
var eventCls = mapEvent[event] || mapEvent[event.toLowerCase()] || mapEvent[event + 'Event'] || mapEvent[event.toLowerCase() + 'event'];
|
||||
if (!eventCls) {
|
||||
try {
|
||||
eventCls = base.getClass(eventCls);
|
||||
} catch (ex) {
|
||||
log.w("事件 %s 未找到!", event);
|
||||
console.warn("事件 %s 未找到!", event);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -115,10 +115,10 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
|
||||
EventPriority[priority],
|
||||
new EventExecutor({
|
||||
execute: function (listener, event) {
|
||||
try{
|
||||
try {
|
||||
exec(event);
|
||||
} catch (ex){
|
||||
log.console('§6插件 §b%s §6处理 §d%s §6事件时发生异常 §4%s', name, event.class.simpleName, ex);
|
||||
} catch (ex) {
|
||||
console.console('§6插件 §b%s §6处理 §d%s §6事件时发生异常 §4%s', name, event.class.simpleName, ex);
|
||||
console.ex(ex);
|
||||
}
|
||||
}
|
||||
@ -131,20 +131,20 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
|
||||
var off = {
|
||||
event: eventCls,
|
||||
listener: listener,
|
||||
off: function(){
|
||||
off: function () {
|
||||
ref.on(this.event).call('getHandlerList').get().unregister(this.listener);
|
||||
log.d('插件 %s 注销事件 %s', name, this.event.simpleName);
|
||||
console.debug('插件 %s 注销事件 %s', name, this.event.simpleName);
|
||||
}
|
||||
}
|
||||
listeners.push(off);
|
||||
// noinspection JSUnresolvedVariable
|
||||
log.d('插件 %s 注册事件 %s 方法 %s', name, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name);
|
||||
console.debug('插件 %s 注册事件 %s 方法 %s', name, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name);
|
||||
return off;
|
||||
}
|
||||
|
||||
var mapEvent = [];
|
||||
// 映射事件名称
|
||||
log.i('Bukkit 事件映射完毕 共计 %s 个事件!', mapEventName().toFixed(0));
|
||||
console.info('Bukkit 事件映射完毕 共计 %s 个事件!', mapEventName().toFixed(0));
|
||||
|
||||
module.exports = {
|
||||
on: listen,
|
||||
|
@ -12,37 +12,37 @@ var manager = require('./bukkit').plugin.manager;
|
||||
var Permission = Java.type("org.bukkit.permissions.Permission");
|
||||
var PermissionDefault = Java.type('org.bukkit.permissions.PermissionDefault');
|
||||
|
||||
function enable (plugin) {
|
||||
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);
|
||||
}
|
||||
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!
|
||||
}
|
||||
console.debug('插件 %s 注册权限 %s Default %s ...', plugin.description.name, name, def);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function disable (plugin) {
|
||||
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);
|
||||
}
|
||||
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!
|
||||
}
|
||||
console.debug('插件 %s 注销权限 %s ...', plugin.description.name, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.enable = enable
|
||||
exports.disable = disable
|
||||
exports.enable = enable;
|
||||
exports.disable = disable;
|
@ -18,9 +18,9 @@ var permission = require('./permission');
|
||||
function loadPlugins(dir) {
|
||||
var plugin = fs.file(root, dir);
|
||||
if (!plugin) {
|
||||
log.i("首次加载 创建文件夹 %s ...", plugin);
|
||||
console.info("首次加载 创建文件夹 %s ...", plugin);
|
||||
} else {
|
||||
log.i("开始扫描 %s 下的插件 ...", plugin);
|
||||
console.info("开始扫描 %s 下的插件 ...", plugin);
|
||||
createUpdate(plugin);
|
||||
var files = [];
|
||||
fs.list(plugin).forEach(function (file) {
|
||||
@ -71,8 +71,8 @@ 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);
|
||||
console.info('自动升级插件 %s', file.name);
|
||||
fs.move(update, file, true);
|
||||
}
|
||||
var plugin = require(file, {
|
||||
cache: false,
|
||||
@ -80,16 +80,16 @@ function loadPlugin(file) {
|
||||
return beforeLoadHook(origin);
|
||||
}
|
||||
});
|
||||
log.d("插件编译结果: %s", plugin.toJson());
|
||||
console.debug("插件编译结果: %s", plugin.toJson());
|
||||
var desc = plugin.description;
|
||||
if (!desc || !desc.name) {
|
||||
log.w("文件 %s 不存在 description 描述信息 无法加载插件!", file);
|
||||
console.warn("文件 %s 不存在 description 描述信息 无法加载插件!", file);
|
||||
} else {
|
||||
initPlugin(file, plugin);
|
||||
afterLoadHook(plugin);
|
||||
plugins.push(plugin);
|
||||
plugins[plugin.description.name] = plugin;
|
||||
log.i('载入插件 %s 版本 %s By %s', desc.name, desc.version || '未知', desc.author || '未知');
|
||||
console.info('载入插件 %s 版本 %s By %s', desc.name, desc.version || '未知', desc.author || '未知');
|
||||
}
|
||||
return plugin;
|
||||
}
|
||||
@ -110,7 +110,7 @@ function afterLoadHook(plugin) {
|
||||
// 给 console 添加插件名称
|
||||
plugin.console.name = plugin.description.name;
|
||||
// 赋值 self
|
||||
for (var i in plugin){
|
||||
for (var i in plugin) {
|
||||
plugin.self[i] = plugin[i];
|
||||
}
|
||||
}
|
||||
@ -118,15 +118,19 @@ function afterLoadHook(plugin) {
|
||||
/**
|
||||
* 初始化插件内容(提供config,__DATA__等参数)
|
||||
*/
|
||||
function initPlugin(file, plugin){
|
||||
function initPlugin(file, plugin) {
|
||||
// 初始化 __FILE__
|
||||
plugin.__FILE__ = file;
|
||||
// 初始化 __DATA__
|
||||
plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name);
|
||||
// 初始化 getDataFolder()
|
||||
plugin.getDataFolder = function() { return plugin.__DATA__; }
|
||||
plugin.getDataFolder = function () {
|
||||
return plugin.__DATA__;
|
||||
};
|
||||
// 初始化 getFile()
|
||||
plugin.getFile = function(name) { return fs.file(plugin.getDataFolder(), name); }
|
||||
plugin.getFile = function (name) {
|
||||
return fs.file(plugin.getDataFolder(), name);
|
||||
};
|
||||
|
||||
// 初始化插件配置相关方法
|
||||
initPluginConfig(plugin);
|
||||
@ -138,7 +142,7 @@ function initPlugin(file, plugin){
|
||||
/**
|
||||
* 初始化插件配置
|
||||
*/
|
||||
function initPluginConfig(plugin){
|
||||
function initPluginConfig(plugin) {
|
||||
// 初始化 config
|
||||
plugin.configFile = plugin.getFile('config.yml');
|
||||
/**
|
||||
@ -146,7 +150,7 @@ function initPluginConfig(plugin){
|
||||
* @constructor
|
||||
* @constructor (file|string)
|
||||
*/
|
||||
plugin.getConfig = function() {
|
||||
plugin.getConfig = function () {
|
||||
switch (arguments.length) {
|
||||
case 0:
|
||||
return plugin.config;
|
||||
@ -157,34 +161,34 @@ function initPluginConfig(plugin){
|
||||
}
|
||||
return yaml.safeLoad(base.read(file));
|
||||
}
|
||||
}
|
||||
};
|
||||
/**
|
||||
* 重载配置文件
|
||||
* @constructor
|
||||
* @constructor (file|string)
|
||||
*/
|
||||
plugin.reloadConfig = function() {
|
||||
plugin.reloadConfig = function () {
|
||||
plugin.config = plugin.getConfig(plugin.configFile);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* 保存配置文件
|
||||
* @constructor
|
||||
* @constructor (file, content)
|
||||
*/
|
||||
plugin.saveConfig = function() {
|
||||
plugin.saveConfig = function () {
|
||||
switch (arguments.length) {
|
||||
case 0:
|
||||
plugin.configFile.parentFile.mkdirs()
|
||||
plugin.configFile.parentFile.mkdirs();
|
||||
base.save(plugin.configFile, plugin.config.toYaml());
|
||||
break;
|
||||
case 2:
|
||||
base.save(arguments[0], arguments[1].toYaml());
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
if (plugin.configFile.isFile()) {
|
||||
plugin.config = plugin.getConfig('config.yml');
|
||||
} else if (plugin.description.config ){
|
||||
} else if (plugin.description.config) {
|
||||
plugin.config = plugin.description.config;
|
||||
plugin.saveConfig();
|
||||
}
|
||||
@ -195,20 +199,28 @@ function runAndCatch(jsp, exec, ext) {
|
||||
try {
|
||||
// 绑定方法的this到插件自身
|
||||
exec.bind(jsp)();
|
||||
if (ext) { ext(); }
|
||||
if (ext) {
|
||||
ext();
|
||||
}
|
||||
} catch (ex) {
|
||||
log.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s', jsp.description.name, exec.name, ex.message);
|
||||
console.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s', jsp.description.name, exec.name, ex.message);
|
||||
console.ex(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function checkAndGet(args) {
|
||||
if (args.length === 0) { return plugins; }
|
||||
if (args.length === 0) {
|
||||
return plugins;
|
||||
}
|
||||
var name = args[0];
|
||||
// 如果是插件 则直接返回
|
||||
if (name.description) { return [name]; }
|
||||
if (!exports.plugins[name]) { throw new Error("插件 " + name + " 不存在!"); }
|
||||
if (name.description) {
|
||||
return [name];
|
||||
}
|
||||
if (!exports.plugins[name]) {
|
||||
throw new Error("插件 " + name + " 不存在!");
|
||||
}
|
||||
return [exports.plugins[name]];
|
||||
}
|
||||
|
||||
@ -220,7 +232,7 @@ exports.init = function (plugin, path) {
|
||||
if (plugin !== null) {
|
||||
// 如果过plugin不等于null 则代表是正式环境
|
||||
exports.$ = plugin;
|
||||
log.i("初始化 MiaoScript 插件系统 版本: %s", plugin.description.version);
|
||||
console.info("初始化 MiaoScript 插件系统 版本: %s", plugin.description.version);
|
||||
}
|
||||
loadPlugins(path);
|
||||
};
|
||||
@ -231,7 +243,7 @@ exports.enable = function () {
|
||||
checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.enable));
|
||||
};
|
||||
exports.disable = function () {
|
||||
checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.disable, function(){
|
||||
checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.disable, function () {
|
||||
event.disable(p);
|
||||
// task.cancel();
|
||||
}));
|
||||
|
Reference in New Issue
Block a user