Skip to content

Commit

Permalink
Revert "[Event] Drafting new event firing"
Browse files Browse the repository at this point in the history
This reverts commit 8614dbb.
  • Loading branch information
ScribbleTAS committed Nov 16, 2023
1 parent c578256 commit cdb2804
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 54 deletions.
13 changes: 11 additions & 2 deletions src/main/java/com/minecrafttas/common/events/EventClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ public static interface EventOpenGui extends EventBase {
public GuiScreen onOpenGui(GuiScreen gui);

public static GuiScreen fireOpenGuiEvent(GuiScreen gui) {
Common.LOGGER.trace(Common.Event, "Firing LaunchIntegratedServer");
return (GuiScreen) EventBase.fireEvent(EventOpenGui.class, gui);
Common.LOGGER.trace(Common.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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.minecrafttas.common.events;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class EventListenerRegistry {

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


public static ArrayList<EventBase> getEventListeners(){
return EVENTLISTENER_REGISTRY;
}

public static interface EventBase {

/**
* Fires the event specified in eventClass
* @param eventClass The event handler class
* @param eventParams The parameters for this event
* @return The return value of the event
*/
public static Object fireEvent(Class<? extends EventListenerRegistry.EventBase> eventClass, Object... eventParams) {

Method methodToFire = null;

for(Method method : eventClass.getMethods()) {
if(Modifier.isAbstract(method.getModifiers())) {
methodToFire = method;
break;
}
}

for(EventListenerRegistry.EventBase eventListener : EventListenerRegistry.EVENTLISTENER_REGISTRY) {
Class<?>[] interfaces = eventListener.getClass().getInterfaces();
for(Class<?> interfaze : interfaces) {
if(interfaze.equals(eventClass)) {
Method methodInClass = null;
Method[] methods = eventListener.getClass().getMethods();
for(Method method : methods) {
if(method.getName().equals(methodToFire.getName())) {
methodInClass = method;
break;
}
}

try {
return methodInClass.invoke(eventListener, eventParams);
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
} catch (IllegalArgumentException e) {
e.printStackTrace();
return null;
} catch (InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
}
}
return null;
}
}
}

0 comments on commit cdb2804

Please sign in to comment.