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 7f5cd1065d
commit f1623e9eab
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
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.EventHandler;
import emu.grasscutter.server.event.HandlerPriority;
import emu.grasscutter.utils.EventConsumer;
import emu.grasscutter.utils.Utils;
import java.io.File;
@ -14,7 +15,6 @@ import java.net.URLClassLoader;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Stream;
/**
* Manages the server's plugins and the event system.
@ -164,6 +164,6 @@ public final class PluginManager {
private void invokeHandler(Event event, EventHandler handler) {
if(!event.isCanceled() ||
(event.isCanceled() && handler.ignoresCanceled())
) handler.getCallback().accept(event);
) handler.getCallback().consume(event);
}
}

View File

@ -1,8 +1,7 @@
package emu.grasscutter.server.event;
import emu.grasscutter.Grasscutter;
import java.util.function.Consumer;
import emu.grasscutter.utils.EventConsumer;
public final class EventHandler {
private final Class<? extends Event> event;
@ -16,15 +15,11 @@ public final class EventHandler {
return new EventHandler(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) {
private EventHandler(Class<? extends Event> event) {
this.event = event;
}
private Consumer<Event> listener;
private EventConsumer listener;
private HandlerPriority priority;
private boolean handleCanceled;
@ -40,7 +35,7 @@ public final class EventHandler {
* Returns the callback for the handler.
* @return A consumer callback.
*/
public Consumer<Event> getCallback() {
public EventConsumer getCallback() {
return this.listener;
}
@ -65,7 +60,7 @@ public final class EventHandler {
* @param listener An event handler method.
* @return Method chaining.
*/
public EventHandler listener(Consumer<Event> listener) {
public EventHandler listener(EventConsumer listener) {
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);
}