diff options
Diffstat (limited to 'src/main/java/org/bukkit/plugin')
3 files changed, 28 insertions, 3 deletions
diff --git a/src/main/java/org/bukkit/plugin/PluginManager.java b/src/main/java/org/bukkit/plugin/PluginManager.java index 804f4415..89697de8 100644 --- a/src/main/java/org/bukkit/plugin/PluginManager.java +++ b/src/main/java/org/bukkit/plugin/PluginManager.java @@ -92,8 +92,11 @@ public interface PluginManager { * Calls an event with the given details * * @param event Event details + * @throws IllegalStateException Thrown when an asynchronous event is fired from synchronous code.<br> + * <i>Note: This is best-effort basis, and should not be used to test synchronized state. This + * is an indicator for flawed flow logic.</i> */ - public void callEvent(Event event); + public void callEvent(Event event) throws IllegalStateException; /** * Registers all the events in the given listener class diff --git a/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/src/main/java/org/bukkit/plugin/SimplePluginManager.java index e017ffdf..ca780fca 100644 --- a/src/main/java/org/bukkit/plugin/SimplePluginManager.java +++ b/src/main/java/org/bukkit/plugin/SimplePluginManager.java @@ -443,11 +443,28 @@ public final class SimplePluginManager implements PluginManager { } /** - * Calls an event with the given details + * Calls an event with the given details.<br> + * This method only synchronizes when the event is not asynchronous. * * @param event Event details */ - public synchronized void callEvent(Event event) { + public void callEvent(Event event) { + if (event.isAsynchronous()) { + if (Thread.holdsLock(this)) { + throw new IllegalStateException(event.getEventName() + " cannot be triggered asynchronously from inside synchronized code."); + } + if (server.isPrimaryThread()) { + throw new IllegalStateException(event.getEventName() + " cannot be triggered asynchronously from primary server thread."); + } + fireEvent(event); + } else { + synchronized (this) { + fireEvent(event); + } + } + } + + private void fireEvent(Event event) { HandlerList handlers = event.getHandlers(); RegisteredListener[] listeners = handlers.getRegisteredListeners(); diff --git a/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java b/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java index 53dfde38..ed25e179 100644 --- a/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java +++ b/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java @@ -18,7 +18,12 @@ public class TimedRegisteredListener extends RegisteredListener { super(pluginListener, eventExecutor, eventPriority, registeredPlugin, listenCancelled); } + @Override public void callEvent(Event event) throws EventException { + if (event.isAsynchronous()) { + super.callEvent(event); + return; + } count++; if (this.event == null) { this.event = event; |