From 314bf387b5b68705baffead3ece2da7833d3f6fd Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sat, 19 Feb 2011 20:43:35 +0000 Subject: [PATCH] Fixed event priorities By: Dinnerbone --- .../org/bukkit/plugin/RegisteredListener.java | 1 - .../bukkit/plugin/SimplePluginManager.java | 40 ++++++++++--------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/plugin/RegisteredListener.java b/paper-api/src/main/java/org/bukkit/plugin/RegisteredListener.java index fc2f53bb0f..b281f29283 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/RegisteredListener.java +++ b/paper-api/src/main/java/org/bukkit/plugin/RegisteredListener.java @@ -3,7 +3,6 @@ package org.bukkit.plugin; import org.bukkit.event.Event; import org.bukkit.event.Listener; -import org.bukkit.plugin.EventExecutor; /** * Stores relevant information for plugin listeners diff --git a/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java index 2af45672aa..1b6127883a 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java +++ b/paper-api/src/main/java/org/bukkit/plugin/SimplePluginManager.java @@ -9,8 +9,9 @@ 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.SortedSet; +import java.util.TreeSet; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; @@ -29,7 +30,18 @@ public final class SimplePluginManager implements PluginManager { private final Map fileAssociations = new HashMap(); private final List plugins = new ArrayList(); private final Map lookupNames = new HashMap(); - private final Map> listeners = new EnumMap>(Event.Type.class); + private final Map> listeners = new EnumMap>(Event.Type.class); + private final Comparator comparer = new Comparator() { + 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) { server = instance; @@ -205,11 +217,10 @@ public final class SimplePluginManager implements PluginManager { * @param event Event details */ public void callEvent(Event event) { - PriorityQueue eventListeners = listeners.get(event.getType()); + SortedSet eventListeners = listeners.get(event.getType()); if (eventListeners != null) { for (RegisteredListener registration : eventListeners) { - // NOTE: no need to check isEnabled as all disabled plugins have their listeners disabled try { registration.callEvent( event ); } catch (Throwable ex) { @@ -228,7 +239,7 @@ public final class SimplePluginManager implements PluginManager { * @param plugin Plugin to register */ 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 */ 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 - * @return PriorityQueue the looked up or create queue matching the requested type + * @return SortedSet the looked up or create queue matching the requested type */ - private PriorityQueue getEventListeners( Event.Type type ) - { - PriorityQueue eventListeners = listeners.get(type); + private SortedSet getEventListeners(Event.Type type) { + SortedSet eventListeners = listeners.get(type); if (eventListeners != null) { return eventListeners; } - eventListeners = new PriorityQueue( - 11, new Comparator() { - public int compare(RegisteredListener i, RegisteredListener j) { - return i.getPriority().compareTo(j.getPriority()); - } - } - ); + eventListeners = new TreeSet(comparer); listeners.put(type, eventListeners); return eventListeners; }