Entity Detector

The detector watches the area around it. It notices when a player, mob or item comes into range or leaves, and it can count and list what it sees. It is the device behind alarms, automatic doors and visitor counters.
How to get it
Section titled “How to get it”
Entity DetectorPlace and connect
Section titled “Place and connect”Place it where things will pass, for example beside a door; its lens faces you. Link it to a Network Repeater. It looks in every direction, so its facing does not change what it sees.
Configure
Section titled “Configure”
Open it with an empty hand:
| Setting | Meaning |
|---|---|
| Device name, Save alias | The name programs use, such as gate. |
| Radius, Save radius | How far it looks, 1 to 16 blocks from its center. Default 8. |
| Events report | Which entities raise entityEnter, entityLeave and countChanged: all, player, hostile, passive, item or other. |
| Refresh | Shows what it sees right now. |
What counts as an entity
Section titled “What counts as an entity”Players (except spectators), mobs, animals, dropped items and other entities. Each has a category: player,
hostile, passive, item or other, matching the Entity constants.
An entity in an event is a dictionary:
| Field | Value |
|---|---|
name |
the player’s name, the custom name or the entity’s displayed name |
type |
the entity type ID, such as minecraft:zombie |
category |
a constant such as Entity.HOSTILE |
hostile, player |
true or false |
x, y, z, distance |
block position and distance in blocks |
uuid |
players only |
A result of scan(...) or nearest(...) has type, category as text (such as "hostile"), x, y, z and
distance, plus name for players and entities with a visible custom name. No health, inventories or other private
details are available.
Drive programs
Section titled “Drive programs”Yes. With a drive, the detector can run its own program and use device.scan(...),
device.count(...), device.nearest(...), device.census(...) and device.radius(), plus device.entityEnter and
the other events.
Program API
Section titled “Program API”Getting it
Section titled “Getting it”detectors.named(alias)
Refer to an Entity Detector by its name. Use it in a constant, then handle its events with on NAME.event(...).
const gate = detectors.named("gate")detectors.from(alias)
Resolve an entity detector by name.
gate, err = detectors.from("gate")Methods
Section titled “Methods”A filter is an Entity constant such as Entity.HOSTILE, the same word as text, or an entity type ID such as
"minecraft:zombie".
Properties: gate.connected, gate.id, gate.kind, gate.name. They are read-only.
gate.census(filter)
Count matches and say whether the scan was cut short: count, truncated (true when more than 256 entities were in range, so count is a lower bound) and nearest (a match or nil). Filters: Entity.ALL, Entity.PLAYER, Entity.HOSTILE, Entity.PASSIVE, Entity.ITEM, Entity.OTHER or an entity type ID such as "minecraft:zombie".
seen, err = gate.census("hostile")gate.count(filter)
Count matching entities within the radius. At most 256 entities are examined; use census to learn whether a count was cut short. Filters: Entity.ALL, Entity.PLAYER, Entity.HOSTILE, Entity.PASSIVE, Entity.ITEM, Entity.OTHER or an entity type ID such as "minecraft:zombie".
zombies, err = gate.count("minecraft:zombie")gate.nearest(filter)
Describe the nearest matching entity, or nil when none is present. Filters: Entity.ALL, Entity.PLAYER, Entity.HOSTILE, Entity.PASSIVE, Entity.ITEM, Entity.OTHER or an entity type ID such as "minecraft:zombie".
closest, err = gate.nearest("player")gate.radius()
Read the configured detection radius in blocks (1–16).
radius, err = gate.radius()gate.scan(filter)
List up to 32 matching entities, nearest first, with type, category, x, y, z, distance and name for players or named entities. Filters: Entity.ALL, Entity.PLAYER, Entity.HOSTILE, Entity.PASSIVE, Entity.ITEM, Entity.OTHER or an entity type ID such as "minecraft:zombie".
hostiles, err = gate.scan("hostile")Events
Section titled “Events”| Event | When 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. |
connected | Runs when the device becomes reachable, for example after its area loads. |
disconnected | Runs when the device stops being reachable: unloaded, unlinked or removed. |
programStarted | Runs when the device's own program starts. |
programStopped | Runs when the device's own program stops. |
programFailed(message) | Runs when the device's own program stops on an error. |
Limits
Section titled “Limits”- It examines at most 256 entities. With more in range,
countstops at 256,censusreportstruncated: true, and events report arrivals but no departures or counts. scanreturns at most 32 entities, nearest first.- A server runs at most 16 new scans per tick; more return
detector.busy, so try again on the next tick. Calls in the same tick share one scan. - Events compare readings every 10 ticks, and only while a program listens.
Example
Section titled “Example”const gate = detectors.named("gate")
on gate.countChanged(count, previous) { if count > previous { print(str(count) + " things near the gate") }}
on gate.entityEnter(entity) { if entity.player { print("Welcome, " + entity.name) } else if entity.hostile { print("A " + entity.type + " is coming!") }}