diff options
author | md_5 <md_5@bigpond.com> | 2012-01-21 15:34:22 +1100 |
---|---|---|
committer | EvilSeph <evilseph@gmail.com> | 2012-02-09 15:51:56 -0500 |
commit | 6efe9a85e54b395858cee6aa8afc1f9023da18bf (patch) | |
tree | ce8e2e928ebb79153fefba4ad9d5c1a88fa420e6 | |
parent | db15c58e31f96609c35ab0dbcf1fc986c3d18643 (diff) | |
download | bukkit-6efe9a85e54b395858cee6aa8afc1f9023da18bf.tar bukkit-6efe9a85e54b395858cee6aa8afc1f9023da18bf.tar.gz bukkit-6efe9a85e54b395858cee6aa8afc1f9023da18bf.tar.lz bukkit-6efe9a85e54b395858cee6aa8afc1f9023da18bf.tar.xz bukkit-6efe9a85e54b395858cee6aa8afc1f9023da18bf.zip |
[Bleeding] Add the ability for plugins to ignore cancelled events when
using the new event system.
6 files changed, 100 insertions, 95 deletions
diff --git a/src/main/java/org/bukkit/event/EventHandler.java b/src/main/java/org/bukkit/event/EventHandler.java index 3bfafd6a..b0a19c4d 100644 --- a/src/main/java/org/bukkit/event/EventHandler.java +++ b/src/main/java/org/bukkit/event/EventHandler.java @@ -11,6 +11,7 @@ import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface EventHandler { - EventPriority priority() default EventPriority.NORMAL; + + boolean ignoreCancelled() default false; } diff --git a/src/main/java/org/bukkit/plugin/PluginManager.java b/src/main/java/org/bukkit/plugin/PluginManager.java index 3fbb187f..bf2da451 100644 --- a/src/main/java/org/bukkit/plugin/PluginManager.java +++ b/src/main/java/org/bukkit/plugin/PluginManager.java @@ -141,6 +141,18 @@ public interface PluginManager { public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin); /** + * Registers the specified executor to the given event class + * + * @param event Event type to register + * @param listener Listener to register + * @param priority Priority to register this event at + * @param executor EventExecutor to register + * @param plugin Plugin to register + * @param ignoreCancelled Whether to pass cancelled events or not + */ + public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled); + + /** * Enables the specified plugin * <p /> * Attempting to enable a plugin that is already enabled will have no effect diff --git a/src/main/java/org/bukkit/plugin/RegisteredListener.java b/src/main/java/org/bukkit/plugin/RegisteredListener.java index 9111ae43..5af42502 100644 --- a/src/main/java/org/bukkit/plugin/RegisteredListener.java +++ b/src/main/java/org/bukkit/plugin/RegisteredListener.java @@ -1,9 +1,6 @@ package org.bukkit.plugin; -import org.bukkit.event.Event; -import org.bukkit.event.EventException; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; +import org.bukkit.event.*; /** * Stores relevant information for plugin listeners @@ -13,12 +10,14 @@ public class RegisteredListener { private final EventPriority priority; private final Plugin plugin; private final EventExecutor executor; + private final boolean ignoreCancelled; - public RegisteredListener(final Listener pluginListener, final EventExecutor eventExecutor, final EventPriority eventPriority, final Plugin registeredPlugin) { - listener = pluginListener; - priority = eventPriority; - plugin = registeredPlugin; - executor = eventExecutor; + public RegisteredListener(final Listener listener, final EventExecutor executor, final EventPriority priority, final Plugin plugin, final boolean ignoreCancelled) { + this.listener = listener; + this.priority = priority; + this.plugin = plugin; + this.executor = executor; + this.ignoreCancelled = ignoreCancelled; } /** @@ -53,7 +52,21 @@ public class RegisteredListener { * * @param event The event */ - public void callEvent(Event event) throws EventException { + public void callEvent(final Event event) throws EventException { + if (event instanceof Cancellable){ + if (((Cancellable) event).isCancelled() && isIgnoringCancelled()){ + return; + } + } executor.execute(listener, event); } + + /** + * Whether this listener accepts cancelled events + * + * @return True when ignoring cancelled events + */ + public boolean isIgnoringCancelled() { + return ignoreCancelled; + } } diff --git a/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/src/main/java/org/bukkit/plugin/SimplePluginManager.java index e302bcc0..3dff6d23 100644 --- a/src/main/java/org/bukkit/plugin/SimplePluginManager.java +++ b/src/main/java/org/bukkit/plugin/SimplePluginManager.java @@ -8,6 +8,7 @@ import java.util.*; import java.util.logging.Level; import java.util.regex.Matcher; import java.util.regex.Pattern; + import org.apache.commons.lang.Validate; import org.bukkit.Server; import org.bukkit.command.Command; @@ -416,7 +417,7 @@ public final class SimplePluginManager implements PluginManager { } catch (Throwable ex) { server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while unregistering events for " + plugin.getDescription().getFullName() + " (Is it up to date?): " + ex.getMessage(), ex); } - + try { server.getMessenger().unregisterIncomingPluginChannel(plugin); server.getMessenger().unregisterOutgoingPluginChannel(plugin); @@ -449,36 +450,34 @@ public final class SimplePluginManager implements PluginManager { handlers.bake(); RegisteredListener[][] listeners = handlers.getRegisteredListeners(); - if (listeners != null) { - for (int i = 0; i < listeners.length; i++) { - for (RegisteredListener registration : listeners[i]) { - if (!registration.getPlugin().isEnabled()) { - continue; - } + for (int i = 0; i < listeners.length; i++) { + for (RegisteredListener registration : listeners[i]) { + if (!registration.getPlugin().isEnabled()) { + continue; + } - try { - registration.callEvent(event); - } catch (AuthorNagException ex) { - Plugin plugin = registration.getPlugin(); - - if (plugin.isNaggable()) { - plugin.setNaggable(false); - - String author = "<NoAuthorGiven>"; - - if (plugin.getDescription().getAuthors().size() > 0) { - author = plugin.getDescription().getAuthors().get(0); - } - server.getLogger().log(Level.SEVERE, String.format( - "Nag author: '%s' of '%s' about the following: %s", - author, - plugin.getDescription().getName(), - ex.getMessage() - )); + try { + registration.callEvent(event); + } catch (AuthorNagException ex) { + Plugin plugin = registration.getPlugin(); + + if (plugin.isNaggable()) { + plugin.setNaggable(false); + + String author = "<NoAuthorGiven>"; + + if (plugin.getDescription().getAuthors().size() > 0) { + author = plugin.getDescription().getAuthors().get(0); } - } catch (Throwable ex) { - server.getLogger().log(Level.SEVERE, "Could not pass event " + event.getEventName() + " to " + registration.getPlugin().getDescription().getName(), ex); + server.getLogger().log(Level.SEVERE, String.format( + "Nag author: '%s' of '%s' about the following: %s", + author, + plugin.getDescription().getName(), + ex.getMessage() + )); } + } catch (Throwable ex) { + server.getLogger().log(Level.SEVERE, "Could not pass event " + event.getEventName() + " to " + registration.getPlugin().getDescription().getName(), ex); } } } @@ -507,29 +506,11 @@ public final class SimplePluginManager implements PluginManager { * @param listener PlayerListener to register * @param priority Priority of this event * @param plugin Plugin to register + * @deprecated See {@link #registerEvent(Class, Listener, EventPriority, EventExecutor, Plugin, boolean)} */ + @Deprecated public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin) { - if (type == null) { - throw new IllegalArgumentException("Type cannot be null"); - } - if (listener == null) { - throw new IllegalArgumentException("Listener cannot be null"); - } - if (priority == null) { - throw new IllegalArgumentException("Priority cannot be null"); - } - if (plugin == null) { - throw new IllegalArgumentException("Plugin cannot be null"); - } - if (!plugin.isEnabled()) { - throw new IllegalPluginAccessException("Plugin attempted to register " + type + " while not enabled"); - } - - if (useTimings) { - getEventListeners(type.getEventClass()).register(new TimedRegisteredListener(listener, plugin.getPluginLoader().createExecutor(type, listener), priority.getNewPriority(), plugin)); - } else { - getEventListeners(type.getEventClass()).register(new RegisteredListener(listener, plugin.getPluginLoader().createExecutor(type, listener), priority.getNewPriority(), plugin)); - } + registerEvent(type.getEventClass(), listener, priority.getNewPriority(), plugin.getPluginLoader().createExecutor(type, listener), plugin); } /** @@ -540,36 +521,21 @@ public final class SimplePluginManager implements PluginManager { * @param executor EventExecutor to register * @param priority Priority of this event * @param plugin Plugin to register + * @deprecated See {@link #registerEvent(Class, Listener, EventPriority, EventExecutor, Plugin, boolean)} */ + @Deprecated public void registerEvent(Event.Type type, Listener listener, EventExecutor executor, Priority priority, Plugin plugin) { - if (type == null) { - throw new IllegalArgumentException("Type cannot be null"); - } - if (listener == null) { - throw new IllegalArgumentException("Listener cannot be null"); - } - if (priority == null) { - throw new IllegalArgumentException("Priority cannot be null"); - } - if (plugin == null) { - throw new IllegalArgumentException("Plugin cannot be null"); - } - if (!plugin.isEnabled()) { - throw new IllegalPluginAccessException("Plugin attempted to register " + type + " while not enabled"); - } + Validate.notNull(type, "Type cannot be null"); + Validate.notNull(priority, "Priority cannot be null"); - if (useTimings) { - getEventListeners(type.getEventClass()).register(new TimedRegisteredListener(listener, executor, priority.getNewPriority(), plugin)); - } - else { - getEventListeners(type.getEventClass()).register(new RegisteredListener(listener, executor, priority.getNewPriority(), plugin)); - } + registerEvent(type.getEventClass(), listener, priority.getNewPriority(), executor, plugin); } public void registerEvents(Listener listener, Plugin plugin) { if (!plugin.isEnabled()) { - throw new IllegalPluginAccessException("Plugin attempted to register " + listener + " while not enabled"); + throw new IllegalPluginAccessException("Plugin attempted to register " + listener + " while not enabled"); } + for (Map.Entry<Class<? extends Event>, Set<RegisteredListener>> entry : plugin.getPluginLoader().createRegisteredListeners(listener, plugin).entrySet()) { Class<? extends Event> delegatedClass = getRegistrationClass(entry.getKey()); if (!entry.getKey().equals(delegatedClass)) { @@ -582,28 +548,41 @@ public final class SimplePluginManager implements PluginManager { } public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin) { + registerEvent(event, listener, priority, executor, plugin, false); + } + + /** + * Registers the given event to the specified listener using a directly passed EventExecutor + * + * @param event Event class to register + * @param listener PlayerListener to register + * @param priority Priority of this event + * @param executor EventExecutor to register + * @param plugin Plugin to register + * @param ignoreCancelled Do not call executor if event was already cancelled + */ + public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) { + Validate.notNull(listener, "Listener cannot be null"); + Validate.notNull(priority, "Priority cannot be null"); + Validate.notNull(executor, "Executor cannot be null"); + Validate.notNull(plugin, "Plugin cannot be null"); + if (!plugin.isEnabled()) { throw new IllegalPluginAccessException("Plugin attempted to register " + event + " while not enabled"); } if (useTimings) { - getEventListeners(event).register(new TimedRegisteredListener(listener, executor, priority, plugin)); + getEventListeners(event).register(new TimedRegisteredListener(listener, executor, priority, plugin, ignoreCancelled)); } else { - getEventListeners(event).register(new RegisteredListener(listener, executor, priority, plugin)); + getEventListeners(event).register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled)); } } - /** - * Returns the specified event type's HandlerList - * - * @param type EventType to lookup - * @return HandlerList The list of registered handlers for the event. - */ private HandlerList getEventListeners(Class<? extends Event> type) { try { Method method = getRegistrationClass(type).getDeclaredMethod("getHandlerList"); method.setAccessible(true); - return (HandlerList)method.invoke(null); + return (HandlerList) method.invoke(null); } catch (Exception e) { throw new IllegalPluginAccessException(e.toString()); } diff --git a/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java b/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java index e2dffdae..53dfde38 100644 --- a/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java +++ b/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java @@ -14,8 +14,8 @@ public class TimedRegisteredListener extends RegisteredListener { private Event event; private boolean multiple = false; - public TimedRegisteredListener(final Listener pluginListener, final EventExecutor eventExecutor, final EventPriority eventPriority, final Plugin registeredPlugin) { - super(pluginListener, eventExecutor, eventPriority, registeredPlugin); + public TimedRegisteredListener(final Listener pluginListener, final EventExecutor eventExecutor, final EventPriority eventPriority, final Plugin registeredPlugin, final boolean listenCancelled) { + super(pluginListener, eventExecutor, eventPriority, registeredPlugin, listenCancelled); } public void callEvent(Event event) throws EventException { diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java index 4fb8e31a..790dd5e9 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java @@ -1028,9 +1028,9 @@ public class JavaPluginLoader implements PluginLoader { } }; if (useTimings) { - eventSet.add(new TimedRegisteredListener(listener, executor, eh.priority(), plugin)); + eventSet.add(new TimedRegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled())); } else { - eventSet.add(new RegisteredListener(listener, executor, eh.priority(), plugin)); + eventSet.add(new RegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled())); } } return ret; |