13
0
geforkt von Mirrors/Paper

[Bleeding] Add the ability for plugins to ignore cancelled events when

using the new event system.

By: md_5 <md_5@bigpond.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2012-01-21 15:34:22 +11:00
Ursprung 3dcab1536a
Commit cb2fe27653
6 geänderte Dateien mit 97 neuen und 92 gelöschten Zeilen

Datei anzeigen

@ -11,6 +11,7 @@ import java.lang.annotation.Target;
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface EventHandler { public @interface EventHandler {
EventPriority priority() default EventPriority.NORMAL; EventPriority priority() default EventPriority.NORMAL;
boolean ignoreCancelled() default false;
} }

Datei anzeigen

@ -140,6 +140,18 @@ public interface PluginManager {
*/ */
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin); 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 * Enables the specified plugin
* <p /> * <p />

Datei anzeigen

@ -1,9 +1,6 @@
package org.bukkit.plugin; package org.bukkit.plugin;
import org.bukkit.event.Event; import org.bukkit.event.*;
import org.bukkit.event.EventException;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
/** /**
* Stores relevant information for plugin listeners * Stores relevant information for plugin listeners
@ -13,12 +10,14 @@ public class RegisteredListener {
private final EventPriority priority; private final EventPriority priority;
private final Plugin plugin; private final Plugin plugin;
private final EventExecutor executor; private final EventExecutor executor;
private final boolean ignoreCancelled;
public RegisteredListener(final Listener pluginListener, final EventExecutor eventExecutor, final EventPriority eventPriority, final Plugin registeredPlugin) { public RegisteredListener(final Listener listener, final EventExecutor executor, final EventPriority priority, final Plugin plugin, final boolean ignoreCancelled) {
listener = pluginListener; this.listener = listener;
priority = eventPriority; this.priority = priority;
plugin = registeredPlugin; this.plugin = plugin;
executor = eventExecutor; this.executor = executor;
this.ignoreCancelled = ignoreCancelled;
} }
/** /**
@ -53,7 +52,21 @@ public class RegisteredListener {
* *
* @param event The event * @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); executor.execute(listener, event);
} }
/**
* Whether this listener accepts cancelled events
*
* @return True when ignoring cancelled events
*/
public boolean isIgnoringCancelled() {
return ignoreCancelled;
}
} }

Datei anzeigen

