Skip to main content

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: PRE runs before the operation, where the event allows changing or cancelling it; POST runs after it committed, to observe.
  • A priority can be given (EventPriority); the default is NORMAL.
  • A failing listener is logged and does not stop the others.
  • MachineEngine.get().events() gives the same bus after startup.
Event (MachineEvents)PayloadWhen
MACHINE_CREATELifecycle(machine)A machine is placed.
MACHINE_LOADLifecycle(machine)A machine is loaded with its chunk.
MACHINE_UNLOADLifecycle(machine)A machine is unloaded.
MACHINE_REMOVELifecycle(machine)A machine is broken.
PROCESS_STARTProcess(machine, process)A process starts (its inputs were taken).
PROCESS_COMPLETEProcess(machine, process)A process finished and delivered its outputs.
UPGRADE_CHANGEUpgrade(machine, upgrade, …)Upgrades are installed or taken out.
ACTIONAction(machine, action, click, …)A machine action ran (a menu click: upgrades, modules…).
RESOURCES_CHANGEDResources(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:

build.gradle.kts
repositories {
maven("https://1robie.github.io/maven/")
}

dependencies {
compileOnly("com.github.1robie.MachineEngine:machineengine-common:0.1.0")
}
EventFiresCancellable
MachineCreateEventA machine is placed.no
MachineLoadEventA machine is loaded.no
MachineUnloadEventA machine is unloaded.no
MachineRemoveEventA machine is broken.no
MachineProcessStartEventA process starts.no
MachineProcessCompleteEventA process completes.no
MachineUpgradeChangeEventUpgrades change.no
MachineEngineReloadEventA registry was reloaded (recipes, tags).no
PlayerMachineInteractEventA 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
}
}
  • MachineEngineReloadEvent runs on the thread of the reload, not a machine's.
  • events.bukkit-bridge: false in config.yml turns the mirrored events off (restart). PlayerMachineInteractEvent is not mirrored: it always fires.
  • Events nobody listens to cost nothing.