Use a custom consumer instead of the built-in one

This commit is contained in:
KingRainbow44 2022-04-26 20:28:13 -04:00
parent 993255235e
commit 88bdaf155f
3 changed files with 14 additions and 12 deletions

View File

@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter;
import emu.grasscutter.server.event.Event; import emu.grasscutter.server.event.Event;
import emu.grasscutter.server.event.EventHandler; import emu.grasscutter.server.event.EventHandler;
import emu.grasscutter.server.event.HandlerPriority; import emu.grasscutter.server.event.HandlerPriority;
import emu.grasscutter.utils.EventConsumer;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
import java.io.File; import java.io.File;
@ -14,7 +15,6 @@ import java.net.URLClassLoader;
import java.util.*; import java.util.*;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.stream.Stream;
/** /**
* Manages the server's plugins and the event system. * Manages the server's plugins and the event system.
@ -164,6 +164,6 @@ public final class PluginManager {
private void invokeHandler(Event event, EventHandler handler) { private void invokeHandler(Event event, EventHandler handler) {
if(!event.isCanceled() || if(!event.isCanceled() ||
(event.isCanceled() && handler.ignoresCanceled()) (event.isCanceled() && handler.ignoresCanceled())
) handler.getCallback().accept(event); ) handler.getCallback().consume(event);
} }
} }

View File

@ -1,8 +1,7 @@
package emu.grasscutter.server.event; package emu.grasscutter.server.event;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.utils.EventConsumer;
import java.util.function.Consumer;
public final class EventHandler { public final class EventHandler {
private final Class<? extends Event> event; private final Class<? extends Event> event;
@ -16,15 +15,11 @@ public final class EventHandler {
return new EventHandler(event); return new EventHandler(event);
} }
/** private EventHandler(Class<? extends Event> event) {
* @deprecated Will be replaced with a private constructor instead. Use {@link #forEvent(Class)} instead.
*/
@Deprecated(forRemoval = true, since = "1.0.1")
public EventHandler(Class<? extends Event> event) {
this.event = event; this.event = event;
} }
private Consumer<Event> listener; private EventConsumer listener;
private HandlerPriority priority; private HandlerPriority priority;
private boolean handleCanceled; private boolean handleCanceled;
@ -40,7 +35,7 @@ public final class EventHandler {
* Returns the callback for the handler. * Returns the callback for the handler.
* @return A consumer callback. * @return A consumer callback.
*/ */
public Consumer<Event> getCallback() { public EventConsumer getCallback() {
return this.listener; return this.listener;
} }
@ -65,7 +60,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(Consumer<Event> listener) { public EventHandler listener(EventConsumer listener) {
this.listener = listener; return this; this.listener = listener; return this;
} }

View File

@ -0,0 +1,7 @@
package emu.grasscutter.utils;
import emu.grasscutter.server.event.Event;
public interface EventConsumer {
<T extends Event> void consume(T event);
}