2022-04-23 05:58:37 +00:00
|
|
|
package emu.grasscutter.server.event;
|
|
|
|
|
2022-04-26 17:52:10 +00:00
|
|
|
import emu.grasscutter.Grasscutter;
|
2022-04-27 00:28:13 +00:00
|
|
|
import emu.grasscutter.utils.EventConsumer;
|
2022-04-26 17:52:10 +00:00
|
|
|
|
2022-04-27 00:47:45 +00:00
|
|
|
public final class EventHandler<T extends Event> {
|
|
|
|
private final Class<T> eventClass;
|
|
|
|
private EventConsumer<T> listener;
|
2022-04-26 17:52:10 +00:00
|
|
|
private HandlerPriority priority;
|
|
|
|
private boolean handleCanceled;
|
2022-04-27 00:47:45 +00:00
|
|
|
|
2022-04-27 00:54:56 +00:00
|
|
|
public EventHandler(Class<T> eventClass) {
|
|
|
|
this.eventClass = eventClass;
|
2022-04-27 00:47:45 +00:00
|
|
|
}
|
2022-04-26 17:52:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets which event this handler is handling.
|
|
|
|
* @return An event class.
|
|
|
|
*/
|
2022-04-27 00:47:45 +00:00
|
|
|
public Class<T> handles() {
|
|
|
|
return this.eventClass;
|
2022-04-26 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the callback for the handler.
|
|
|
|
* @return A consumer callback.
|
|
|
|
*/
|
2022-04-27 00:47:45 +00:00
|
|
|
public EventConsumer<T> getCallback() {
|
2022-04-26 17:52:10 +00:00
|
|
|
return this.listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the handler's priority.
|
|
|
|
* @return The priority of the handler.
|
|
|
|
*/
|
|
|
|
public HandlerPriority getPriority() {
|
|
|
|
return this.priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if the handler will ignore cancelled events.
|
|
|
|
* @return The ignore cancelled state.
|
|
|
|
*/
|
|
|
|
public boolean ignoresCanceled() {
|
|
|
|
return this.handleCanceled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the callback method for when the event is invoked.
|
|
|
|
* @param listener An event handler method.
|
|
|
|
* @return Method chaining.
|
|
|
|
*/
|
2022-04-27 00:47:45 +00:00
|
|
|
public EventHandler<T> listener(EventConsumer<T> listener) {
|
2022-04-26 17:52:10 +00:00
|
|
|
this.listener = listener; return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the handler's priority in handling events.
|
|
|
|
* @param priority The priority of the handler.
|
|
|
|
* @return Method chaining.
|
|
|
|
*/
|
2022-04-27 00:47:45 +00:00
|
|
|
public EventHandler<T> priority(HandlerPriority priority) {
|
2022-04-26 17:52:10 +00:00
|
|
|
this.priority = priority; return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets if the handler will ignore cancelled events.
|
|
|
|
* @param ignore If the handler should ignore cancelled events.
|
|
|
|
* @return Method chaining.
|
|
|
|
*/
|
2022-04-27 00:47:45 +00:00
|
|
|
public EventHandler<T> ignore(boolean ignore) {
|
2022-04-26 17:52:10 +00:00
|
|
|
this.handleCanceled = ignore; return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the handler into the PluginManager.
|
|
|
|
*/
|
|
|
|
public void register() {
|
|
|
|
Grasscutter.getPluginManager().registerListener(this);
|
|
|
|
}
|
2022-04-23 05:58:37 +00:00
|
|
|
}
|