2014-09-30 00:42:41 +02:00
|
|
|
var cmPriority = Packages.net.canarymod.plugin.Priority,
|
|
|
|
cmCanary = Packages.net.canarymod.Canary,
|
|
|
|
cmDispatcher = Packages.net.canarymod.hook.Dispatcher,
|
|
|
|
cmRegisteredPluginListener = Packages.net.canarymod.plugin.RegisteredPluginListener,
|
|
|
|
cmPluginListener = Packages.net.canarymod.plugin.PluginListener;
|
|
|
|
var cmHookExecutor = cmCanary.hooks();
|
|
|
|
|
|
|
|
exports.on = function(
|
|
|
|
/* Java Class */
|
|
|
|
eventType,
|
|
|
|
/* function( registeredListener, event) */
|
|
|
|
handler,
|
|
|
|
/* (optional) String (CRITICAL, HIGH, NORMAL, LOW, PASSIVE), */
|
|
|
|
priority ) {
|
|
|
|
var handlerList,
|
|
|
|
regd,
|
|
|
|
eventExecutor;
|
|
|
|
|
|
|
|
if ( typeof priority == 'undefined' ) {
|
|
|
|
priority = cmPriority.NORMAL;
|
|
|
|
} else {
|
|
|
|
priority = cmPriority[priority.toUpperCase().trim()];
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = { };
|
2014-10-19 16:41:06 +02:00
|
|
|
eventExecutor = __plugin.getDispatcher( function(l,e){
|
|
|
|
try {
|
|
|
|
handler.call(result, e);
|
|
|
|
} catch ( error ){
|
|
|
|
console.log('Error while executing handler:' + handler +
|
|
|
|
' for event type:' + eventType +
|
|
|
|
' error: ' + error);
|
2014-09-30 00:42:41 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
/*
|
|
|
|
wph 20130222 issue #64 bad interaction with Essentials plugin
|
|
|
|
if another plugin tries to unregister a Listener (not a Plugin or a RegisteredListener)
|
|
|
|
then BOOM! the other plugin will throw an error because Rhino can't coerce an
|
|
|
|
equals() method from an Interface.
|
|
|
|
The workaround is to make the ScriptCraftPlugin java class a Listener.
|
|
|
|
Should only unregister() registered plugins in ScriptCraft js code.
|
|
|
|
*/
|
2014-10-19 16:41:06 +02:00
|
|
|
try {
|
|
|
|
// nashorn
|
|
|
|
eventType = eventType.class;
|
|
|
|
} catch ( e ){
|
|
|
|
// non-nashorn
|
|
|
|
eventType = eventType;
|
|
|
|
}
|
2014-09-30 00:42:41 +02:00
|
|
|
regd = new cmPluginListener({});
|
2014-10-19 16:41:06 +02:00
|
|
|
cmHookExecutor.registerHook(regd, __plugin, eventType, eventExecutor, priority);
|
2014-09-30 00:42:41 +02:00
|
|
|
result.unregister = function(){
|
|
|
|
cmHookExecutor.unregisterPluginListener(regd);
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
};
|