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

3
4
5
6
import emu.grasscutter.Grasscutter;

import java.util.function.Consumer;

KingRainbow44's avatar
KingRainbow44 committed
7
public final class EventHandler {
KingRainbow44's avatar
KingRainbow44 committed
8
9
10
11
12
13
14
15
16
17
    private final Class<? extends Event> event;

    /**
     * Creates an instance of {@link EventHandler} for the specified event.
     * @param event The event to handle.
     * @return An instance of {@link EventHandler}.
     */
    public static EventHandler forEvent(Class<? extends Event> event) {
        return new EventHandler(event);
    }
KingRainbow44's avatar
KingRainbow44 committed
18
    
KingRainbow44's avatar
KingRainbow44 committed
19
20
21
22
23
    /**
     * @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) {
KingRainbow44's avatar
KingRainbow44 committed
24
25
        this.event = event;
    }
26
27
28
29
30
31
32
33
34
    
    private Consumer<Event> listener;
    private HandlerPriority priority;
    private boolean handleCanceled;

    /**
     * Gets which event this handler is handling.
     * @return An event class.
     */
KingRainbow44's avatar
KingRainbow44 committed
35
    public Class<? extends Event> handles() {
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        return this.event;
    }

    /**
     * Returns the callback for the handler.
     * @return A consumer callback.
     */
    public Consumer<Event> getCallback() {
        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
68
    public EventHandler listener(Consumer<Event> listener) {
69
70
71
72
73
74
75
76
        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
77
    public EventHandler priority(HandlerPriority priority) {
78
79
80
81
82
83
84
85
        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
86
    public EventHandler ignore(boolean ignore) {
87
88
89
90
91
92
93
94
95
        this.handleCanceled = ignore; return this;
    }

    /**
     * Registers the handler into the PluginManager.
     */
    public void register() {
        Grasscutter.getPluginManager().registerListener(this);
    }
KingRainbow44's avatar
KingRainbow44 committed
96
}