mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-30 19:48:51 +00:00
woo back to generics
This commit is contained in:
parent
7ac31d9991
commit
cf7fae8d06
@ -21,7 +21,7 @@ import java.util.jar.JarFile;
|
|||||||
*/
|
*/
|
||||||
public final class PluginManager {
|
public final class PluginManager {
|
||||||
private final Map<String, Plugin> plugins = new HashMap<>();
|
private final Map<String, Plugin> plugins = new HashMap<>();
|
||||||
private final List<EventHandler> listeners = new LinkedList<>();
|
private final List<EventHandler<? extends Event>> listeners = new LinkedList<>();
|
||||||
|
|
||||||
public PluginManager() {
|
public PluginManager() {
|
||||||
this.loadPlugins(); // Load all plugins from the plugins directory.
|
this.loadPlugins(); // Load all plugins from the plugins directory.
|
||||||
@ -131,7 +131,7 @@ public final class PluginManager {
|
|||||||
* Registers a plugin's event listener.
|
* Registers a plugin's event listener.
|
||||||
* @param listener The event listener.
|
* @param listener The event listener.
|
||||||
*/
|
*/
|
||||||
public void registerListener(EventHandler listener) {
|
public void registerListener(EventHandler<? extends Event> listener) {
|
||||||
this.listeners.add(listener);
|
this.listeners.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,9 +161,10 @@ public final class PluginManager {
|
|||||||
* @param event The event passed through to the handler.
|
* @param event The event passed through to the handler.
|
||||||
* @param handler The handler to invoke.
|
* @param handler The handler to invoke.
|
||||||
*/
|
*/
|
||||||
private void invokeHandler(Event event, EventHandler handler) {
|
@SuppressWarnings("unchecked")
|
||||||
|
private <T extends Event> void invokeHandler(Event event, EventHandler<T> handler) {
|
||||||
if(!event.isCanceled() ||
|
if(!event.isCanceled() ||
|
||||||
(event.isCanceled() && handler.ignoresCanceled())
|
(event.isCanceled() && handler.ignoresCanceled())
|
||||||
) handler.getCallback().consume(event);
|
) handler.getCallback().consume((T) event);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,39 +3,32 @@ package emu.grasscutter.server.event;
|
|||||||
import emu.grasscutter.Grasscutter;
|
import emu.grasscutter.Grasscutter;
|
||||||
import emu.grasscutter.utils.EventConsumer;
|
import emu.grasscutter.utils.EventConsumer;
|
||||||
|
|
||||||
public final class EventHandler {
|
import java.lang.reflect.ParameterizedType;
|
||||||
private final Class<? extends Event> event;
|
|
||||||
|
|
||||||
/**
|
public final class EventHandler<T extends Event> {
|
||||||
* Creates an instance of {@link EventHandler} for the specified event.
|
private final Class<T> eventClass;
|
||||||
* @param event The event to handle.
|
private EventConsumer<T> listener;
|
||||||
* @return An instance of {@link EventHandler}.
|
|
||||||
*/
|
|
||||||
public static EventHandler forEvent(Class<? extends Event> event) {
|
|
||||||
return new EventHandler(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
private EventHandler(Class<? extends Event> event) {
|
|
||||||
this.event = event;
|
|
||||||
}
|
|
||||||
|
|
||||||
private EventConsumer listener;
|
|
||||||
private HandlerPriority priority;
|
private HandlerPriority priority;
|
||||||
private boolean handleCanceled;
|
private boolean handleCanceled;
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public EventHandler() {
|
||||||
|
this.eventClass = (Class<T>) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[0];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets which event this handler is handling.
|
* Gets which event this handler is handling.
|
||||||
* @return An event class.
|
* @return An event class.
|
||||||
*/
|
*/
|
||||||
public Class<? extends Event> handles() {
|
public Class<T> handles() {
|
||||||
return this.event;
|
return this.eventClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the callback for the handler.
|
* Returns the callback for the handler.
|
||||||
* @return A consumer callback.
|
* @return A consumer callback.
|
||||||
*/
|
*/
|
||||||
public EventConsumer getCallback() {
|
public EventConsumer<T> getCallback() {
|
||||||
return this.listener;
|
return this.listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +53,7 @@ public final class EventHandler {
|
|||||||
* @param listener An event handler method.
|
* @param listener An event handler method.
|
||||||
* @return Method chaining.
|
* @return Method chaining.
|
||||||
*/
|
*/
|
||||||
public EventHandler listener(EventConsumer listener) {
|
public EventHandler<T> listener(EventConsumer<T> listener) {
|
||||||
this.listener = listener; return this;
|
this.listener = listener; return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +62,7 @@ public final class EventHandler {
|
|||||||
* @param priority The priority of the handler.
|
* @param priority The priority of the handler.
|
||||||
* @return Method chaining.
|
* @return Method chaining.
|
||||||
*/
|
*/
|
||||||
public EventHandler priority(HandlerPriority priority) {
|
public EventHandler<T> priority(HandlerPriority priority) {
|
||||||
this.priority = priority; return this;
|
this.priority = priority; return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +71,7 @@ public final class EventHandler {
|
|||||||
* @param ignore If the handler should ignore cancelled events.
|
* @param ignore If the handler should ignore cancelled events.
|
||||||
* @return Method chaining.
|
* @return Method chaining.
|
||||||
*/
|
*/
|
||||||
public EventHandler ignore(boolean ignore) {
|
public EventHandler<T> ignore(boolean ignore) {
|
||||||
this.handleCanceled = ignore; return this;
|
this.handleCanceled = ignore; return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,6 @@ package emu.grasscutter.utils;
|
|||||||
|
|
||||||
import emu.grasscutter.server.event.Event;
|
import emu.grasscutter.server.event.Event;
|
||||||
|
|
||||||
public interface EventConsumer {
|
public interface EventConsumer<T extends Event> {
|
||||||
<T extends Event> void consume(T event);
|
void consume(T event);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user