use of org.bukkit.event.EventPriority 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;
}
}
}
Aggregations