aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/me/bush/eventbuskotlin/Event.kt
blob: 6b9646168b7dbbba11f3a45906848ca41ad63c04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package me.bush.eventbuskotlin

/**
 * A base class for events that can be cancelled.
 *
 * [Information and examples](https://github.com/therealbush/eventbus-kotlin#creating-an-event)
 *
 * @author bush
 * @since 1.0.0
 */
abstract class Event {

    /**
     * Whether this event is cancelled or not. If it is, only future listeners with
     * [Listener.receiveCancelled] will receive it. However, it can be set back to
     * `false`, and listeners will be able to receive it again.
     *
     * [Information and examples](https://github.com/therealbush/eventbus-kotlin#posting-an-event)
     */
    var cancelled = false
        set(value) {
            if (cancellable) field = value
        }

    /**
     * Determines if this event can be [cancelled]. This does not have to return a constant value.
     *
     * [Information and examples](https://github.com/therealbush/eventbus-kotlin#creating-an-event)
     */
    protected abstract val cancellable: Boolean

    /**
     * Sets [cancelled] to true.
     *
     * [Information and examples](https://github.com/therealbush/eventbus-kotlin#posting-an-event)
     */
    fun cancel() {
        cancelled = true
    }
}