-
-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add Event Filters #927
Conversation
e21236c
to
e2ecddc
Compare
I'm really onboard with this concept because I can see it being used to cut out a few lines of code :P. I know I'd use it myself. |
i agree although i would prefer if the event filter (subtype or cancelled state) to be applied to the method instead of (or alternativly) the param because its easier to read there. |
Not sure if the annotations should be really subclasses, most code styles (including ours) will always import this like |
Maybe we should remove the |
@ST-DDT Could do yeah, it would change the target type which would be good because it would prevent you from applying these filters to any other parameters. @Minecrell Thats a good point I'll move them out. @Saladoc I thought about that but the two functions are a little different. Although while thinking about it perhaps I could add a tristate to it to specify the three cases NOT_CANCELLED, IGNORE_CANCELLED, and ONLY_CANCELLED. |
628ee46
to
34841fe
Compare
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface First { | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing newline at EOF
9a87f46
to
214503a
Compare
214503a
to
e7b0aeb
Compare
e7b0aeb
to
cbb6fd8
Compare
cbb6fd8
to
c7a9590
Compare
Nice |
The annotation |
Add |
To replicate the old |
@simon816, thanks. Everything works well. |
Impl
Event Filtering
This branch adds annotation based event filtering to assist with reducing the amount of boilerplate necessary while writing event listeners.
Event type Filters
These filters are applied to the method and apply a range of filters based on the event object.
Include
This annotation allows you to listen to a supertype event (such as EntityEvent) and filter it to only receive a specified subset of the event type's subclasses.
Exclude
The opposite of include, this allows you to listen to a supertype event and filter it to not receive a specified subset of the event type's subclasses.
IsCancelled
This will filter the event such that your listener will only be called if the event has been cancelled by a previous event listener. This is essentially the opposite of the
ignoreCancelled
flag on the Listener annotation.Parameter Sources
These annotations are applied to additional parameters in the event handler method and specify a source for that parameter. If an object matching the source and type is not found in the cause then your listener is not called.
First
This gets the first object from the event
Cause
matching the parameter type.A fairly typical event listener for a player event will go something like the following
With the
@First
annotation this can be simplified to the followingLast
Equivalent to
First
except it callsCause#last(Class)
.All
The
All
annotation allows you to receive all cause objects of a certain type. It must be applied to an array type and is equivalent toCause#allOf(Class)
By default your listener will be skipped if the returned array would be empty, this can be disabled however by setting the ignoreEmpty flag to false.
Parameter Filters
These filters apply additional conditions to a parameter. These require that the paramter also have a parameter source annotation (see above).
Has
This takes a data manipulator type and checks that the annotated data holder has a data manipulator of the specified type. If the object is not a
DataHolder
or does not have a matching manipulator then your listener is not called. Equivalent toDataHolder#has(Class)
.Supports
Similar to
Has
except the it is only checked that the DataHolder supports the given data manipulator type. Equivalent toDataHolder#supports(Class)
.A full example plugin can be found here
TODO:
ignoreCancelled
from Listener