Events
There are two ways to react to machines:
- The event bus — MachineEngine's own, for addons using the API. All events, including the per-tick ones.
- Bukkit events — ordinary
@EventHandlers, for plugins that do not use the API. The main machine events, mirrored.
Both run on the thread doing the operation — for machine events, the machine's thread (its region on Folia). Listeners must not block.
The event bus
Subscribe in your registration callback (see Add the dependency), with your addon as the owner:
ctx.events().subscribe(ctx.addon(), MachineEvents.PROCESS_COMPLETE, EventPhase.POST, event -> {
if (event.machine().type() == LavaGenerator.GENERATOR) {
// a lava generator finished burning a batch
}
});
- Phases:
PREruns before the operation, where the event allows changing or cancelling it;POSTruns after it committed, to observe. - A priority can be given (
EventPriority); the default isNORMAL. - A failing listener is logged and does not stop the others.
MachineEngine.get().events()gives the same bus after startup.
Event (MachineEvents) | Payload | When |
|---|---|---|
MACHINE_CREATE | Lifecycle(machine) | A machine is placed. |
MACHINE_LOAD | Lifecycle(machine) | A machine is loaded with its chunk. |
MACHINE_UNLOAD | Lifecycle(machine) | A machine is unloaded. |
MACHINE_REMOVE | Lifecycle(machine) | A machine is broken. |
PROCESS_START | Process(machine, process) | A process starts (its inputs were taken). |
PROCESS_COMPLETE | Process(machine, process) | A process finished and delivered its outputs. |
UPGRADE_CHANGE | Upgrade(machine, upgrade, …) | Upgrades are installed or taken out. |
ACTION | Action(machine, action, click, …) | A machine action ran (a menu click: upgrades, modules…). |
RESOURCES_CHANGED | Resources(machine, …) | A machine's storages changed this tick. Bus only. |
Also on the bus only: PlayerEvents.SETTING_CHANGED (a player changed a
setting) and EngineEvents.REGISTRIES_FROZEN (registration is over).
Bukkit events
In the package fr.robie.machineengine.paper.event. They are in the
machineengine-common artifact, so compile against it:
- Gradle (Kotlin)
- Gradle (Groovy)
- Maven
build.gradle.kts
repositories {
maven("https://1robie.github.io/maven/")
}
dependencies {
compileOnly("com.github.1robie.MachineEngine:machineengine-common:0.1.0")
}
build.gradle
repositories {
maven { url 'https://1robie.github.io/maven/' }
}
dependencies {
compileOnly 'com.github.1robie.MachineEngine:machineengine-common:0.1.0'
}
pom.xml
<repositories>
<repository>
<id>1robie</id>
<url>https://1robie.github.io/maven/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.1robie.MachineEngine</groupId>
<artifactId>machineengine-common</artifactId>
<version>0.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
| Event | Fires | Cancellable |
|---|---|---|
MachineCreateEvent | A machine is placed. | no |
MachineLoadEvent | A machine is loaded. | no |
MachineUnloadEvent | A machine is unloaded. | no |
MachineRemoveEvent | A machine is broken. | no |
MachineProcessStartEvent | A process starts. | no |
MachineProcessCompleteEvent | A process completes. | no |
MachineUpgradeChangeEvent | Upgrades change. | no |
MachineEngineReloadEvent | A registry was reloaded (recipes, tags). | no |
PlayerMachineInteractEvent | A player opens a machine's menu, before it opens. | yes |
Every machine event has getMachine(), getMachineType() (a Key),
getWorld() and getLocation(); the process events add getProcess() and
getRecipe().
@EventHandler
public void onInteract(PlayerMachineInteractEvent event) {
if (!event.getPlayer().hasPermission("lavagen.use")
&& event.getMachineType().equals(LavaGenerator.GENERATOR.key())) {
event.setCancelled(true); // the menu does not open
}
}
MachineEngineReloadEventruns on the thread of the reload, not a machine's.events.bukkit-bridge: falseinconfig.ymlturns the mirrored events off (restart).PlayerMachineInteractEventis not mirrored: it always fires.- Events nobody listens to cost nothing.