Skip to content

TargetSelector 5.46.0 ​

final class · pocketmine\command\selector\TargetSelector · source

A Bedrock-style target selector (@p, @a, @r, @e, @n, @s, @initiator) with its arguments, parsed by parse().

Supported arguments: x, y, z (absolute or ~relative), r, rm, dx, dy, dz, c (negative = farthest first), type, name, tag, family, m (game mode), l, lm, rx, rxm, ry, rym. type, name, m, tag and family accept "!" negation; type, name and m may appear several times only when negated, tag and family may repeat. hasitem, scores, haspermission and has_property are recognised but not supported (a TargetSelectorException explains that).

Semantics follow Bedrock: @p is the nearest living player (spectators excluded), @a every online player (dead ones too), @r a random living player (any entity when a non-player type is given), @e every living entity, @n the nearest living entity of any type (like @e[c=1]), @s the executing entity. type= cannot be used with @a and @p. Without position arguments @a, @r and @e look in every world; any position argument, c, @p or @n restricts the selection to the origin's world. Console senders use the default world spawn as origin.

Errors use the vanilla translation keys (commands.generic.*) when Bedrock has one, so clients show them in their own language.

Use CommandTargets to resolve command arguments that may be a selector or a player name.

Constants ​

ALL_ENTITIES ​

php
public const ALL_ENTITIES = "e"

ALL_PLAYERS ​

php
public const ALL_PLAYERS = "a"

ALL_TYPES ​

php
public const ALL_TYPES = [self::ALL_PLAYERS, self::ALL_ENTITIES, self::NEAREST_ENTITY, self::NEAREST_PLAYER, self::RANDOM_PLAYER, self::SELF, self::INITIATOR]

Selector variables in the order the Bedrock client suggests them.

INITIATOR ​

php
public const INITIATOR = "initiator"

NEAREST_ENTITY ​

php
public const NEAREST_ENTITY = "n"

Nearest entity of any type (Bedrock 1.21+).

NEAREST_PLAYER ​

php
public const NEAREST_PLAYER = "p"

PLAYER_TYPE ​

php
public const PLAYER_TYPE = "minecraft:player"

RANDOM_PLAYER ​

php
public const RANDOM_PLAYER = "r"

SELF ​

php
public const SELF = "s"

Methods ​

getEntityName() ​

php
public static function getEntityName(\pocketmine\entity\Entity $entity) : string

Name of an entity as shown in command feedback: the player name, the name tag, or the entity type.

getText() ​

php
public function getText() : string

The selector text as it was given.

getType() ​

php
public function getType() : string

Selector type: one of the class constants (p, a, r, e, n, s, initiator).

isInFamily() ​

php
public static function isInFamily(\pocketmine\entity\Entity $entity, string $family) : bool

Bedrock entity families supported by family=: player, mob (any living entity but players), monster, undead, animal, illager, inanimate (non-living), and the entity's own type name (family=zombie matches zombies).

isPlayerOnly() ​

php
public function isPlayerOnly() : bool

Returns whether this selector can only select players (@p, @a, @s, @initiator, @r without a non-player type,

isSelector() ​

php
public static function isSelector(string $text) : bool

Returns whether the string looks like a target selector (starts with "@" followed by a selector type), so that commands can tell selectors from player names.

isSelectorArgument() ​

php
public static function isSelectorArgument(string $text) : bool

Returns whether a command argument must be treated as a target selector: anything starting with "@" (player names cannot contain "@"), so that a mistyped selector like "@x" gets a syntax error instead of "player not found".

joinNames() ​

php
public static function joinNames(array $entities) : string

Names of the given entities joined like Bedrock command feedback ("Steve, Alex, Cow").

normalizeEntityType() ​

php
public static function normalizeEntityType(string $type) : ?string

Returns the namespaced entity type id ("cow" -> "minecraft:cow"), or null for an unknown vanilla type. Ids with another namespace (custom entities) are accepted as they are.

parse() ​

php
public static function parse(string $text) : self

select() ​

php
public function select(\pocketmine\command\selector\SelectorContext $context) : array

selectEntities() ​

php
public function selectEntities(\pocketmine\command\CommandSender $sender) : array

Selects the matching entities as seen by the given command sender.

selectPlayers() ​

php
public function selectPlayers(\pocketmine\command\CommandSender $sender) : array

Selects the matching players as seen by the given command sender (non-player matches are dropped).

VanillaMine is a fork of PocketMine-MP, maintained by Luxury Nodes. Not affiliated with the PMMP team or Mojang.