Events
MachineEngine's event bus and the Bukkit events it mirrors.
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.api.paper.event, in the API artifact
(since 0.2.0; in 0.1.0 they were in machineengine-common, package
fr.robie.machineengine.paper.event):
repositories {
maven("https://1robie.github.io/maven/")
}
dependencies {
compileOnly("com.github.1robie.MachineEngine:machineengine-api:0.2.0")
}| 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.
World fluids
Fluids poured into the world (World fluids) fire their own Bukkit events, on the thread of the block or entity. They are not on the bus and always fire.
| Event | Fires | Cancellable |
|---|---|---|
WorldFluidPlaceEvent | A placeable fluid is about to be in a block. Cause: BUCKET, SPREAD, MACHINE, COMMAND, API. | yes |
WorldFluidRemoveEvent | A placeable fluid left a block. Cause: BUCKET, DRIED, REPLACED, MACHINE, COMMAND, API. | no |
EntityFluidEvent.Enter | An entity entered a placeable fluid. | no |
EntityFluidEvent.Leave | An entity left it. | no |
Each has getFluid() (a FluidType); the world events add getBlock() and
getCause(), the entity events getEntity().
Last updated on