You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/paper/dev/event-api/custom-events.md
+23-5Lines changed: 23 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,11 +5,12 @@ slug: /dev/custom-events
5
5
# Custom Events
6
6
7
7
Creating custom events is a great way to add functionality to your plugin.
8
-
This will allow for other people to listen for your custom events and add functionality to your plugin.
8
+
This will allow other plugins to listen to your custom events and add functionality to your plugin.
9
9
10
10
## Creating a custom event
11
11
12
-
To create a custom event, you need to create a class that extends `Event`. Each event has a `HandlerList` that contains all the listeners that are listening for that event.
12
+
To create a custom event, you need to create a class that extends `Event`. Each event requires a `HandlerList` that will contain all the listeners that are listening to that event. The only exception to this requirement is when you have an event class that cannot be fired, but serves as a parent for other events instead.
13
+
An example of this is the BlockPistonEvent, which cannot be listened to directly.
13
14
14
15
This list is used to call the listeners when the event is called.
15
16
@@ -78,7 +79,7 @@ public class ExamplePlugin extends JavaPlugin {
78
79
// ...
79
80
80
81
publicvoidcallCoolPaperEvent() {
81
-
PaperIsCoolEvent coolEvent =newPaperIsCoolEvent(Component.text("Paper is cool!"))
82
+
PaperIsCoolEvent coolEvent =newPaperIsCoolEvent(Component.text("Paper is cool!"));
82
83
coolEvent.callEvent();
83
84
// Plugins could have changed the message from inside their listeners here. So we need to get the message again.
84
85
// This event structure allows for other plugins to change the message to their taste.
@@ -121,11 +122,28 @@ public class ExamplePlugin extends JavaPlugin {
121
122
// ...
122
123
123
124
publicvoidcallCoolPaperEvent() {
124
-
PaperIsCoolEvent coolEvent =newPaperIsCoolEvent(Component.text("Paper is cool!"))
125
+
PaperIsCoolEvent coolEvent =newPaperIsCoolEvent(Component.text("Paper is cool!"));
125
126
coolEvent.callEvent();
126
127
if (!coolEvent.isCancelled()) {
127
128
Bukkit.broadcast(coolEvent.getMessage());
128
129
}
129
130
}
130
131
}
131
-
```
132
+
```
133
+
134
+
When an event is cancellable, `Event#callEvent` will return false if the event was cancelled. This allows you to directly use `callEvent`
135
+
in your if statement, instead of having to check `Cancellable#isCancelled` manually.
136
+
137
+
```java title="ExamplePlugin.java"
138
+
publicclassExamplePluginextendsJavaPlugin {
139
+
140
+
// ...
141
+
142
+
publicvoidcallCoolPaperEvent() {
143
+
PaperIsCoolEvent coolEvent =newPaperIsCoolEvent(Component.text("Paper is cool!"));
144
+
if (coolEvent.callEvent()) { // Directly get the output from callEvent
0 commit comments