feat: 完善命令部分 添加事件自动注销

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-10-11 14:20:06 +08:00
parent 6004590323
commit afdc39b1de
5 changed files with 73 additions and 59 deletions

View File

@ -10,8 +10,13 @@ var NoSuchFieldException = Java.type('java.lang.NoSuchFieldException');
var methodCache = []; var methodCache = [];
function Reflect(obj) { function Reflect(obj) {
this.obj = obj; if (obj instanceof Class) {
this.class = obj instanceof Class ? obj : obj.class; this.obj = null;
this.class = obj;
} else {
this.obj = obj;
this.class = obj.class;
}
this.field = function (name) { this.field = function (name) {
try { try {
// Try getting a public field // Try getting a public field
@ -27,9 +32,8 @@ function Reflect(obj) {
} }
}; };
this.method = function () { this.method = function (name, param) {
var name = arguments[0]; var clazzs = types(param);
var clazzs = Array.prototype.slice.call(arguments, 1);
try { try {
return this.class.getMethod(name, clazzs); return this.class.getMethod(name, clazzs);
} catch (ex) { } catch (ex) {
@ -37,11 +41,10 @@ function Reflect(obj) {
} }
}; };
this.cacheMethod = function () { this.cacheMethod = function (name, param) {
var name = arguments[0];
var mkey = this.class.name + '.' + name; var mkey = this.class.name + '.' + name;
if (!methodCache[mkey]) { if (!methodCache[mkey]) {
methodCache[mkey] = this.method(name, arguments.slice(1)); methodCache[mkey] = this.method(name, param);
} }
return methodCache[mkey]; return methodCache[mkey];
}; };
@ -91,10 +94,10 @@ function declaredConstructor(clazz, param) {
var constructor; var constructor;
try { try {
constructor = clazz.getDeclaredConstructor(types(param)); constructor = clazz.getDeclaredConstructor(types(param));
}catch(ex) { } catch (ex) {
try { try {
constructor = clazz.getDeclaredConstructor(types(param, true)); constructor = clazz.getDeclaredConstructor(types(param, true));
}catch(ex) { } catch (ex) {
constructor = clazz.getDeclaredConstructors()[0]; constructor = clazz.getDeclaredConstructors()[0];
} }
} }

View File

@ -41,21 +41,21 @@ function register(jsp, name, cmd) {
// //
// } // }
// }; // };
/
exports.on = function (jsp, name, exec) { exports.on = function (jsp, name, exec) {
var c = create(jsp, name); var c = create(jsp, name);
if (exec.cmd) { if (exec.cmd) {
c.setExecutor( c.setExecutor(function (sender, cmd, command, args) {
function (sender, cmd, command, args) { return exec.cmd(sender, command, args);
return exec.cmd(sender, command, args); });
}
);
} }
if (exec.tab) { if (exec.tab) {
c.setTabCompleter( c.setTabCompleter(function (sender, cmd, command, args) {
function (sender, cmd, command, args) { return Arrays.asList(exec.tab(sender, command, args));
return Arrays.asList(exec.tab(sender, command, args)); });
}
);
} }
}; };
exports.off = function () {
};

View File

@ -8,12 +8,17 @@ var Bukkit = Java.type("org.bukkit.Bukkit");
var Listener = Java.type("org.bukkit.event.Listener"); var Listener = Java.type("org.bukkit.event.Listener");
var Modifier = Java.type("java.lang.reflect.Modifier"); var Modifier = Java.type("java.lang.reflect.Modifier");
var BukkitEvent = Java.type("org.bukkit.event.Event"); var BukkitEvent = Java.type("org.bukkit.event.Event");
var HandlerList = Java.type('org.bukkit.event.HandlerList');
var EventPriority = Java.type("org.bukkit.event.EventPriority"); var EventPriority = Java.type("org.bukkit.event.EventPriority");
var EventExecutor = Java.type("org.bukkit.plugin.EventExecutor"); var EventExecutor = Java.type("org.bukkit.plugin.EventExecutor");
var IllegalStateException = Java.type("java.lang.IllegalStateException"); var IllegalStateException = Java.type("java.lang.IllegalStateException");
var plugin = base.plugin; var plugin = base.plugin;
var ref = require('kit/reflect');
var jspListener = [];
/** /**
* 扫描包 org.bukkit.event 下的所有事件 * 扫描包 org.bukkit.event 下的所有事件
* 映射简写名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent * 映射简写名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent
@ -69,12 +74,17 @@ function isVaildEvent(clz) {
/** /**
* 添加事件监听 * 添加事件监听
* @param jsp
* @param event * @param event
* @param exec {function} * @param exec {function}
* @param priority * @param priority
* @param ignoreCancel * @param ignoreCancel
*/ */
function listen(event, exec, priority, ignoreCancel) { function listen(jsp, event, exec, priority, ignoreCancel) {
var pname = jsp.description.name;
if (ext.isNull(pname)) {
throw new TypeError('插件名称为空 请检查传入参数!');
}
var eventCls = mapEvent[event]; var eventCls = mapEvent[event];
if (!eventCls) { if (!eventCls) {
try { try {
@ -90,6 +100,9 @@ function listen(event, exec, priority, ignoreCancel) {
if (ignoreCancel === undefined) { if (ignoreCancel === undefined) {
ignoreCancel = false; ignoreCancel = false;
} }
if (!jspListener[jsp.description.name]) {
jspListener[jsp.description.name] = [];
}
var listener = new Listener({}); var listener = new Listener({});
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
/** /**
@ -111,6 +124,9 @@ function listen(event, exec, priority, ignoreCancel) {
}), }),
plugin, plugin,
ignoreCancel); ignoreCancel);
// 添加到缓存 用于关闭插件的时候关闭事件
var listeners = jspListener[jsp.description.name];
listeners.push(listener);
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
log.d('注册事件 %s 方法 %s', eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name); log.d('注册事件 %s 方法 %s', eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name);
return { return {
@ -127,7 +143,7 @@ function unlisten(listener) {
if (!listener.event || !listener.listener) { if (!listener.event || !listener.listener) {
throw new IllegalStateException("非法的监听器对象 无法取消事件!"); throw new IllegalStateException("非法的监听器对象 无法取消事件!");
} }
listener.event.getMethod("getHandlerList").invoke(null).unregister(listener.listener); ref.on(listener.event).call('getHandlerList').get().unregister(listener.listener);
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
log.d('注销事件 %s', listener.event.simpleName); log.d('注销事件 %s', listener.event.simpleName);
} }
@ -139,5 +155,10 @@ mapEventName();
module.exports = { module.exports = {
on: listen, on: listen,
off: unlisten off: unlisten,
disable: function (jsp) {
jspListener[jsp.description.name].forEach(function (t) {
ref.on(HandlerList).call('unregisterAll', t);
})
}
}; };

View File

@ -5,6 +5,7 @@
/*global Java, base, module, exports, require, __FILE__*/ /*global Java, base, module, exports, require, __FILE__*/
// var zip = require("core/zip"); // var zip = require("core/zip");
var fs = require('core/fs'); var fs = require('core/fs');
var event = require('modules/event');
/** /**
* 载入插件 * 载入插件
@ -36,7 +37,8 @@ function updatePlugins(path) {
update.mkdirs(); update.mkdirs();
} else { } else {
fs.list(update).forEach(function (file) { fs.list(update).forEach(function (file) {
fs.move(fs.file(update, file.name), fs.file(path, file.name), true); log.i('自动升级插件 %s', file);
fs.move(file, fs.file(path, file.name), true);
}) })
} }
} }
@ -75,22 +77,30 @@ function loadJsPlugin(files) {
}) })
} }
function runAndCatch(name, exec) { function runAndCatch(jsp, exec, ext) {
if (exec) { if (exec) {
try { try {
exec(); // 绑定方法的this到插件自身
exec.bind(jsp)();
if (ext) {
ext();
}
} catch (ex) { } catch (ex) {
log.w('插件 %s 执行 %s 发生错误: %s', name, exec.name, ex.message); log.w('插件 %s 执行 %s 发生错误: %s', jsp.description.name, exec.name, ex.message);
ex.printStackTrace(); ex.printStackTrace();
} }
} }
} }
function checkAndGet(name) { function checkAndGet(args) {
if (args.length === 0) {
return plugins;
}
var name = args[0];
if (!exports.plugins[name]) { if (!exports.plugins[name]) {
throw new Error("插件 " + name + "不存在!"); throw new Error("插件 " + name + "不存在!");
} }
return exports.plugins[name]; return [exports.plugins[name]];
} }
var plugins = []; var plugins = [];
@ -106,32 +116,11 @@ exports.init = function (plugin, path) {
loadPlugins(path); loadPlugins(path);
}; };
exports.load = function () { exports.load = function () {
if (arguments.length === 0) { checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.load));
plugins.forEach(function (p) {
runAndCatch(p.description.name, p.load);
})
} else {
var p = checkAndGet(arguments[0]);
runAndCatch(p.description.name, p.load);
}
}; };
exports.enable = function () { exports.enable = function () {
if (arguments.length === 0) { checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.enable));
plugins.forEach(function (p) {
runAndCatch(p.description.name, p.enable);
})
} else {
var p = checkAndGet(arguments[0]);
runAndCatch(p.description.name, p.enable);
}
}; };
exports.disable = function () { exports.disable = function () {
if (arguments.length === 0) { checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.disable, function () event.disable(p)));
plugins.forEach(function (p) {
runAndCatch(p.description.name, p.disable);
})
} else {
var p = checkAndGet(arguments[0]);
runAndCatch(p.description.name, p.disable);
}
}; };

View File

@ -19,7 +19,7 @@ function load() {
function enable() { function enable() {
console.log('启用 Hello Wrold 测试插件!'); console.log('启用 Hello Wrold 测试插件!');
join = event.on('playerloginevent', function join(event) { join = event.on(this, 'playerloginevent', function join(event) {
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
log.d('玩家 %s 触发事件 %s', event.player.name, event.name); log.d('玩家 %s 触发事件 %s', event.player.name, event.name);
setTimeout(function () { setTimeout(function () {
@ -27,9 +27,9 @@ function enable() {
event.player.sendMessage(require("plugins/ext/papi").$(event.player, "§a欢迎来到 §bMiaoScript §a的世界! 当前在线: %server_online%")); event.player.sendMessage(require("plugins/ext/papi").$(event.player, "§a欢迎来到 §bMiaoScript §a的世界! 当前在线: %server_online%"));
}, 10); }, 10);
}); });
cmd.on(module.exports, 'hello', { cmd.on(this, 'hello', {
cmd: function (sender, command, args) { cmd: function (sender, command, args) {
console.log(command, args); console.log(command, args.join(' '));
return true; return true;
}, },
tab: function (sender, command, args) { tab: function (sender, command, args) {
@ -39,8 +39,9 @@ function enable() {
} }
function disable() { function disable() {
log.i('卸载 Hello Wrold 测试插件!'); console.log('卸载 Hello Wrold 测试插件!');
event.off(join); // 可以不用关闭事件 程序将自动处理
// event.off(join);
} }
module.exports = { module.exports = {