Search in sources :

Example 6 with HandlerList

use of org.bukkit.event.HandlerList in project BKCommonLib by bergerhealer.

the class CommonUtil method setListenerOrder.

private static void setListenerOrder(Listener listener, Class<?> eventClass, boolean first) {
    HandlerList list = getEventHandlerList(eventClass);
    final EventPriority prio = first ? EventPriority.LOWEST : EventPriority.MONITOR;
    synchronized (list) {
        EnumMap<EventPriority, ArrayList<RegisteredListener>> handlerSlots = BHandlerList.handlerslots.get(list);
        ArrayList<RegisteredListener> registeredListenerList = handlerSlots.get(prio);
        int requestedIndex = first ? 0 : (registeredListenerList.size() - 1);
        // Try to find the registered listener
        for (int i = 0; i < registeredListenerList.size(); i++) {
            RegisteredListener registeredListener = registeredListenerList.get(i);
            // Check that the Listener matches
            if (registeredListener.getListener() != listener) {
                continue;
            } else if (i == requestedIndex) {
                // Already in order, do not do anything
                return;
            }
            RegisteredListener[] allListeners = list.getRegisteredListeners();
            // Change order in list
            registeredListenerList.remove(i);
            registeredListenerList.add(requestedIndex, registeredListener);
            // Get the index of the listener in the baked array
            ArrayList<RegisteredListener> newListeners = new ArrayList<RegisteredListener>(allListeners.length);
            if (first) {
                newListeners.add(registeredListener);
                for (RegisteredListener otherListener : allListeners) {
                    if (otherListener != registeredListener) {
                        newListeners.add(otherListener);
                    }
                }
            } else {
                for (RegisteredListener otherListener : allListeners) {
                    if (otherListener != registeredListener) {
                        newListeners.add(otherListener);
                    }
                }
                newListeners.add(registeredListener);
            }
            // Transfer the listeners over
            if (newListeners.toArray(allListeners) != allListeners) {
                // Strange failure, ensure a bake
                list.bake();
            }
            return;
        }
    }
}
Also used : HandlerList(org.bukkit.event.HandlerList) BHandlerList(com.bergerkiller.reflection.org.bukkit.BHandlerList) RegisteredListener(org.bukkit.plugin.RegisteredListener) EventPriority(org.bukkit.event.EventPriority)

Aggregations

HandlerList (org.bukkit.event.HandlerList)6 RegisteredListener (org.bukkit.plugin.RegisteredListener)3 Method (java.lang.reflect.Method)2 MalformedURLException (java.net.MalformedURLException)2 AuthorNagException (org.bukkit.plugin.AuthorNagException)2 BHandlerList (com.bergerkiller.reflection.org.bukkit.BHandlerList)1 EventPriority (org.bukkit.event.EventPriority)1 IllegalPluginAccessException (org.bukkit.plugin.IllegalPluginAccessException)1 InvalidDescriptionException (org.bukkit.plugin.InvalidDescriptionException)1 InvalidPluginException (org.bukkit.plugin.InvalidPluginException)1 Plugin (org.bukkit.plugin.Plugin)1 TimedRegisteredListener (org.bukkit.plugin.TimedRegisteredListener)1 UnknownDependencyException (org.bukkit.plugin.UnknownDependencyException)1