Skip to main content

Interface: EntityQueryOptions

Contains options for selecting entities within an area.

Examples

import { Dimension } from '@minecraft/server';

// Having this command:

// execute as @e[type=fox] positioned as @s if block ^ ^-1 ^ stone run summon salmon

// Equivalent scripting code would be:
function spawnFish(dimension: Dimension) {
dimension
.getEntities({
type: 'fox',
})
.filter(entity => {
const block = dimension.getBlock({
x: entity.location.x,
y: entity.location.y - 1,
z: entity.location.z,
});

return block !== undefined && block.matches('minecraft:stone');
})
.forEach(entity => {
dimension.spawnEntity('salmon', entity.location);
});
}
import { Dimension } from '@minecraft/server';

// Having this command:

// execute as @e[type=armor_stand,name=myArmorStand,tag=dummyTag1,tag=!dummyTag2] run playsound raid.horn @a

// Equivalent scripting code would be:
function playSounds(dimension: Dimension) {
const targetPlayers = dimension.getPlayers();
const originEntities = dimension.getEntities({
type: 'armor_stand',
name: 'myArmorStand',
tags: ['dummyTag1'],
excludeTags: ['dummyTag2'],
});

originEntities.forEach(entity => {
targetPlayers.forEach(player => {
player.playSound('raid.horn');
});
});
}
import { world, EntityQueryOptions } from '@minecraft/server';

// Having this command:

// execute as @e[has_property={propId=propValue}]

// Equivalent scripting code would be:
function findEntitiesHavingPropertyEqualsTo(propId: string, propValue: boolean | number | string) {
const queryOption: EntityQueryOptions = {
propertyOptions: [{ propertyId: propId, value: { equals: propValue } }]
};

const overworld = world.getDimension('overworld');
const entities = overworld.getEntities(queryOption);
}
import { world, EntityQueryOptions } from '@minecraft/server';

// No equivalent commands as `propValue..` is inclusive in commands

function findEntitiesHavingPropertyGreaterThan(propId: string, propValue: number) {
const queryOption: EntityQueryOptions = {
propertyOptions: [{ propertyId: propId, value: { greaterThan: propValue } }]
};

const overworld = world.getDimension('overworld');
const entities = overworld.getEntities(queryOption);
}
import { world, EntityQueryOptions } from '@minecraft/server';

// Having this command:

// execute as @e[has_property={propId=propValue..}]

// Equivalent scripting code would be:
function findEntitiesHavingPropertyGreaterThanOrEqualsTo(propId: string, propValue: number) {
const queryOption: EntityQueryOptions = {
propertyOptions: [{ propertyId: propId, value: { greaterThanOrEquals: propValue } }]
};

const overworld = world.getDimension('overworld');
const entities = overworld.getEntities(queryOption);
}
import { world, EntityQueryOptions } from '@minecraft/server';

// Having this command:

// execute as @e[has_property={property=propId}]

// Equivalent scripting code would be:
function findEntitiesHavingAProperty(propId: string) {
const queryOption: EntityQueryOptions = {
propertyOptions: [{ propertyId: propId }]
};

const overworld = world.getDimension('overworld');
const entities = overworld.getEntities(queryOption);
}
import { world, EntityQueryOptions } from '@minecraft/server';

// No equivalent commands as `..propValue` is inclusive in commands

function findEntitiesHavingPropertyLessThan(propId: string, propValue: number) {
const queryOption: EntityQueryOptions = {
propertyOptions: [{ propertyId: propId, value: { lessThan: propValue } }]
};

const overworld = world.getDimension('overworld');
const entities = overworld.getEntities(queryOption);
}
import { world, EntityQueryOptions } from '@minecraft/server';

// Having this command:

// execute as @e[has_property={propId=..propValue}]

// Equivalent scripting code would be:
function findEntitiesHavingPropertyLessThanOrEqualsTo(propId: string, propValue: number) {
const queryOption: EntityQueryOptions = {
propertyOptions: [{ propertyId: propId, value: { lessThanOrEquals: propValue } }]
};

const overworld = world.getDimension('overworld');
const entities = overworld.getEntities(queryOption);
}
import { world, EntityQueryOptions } from '@minecraft/server';

// Having this command:

// execute as @e[has_property={propId=!propValue}]

// Equivalent scripting code would be:
function findEntitiesHavingPropertyNotEqualsTo(propId: string, propValue: boolean | number | string) {
const queryOption: EntityQueryOptions = {
propertyOptions: [{ propertyId: propId, value: { notEquals: propValue } }]
};

const overworld = world.getDimension('overworld');
const entities = overworld.getEntities(queryOption);
}
import { world, EntityQueryOptions } from '@minecraft/server';

// Having this command:

// execute as @e[has_property={property=!propId}]

// Equivalent scripting code would be:
function findEntitiesNotHavingAProperty(propId: string) {
const queryOption: EntityQueryOptions = {
propertyOptions: [{ propertyId: propId, exclude: true }]
};

const overworld = world.getDimension('overworld');
const entities = overworld.getEntities(queryOption);
}
import { world, EntityQueryOptions } from '@minecraft/server';

// Having this command:

// execute as @e[has_property={propId=lowerBoundValue..upperBoundValue}]

// Equivalent scripting code would be:
function findEntitiesHavingPropertyWithinRange(propId: string, lowerBoundValue: number, upperBoundValue: number) {
const queryOption: EntityQueryOptions = {
propertyOptions: [{ propertyId: propId, value: { lowerBound: lowerBoundValue, upperBound: upperBoundValue } }]
};

const overworld = world.getDimension('overworld');
const entities = overworld.getEntities(queryOption);
}
import { Dimension } from '@minecraft/server';

// Having this command:

