Skip to content

Commit dc9334d

Browse files
authored
[MCTCommon/Events] Improve event firing and listening (#199)
- New event listener registering - New event firing
2 parents f3b345e + 4df8d73 commit dc9334d

27 files changed

+585
-420
lines changed
Lines changed: 17 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.minecrafttas.mctcommon.events;
22

3-
import com.minecrafttas.mctcommon.MCTCommon;
43
import com.minecrafttas.mctcommon.events.EventListenerRegistry.EventBase;
54
import com.minecrafttas.mctcommon.server.Client;
65
import com.mojang.authlib.GameProfile;
@@ -9,13 +8,19 @@
98
import net.minecraft.client.entity.EntityPlayerSP;
109
import net.minecraft.client.gui.GuiScreen;
1110

11+
/**
12+
* Contains all events fired on the client side
13+
*
14+
* @author Scribble
15+
*/
1216
public interface EventClient {
1317

1418
/**
1519
* Fired when a gui is opened (Minecraft#displayGuiScreen)
1620
* @author Scribble
1721
*
1822
*/
23+
@FunctionalInterface
1924
public static interface EventOpenGui extends EventBase {
2025

2126
/**
@@ -24,143 +29,87 @@ public static interface EventOpenGui extends EventBase {
2429
* @return
2530
*/
2631
public GuiScreen onOpenGui(GuiScreen gui);
27-
28-
public static GuiScreen fireOpenGuiEvent(GuiScreen gui) {
29-
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing OpenGuiEvent");
30-
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
31-
if(eventListener instanceof EventOpenGui) {
32-
EventOpenGui event = (EventOpenGui) eventListener;
33-
GuiScreen newGui = event.onOpenGui(gui);
34-
if(newGui != gui) {
35-
return newGui;
36-
}
37-
}
38-
}
39-
return gui;
40-
}
4132
}
4233

4334
/**
4435
* Fired when the integrated server is launched
4536
* @author Scribble
4637
*
4738
*/
39+
@FunctionalInterface
4840
public static interface EventLaunchIntegratedServer extends EventBase {
4941

5042
/**
5143
* Fired when the integrated server is launched
5244
*/
5345
public void onLaunchIntegratedServer();
54-
55-
public static void fireOnLaunchIntegratedServer() {
56-
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing LaunchIntegratedServer");
57-
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
58-
if(eventListener instanceof EventLaunchIntegratedServer) {
59-
EventLaunchIntegratedServer event = (EventLaunchIntegratedServer) eventListener;
60-
event.onLaunchIntegratedServer();
61-
}
62-
}
63-
}
6446
}
6547

6648
/**
6749
* Fired when the world is done loading, before the player joined the world
6850
* @author Scribble
6951
*
7052
*/
53+
@FunctionalInterface
7154
public static interface EventDoneLoadingWorld extends EventBase {
7255

7356
/**
7457
* Fired when the world is done loading, before the player joined the world
7558
*/
7659
public void onDoneLoadingWorld();
77-
78-
public static void fireOnDoneLoadingWorld() {
79-
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing DoneLoadingWorld");
80-
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
81-
if(eventListener instanceof EventDoneLoadingWorld) {
82-
EventDoneLoadingWorld event = (EventDoneLoadingWorld) eventListener;
83-
event.onDoneLoadingWorld();
84-
}
85-
}
86-
}
8760
}
8861

8962
/**
9063
* Fired when the client ticks
9164
* @author Scribble
9265
*
9366
*/
67+
@FunctionalInterface
9468
public static interface EventClientTick extends EventBase {
9569

9670
/**
9771
* Fired when the client ticks
9872
* @param mc The ticking Minecraft instance
9973
*/
10074
public void onClientTick(Minecraft mc);
101-
102-
public static void fireOnClientTick(Minecraft mc) {
103-
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
104-
if(eventListener instanceof EventClientTick) {
105-
EventClientTick event = (EventClientTick) eventListener;
106-
event.onClientTick(mc);
107-
}
108-
}
109-
}
11075
}
11176

11277
/**
11378
* Fires after the client is initialised
11479
* @author Scribble
11580
*
11681
*/
82+
@FunctionalInterface
11783
public static interface EventClientInit extends EventBase {
11884

11985
/**
12086
* Fires after the client is initialised
12187
* @param mc The initialized Minecraft instance
12288
*/
12389
public void onClientInit(Minecraft mc);
124-
125-
public static void fireOnClientInit(Minecraft mc) {
126-
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing ClientInit");
127-
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
128-
if(eventListener instanceof EventClientInit) {
129-
EventClientInit event = (EventClientInit) eventListener;
130-
event.onClientInit(mc);
131-
}
132-
}
133-
}
13490
}
13591

13692
/**
13793
* Fired when when the client runs a game loop, which is tick independent
13894
* @author Scribble
13995
*
14096
*/
97+
@FunctionalInterface
14198
public static interface EventClientGameLoop extends EventBase {
14299

143100
/**
144101
* Fired when when the client runs a game loop, which is tick independent
145102
* @param mc The Minecraft instance that is looping
146103
*/
147104
public void onRunClientGameLoop(Minecraft mc);
148-
149-
public static void fireOnClientGameLoop(Minecraft mc) {
150-
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
151-
if(eventListener instanceof EventClientGameLoop) {
152-
EventClientGameLoop event = (EventClientGameLoop) eventListener;
153-
event.onRunClientGameLoop(mc);
154-
}
155-
}
156-
}
157105
}
158106

