TargetSelector 5.46.0
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
public const ALL_ENTITIES = "e"ALL_PLAYERS
public const ALL_PLAYERS = "a"ALL_TYPES
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
public const INITIATOR = "initiator"NEAREST_ENTITY
public const NEAREST_ENTITY = "n"Nearest entity of any type (Bedrock 1.21+).
NEAREST_PLAYER
public const NEAREST_PLAYER = "p"PLAYER_TYPE
public const PLAYER_TYPE = "minecraft:player"RANDOM_PLAYER
public const RANDOM_PLAYER = "r"SELF
public const SELF = "s"Methods
getEntityName()
public static function getEntityName(\pocketmine\entity\Entity $entity) : stringName of an entity as shown in command feedback: the player name, the name tag, or the entity type.
getText()
public function getText() : stringThe selector text as it was given.
getType()
public function getType() : stringSelector type: one of the class constants (p, a, r, e, n, s, initiator).
isInFamily()
public static function isInFamily(\pocketmine\entity\Entity $entity, string $family) : boolBedrock 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()
public function isPlayerOnly() : boolReturns whether this selector can only select players (@p, @a, @s, @initiator, @r without a non-player type,
isSelector()
public static function isSelector(string $text) : boolReturns 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()
public static function isSelectorArgument(string $text) : boolReturns 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()
public static function joinNames(array $entities) : stringNames of the given entities joined like Bedrock command feedback ("Steve, Alex, Cow").
normalizeEntityType()
public static function normalizeEntityType(string $type) : ?stringReturns 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()
public static function parse(string $text) : selfselect()
public function select(\pocketmine\command\selector\SelectorContext $context) : arrayselectEntities()
public function selectEntities(\pocketmine\command\CommandSender $sender) : arraySelects the matching entities as seen by the given command sender.
selectPlayers()
public function selectPlayers(\pocketmine\command\CommandSender $sender) : arraySelects the matching players as seen by the given command sender (non-player matches are dropped).
