Commit 6056e962 authored by KingRainbow44's avatar KingRainbow44
Browse files

Implement an event handler

parent 7a3fbcdc
package emu.grasscutter.plugin;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.server.event.Event;
import emu.grasscutter.server.event.EventHandler;
import emu.grasscutter.server.event.Listener;
import emu.grasscutter.utils.Utils;
import org.reflections.Reflections;
import java.io.File;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* Manages the server's plugins & the event system.
*/
public final class PluginManager {
private final Map<String, Plugin> plugins = new HashMap<>();
private final Map<Plugin, List<Listener>> listeners = new HashMap<>();
public PluginManager() {
this.loadPlugins(); // Load all plugins from the plugins directory.
......@@ -113,4 +115,38 @@ public final class PluginManager {
plugin.onDisable();
});
}
/**
* Registers a plugin's event listener.
* @param plugin The plugin instance.
* @param listener The event listener.
*/
public void registerListener(Plugin plugin, Listener listener) {
this.listeners.computeIfAbsent(plugin, k -> new ArrayList<>()).add(listener);
}
/**
* Invoke the provided event on all registered event listeners.
* @param event The event to invoke.
*/
public void invokeEvent(Event event) {
this.listeners.values().stream()
.flatMap(Collection::stream)
.forEach(listener -> this.invokeOnListener(listener, event));
}
/**
* Attempts to invoke the event on the provided listener.
*/
private void invokeOnListener(Listener listener, Event event) {
try {
Class<?> listenerClass = listener.getClass();
Method[] methods = listenerClass.getMethods();
for (Method method : methods) {
if(!method.isAnnotationPresent(EventHandler.class)) return;
if(!method.getParameterTypes()[0].isAssignableFrom(event.getClass())) return;
method.invoke(listener, event);
}
} catch (Exception ignored) { }
}
}
\ No newline at end of file
package emu.grasscutter.server.event;
/**
* Implementing this interface marks an event as cancellable.
*/
public interface Cancellable {
void cancel();
}
package emu.grasscutter.server.event;
import emu.grasscutter.Grasscutter;
/**
* A generic server event.
*/
public abstract class Event {
private boolean cancelled = false;
/**
* Return the cancelled state of the event.
*/
public boolean isCanceled() {
return this.cancelled;
}
/**
* Cancels the event if possible.
*/
public void cancel() throws IllegalAccessException {
if(!(this instanceof Cancellable))
throw new IllegalAccessException("Event is not cancellable.");
this.cancelled = true;
}
/**
* Pushes this event to all listeners.
*/
public void call() {
Grasscutter.getPluginManager().invokeEvent(this);
}
}
package emu.grasscutter.server.event;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Declares a class as an event listener/handler.
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface EventHandler {
}
\ No newline at end of file
package emu.grasscutter.server.event;
/**
* Implementing this interface declares a class as an event listener.
*/
public interface Listener {
}
package emu.grasscutter.server.event;
/**
* An event that is related to the internals of the server.
*/
public abstract class ServerEvent extends Event {
protected final Type type;
public ServerEvent(Type type) {
this.type = type;
}
public enum Type {
DISPATCH,
GAME
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment