From ae6a6abcd1ca0588bfcb6b493eec349b114ca5b1 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 20:47:45 -0400 Subject: [PATCH] woo back to generics --- .../emu/grasscutter/plugin/PluginManager.java | 9 +++-- .../server/event/EventHandler.java | 37 ++++++++----------- .../emu/grasscutter/utils/EventConsumer.java | 4 +- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/main/java/emu/grasscutter/plugin/PluginManager.java b/src/main/java/emu/grasscutter/plugin/PluginManager.java index 53ac467f3..d436b54d7 100644 --- a/src/main/java/emu/grasscutter/plugin/PluginManager.java +++ b/src/main/java/emu/grasscutter/plugin/PluginManager.java @@ -21,7 +21,7 @@ import java.util.jar.JarFile; */ public final class PluginManager { private final Map plugins = new HashMap<>(); - private final List listeners = new LinkedList<>(); + private final List> listeners = new LinkedList<>(); public PluginManager() { this.loadPlugins(); // Load all plugins from the plugins directory. @@ -131,7 +131,7 @@ public final class PluginManager { * Registers a plugin's event listener. * @param listener The event listener. */ - public void registerListener(EventHandler listener) { + public void registerListener(EventHandler listener) { this.listeners.add(listener); } @@ -161,9 +161,10 @@ public final class PluginManager { * @param event The event passed through to the handler. * @param handler The handler to invoke. */ - private void invokeHandler(Event event, EventHandler handler) { + @SuppressWarnings("unchecked") + private void invokeHandler(Event event, EventHandler handler) { if(!event.isCanceled() || (event.isCanceled() && handler.ignoresCanceled()) - ) handler.getCallback().consume(event); + ) handler.getCallback().consume((T) event); } } \ No newline at end of file diff --git a/src/main/java/emu/grasscutter/server/event/EventHandler.java b/src/main/java/emu/grasscutter/server/event/EventHandler.java index 14da6f6a1..be6b1c452 100644 --- a/src/main/java/emu/grasscutter/server/event/EventHandler.java +++ b/src/main/java/emu/grasscutter/server/event/EventHandler.java @@ -3,39 +3,32 @@ package emu.grasscutter.server.event; import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.EventConsumer; -public final class EventHandler { - private final Class event; +import java.lang.reflect.ParameterizedType; - /** - * Creates an instance of {@link EventHandler} for the specified event. - * @param event The event to handle. - * @return An instance of {@link EventHandler}. - */ - public static EventHandler forEvent(Class event) { - return new EventHandler(event); - } - - private EventHandler(Class event) { - this.event = event; - } - - private EventConsumer listener; +public final class EventHandler { + private final Class eventClass; + private EventConsumer listener; private HandlerPriority priority; private boolean handleCanceled; + + @SuppressWarnings("unchecked") + public EventHandler() { + this.eventClass = (Class) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[0]; + } /** * Gets which event this handler is handling. * @return An event class. */ - public Class handles() { - return this.event; + public Class handles() { + return this.eventClass; } /** * Returns the callback for the handler. * @return A consumer callback. */ - public EventConsumer getCallback() { + public EventConsumer getCallback() { return this.listener; } @@ -60,7 +53,7 @@ public final class EventHandler { * @param listener An event handler method. * @return Method chaining. */ - public EventHandler listener(EventConsumer listener) { + public EventHandler listener(EventConsumer listener) { this.listener = listener; return this; } @@ -69,7 +62,7 @@ public final class EventHandler { * @param priority The priority of the handler. * @return Method chaining. */ - public EventHandler priority(HandlerPriority priority) { + public EventHandler priority(HandlerPriority priority) { this.priority = priority; return this; } @@ -78,7 +71,7 @@ public final class EventHandler { * @param ignore If the handler should ignore cancelled events. * @return Method chaining. */ - public EventHandler ignore(boolean ignore) { + public EventHandler ignore(boolean ignore) { this.handleCanceled = ignore; return this; } diff --git a/src/main/java/emu/grasscutter/utils/EventConsumer.java b/src/main/java/emu/grasscutter/utils/EventConsumer.java index ae8bb5a9b..baa531944 100644 --- a/src/main/java/emu/grasscutter/utils/EventConsumer.java +++ b/src/main/java/emu/grasscutter/utils/EventConsumer.java @@ -2,6 +2,6 @@ package emu.grasscutter.utils; import emu.grasscutter.server.event.Event; -public interface EventConsumer { - void consume(T event); +public interface EventConsumer { + void consume(T event); }