Listen to Events
About 238 wordsLess than 1 minute
2025-08-21
Basics
Like all other BukkitEvents, to listen to Dominion events, you need to implement a listener class and handle the events you want to listen to within it. You can cancel the event (prevent it from happening) or modify the data in the event as needed.
// DominionEventHandler.java
import cn.lunadeer.dominion.events.dominion.DominionCreateEvent;
public class YourPluginMainClass extends JavaPlugin implements Listener {
@EventHandler
public void onDominionCreateEvent(DominionCreateEvent event) {
if (event.isCancelled()) return;
// do something you want
}
@Override
public void onEnable() {
// Plugin startup logic
getServer().getPluginManager().registerEvents(this, this);
}
}
Getting the Result of Event Operations
For some events related to territory data operations (such as DominionCreateEvent
, MemberAddedEvent
, etc.), the actual data processing is performed after all event listeners have finished executing. Therefore, it is not possible to obtain the operation result directly from the event itself. However, you can configure a callback for the event to handle the result after the data operation is completed.
Take MemberAddedEvent
as an example. Suppose we want to automatically teleport the member to the territory after they are successfully added:
import cn.lunadeer.dominion.events.member.MemberAddedEvent;
public class AutoTpMember implements Listener {
@EventHandler
public void onDominionAddMember(MemberAddedEvent event) {
if (event.isCancelled()) return;
// the lambda function will be called after the member is added
event.afterAdded(memberDTO -> {
if (memberDTO == null) return; // if memberDTO is null, means the addition failed
DominionDTO dominion = event.getDominion();
Player player = Bukkit.getPlayer(memberDTO.getPlayerUUID());
player.teleportAsync(dominion.getTpLocation());
});
}
}