Skip to content

Cancelling event flow

Nurio Fernández edited this page Aug 31, 2020 · 1 revision

With the ReflectedEventHandler library, you can cancel the event execution flow inside an EventHandler. This can allow you to control problems, exceptions, etc. To do that, you will need to implement EventCancellable at your event class.

public class CancellableEvent extends Event implements EventCancellable {

    private boolean cancelled;

    public boolean isCancelled() {
        return cancelled;
    }

    public void setCancelled(boolean status) {
        this.cancelled= status;
    }

}

With this, you can simply call setCancelled(true) method on your event handler method.

@EventHandler
public void cancelEvent(CancellableEvent eve) {
    eve.setCancelled(true);
}

Then all pending EventHandlers will be ignored, as long as the ignoredCancelled property was not enabled.

@EventHandler(ignoreCancelled = true)
public void cancelledEventIgnored(CancellableEvent eve) {
    System.out.println("This will be executed inclusive if the event was cancelled.");
}

Remember this can be done together with the priority EventHandler property.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void cancelledEventIgnored(CancellableEvent eve) {
    System.out.println("This will be executed inclusive if the event was cancelled.");
}

More information about execution priority.

Clone this wiki locally