Skip to content

Commit

Permalink
[MCTCommon/Events] Added new way of registering event listeners
Browse files Browse the repository at this point in the history
- Added new way to fire events
  • Loading branch information
ScribbleTAS committed Feb 22, 2024
1 parent f3b345e commit 6d22557
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 131 deletions.
130 changes: 11 additions & 119 deletions src/main/java/com/minecrafttas/mctcommon/events/EventClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.minecrafttas.mctcommon.events;

import com.minecrafttas.mctcommon.MCTCommon;
import com.minecrafttas.mctcommon.events.EventListenerRegistry.EventBase;
import com.minecrafttas.mctcommon.server.Client;
import com.mojang.authlib.GameProfile;
Expand All @@ -16,6 +15,7 @@ public interface EventClient {
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventOpenGui extends EventBase {

/**
Expand All @@ -24,143 +24,87 @@ public static interface EventOpenGui extends EventBase {
* @return
*/
public GuiScreen onOpenGui(GuiScreen gui);

public static GuiScreen fireOpenGuiEvent(GuiScreen gui) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing OpenGuiEvent");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventOpenGui) {
EventOpenGui event = (EventOpenGui) eventListener;
GuiScreen newGui = event.onOpenGui(gui);
if(newGui != gui) {
return newGui;
}
}
}
return gui;
}
}

/**
* Fired when the integrated server is launched
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventLaunchIntegratedServer extends EventBase {

/**
* Fired when the integrated server is launched
*/
public void onLaunchIntegratedServer();

public static void fireOnLaunchIntegratedServer() {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing LaunchIntegratedServer");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventLaunchIntegratedServer) {
EventLaunchIntegratedServer event = (EventLaunchIntegratedServer) eventListener;
event.onLaunchIntegratedServer();
}
}
}
}

/**
* Fired when the world is done loading, before the player joined the world
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventDoneLoadingWorld extends EventBase {

/**
* Fired when the world is done loading, before the player joined the world
*/
public void onDoneLoadingWorld();

public static void fireOnDoneLoadingWorld() {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing DoneLoadingWorld");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventDoneLoadingWorld) {
EventDoneLoadingWorld event = (EventDoneLoadingWorld) eventListener;
event.onDoneLoadingWorld();
}
}
}
}

/**
* Fired when the client ticks
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventClientTick extends EventBase {

/**
* Fired when the client ticks
* @param mc The ticking Minecraft instance
*/
public void onClientTick(Minecraft mc);

public static void fireOnClientTick(Minecraft mc) {
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventClientTick) {
EventClientTick event = (EventClientTick) eventListener;
event.onClientTick(mc);
}
}
}
}

/**
* Fires after the client is initialised
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventClientInit extends EventBase {

/**
* Fires after the client is initialised
* @param mc The initialized Minecraft instance
*/
public void onClientInit(Minecraft mc);

public static void fireOnClientInit(Minecraft mc) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing ClientInit");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventClientInit) {
EventClientInit event = (EventClientInit) eventListener;
event.onClientInit(mc);
}
}
}
}

/**
* Fired when when the client runs a game loop, which is tick independent
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventClientGameLoop extends EventBase {

/**
* Fired when when the client runs a game loop, which is tick independent
* @param mc The Minecraft instance that is looping
*/
public void onRunClientGameLoop(Minecraft mc);

public static void fireOnClientGameLoop(Minecraft mc) {
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventClientGameLoop) {
EventClientGameLoop event = (EventClientGameLoop) eventListener;
event.onRunClientGameLoop(mc);
}
}
}
}

/**
* Fired when the camera is updated
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventCamera extends EventBase {

/**
Expand All @@ -170,19 +114,6 @@ public static interface EventCamera extends EventBase {
*/
public CameraData onCameraEvent(CameraData dataIn);

public static CameraData fireCameraEvent(CameraData dataIn) {
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventCamera) {
EventCamera event = (EventCamera) eventListener;
CameraData data = event.onCameraEvent(dataIn);
if(!data.equals(dataIn)) {
return data;
}
}
}
return dataIn;
}

public static class CameraData{
public float pitch;
public float yaw;
Expand Down Expand Up @@ -214,95 +145,56 @@ public boolean equals(Object obj) {
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventPlayerLeaveClientSide extends EventBase {

/**
* Fired when a player leaves a server or a world
* @param player The player that leaves the server or the world
*/
public void onPlayerLeaveClientSide(EntityPlayerSP player);

public static void firePlayerLeaveClientSide(EntityPlayerSP player) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing PlayerLeaveClientSideEvent");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventPlayerLeaveClientSide) {
EventPlayerLeaveClientSide event = (EventPlayerLeaveClientSide) eventListener;
event.onPlayerLeaveClientSide(player);
}
}
}
}

/**
* Fired when a player joins a server or a world
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventPlayerJoinedClientSide extends EventBase {

/**
* Fired when a player joins a server or a world
* @param player The player that joins the server or the world
*/
public void onPlayerJoinedClientSide(EntityPlayerSP player);

public static void firePlayerJoinedClientSide(EntityPlayerSP player) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing PlayerJoinedClientSide");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventPlayerJoinedClientSide) {
EventPlayerJoinedClientSide event = (EventPlayerJoinedClientSide) eventListener;
event.onPlayerJoinedClientSide(player);
}
}
}

}

/**
* Fired when a different player other than yourself joins a server or a world
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventOtherPlayerJoinedClientSide extends EventBase {

/**
* Fired when a different player other than yourself joins a server or a world
* @param player The game profile of the player that joins the server or the world
*/
public void onOtherPlayerJoinedClientSide(GameProfile profile);


public static void fireOtherPlayerJoinedClientSide(GameProfile profile) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing OtherPlayerJoinedClientSide");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventOtherPlayerJoinedClientSide) {
EventOtherPlayerJoinedClientSide event = (EventOtherPlayerJoinedClientSide) eventListener;
event.onOtherPlayerJoinedClientSide(profile);
}
}
}

}

/**
* Fired when the connection to the custom server was closed on the client side.
*/
@FunctionalInterface
public static interface EventDisconnectClient extends EventBase {

/**
* Fired when the connection to the custom server was closed on the client side.
* @param client The client that is disconnecting
*/
public void onDisconnectClient(Client client);

public static void fireDisconnectClient(Client client) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing EventDisconnectClient");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventDisconnectClient) {
EventDisconnectClient event = (EventDisconnectClient) eventListener;
event.onDisconnectClient(client);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package com.minecrafttas.mctcommon.events;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

public class EventListenerRegistry {

private static ArrayList<EventBase> EVENTLISTENER_REGISTRY = new ArrayList<>();
private static HashMap<Class<?>, ArrayList<EventBase>> EVENTLISTENER_REGISTRY = new HashMap<>();


public static void register(EventBase eventListener) {
if (eventListener == null) {
throw new NullPointerException("Tried to register a packethandler with value null");
}
EVENTLISTENER_REGISTRY.add(eventListener);
for (Class<?> type : eventListener.getClass().getInterfaces()) {
if(EventBase.class.isAssignableFrom(type)) {
ArrayList<EventBase> registryList = EVENTLISTENER_REGISTRY.putIfAbsent(type, new ArrayList<>());
if(registryList==null) {
registryList = EVENTLISTENER_REGISTRY.get(type);
}
registryList.add(eventListener);
}
}
}

public static void unregister(EventBase eventListener) {
Expand All @@ -21,12 +32,41 @@ public static void unregister(EventBase eventListener) {
EVENTLISTENER_REGISTRY.remove(eventListener);
}

public static Object fireEvent(Class<? extends EventListenerRegistry.EventBase> eventClass) throws Exception {
return fireEvent(eventClass, new Object[] {});
}

public static Object fireEvent(Class<? extends EventListenerRegistry.EventBase> eventClass, Object... eventParams) throws Exception {
ArrayList<EventBase> registryList = EVENTLISTENER_REGISTRY.get(eventClass);
if(registryList == null) {
throw new Exception(String.format("The event {} has not been registered yet", eventClass));
}

Method methodToCheck = getEventMethod(eventClass);

for (EventBase eventListener : registryList) {
Method[] methodsInListener = eventListener.getClass().getDeclaredMethods();

for (Method method : methodsInListener) {
if(method.getName().equals(methodToCheck.getName())) {
method.setAccessible(true);
return method.invoke(eventListener, eventParams);
}
}
}
throw new Exception(String.format("The event {} has not been registered yet", eventClass));
}

public static ArrayList<EventBase> getEventListeners(){
return EVENTLISTENER_REGISTRY;
public static Method getEventMethod(Class<? extends EventListenerRegistry.EventBase> eventClass) throws Exception {
Method[] test = eventClass.getDeclaredMethods();
if(test.length != 1) {
throw new Exception("The event method is not properly defined. Only one method is allowed inside of an event");
}

return test[0];
}

public static interface EventBase {

}
}
7 changes: 6 additions & 1 deletion src/main/java/com/minecrafttas/mctcommon/server/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.minecrafttas.mctcommon.MCTCommon;
import com.minecrafttas.mctcommon.events.EventClient.EventDisconnectClient;
import com.minecrafttas.mctcommon.events.EventListenerRegistry;
import com.minecrafttas.mctcommon.events.EventServer.EventClientCompleteAuthentication;
import com.minecrafttas.mctcommon.server.exception.InvalidPacketException;
import com.minecrafttas.mctcommon.server.exception.PacketNotImplementedException;
Expand Down Expand Up @@ -83,7 +84,11 @@ public Client(String host, int port, PacketID[] packetIDs, String name, boolean
this.createHandlers();

this.callback = (client) -> {
EventDisconnectClient.fireDisconnectClient(client);
try {
EventListenerRegistry.fireEvent(EventDisconnectClient.class, client);
} catch (Exception e) {
e.printStackTrace();
}
};

this.local = local;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minecrafttas.mctcommon.events;
package com.minecrafttas.mctcommon.server;

import java.nio.ByteBuffer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.minecrafttas.mctcommon.server.interfaces;

import com.minecrafttas.mctcommon.events.CompactPacketHandler;
import com.minecrafttas.mctcommon.server.CompactPacketHandler;
import com.minecrafttas.mctcommon.server.Client.Side;

public interface PacketID {
Expand Down
Loading

0 comments on commit 6d22557

Please sign in to comment.