EventHandler.java 2.16 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
package emu.grasscutter.server.event;

3
import emu.grasscutter.Grasscutter;
4
import emu.grasscutter.plugin.Plugin;
5
import emu.grasscutter.utils.EventConsumer;
6

KingRainbow44's avatar
KingRainbow44 committed
7
8
9
public final class EventHandler<T extends Event> {
    private final Class<T> eventClass;
    private EventConsumer<T> listener;
10
11
    private HandlerPriority priority;
    private boolean handleCanceled;
KingRainbow44's avatar
KingRainbow44 committed
12
    
13
14
    public EventHandler(Class<T> eventClass) {
        this.eventClass = eventClass;
KingRainbow44's avatar
KingRainbow44 committed
15
    }
16
17
18
19
20

    /**
     * Gets which event this handler is handling.
     * @return An event class.
     */
KingRainbow44's avatar
KingRainbow44 committed
21
22
    public Class<T> handles() {
        return this.eventClass;
23
24
25
26
27
28
    }

    /**
     * Returns the callback for the handler.
     * @return A consumer callback.
     */
KingRainbow44's avatar
KingRainbow44 committed
29
    public EventConsumer<T> getCallback() {
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        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.
     */
KingRainbow44's avatar
KingRainbow44 committed
54
    public EventHandler<T> listener(EventConsumer<T> listener) {
55
56
57
58
59
60
61
62
        this.listener = listener; return this;
    }

    /**
     * Changes the handler's priority in handling events.
     * @param priority The priority of the handler.
     * @return Method chaining.
     */
KingRainbow44's avatar
KingRainbow44 committed
63
    public EventHandler<T> priority(HandlerPriority priority) {
64
65
66
67
68
69
70
71
        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.
     */
KingRainbow44's avatar
KingRainbow44 committed
72
    public EventHandler<T> ignore(boolean ignore) {
73
74
75
76
77
78
        this.handleCanceled = ignore; return this;
    }

    /**
     * Registers the handler into the PluginManager.
     */
79
80
    public void register(Plugin plugin) {
        Grasscutter.getPluginManager().registerListener(plugin, this);
81
    }
KingRainbow44's avatar
KingRainbow44 committed
82
}