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)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventHandler {
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);
/**
* 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 />

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -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;
@ -449,7 +450,6 @@ 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()) {
@ -481,7 +481,6 @@ public final class SimplePluginManager implements PluginManager {
}
}
}
}
// This is an ugly hack to handle old-style custom events in old plugins without breakage. All in the name of plugin compatibility.
if (event.getType() == Event.Type.CUSTOM_EVENT) {
TransitionalCustomEvent.getHandlerList().bake();
@ -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");
}
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,23 +548,36 @@ 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");

Datei anzeigen

@ -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 {

Datei anzeigen

@ -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;