Event hooks were ordered in backwards order versus the expected way. To avoid sorting problems and manual sorting, changed priority to be handled by a PriorityQueue<>() rather than a manually sorted List().

Resolves issue 211 http://bugs.bukkit.org/issues/211

By: mcasper <mcasper@FLARE>
This commit is contained in:
Bukkit/Spigot 2011-01-19 10:23:00 +08:00
parent 4676f99999
commit 14009cb350

View File

@ -4,10 +4,12 @@ package org.bukkit.plugin;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -27,7 +29,7 @@ public final class SimplePluginManager implements PluginManager {
private final Map<Pattern, PluginLoader> fileAssociations = new HashMap<Pattern, PluginLoader>();
private final List<Plugin> plugins = new ArrayList<Plugin>();
private final Map<String, Plugin> lookupNames = new HashMap<String, Plugin>();
private final Map<Event.Type, List<RegisteredListener>> listeners = new EnumMap<Event.Type, List<RegisteredListener>>(Event.Type.class);
private final Map<Event.Type, PriorityQueue<RegisteredListener>> listeners = new EnumMap<Event.Type, PriorityQueue<RegisteredListener>>(Event.Type.class);
public SimplePluginManager(Server instance) {
server = instance;
@ -187,7 +189,7 @@ public final class SimplePluginManager implements PluginManager {
* @param event Event details
*/
public void callEvent(Event event) {
List<RegisteredListener> eventListeners = listeners.get(event.getType());
PriorityQueue<RegisteredListener> eventListeners = listeners.get(event.getType());
if (eventListeners != null) {
for (RegisteredListener registration : eventListeners) {
@ -214,22 +216,20 @@ public final class SimplePluginManager implements PluginManager {
* @param plugin Plugin to register
*/
public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin) {
List<RegisteredListener> eventListeners = listeners.get(type);
PriorityQueue<RegisteredListener> eventListeners = listeners.get(type);
int position = 0;
if (eventListeners != null) {
for (RegisteredListener registration : eventListeners) {
if (registration.getPriority().compareTo(priority) < 0) {
break;
}
position++;
}
} else {
eventListeners = new ArrayList<RegisteredListener>();
if (eventListeners == null) {
eventListeners = new PriorityQueue<RegisteredListener>(11,
new Comparator<RegisteredListener>( ) {
public int compare(RegisteredListener i, RegisteredListener j) {
return i.getPriority().compareTo(j.getPriority());
}
}
);
listeners.put(type, eventListeners);
}
eventListeners.add(position, new RegisteredListener(listener, priority, plugin));
eventListeners.offer(new RegisteredListener(listener, priority, plugin));
}
}