@ -8,6 +8,7 @@ import java.util.*;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -416,7 +417,7 @@ public final class SimplePluginManager implements PluginManager {
} catch (Throwable ex) { } 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); 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 { try {
server.getMessenger().unregisterIncomingPluginChannel(plugin); server.getMessenger().unregisterIncomingPluginChannel(plugin);
server.getMessenger().unregisterOutgoingPluginChannel(plugin); server.getMessenger().unregisterOutgoingPluginChannel(plugin);
@ -449,36 +450,34 @@ public final class SimplePluginManager implements PluginManager {
handlers.bake(); handlers.bake();
RegisteredListener[][] listeners = handlers.getRegisteredListeners(); RegisteredListener[][] listeners = handlers.getRegisteredListeners();
if (listeners != null) { for (int i = 0; i < listeners.length; i++) {
for (int i = 0; i < listeners.length; i++) { for (RegisteredListener registration : listeners[i]) {
for (RegisteredListener registration : listeners[i]) { if (!registration.getPlugin().isEnabled()) {
if (!registration.getPlugin().isEnabled()) { continue;
continue; }
}
try { try {
registration.callEvent(event); registration.callEvent(event);
} catch (AuthorNagException ex) { } catch (AuthorNagException ex) {
Plugin plugin = registration.getPlugin(); Plugin plugin = registration.getPlugin();
if (plugin.isNaggable()) { if (plugin.isNaggable()) {
plugin.setNaggable(false); plugin.setNaggable(false);
String author = "<NoAuthorGiven>"; String author = "<NoAuthorGiven>";
if (plugin.getDescription().getAuthors().size() > 0) { if (plugin.getDescription().getAuthors().size() > 0) {
author = plugin.getDescription().getAuthors().get(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()
));
} }
} catch (Throwable ex) { server.getLogger().log(Level.SEVERE, String.format(
server.getLogger().log(Level.SEVERE, "Could not pass event " + event.getEventName() + " to " + registration.getPlugin().getDescription().getName(), ex); "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 listener PlayerListener to register
* @param priority Priority of this event * @param priority Priority of this event
* @param plugin Plugin to register * @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) { public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin) {
if (type == null) { registerEvent(type.getEventClass(), listener, priority.getNewPriority(), plugin.getPluginLoader().createExecutor(type, listener), plugin);
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));
}
} }
/** /**
@ -540,36 +521,21 @@ public final class SimplePluginManager implements PluginManager {
* @param executor EventExecutor to register * @param executor EventExecutor to register
* @param priority Priority of this event * @param priority Priority of this event
* @param plugin Plugin to register * @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) { public void registerEvent(Event.Type type, Listener listener, EventExecutor executor, Priority priority, Plugin plugin) {
if (type == null) { Validate.notNull(type, "Type cannot be null");
throw new IllegalArgumentException("Type cannot be null"); Validate.notNull(priority, "Priority 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) { registerEvent(type.getEventClass(), listener, priority.getNewPriority(), executor, plugin);
getEventListeners(type.getEventClass()).register(new TimedRegisteredListener(listener, executor, priority.getNewPriority(), plugin));
}
else {
getEventListeners(type.getEventClass()).register(new RegisteredListener(listener, executor, priority.getNewPriority(), plugin));
}
} }
public void registerEvents(Listener listener, Plugin plugin) { public void registerEvents(Listener listener, Plugin plugin) {
if (!plugin.isEnabled()) { 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()) { for (Map.Entry<Class<? extends Event>, Set<RegisteredListener>> entry : plugin.getPluginLoader().createRegisteredListeners(listener, plugin).entrySet()) {
Class<? extends Event> delegatedClass = getRegistrationClass(entry.getKey()); Class<? extends Event> delegatedClass = getRegistrationClass(entry.getKey());
if (!entry.getKey().equals(delegatedClass)) { 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) { 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()) { if (!plugin.isEnabled()) {
throw new IllegalPluginAccessException("Plugin attempted to register " + event + " while not enabled"); throw new IllegalPluginAccessException("Plugin attempted to register " + event + " while not enabled");
} }
if (useTimings) { if (useTimings) {
getEventListeners(event).register(new TimedRegisteredListener(listener, executor, priority, plugin)); getEventListeners(event).register(new TimedRegisteredListener(listener, executor, priority, plugin, ignoreCancelled));
} else { } 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) { private HandlerList getEventListeners(Class<? extends Event> type) {
try { try {
Method method = getRegistrationClass(type).getDeclaredMethod("getHandlerList"); Method method = getRegistrationClass(type).getDeclaredMethod("getHandlerList");
method.setAccessible(true); method.setAccessible(true);
return (HandlerList)method.invoke(null); return (HandlerList) method.invoke(null);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalPluginAccessException(e.toString()); throw new IllegalPluginAccessException(e.toString());
} }

Datei anzeigen

@ -14,8 +14,8 @@ public class TimedRegisteredListener extends RegisteredListener {
private Event event; private Event event;
private boolean multiple = false; private boolean multiple = false;
public TimedRegisteredListener(final Listener pluginListener, final EventExecutor eventExecutor, final EventPriority eventPriority, final Plugin registeredPlugin) { public TimedRegisteredListener(final Listener pluginListener, final EventExecutor eventExecutor, final EventPriority eventPriority, final Plugin registeredPlugin, final boolean listenCancelled) {
super(pluginListener, eventExecutor, eventPriority, registeredPlugin); super(pluginListener, eventExecutor, eventPriority, registeredPlugin, listenCancelled);
} }
public void callEvent(Event event) throws EventException { public void callEvent(Event event) throws EventException {

Datei anzeigen

@ -1028,9 +1028,9 @@ public class JavaPluginLoader implements PluginLoader {
} }
}; };
if (useTimings) { if (useTimings) {
eventSet.add(new TimedRegisteredListener(listener, executor, eh.priority(), plugin)); eventSet.add(new TimedRegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled()));
} else { } else {
eventSet.add(new RegisteredListener(listener, executor, eh.priority(), plugin)); eventSet.add(new RegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled()));
} }
} }
return ret; return ret;