/*global Java, exports, org, __plugin */
var bkEventPriority = org.bukkit.event.EventPriority,
  bkEventExecutor = org.bukkit.plugin.EventExecutor,
  bkRegisteredListener = org.bukkit.plugin.RegisteredListener;

var nashorn = typeof Java != 'undefined';

function getHandlerListForEventType(eventType) {
  var result = null;
  var clazz = null;
  if (nashorn) {
    //Nashorn doesn't like when getHandlerList is in a superclass of your event
    //so to avoid this problem, call getHandlerList using java.lang.reflect
    //methods
    clazz = eventType['class'];
    result = clazz.getMethod('getHandlerList').invoke(null);
  } else {
    result = eventType.getHandlerList();
  }

  return result;
}
exports.on = function(
  /* Java Class */
  eventType,
  /* function( registeredListener, event) */

  handler,
  /* (optional) String (HIGH, HIGHEST, LOW, LOWEST, NORMAL, MONITOR), */
  priority
) {
  var handlerList, regd, eventExecutor;

  if (typeof priority == 'undefined') {
    priority = bkEventPriority.HIGHEST;
  } else {
    priority = bkEventPriority[priority.toUpperCase().trim()];
  }
  handlerList = getHandlerListForEventType(eventType);

  var result = {};
  eventExecutor = new bkEventExecutor({
    execute: function(l, evt) {
      function cancel() {
        if (evt instanceof org.bukkit.event.Cancellable) {
          evt.setCancelled(true);
        }
      }
      /*
       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;
      handler.call(bound, evt, cancel);
    }
  });
  /* 
   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.
   */
  regd = new bkRegisteredListener(
    __plugin,
    eventExecutor,
    priority,
    __plugin,
    false
  );
  handlerList.register(regd);
  result.unregister = function() {
    handlerList.unregister(regd);
  };
  return result;
};

@@ -209,51 +209,8 @@
 r;%0A%0A
-var nashorn = typeof Java != 'undefined';%0A%0A
 func
@@ -260,151 +260,223 @@
 %7B%0A  
-var result = null;%0A  var clazz = null;%0A  if (nashorn) %7B%0A    //Nashorn doesn't like when getHandlerList is in a superclass of your event%0A 
+// Nashorn doesn't make inherited static methods accessible on derived%0A  // classes (see https://stackoverflow.com/a/38258630), e.g.%0A  // org.bukkit.event.block.BlockBreakEvent.getHandlerList() does not work.%0A
   
- 
 //
-s
+ S
 o to
@@ -544,33 +544,30 @@
 ect%0A
-  
   //
+ 
 methods
+.
 %0A  
-  clazz =
+return
  eve
@@ -576,37 +576,14 @@
 Type
-%5B'
+.
 class
-'%5D;%0A    result = clazz
 .get
@@ -625,83 +625,10 @@
 l);%0A
-  %7D else %7B%0A    result = eventType.getHandlerList();%0A  %7D%0A%0A  return result;%0A
 %7D
+%0A
 %0Aexp

Simplify and better document workaround for static method inheritance