// execute as @e[type=armor_stand,name=myArmorStand,tag=dummyTag1,tag=!dummyTag2] run tellraw @a { "rawtext": [{"translate": "hello.world" }] }

// Equivalent scripting code would be:
function sendMessagesToPlayers(dimension: Dimension) {
const targetPlayers = dimension.getPlayers();
const originEntities = dimension.getEntities({
type: 'armor_stand',
name: 'myArmorStand',
tags: ['dummyTag1'],
excludeTags: ['dummyTag2'],
});

originEntities.forEach(entity => {
targetPlayers.forEach(player => {
player.sendMessage({ rawtext: [{ translate: 'hello.world' }] });
});
});
}
import { Dimension, world } from '@minecraft/server';

// Having these commands:

// scoreboard objectives add scoreObjective1 dummy
// scoreboard players set @e[type=armor_stand,name=myArmorStand] scoreObjective1 -1

// Equivalent scripting code would be:
function setScores(dimension: Dimension) {
const objective = world.scoreboard.addObjective('scoreObjective1', 'dummy');
dimension
.getEntities({
type: 'armor_stand',
name: 'myArmorStand',
})
.forEach(entity => {
if (entity.scoreboardIdentity !== undefined) {
objective.setScore(entity.scoreboardIdentity, -1);
}
});
}
import { Dimension } from '@minecraft/server';

// Having this command:

// execute as @e[type=armor_stand] run execute as @a[x=0,y=-60,z=0,c=4,r=15] run summon pig ~1 ~ ~

// Equivalent scripting code would be:
function spawnPigs(dimension: Dimension) {
const armorStandArray = dimension.getEntities({
type: 'armor_stand',
});
const playerArray = dimension.getPlayers({
location: { x: 0, y: -60, z: 0 },
closest: 4,
maxDistance: 15,
});
armorStandArray.forEach(entity => {
playerArray.forEach(player => {
dimension.spawnEntity('pig', {
x: player.location.x + 1,
y: player.location.y,
z: player.location.z,
});
});
});
}

Extends

Properties

closest?

optional closest: number

Remarks

Limits the number of entities to return, opting for the closest N entities as specified by this property. The location value must also be specified on the query options object.


excludeFamilies?

optional excludeFamilies: string[]

Remarks

Excludes entities that match one or more of the specified families.

Inherited from

EntityFilter . excludeFamilies


excludeGameModes?

optional excludeGameModes: GameMode[]

Remarks

Excludes entities if have a specific gamemode that matches the specified gamemode.

Inherited from

EntityFilter . excludeGameModes


excludeNames?

optional excludeNames: string[]

Remarks

Excludes entities that have a name that match one of the specified values.

Inherited from

EntityFilter . excludeNames


excludeTags?

optional excludeTags: string[]

Remarks

Excludes entities with a tag that matches one of the specified values.

Inherited from

EntityFilter . excludeTags


excludeTypes?

optional excludeTypes: string[]

Remarks

Excludes entities if they are one of the specified types.

Inherited from

EntityFilter . excludeTypes


families?

optional families: string[]

Remarks

If specified, includes entities that match all of the specified families.

Inherited from

EntityFilter . families


farthest?

optional farthest: number

Remarks

Limits the number of entities to return, opting for the farthest N entities as specified by this property. The location value must also be specified on the query options object.


gameMode?

optional gameMode: GameMode

Remarks

If specified, includes entities with a gamemode that matches the specified gamemode.

Inherited from

EntityFilter . gameMode


location?

optional location: Vector3

Remarks

Adds a seed location to the query that is used in conjunction with closest, farthest, limit, volume, and distance properties.


maxDistance?

optional maxDistance: number

Remarks

If specified, includes entities that are less than this distance away from the location specified in the location property.


maxHorizontalRotation?

optional maxHorizontalRotation: number

Remarks

If specified, will only include entities that have at most this horizontal rotation.

Inherited from

EntityFilter . maxHorizontalRotation


maxLevel?

optional maxLevel: number

Remarks

If defined, only players that have at most this level are returned.

Inherited from

EntityFilter . maxLevel


maxVerticalRotation?

optional maxVerticalRotation: number

Remarks

If specified, only entities that have at most this vertical rotation are returned.

Inherited from

EntityFilter . maxVerticalRotation


minDistance?

optional minDistance: number

Remarks

If specified, includes entities that are least this distance away from the location specified in the location property.


minHorizontalRotation?

optional minHorizontalRotation: number

Remarks

If specified, will only include entities that have at a minimum this horizontal rotation.

Inherited from

EntityFilter . minHorizontalRotation


minLevel?

optional minLevel: number

Remarks

If defined, only players that have at least this level are returned.

Inherited from

EntityFilter . minLevel


minVerticalRotation?

optional minVerticalRotation: number

Remarks

If specified, will only include entities that have at least this vertical rotation.

Inherited from

EntityFilter . minVerticalRotation


name?

optional name: string

Remarks

Includes entities with the specified name.

Inherited from

EntityFilter . name


propertyOptions?

Beta

optional propertyOptions: EntityQueryPropertyOptions[]

Inherited from

EntityFilter . propertyOptions


scoreOptions?

optional scoreOptions: EntityQueryScoreOptions[]

Remarks

Gets/sets a collection of EntityQueryScoreOptions objects with filters for specific scoreboard objectives.

Inherited from

EntityFilter . scoreOptions


tags?

optional tags: string[]

Remarks

Includes entities that match all of the specified tags.

Inherited from

EntityFilter . tags


type?

optional type: string

Remarks

If defined, entities that match this type are included.

Inherited from

EntityFilter . type


volume?

Beta

optional volume: Vector3

Remarks

In conjunction with location, specified a cuboid volume of entities to include.