159107
/**
160108
* Fired when the camera is updated
161109
* @author Scribble
162110
*
163111
*/
112+
@FunctionalInterface
164113
public static interface EventCamera extends EventBase {
165114

166115
/**
@@ -170,19 +119,6 @@ public static interface EventCamera extends EventBase {
170119
*/
171120
public CameraData onCameraEvent(CameraData dataIn);
172121

173-
public static CameraData fireCameraEvent(CameraData dataIn) {
174-
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
175-
if(eventListener instanceof EventCamera) {
176-
EventCamera event = (EventCamera) eventListener;
177-
CameraData data = event.onCameraEvent(dataIn);
178-
if(!data.equals(dataIn)) {
179-
return data;
180-
}
181-
}
182-
}
183-
return dataIn;
184-
}
185-
186122
public static class CameraData{
187123
public float pitch;
188124
public float yaw;
@@ -214,95 +150,56 @@ public boolean equals(Object obj) {
214150
* @author Scribble
215151
*
216152
*/
153+
@FunctionalInterface
217154
public static interface EventPlayerLeaveClientSide extends EventBase {
218155

219156
/**
220157
* Fired when a player leaves a server or a world
221158
* @param player The player that leaves the server or the world
222159
*/
223160
public void onPlayerLeaveClientSide(EntityPlayerSP player);
224-
225-
public static void firePlayerLeaveClientSide(EntityPlayerSP player) {
226-
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing PlayerLeaveClientSideEvent");
227-
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
228-
if(eventListener instanceof EventPlayerLeaveClientSide) {
229-
EventPlayerLeaveClientSide event = (EventPlayerLeaveClientSide) eventListener;
230-
event.onPlayerLeaveClientSide(player);
231-
}
232-
}
233-
}
234161
}
235162

236163
/**
237164
* Fired when a player joins a server or a world
238165
* @author Scribble
239166
*
240167
*/
168+
@FunctionalInterface
241169
public static interface EventPlayerJoinedClientSide extends EventBase {
242170

243171
/**
244172
* Fired when a player joins a server or a world
245173
* @param player The player that joins the server or the world
246174
*/
247175
public void onPlayerJoinedClientSide(EntityPlayerSP player);
248-
249-
public static void firePlayerJoinedClientSide(EntityPlayerSP player) {
250-
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing PlayerJoinedClientSide");
251-
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
252-
if(eventListener instanceof EventPlayerJoinedClientSide) {
253-
EventPlayerJoinedClientSide event = (EventPlayerJoinedClientSide) eventListener;
254-
event.onPlayerJoinedClientSide(player);
255-
}
256-
}
257-
}
258-
259176
}
260177

261178
/**
262179
* Fired when a different player other than yourself joins a server or a world
263180
* @author Scribble
264181
*
265182
*/
183+
@FunctionalInterface
266184
public static interface EventOtherPlayerJoinedClientSide extends EventBase {
267185

268186
/**
269187
* Fired when a different player other than yourself joins a server or a world
270-
* @param player The game profile of the player that joins the server or the world
188+
* @param profile The game profile of the player that joins the server or the world
271189
*/
272190
public void onOtherPlayerJoinedClientSide(GameProfile profile);
273-
274-
275-
public static void fireOtherPlayerJoinedClientSide(GameProfile profile) {
276-
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing OtherPlayerJoinedClientSide");
277-
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
278-
if(eventListener instanceof EventOtherPlayerJoinedClientSide) {
279-
EventOtherPlayerJoinedClientSide event = (EventOtherPlayerJoinedClientSide) eventListener;
280-
event.onOtherPlayerJoinedClientSide(profile);
281-
}
282-
}
283-
}
284-
285191
}
286192

287193
/**
288194
* Fired when the connection to the custom server was closed on the client side.
289195
*/
196+
@FunctionalInterface
290197
public static interface EventDisconnectClient extends EventBase {
291198

292199
/**
293200
* Fired when the connection to the custom server was closed on the client side.
294201
* @param client The client that is disconnecting
295202
*/
296203
public void onDisconnectClient(Client client);
297-
298-
public static void fireDisconnectClient(Client client) {
299-
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing EventDisconnectClient");
300-
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
301-
if(eventListener instanceof EventDisconnectClient) {
302-
EventDisconnectClient event = (EventDisconnectClient) eventListener;
303-
event.onDisconnectClient(client);
304-
}
305-
}
306-
}
307204
}
308205
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.minecrafttas.mctcommon.events;
2+
3+
public class EventException extends RuntimeException {
4+
5+
public EventException(String message, Class<? extends EventListenerRegistry.EventBase> eventClass) {
6+
super(eventClass.getName() + ": " + message);
7+
}
8+
9+
public EventException(String message, Class<? extends EventListenerRegistry.EventBase> eventClass, Throwable cause) {
10+
super(eventClass.getName() + ": " + message, cause);
11+
}
12+
13+
public EventException(Class<? extends EventListenerRegistry.EventBase> eventClass, Throwable cause) {
14+
super(eventClass.getName(), cause);
15+
}
16+
}

0 commit comments

Comments
 (0)