Implement an event handler

This commit is contained in:
KingRainbow44
2022-04-23 01:58:37 -04:00
parent 7a3fbcdcf7
commit 6056e962d6
6 changed files with 116 additions and 4 deletions

View File

@@ -0,0 +1,8 @@
package emu.grasscutter.server.event;
/**
* Implementing this interface marks an event as cancellable.
*/
public interface Cancellable {
void cancel();
}

View File

@@ -0,0 +1,33 @@
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);
}
}

View File

@@ -0,0 +1,11 @@
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 {
}

View File

@@ -0,0 +1,7 @@
package emu.grasscutter.server.event;
/**
* Implementing this interface declares a class as an event listener.
*/
public interface Listener {
}

View File

@@ -0,0 +1,17 @@
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
}
}