diff --git a/src/main/java/com/minecrafttas/mctcommon/events/EventClient.java b/src/main/java/com/minecrafttas/mctcommon/events/EventClient.java index 7f1f6925..be971974 100644 --- a/src/main/java/com/minecrafttas/mctcommon/events/EventClient.java +++ b/src/main/java/com/minecrafttas/mctcommon/events/EventClient.java @@ -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; @@ -16,6 +15,7 @@ public interface EventClient { * @author Scribble * */ + @FunctionalInterface public static interface EventOpenGui extends EventBase { /** @@ -24,20 +24,6 @@ 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; - } } /** @@ -45,22 +31,13 @@ public static GuiScreen fireOpenGuiEvent(GuiScreen gui) { * @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(); - } - } - } } /** @@ -68,22 +45,13 @@ public static void fireOnLaunchIntegratedServer() { * @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(); - } - } - } } /** @@ -91,6 +59,7 @@ public static void fireOnDoneLoadingWorld() { * @author Scribble * */ + @FunctionalInterface public static interface EventClientTick extends EventBase { /** @@ -98,15 +67,6 @@ public static interface EventClientTick extends EventBase { * @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); - } - } - } } /** @@ -114,6 +74,7 @@ public static void fireOnClientTick(Minecraft mc) { * @author Scribble * */ + @FunctionalInterface public static interface EventClientInit extends EventBase { /** @@ -121,16 +82,6 @@ public static interface EventClientInit extends EventBase { * @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); - } - } - } } /** @@ -138,6 +89,7 @@ public static void fireOnClientInit(Minecraft mc) { * @author Scribble * */ + @FunctionalInterface public static interface EventClientGameLoop extends EventBase { /** @@ -145,15 +97,6 @@ public static interface EventClientGameLoop extends EventBase { * @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); - } - } - } } /** @@ -161,6 +104,7 @@ public static void fireOnClientGameLoop(Minecraft mc) { * @author Scribble * */ + @FunctionalInterface public static interface EventCamera extends EventBase { /** @@ -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; @@ -214,6 +145,7 @@ public boolean equals(Object obj) { * @author Scribble * */ + @FunctionalInterface public static interface EventPlayerLeaveClientSide extends EventBase { /** @@ -221,16 +153,6 @@ public static interface EventPlayerLeaveClientSide extends EventBase { * @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); - } - } - } } /** @@ -238,6 +160,7 @@ public static void firePlayerLeaveClientSide(EntityPlayerSP player) { * @author Scribble * */ + @FunctionalInterface public static interface EventPlayerJoinedClientSide extends EventBase { /** @@ -245,17 +168,6 @@ public static interface EventPlayerJoinedClientSide extends EventBase { * @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); - } - } - } - } /** @@ -263,6 +175,7 @@ public static void firePlayerJoinedClientSide(EntityPlayerSP player) { * @author Scribble * */ + @FunctionalInterface public static interface EventOtherPlayerJoinedClientSide extends EventBase { /** @@ -270,23 +183,12 @@ public static interface EventOtherPlayerJoinedClientSide extends EventBase { * @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 { /** @@ -294,15 +196,5 @@ public static interface EventDisconnectClient extends EventBase { * @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); - } - } - } } } diff --git a/src/main/java/com/minecrafttas/mctcommon/events/EventListenerRegistry.java b/src/main/java/com/minecrafttas/mctcommon/events/EventListenerRegistry.java index a7cde0c6..67623afb 100644 --- a/src/main/java/com/minecrafttas/mctcommon/events/EventListenerRegistry.java +++ b/src/main/java/com/minecrafttas/mctcommon/events/EventListenerRegistry.java @@ -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 EVENTLISTENER_REGISTRY = new ArrayList<>(); + private static HashMap, ArrayList> 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 registryList = EVENTLISTENER_REGISTRY.putIfAbsent(type, new ArrayList<>()); + if(registryList==null) { + registryList = EVENTLISTENER_REGISTRY.get(type); + } + registryList.add(eventListener); + } + } } public static void unregister(EventBase eventListener) { @@ -21,12 +32,41 @@ public static void unregister(EventBase eventListener) { EVENTLISTENER_REGISTRY.remove(eventListener); } + public static Object fireEvent(Class eventClass) throws Exception { + return fireEvent(eventClass, new Object[] {}); + } + + public static Object fireEvent(Class eventClass, Object... eventParams) throws Exception { + ArrayList 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 getEventListeners(){ - return EVENTLISTENER_REGISTRY; + public static Method getEventMethod(Class 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 { - + } } diff --git a/src/main/java/com/minecrafttas/mctcommon/server/Client.java b/src/main/java/com/minecrafttas/mctcommon/server/Client.java index a013dc87..0ffdf9ce 100644 --- a/src/main/java/com/minecrafttas/mctcommon/server/Client.java +++ b/src/main/java/com/minecrafttas/mctcommon/server/Client.java @@ -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; @@ -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; diff --git a/src/main/java/com/minecrafttas/mctcommon/events/CompactPacketHandler.java b/src/main/java/com/minecrafttas/mctcommon/server/CompactPacketHandler.java similarity index 86% rename from src/main/java/com/minecrafttas/mctcommon/events/CompactPacketHandler.java rename to src/main/java/com/minecrafttas/mctcommon/server/CompactPacketHandler.java index ee59e61d..8cd5c2b1 100644 --- a/src/main/java/com/minecrafttas/mctcommon/events/CompactPacketHandler.java +++ b/src/main/java/com/minecrafttas/mctcommon/server/CompactPacketHandler.java @@ -1,4 +1,4 @@ -package com.minecrafttas.mctcommon.events; +package com.minecrafttas.mctcommon.server; import java.nio.ByteBuffer; diff --git a/src/main/java/com/minecrafttas/mctcommon/server/interfaces/PacketID.java b/src/main/java/com/minecrafttas/mctcommon/server/interfaces/PacketID.java index d2388a38..462ab012 100644 --- a/src/main/java/com/minecrafttas/mctcommon/server/interfaces/PacketID.java +++ b/src/main/java/com/minecrafttas/mctcommon/server/interfaces/PacketID.java @@ -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 { diff --git a/src/main/java/com/minecrafttas/tasmod/networking/TASmodPackets.java b/src/main/java/com/minecrafttas/tasmod/networking/TASmodPackets.java index 74e1ff3e..bd874206 100644 --- a/src/main/java/com/minecrafttas/tasmod/networking/TASmodPackets.java +++ b/src/main/java/com/minecrafttas/tasmod/networking/TASmodPackets.java @@ -1,6 +1,6 @@ package com.minecrafttas.tasmod.networking; -import com.minecrafttas.mctcommon.events.CompactPacketHandler; +import com.minecrafttas.mctcommon.server.CompactPacketHandler; import com.minecrafttas.mctcommon.server.Client.Side; import com.minecrafttas.mctcommon.server.interfaces.PacketID; import com.minecrafttas.tasmod.commands.CommandFolder; diff --git a/src/test/java/mctcommon/event/EventTest.java b/src/test/java/mctcommon/event/EventTest.java new file mode 100644 index 00000000..72dd9c27 --- /dev/null +++ b/src/test/java/mctcommon/event/EventTest.java @@ -0,0 +1,38 @@ +package mctcommon.event; + +import org.junit.jupiter.api.Test; + +import com.minecrafttas.mctcommon.events.EventListenerRegistry; +import com.minecrafttas.mctcommon.events.EventListenerRegistry.EventBase; + +public class EventTest { + + @FunctionalInterface + private interface TestEvent extends EventBase{ + + public void onTestEvent(); + + public static void test() {}; + } + + @Test + void testRegisteringEvents() { + + + TestEvent event = new TestEvent() { + @Override + public void onTestEvent() { + System.out.println("Test"); + } + }; + + EventListenerRegistry.register(event); + + try { + EventListenerRegistry.fireEvent(TestEvent.class); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/src/test/java/mctcommon/server/ByteBufferBuilderTest.java b/src/test/java/mctcommon/server/ByteBufferBuilderTest.java index 6d7c24ee..8a78432f 100644 --- a/src/test/java/mctcommon/server/ByteBufferBuilderTest.java +++ b/src/test/java/mctcommon/server/ByteBufferBuilderTest.java @@ -10,8 +10,8 @@ import org.junit.jupiter.api.Test; -import com.minecrafttas.mctcommon.events.CompactPacketHandler; import com.minecrafttas.mctcommon.server.ByteBufferBuilder; +import com.minecrafttas.mctcommon.server.CompactPacketHandler; import com.minecrafttas.mctcommon.server.Client.Side; import com.minecrafttas.mctcommon.server.interfaces.PacketID; diff --git a/src/test/java/mctcommon/server/ServerTest.java b/src/test/java/mctcommon/server/ServerTest.java index 457c2f87..93459991 100644 --- a/src/test/java/mctcommon/server/ServerTest.java +++ b/src/test/java/mctcommon/server/ServerTest.java @@ -15,9 +15,9 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import com.minecrafttas.mctcommon.events.CompactPacketHandler; import com.minecrafttas.mctcommon.server.ByteBufferBuilder; import com.minecrafttas.mctcommon.server.Client; +import com.minecrafttas.mctcommon.server.CompactPacketHandler; import com.minecrafttas.mctcommon.server.Client.Side; import com.minecrafttas.mctcommon.server.PacketHandlerRegistry; import com.minecrafttas.mctcommon.server.Server; diff --git a/src/test/java/tasmod/networking/TASmodByteBufferBuilderTest.java b/src/test/java/tasmod/networking/TASmodByteBufferBuilderTest.java index 9f20bbc0..7fd8ebb5 100644 --- a/src/test/java/tasmod/networking/TASmodByteBufferBuilderTest.java +++ b/src/test/java/tasmod/networking/TASmodByteBufferBuilderTest.java @@ -10,7 +10,7 @@ import org.junit.jupiter.api.Test; -import com.minecrafttas.mctcommon.events.CompactPacketHandler; +import com.minecrafttas.mctcommon.server.CompactPacketHandler; import com.minecrafttas.mctcommon.server.Client.Side; import com.minecrafttas.mctcommon.server.interfaces.PacketID; import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;