2014-12-25 15:04:45 +01:00
|
|
|
/*global nashorn, exports, require, Packages, __plugin*/
|
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){
|
2015-01-18 21:27:37 +01:00
|
|
|
function cancel(){
|
|
|
|
if (e.setCanceled){
|
|
|
|
e.setCanceled();
|
|
|
|
}
|
|
|
|
}
|
2015-02-14 12:50:20 +01:00
|
|
|
/*
|
|
|
|
let handlers use this.cancel() to cancel the current event
|
|
|
|
or this.unregister() to unregister from future events.
|
|
|
|
*/
|
|
|
|
var bound = {};
|
|
|
|
for (var i in result){
|
|
|
|
bound[i] = result[i];
|
|
|
|
}
|
|
|
|
bound.cancel = cancel;
|
2014-10-19 16:41:06 +02:00
|
|
|
try {
|
2015-02-14 12:50:20 +01:00
|
|
|
handler.call(bound, e, cancel);
|
2014-10-19 16:41:06 +02:00
|
|
|
} catch ( error ){
|
|
|
|
console.log('Error while executing handler:' + handler +
|
2015-01-18 21:27:37 +01:00
|
|
|
' 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-12-25 15:04:45 +01:00
|
|
|
if (nashorn){
|
2014-10-19 16:41:06 +02:00
|
|
|
// nashorn
|
2014-12-25 15:04:45 +01:00
|
|
|
eventType = require('nashorn-type')(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;
|
|
|
|
};
|