Skip to content

Commit 3d6d04b

Browse files
committed
Slight refactor of the EventManager.
- Shuffle out the clutter of deviations of registerListener to a builder system that is much more flexible to future changes - Remove unregisterPluginListeners. It is a pointless separation when unregisterListeners could simply account for it in an implementation as dictated by the javadoc specification Signed-off-by: Chris Sanders <[email protected]> Javadocs, TypeToken -> Type. Signed-off-by: Chris Sanders <[email protected]>
1 parent 89ca014 commit 3d6d04b

File tree

3 files changed

+95
-117
lines changed

3 files changed

+95
-117
lines changed

src/main/java/org/spongepowered/api/event/EventListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*
3030
* @param <T> The type of the event
3131
*/
32+
@FunctionalInterface
3233
public interface EventListener<T extends Event> {
3334

3435
/**
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.event;
26+
27+
import io.leangen.geantyref.TypeToken;
28+
import org.spongepowered.api.Sponge;
29+
import org.spongepowered.api.util.ResettableBuilder;
30+
import org.spongepowered.plugin.PluginContainer;
31+
32+
import java.lang.reflect.Type;
33+
import java.util.Objects;
34+
35+
/**
36+
* Represents the composition of a {@link EventListener listener} and the attributes that define it
37+
* to the system.
38+
*
39+
* @param <T> The event type
40+
*/
41+
public interface EventListenerRegistration<T extends Event> {
42+
43+
static <T extends Event> EventListenerRegistration.Builder<T> builder(final TypeToken<T> eventType) {
44+
return Sponge.game().factoryProvider().provide(Factory.class).builder(Objects.requireNonNull(eventType, "eventType"));
45+
}
46+
47+
static <T extends Event> EventListenerRegistration.Builder<T> builder(final Class<T> eventClass) {
48+
return Sponge.game().factoryProvider().provide(Factory.class).builder(TypeToken.get(eventClass));
49+
}
50+
51+
Type eventType();
52+
53+
PluginContainer plugin();
54+
55+
Order order();
56+
57+
boolean beforeModifications();
58+
59+
EventListener<? super T> listener();
60+
61+
interface Builder<T extends Event> extends ResettableBuilder<EventListenerRegistration<T>, Builder<T>> {
62+
63+
Builder<T> plugin(PluginContainer plugin);
64+
65+
Builder<T> order(Order order);
66+
67+
Builder<T> beforeModifications(boolean beforeModifications);
68+
69+
Builder<T> listener(EventListener<? super T> listener);
70+
71+
EventListenerRegistration<T> build();
72+
}
73+
74+
interface Factory {
75+
76+
<T extends Event> Builder<T> builder(TypeToken<T> eventType);
77+
}
78+
}

src/main/java/org/spongepowered/api/event/EventManager.java

Lines changed: 16 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@
2424
*/
2525
package org.spongepowered.api.event;
2626

27-
import io.leangen.geantyref.TypeToken;
2827
import org.spongepowered.plugin.PluginContainer;
2928

3029
/**
3130
* Manages the registration of event listeners and the dispatching of events.
3231
*/
3332
public interface EventManager {
3433

34+
/**
35+
* Submits a new {@link EventListenerRegistration listener registration} to this manager.
36+
* @param registration The registration
37+
* @param <E> The event type
38+
* @return This manager, for fluency
39+
*/
40+
<E extends Event> EventManager registerListener(EventListenerRegistration<E> registration);
41+
3542
/**
3643
* Registers {@link Event} methods annotated with @{@link Listener} in the
3744
* specified object.
@@ -41,131 +48,23 @@ public interface EventManager {
4148
*
4249
* @param plugin The plugin instance
4350
* @param obj The object
44-
* @throws IllegalArgumentException Thrown if {@code plugin} is not a plugin
45-
* instance
46-
*/
47-
void registerListeners(PluginContainer plugin, Object obj);
48-
49-
/**
50-
* Registers an event listener for a specific event class.
51-
*
52-
* <p>Normally, the annotation-based way in
53-
* {@link #registerListeners(PluginContainer, Object)} should be preferred over this way. This
54-
* method exists primarily to support dynamic event registration like needed
55-
* in scripting plugins.</p>
56-
*
57-
* @param plugin The plugin instance
58-
* @param eventClass The event to listen to
59-
* @param listener The listener to receive the events
60-
* @param <T> The type of the event
51+
* @return This manager, for fluency
6152
*/
62-
<T extends Event> void registerListener(PluginContainer plugin, Class<T> eventClass, EventListener<? super T> listener);
63-
64-
/**
65-
* Registers an event listener for a specific event {@link TypeToken}.
66-
*
67-
* <p>Normally, the annotation-based way in
68-
* {@link #registerListeners(PluginContainer, Object)} should be preferred over this way. This
69-
* method exists primarily to support dynamic event registration like needed
70-
* in scripting plugins.</p>
71-
*
72-
* @param plugin The plugin instance
73-
* @param eventType The event to listen to
74-
* @param listener The listener to receive the events
75-
* @param <T> The type of the event
76-
*/
77-
<T extends Event> void registerListener(PluginContainer plugin, TypeToken<T> eventType, EventListener<? super T> listener);
78-
79-
/**
80-
* Registers an event listener with the specified order for a specific event
81-
* class.
82-
*
83-
* <p>Normally, the annotation-based way in
84-
* {@link #registerListeners(PluginContainer, Object)} should be preferred over this way. This
85-
* method exists primarily to support dynamic event registration like needed
86-
* in scripting plugins.</p>
87-
*
88-
* @param plugin The plugin instance
89-
* @param eventClass The event to listen to
90-
* @param order The order the listener will get called at
91-
* @param listener The listener to receive the events
92-
* @param <T> The type of the event
93-
*/
94-
<T extends Event> void registerListener(PluginContainer plugin, Class<T> eventClass, Order order, EventListener<? super T> listener);
95-
96-
/**
97-
* Registers an event listener with the specified order for a specific event
98-
* {@link TypeToken}.
99-
*
100-
* <p>Normally, the annotation-based way in
101-
* {@link #registerListeners(PluginContainer, Object)} should be preferred over this way. This
102-
* method exists primarily to support dynamic event registration like needed
103-
* in scripting plugins.</p>
104-
*
105-
* @param plugin The plugin instance
106-
* @param eventType The event to listen to
107-
* @param order The order the listener will get called at
108-
* @param listener The listener to receive the events
109-
* @param <T> The type of the event
110-
*/
111-
<T extends Event> void registerListener(PluginContainer plugin, TypeToken<T> eventType, Order order, EventListener<? super T> listener);
112-
113-
/**
114-
* Registers an event listener with the specified order for a specific event
115-
* class.
116-
*
117-
* <p>Normally, the annotation-based way in
118-
* {@link #registerListeners(PluginContainer, Object)} should be preferred over this way. This
119-
* method exists primarily to support dynamic event registration like needed
120-
* in scripting plugins.</p>
121-
*
122-
* @param plugin The plugin instance
123-
* @param eventClass The event to listen to
124-
* @param order The order the listener will get called at
125-
* @param beforeModifications Whether to call the listener before other
126-
* server modifications
127-
* @param listener The listener to receive the events
128-
* @param <T> The type of the event
129-
*/
130-
<T extends Event> void registerListener(PluginContainer plugin, Class<T> eventClass, Order order, boolean beforeModifications,
131-
EventListener<? super T> listener);
132-
133-
/**
134-
* Registers an event listener with the specified order for a specific event
135-
* class.
136-
*
137-
* <p>Normally, the annotation-based way in
138-
* {@link #registerListeners(PluginContainer, Object)} should be preferred over this way. This
139-
* method exists primarily to support dynamic event registration like needed
140-
* in scripting plugins.</p>
141-
*
142-
* @param plugin The plugin instance
143-
* @param eventType The event to listen to
144-
* @param order The order the listener will get called at
145-
* @param beforeModifications Whether to call the listener before other
146-
* server modifications
147-
* @param listener The listener to receive the events
148-
* @param <T> The type of the event
149-
*/
150-
<T extends Event> void registerListener(PluginContainer plugin, TypeToken<T> eventType, Order order, boolean beforeModifications,
151-
EventListener<? super T> listener);
53+
EventManager registerListeners(PluginContainer plugin, Object obj);
15254

15355
/**
15456
* Un-registers an object from receiving {@link Event}s.
15557
*
156-
* @param obj The object
157-
*/
158-
void unregisterListeners(Object obj);
159-
160-
/**
161-
* Un-registers all event listeners of a plugin.
58+
* <p>If the provided object is a {@link PluginContainer plugin}, all events associated
59+
* with that plugin will be unregistered.</p>
16260
*
163-
* @param plugin The plugin instance
61+
* @param obj The object
62+
* @return This manager, for fluency
16463
*/
165-
void unregisterPluginListeners(PluginContainer plugin);
64+
EventManager unregisterListeners(Object obj);
16665

16766
/**
168-
* Calls a {@link Event} to all listeners that listen to it.
67+
* Calls an {@link Event} to all listeners that listen to it.
16968
*
17069
* @param event The event
17170
* @return True if cancelled, false if not

0 commit comments

Comments
 (0)