Fixed event priorities

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2011-02-19 20:43:35 +00:00
parent a36de75a17
commit 314bf387b5
2 changed files with 22 additions and 19 deletions

View File

@ -3,7 +3,6 @@ package org.bukkit.plugin;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.EventExecutor;
/** /**
* Stores relevant information for plugin listeners * Stores relevant information for plugin listeners

View File

@ -9,8 +9,9 @@ import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set; import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -29,7 +30,18 @@ public final class SimplePluginManager implements PluginManager {
private final Map<Pattern, PluginLoader> fileAssociations = new HashMap<Pattern, PluginLoader>(); private final Map<Pattern, PluginLoader> fileAssociations = new HashMap<Pattern, PluginLoader>();
private final List<Plugin> plugins = new ArrayList<Plugin>(); private final List<Plugin> plugins = new ArrayList<Plugin>();
private final Map<String, Plugin> lookupNames = new HashMap<String, Plugin>(); private final Map<String, Plugin> lookupNames = new HashMap<String, Plugin>();
private final Map<Event.Type, PriorityQueue<RegisteredListener>> listeners = new EnumMap<Event.Type, PriorityQueue<RegisteredListener>>(Event.Type.class); private final Map<Event.Type, SortedSet<RegisteredListener>> listeners = new EnumMap<Event.Type, SortedSet<RegisteredListener>>(Event.Type.class);
private final Comparator<RegisteredListener> comparer = new Comparator<RegisteredListener>() {
public int compare(RegisteredListener i, RegisteredListener j) {
int result = i.getPriority().compareTo(j.getPriority());
if ((result == 0) && (i != j)) {
result = 1;
}
return result;
}
};
public SimplePluginManager(Server instance) { public SimplePluginManager(Server instance) {
server = instance; server = instance;
@ -205,11 +217,10 @@ public final class SimplePluginManager implements PluginManager {
* @param event Event details * @param event Event details
*/ */
public void callEvent(Event event) { public void callEvent(Event event) {
PriorityQueue<RegisteredListener> eventListeners = listeners.get(event.getType()); SortedSet<RegisteredListener> eventListeners = listeners.get(event.getType());
if (eventListeners != null) { if (eventListeners != null) {
for (RegisteredListener registration : eventListeners) { for (RegisteredListener registration : eventListeners) {
// NOTE: no need to check isEnabled as all disabled plugins have their listeners disabled
try { try {
registration.callEvent( event ); registration.callEvent( event );
} catch (Throwable ex) { } catch (Throwable ex) {
@ -228,7 +239,7 @@ public final class SimplePluginManager implements PluginManager {
* @param plugin Plugin to register * @param plugin Plugin to register
*/ */
public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin) { public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin) {
getEventListeners( type ).offer(new RegisteredListener(listener, priority, plugin, type)); getEventListeners( type ).add(new RegisteredListener(listener, priority, plugin, type));
} }
/** /**
@ -241,30 +252,23 @@ public final class SimplePluginManager implements PluginManager {
* @param plugin Plugin to register * @param plugin Plugin to register
*/ */
public void registerEvent(Event.Type type, Listener listener, EventExecutor executor, Priority priority, Plugin plugin) { public void registerEvent(Event.Type type, Listener listener, EventExecutor executor, Priority priority, Plugin plugin) {
getEventListeners( type ).offer(new RegisteredListener(listener, executor, priority, plugin)); getEventListeners( type ).add(new RegisteredListener(listener, executor, priority, plugin));
} }
/** /**
* Returns a PriorityQueue of RegisteredListener for the specified event type creating a new queue if needed * Returns a SortedSet of RegisteredListener for the specified event type creating a new queue if needed
* *
* @param type EventType to lookup * @param type EventType to lookup
* @return PriorityQueue<RegisteredListener> the looked up or create queue matching the requested type * @return SortedSet<RegisteredListener> the looked up or create queue matching the requested type
*/ */
private PriorityQueue<RegisteredListener> getEventListeners( Event.Type type ) private SortedSet<RegisteredListener> getEventListeners(Event.Type type) {
{ SortedSet<RegisteredListener> eventListeners = listeners.get(type);
PriorityQueue<RegisteredListener> eventListeners = listeners.get(type);
if (eventListeners != null) { if (eventListeners != null) {
return eventListeners; return eventListeners;
} }
eventListeners = new PriorityQueue<RegisteredListener>( eventListeners = new TreeSet<RegisteredListener>(comparer);
11, new Comparator<RegisteredListener>() {
public int compare(RegisteredListener i, RegisteredListener j) {
return i.getPriority().compareTo(j.getPriority());
}
}
);
listeners.put(type, eventListeners); listeners.put(type, eventListeners);
return eventListeners; return eventListeners;
} }