Minestom wiki
Search…
Events

Overview

Event listening is a fairly hard part to keep easy while having a clear understanding of the execution flow. In Minestom, a tree is used to define inheritance for filtering and extensibility. Each node of the tree contains:
  • Event class, where only subclasses are allowed to enter (Event/PlayerEvent/etc...)
  • Condition for filtering
  • List of listeners
  • Name for identification
  • Priority
Event tree with all nodes being executed
The tree structure provides us many advantages:
  • Context-aware listeners due to node filtering
  • Clear execution order
  • Ability to store the event tree as an image for documentation purpose
  • Listener injection into existing nodes

API

Node

1
// Can listen to any Event, without any condition
2
EventNode<Event> node = EventNode.all("demo");
3
// Can only listen to entity events
4
EventNode<EntityEvent> entityNode = EventNode.type("entity-listener", EventFilter.ENTITY);
5
// Can only listen to player events
6
EventNode<PlayerEvent> playerNode = EventNode.type("player-listener", EventFilter.PLAYER);
7
// Listen to player events with the player in creative mode
8
EventNode<PlayerEvent> creativeNode = EventNode.value("creative-listener", EventFilter.PLAYER, Player::isCreative);
Copied!
Each node needs a name to be debuggable and be retrieved later on, an EventFilter containing the event type target and a way to retrieve its actor (i.e. a Player from a PlayerEvent). All factory methods accept a predicate to provide an additional condition for filtering purposes.

Listener

1
EventNode<Event> node = EventNode.all("demo");
2
node.addListener(EntityTickEvent.class, event -> {
3
// Inline listener
4
});
5
node.addListener(EventListener.builder(EntityTickEvent.class)
6
.expireCount(50) // Stop after 50 executions
7
.expireWhen(event -> event.getEntity().isGlowing()) // Stop once the predicate returns true
8
.handler(entityTickEvent ->
9
System.out.println("Entity tick!"))
10
.build());
11
12
EventNode<PlayerEvent> playerNode = EventNode.type("player-listener", EventFilter.PLAYER);
13
// playerNode.addListener(EntityTickEvent.class, event -> {}); -> does not work as playerNode only accept player events
14
playerNode.addListener(PlayerTickEvent.class, event -> {});
Copied!

Child

Children take the condition of their parent and are able to append to it.
1
EventNode<Event> node = EventNode.all("demo");
2
EventNode<PlayerEvent> playerNode = EventNode.type("player-listener", EventFilter.PLAYER);
3
4
node.addChild(playerNode); // Works as PlayerEvent is also an Event
5
6
// playerNode.addChild(node); -> Doesn't compile as the parent would be more restrictive than the child
Copied!

Event execution

Events can be executed from anywhere, not only the root node.
1
EventNode<Event> node = EventNode.all("demo");
2
node.call(new MyEvent());
Copied!

In practice

Now that you are familiar with the API, here is how you should use it inside your Minestom project.

Node to use

Server JAR

The root node of the server can be retrieved using MinecraftServer#getGlobalEventHandler(), you can safely insert new nodes.
1
var handler = MinecraftServer.getGlobalEventHandler();
2
handler.addListener(PlayerChatEvent.class,
3
event -> event.getPlayer().sendMessage("You sent a message!"));
4
var node = EventNode.all("demo");
5
node.addListener(PlayerMoveEvent.class,
6
event -> event.getPlayer().sendMessage("You moved!"));
7
handler.addChild(node);
Copied!

Extensions

Extensions should use their defined node from Extension#getEventNode(), which is removed from the root node once unloaded. Listeners inserted to external nodes must be removed manually.

Structure

Having an image of your tree is highly recommended, for documentation purposes and ensuring an optimal filtering path. It is then possible to use packages for major nodes, and classes for minor filtering.
1
Server/
2
Global.java
3
Lobby/
4
Rank/
5
- AdminRank.java
6
- VipRank.java
7
- DefaultRank.java
8
Game/
9
Bedwars/
10
Kit/
11
PvpKit.java
12
BuildKit.java
13
Bedwars.java
14
Skywars/
15
Kit/
16
PvpKit.java
17
BuildKit.java
18
Skywars.java
Copied!

Custom event

Event is an interface that you can freely implement, traits like CancellableEvent (to stop the execution after a certain point) and EntityEvent (telling the dispatcher that the event contains an entity actor) are also present to ensure your code will work with existing logic. You can then choose to run your custom event from an arbitrary node (see example), or from the root with EventDispatcher#call(Event).
Last modified 3mo ago