Skip to content

Events

Handle an event with on. The events guide explains how handlers work; this page lists every event.

Every program, on a computer or a drive, can handle these:

EventWhen it runs
shutdownRuns once when the program stops.
startupRuns once when the program starts.
timer.every(ticks)Runs every given number of ticks. 20 ticks is about one second.

on timer.every(ticks) needs a whole number of ticks, at least 1. Ticks that pass while a program is paused by the debugger are skipped.

Use a device constant: const gate = detectors.named("gate"), then on gate.entityEnter(entity) { … }. A drive program handles its own device’s events as on device.entityEnter(entity) { … }.

EventWhen it runs
entityEnter(entity)Runs when an entity enters the detector's range. entity has name, type, category, hostile, player, x, y, z and distance.
entityLeave(entity)Runs when an entity leaves the detector's range.
countChanged(count, previous)Runs when the number of entities in range changes.
connectedRuns when the device becomes reachable, for example after its area loads.
disconnectedRuns when the device stops being reachable: unloaded, unlinked or removed.
programStartedRuns when the device's own program starts.
programStoppedRuns when the device's own program stops.
programFailed(message)Runs when the device's own program stops on an error.
EventWhen it runs
rainStartRuns when it starts raining or snowing.
rainStopRuns when rain or snow stops.
thunderStartRuns when a thunderstorm starts.
thunderStopRuns when a thunderstorm ends.
dayStartRuns at sunrise.
nightStartRuns at nightfall.
weatherChanged(weather, previous)Runs when the weather changes between Weather.CLEAR, Weather.RAIN and Weather.THUNDER.
connectedRuns when the device becomes reachable, for example after its area loads.
disconnectedRuns when the device stops being reachable: unloaded, unlinked or removed.
programStartedRuns when the device's own program starts.
programStoppedRuns when the device's own program stops.
programFailed(message)Runs when the device's own program stops on an error.
EventWhen it runs
lightChanged(light, previous)Runs when the light level above the sensor changes.
skyLightChanged(light, previous)Runs when the sky light above the sensor changes.
darkened(light)Runs when the light drops below the sensor's threshold.
brightened(light)Runs when the light reaches the sensor's threshold.
connectedRuns when the device becomes reachable, for example after its area loads.
disconnectedRuns when the device stops being reachable: unloaded, unlinked or removed.
programStartedRuns when the device's own program starts.
programStoppedRuns when the device's own program stops.
programFailed(message)Runs when the device's own program stops on an error.
EventWhen it runs
inputChanged(face, power, previous)Runs when the redstone power arriving at an input face changes. face is a Direction.
signalOn(face, power)Runs when an input face turns on.
signalOff(face)Runs when an input face turns off.
connectedRuns when the device becomes reachable, for example after its area loads.
disconnectedRuns when the device stops being reachable: unloaded, unlinked or removed.
programStartedRuns when the device's own program starts.
programStoppedRuns when the device's own program stops.
programFailed(message)Runs when the device's own program stops on an error.
EventWhen it runs
contentsChanged(face)Runs when the items in an approved inventory change.
itemChanged(face, item, count, previous)Runs when the total count of one item type changes, such as minecraft:iron_ingot.
connectedRuns when the device becomes reachable, for example after its area loads.
disconnectedRuns when the device stops being reachable: unloaded, unlinked or removed.
programStartedRuns when the device's own program starts.
programStoppedRuns when the device's own program stops.
programFailed(message)Runs when the device's own program stops on an error.
EventWhen it runs
activatedRuns when the alarm starts sounding.
deactivatedRuns when the alarm is silenced.
finishedRuns when the alarm stops after its configured duration.
connectedRuns when the device becomes reachable, for example after its area loads.
disconnectedRuns when the device stops being reachable: unloaded, unlinked or removed.
programStartedRuns when the device's own program starts.
programStoppedRuns when the device's own program stops.
programFailed(message)Runs when the device's own program stops on an error.
EventWhen it runs
accessAttempted(player)Runs when a player uses the controller. player has name, uuid and result (Access.GRANTED or Access.DENIED).
accessGranted(player)Runs when a player is let in.
accessDenied(player)Runs when a player is refused.
connectedRuns when the device becomes reachable, for example after its area loads.
disconnectedRuns when the device stops being reachable: unloaded, unlinked or removed.
programStartedRuns when the device's own program starts.
programStoppedRuns when the device's own program stops.
programFailed(message)Runs when the device's own program stops on an error.
EventWhen it runs
buttonPressed(button)Runs when a player presses a button. button has id (Button.ONE to Button.FOUR), number, label and player.
buttonOne(player)Runs when button 1 is pressed. player is the name of who pressed it.
buttonTwo(player)Runs when button 2 is pressed.
buttonThree(player)Runs when button 3 is pressed.
buttonFour(player)Runs when button 4 is pressed.
connectedRuns when the device becomes reachable, for example after its area loads.
disconnectedRuns when the device stops being reachable: unloaded, unlinked or removed.
programStartedRuns when the device's own program starts.
programStoppedRuns when the device's own program stops.
programFailed(message)Runs when the device's own program stops on an error.
EventWhen it runs
connectedRuns when the device becomes reachable, for example after its area loads.
disconnectedRuns when the device stops being reachable: unloaded, unlinked or removed.
programStartedRuns when the device's own program starts.
programStoppedRuns when the device's own program stops.
programFailed(message)Runs when the device's own program stops on an error.

Storage Server, Network Repeater and Wireless Bridge

Section titled “Storage Server, Network Repeater and Wireless Bridge”

These devices have only the connection events:

EventWhen it runs
connectedRuns when the device becomes reachable, for example after its area loads.
disconnectedRuns when the device stops being reachable: unloaded, unlinked or removed.

The programStarted, programStopped and programFailed events belong to devices that can run drive programs. They run when that device’s own program changes state.

A computer program can also handle these events, with a name as the filter. They come from named ports of a Network Interface, named sides of a Redstone Controller (written "controller:side", such as "lamps:north"), older Sensors and custom events:

EventWhen it runs
events.custom("name", value, sender)Runs when another program on the network sends this custom event. sender has name, kind and id.
redstone.changed("name", strength)Runs when a named input port, or a controller face such as "gate:north", changes.
redstone.falling("name", strength)Runs when a named input turns off.
redstone.rising("name", strength)Runs when a named input turns on.
sensor.cleared("name", detected)Runs when a named Sensor sees no player any more.
sensor.detected("name", detected)Runs when a named Sensor first sees a player.

A drive program on a Redstone Controller can also use the older on redstone.rising("north"), redstone.falling and redstone.changed for its own sides, and every drive program can handle events.custom.

Rule Detail
Sampled Entity Detector every 10 ticks; Weather Station, Environment Sensor and Inventory Module every 20; only while listened to; at most 32 devices sampled per tick.
Immediate Redstone, Alarm, Access Controller and Control Panel events.
Baseline The first reading after a program starts sets the baseline; it raises no events.
Queue Each program queues at most 32 events. Extra events are dropped and counted.
Order One handler per program per tick, never while another handler of that program runs.
Reach Computers: loaded devices connected to the computer. Drive programs: loaded devices of the same network connected to them through loaded devices.
Values Copied for every handler. Lists, tuples and dictionaries at most 8 levels, 512 values, 16,384 characters.