Skip to content

Commit 4df8d73

Browse files
committed
[MCTCommon/Events] Added documentation to event registry
1 parent 8d8f42c commit 4df8d73

File tree

2 files changed

+97
-17
lines changed

2 files changed

+97
-17
lines changed

src/main/java/com/minecrafttas/mctcommon/events/EventListenerRegistry.java

Lines changed: 96 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,64 @@
77
import java.util.ArrayList;
88
import java.util.HashMap;
99

10+
/**
11+
* Registry for making objects available to listen for events.<br>
12+
* <br>
13+
* Implement an EventInterface into your class, then implement the function.<br>
14+
* <br>
15+
* In your initializer method, register the instance of the class with {@link EventListenerRegistry#register(EventBase)}
16+
* <br>
17+
* Example:
18+
* <pre>
19+
* public class ClassWithListener implements EventInit {
20+
*
21+
* @ Override
22+
* public void onEventInit() {
23+
* // Implement event specific code
24+
* }
25+
* }
26+
* </pre>
27+
* <br>
28+
* To create and fire your own events, check {@link EventBase}<br>
29+
*/
1030
public class EventListenerRegistry {
1131

1232
/**
13-
* Eventlistener Registry.<br>
33+
* Base interface for events.<br>
34+
* <br>
35+
* To create a new event, create an interface and extend EventBase.<br>
36+
* Only 1 method is accepted in an event, so it's best to add {@link FunctionalInterface} to the interface.<br>
37+
* <br>
38+
* Example:
39+
* <pre>
40+
* @ FunctionalInterface
41+
* public interface EventInit extends EventBase {
42+
* public void onEventInit();
43+
* }
44+
*
45+
* @ FunctionalInterface
46+
* public interface EventAdd extends EventBase {
47+
* public int onEventAdd(int a, int b); // Accepts parameters and return types
48+
* }
49+
* </pre>
50+
*
51+
* To fire your event, use {@link EventListenerRegistry#fireEvent(Class)} with the parameter being the event class.<br>
52+
*
53+
* <pre>
54+
* EventListenerRegistry.fireEvent(EventInit.class);
55+
* </pre>
56+
*
57+
* To fire an event with parameters and return types:
58+
* <pre>
59+
* int eventResult = (int) EventListenerRegistry.fireEvent(EventAdd.class, a, b);
60+
* </pre>
61+
* When using parameters, the type and the amount of parameters has to match!
62+
*/
63+
public interface EventBase {
64+
}
65+
66+
/**
67+
* Stores the event listener objects and calls their event methods during {@link EventListenerRegistry#fireEvent(Class, Object...)}<br>
1468
* <br>
1569
* Consists of multiple lists seperated by event types.<br>
1670
* If it were a single ArrayList, firing an event means that you'd have to unnecessarily iterate over the entire list,<br>
@@ -20,7 +74,10 @@ public class EventListenerRegistry {
2074
*/
2175
private static final HashMap<Class<?>, ArrayList<EventBase>> EVENTLISTENER_REGISTRY = new HashMap<>();
2276

23-
77+
/**
78+
* Registers an object to be an event listener. The object must implement an event extending {@link EventBase}
79+
* @param eventListener The event listener to register
80+
*/
2481
public static void register(EventBase eventListener) {
2582
if (eventListener == null) {
2683
throw new NullPointerException("Tried to register a packethandler with value null");
@@ -38,6 +95,10 @@ public static void register(EventBase eventListener) {
3895
}
3996
}
4097

98+
/**
99+
* Unregisters an object from being an event listener.
100+
* @param eventListener The event listener to unregister
101+
*/
41102
public static void unregister(EventBase eventListener) {
42103
if (eventListener == null) {
43104
throw new NullPointerException("Tried to unregister a packethandler with value null");
@@ -74,35 +135,45 @@ public static Object fireEvent(Class<? extends EventListenerRegistry.EventBase>
74135
* @return The result of the event, might be null if the event returns nothing
75136
*/
76137
public static Object fireEvent(Class<? extends EventListenerRegistry.EventBase> eventClass, Object... eventParams) {
77-
ArrayList<EventBase> registryList = EVENTLISTENER_REGISTRY.get(eventClass);
78-
if (registryList == null) {
138+
ArrayList<EventBase> listenerList = EVENTLISTENER_REGISTRY.get(eventClass);
139+
if (listenerList == null) {
79140
throw new EventException("The event has not been registered yet", eventClass);
80141
}
81142

143+
// Exception to be thrown at the end of the method. If null then no exception is thrown
82144
EventException toThrow = null;
83145

84-
Method methodToCheck = getEventMethod(eventClass);
146+
// Get the method from the event that we are looking for in the event listeners
147+
Method methodToFind = getEventMethod(eventClass);
85148

149+
// Variable for the return value. The last registered listener will return its value
86150
Object returnValue = null;
87-
for (EventBase eventListener : registryList) {
151+
152+
// Iterate through the list of eventListeners
153+
for (EventBase eventListener : listenerList) {
154+
// Get all methods in this event listener
88155
Method[] methodsInListener = eventListener.getClass().getDeclaredMethods();
89156

157+
// Iterate through all methods
90158
for (Method method : methodsInListener) {
91159

92-
if (!checkName(method, methodToCheck.getName())) {
160+
// Check if the current method has the same name as the method we are looking for
161+
if (!checkName(method, methodToFind.getName())) {
93162
continue;
94163
}
95164

165+
// Check if the length is the same before we check for types
96166
if (!checkLength(method, eventParams)) {
97167
toThrow = new EventException(String.format("Event fired with the wrong number of parameters. Expected: %s, Actual: %s", method.getParameterCount(), eventParams.length), eventClass);
98168
continue;
99169
}
100170

171+
// Check the types of the method
101172
if (checkTypes(method, eventParams)) {
102-
toThrow = null;
173+
toThrow = null; // Reset toThrow as the correct method was found
103174
method.setAccessible(true);
104175
try {
105-
returnValue = method.invoke(eventListener, eventParams);
176+
returnValue = method.invoke(eventListener, eventParams); // Call the method
106177
} catch (IllegalAccessException | InvocationTargetException e) {
107178
throw new EventException(eventClass, e);
108179
} catch (IllegalArgumentException e) {
@@ -114,6 +185,7 @@ public static Object fireEvent(Class<? extends EventListenerRegistry.EventBase>
114185
}
115186
}
116187

188+
// Throw the exception
117189
if (toThrow != null) {
118190
throw toThrow;
119191
}
@@ -130,14 +202,29 @@ private static Method getEventMethod(Class<? extends EventListenerRegistry.Event
130202
return test[0];
131203
}
132204

205+
/**
206+
* @param method The method to check
207+
* @param name The name to check
208+
* @return If method.getName equals name
209+
*/
133210
private static boolean checkName(Method method, String name) {
134211
return method.getName().equals(name);
135212
}
136213

214+
/**
215+
* @param method The method to check
216+
* @param parameters The list of parameters
217+
* @return True, if length of the method parameters is equal to the length of the object parameters
218+
*/
137219
private static boolean checkLength(Method method, Object... parameters) {
138220
return method.getParameterCount() == parameters.length;
139221
}
140222

223+
/**
224+
* @param method The method to check
225+
* @param parameters The list of parameters
226+
* @return True, if the types of the parameters equal the object parameters
227+
*/
141228
private static boolean checkTypes(Method method, Object... parameters) {
142229
Class<?>[] methodParameterTypes = ClassUtils.primitivesToWrappers(method.getParameterTypes());
143230
Class<?>[] eventParameterTypes = getParameterTypes(parameters);
@@ -166,7 +253,4 @@ private static Class<?>[] getParameterTypes(Object... parameters) {
166253
public static void clear() {
167254
EVENTLISTENER_REGISTRY.clear();
168255
}
169-
170-
public interface EventBase {
171-
}
172256
}

src/main/java/com/minecrafttas/mctcommon/server/Client.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ public Client(String host, int port, PacketID[] packetIDs, String name, boolean
8484
this.createHandlers();
8585

8686
this.callback = (client) -> {
87-
try {
88-
EventListenerRegistry.fireEvent(EventDisconnectClient.class, client);
89-
} catch (Exception e) {
90-
e.printStackTrace();
91-
}
87+
EventListenerRegistry.fireEvent(EventDisconnectClient.class, client);
9288
};
9389

9490
this.local = local;

0 commit comments

Comments
 (0)