Target selectors
Target selectors or selectors are a way to filter any minecraft entity from each other. Using selectors you have to start with an @ symbol.
This also helps reduce lag if selectors are used to narrow down entities. Selectors are composed of a base variable @e
and a set of arguments enclosed in square brackets [].
Arguments are optional when it comes to selectors.
Base variables
Base variables always start the selector like @e[type=player].
@p: Nearest player@r: Random Entity@a: All players@e: All entities@s: Current entity@n: Nearest entity
Arguments
Arguments add a layer to filter entities and are seperated by a comma ,. Selectors are also booleans which means you can invert their output with !.
type: Entity typetag: Entity internal tagname: Entity namelimit: Max count of entitysort: Sorting method (nearest, furthest, random, arbitrary)distance: Distance of int or range of an entity to the where the command runsadvancements: Players with certain advancementsgamemode: Players in a certain gamemodelevel: Experience level of an entitynbt: Entity with a specific or general NBT Componentpredicate: Filter based on a datapack file that has more argumentsscores: Entity with a scoreboardteam: Entity on a certain teamx: Entity on a certain x axisy: Entity on a certain y axisz: Entity on a certain z axisdx: Entity between x and x + dxdy: Entity between y and y + dydz: Entity between z and z + dzx_rotation: Entity's rotation on the x axisy_rotation: Entity's rotation on the y axisz_rotation: Entity's rotation on the z axis
Chaining arguments
Arguments can be chained together with a comma ,. Though chaining some things like multiple of the same type when they both cancel out each
other won't pass through as a valid command.
Example would be passing through multiple tag elements: @e[tag=good,tag=bob].
Example of passing conflicting arguments: @e[type=minecraft:zombie,type=minecraft:player]
Not arguments
Arguments can also be inverted like booleans! You can invert something that would output true as false. With arguments you can invert like by adding an exlamation point to the front of the argument.
Example of inverting an argument: @e[!tag=bob].
Advanced arguments
Sometimes arguments offer more options based on variables being passed. Some arguments don't take strict syntax and offer more emergent properties that allow the selector to specify more general/specific catagories.
Distance, level, and scores
When using ints in these catagories of arguments they offer the ability to choose a range of numbers. This is incredibly usefull for arguments like distance because it won't give an exact radius that an entity has to be in, but rather a distance range from that point.
Use of ranges in minecraft syntax looks like .. with numbers before or after or on either side.
An example of this would be @e[distance=..5].
This distance command is from range 0 up to and including 5.
Scores
Scores have an interesting list operation so you don't have to repeat yourself when looking for extra scoreboard values. They also have not comparisons to the value of a spesific scoreboard itself.
An example of multiple scoreboard comparison: @e[scores={Objective1=1,Objective2=19}].
An example of not comparison @e[scores={Objective1=!1}].
Having multiple scoreboards in a selector statement means that all scoreboards have to return a true before the whole scores argument runs true. With not comparisons you can also use it inside a range of values.
Type
There are forbidden selectors when using type like @p and @a because their type is already built in.
Types are also case sensitive, always following snake case statements like "Hello World" -> "hello_world"
Type can have multiple entities if and only if its followed by a ! symbol. This is because the type argument narrows down by entity, and an
entity cannot be two types of entities at once.
Example of an incorrect statement: @e[type=pig,type=zombie].
Example of a correct statement: @e[type=!pig,type=!zombie].
Type arguments can also have catagories, and to start a catagory you use #. Catagories exist all throughout minecraft, and it helps
generalize minecraft entities for this use case instead of having to type multiple not statements.
Example of catagory statement: @e[type=#minecraft_skeletons].
NBT
The NBT argument is an incredibly powerful selector. It selects based on internal entity data, such as inventory, custom names, or health.
The syntax of nbt always runs as a list like scores.
Example of starting nbt: @e[nbt={}].
NBT selectors are great for small/medium projects, but using predicates offers more versatality.
Exact names
Target selectors also allow for specific selections either by name or UUID. To start out a command like running a command to execute at
a player named bob we just run /execute at Bob run say hi which this command runs at bobs position.
UUID
With UUID (Universally Unique Identifier) running a command removed the whole check process, this makes commands that check over lots of entities faster if using UUID.
UUID looks like b7d2fce5-e292-49b4-b136-98c0f2b75063 which is represented as a 36 character string. This method will help cut down on lookup time for larger projects.
You just look at the entity when typing out the command, when the selector section pops up you get a UUID string of that entity.
Use cases
Executing commands at entities. Taking count of entities. Clearing all item entities.
Reference
Information:
https://learn.microsoft.com/en-us/minecraft/creator/reference/content/commandsreference/examples/commandtypes/type_selection?view=minecraft-bedrock-stable
https://minecraft.fandom.com/wiki/Target_selectors
https://minecraft.wiki/w/Target_selectors
https://maven.fabricmc.net/docs/yarn-1.19.4+build.1/net/minecraft/command/EntitySelector.html