You are viewing the Target Selectors tutorial
Tutorial Selection
Tutorial Contents
What are Target Selectors?
A "target selector" is the intended target for the command you're trying to run. Commands that require a target selector are asking for the entity you intend to run the command as, from or on. This tutorial will discuss everything from the different target selectors to each individual target selector filter available.
Note: There is a lot of information throughout this tutorial, if you'd like to search through it quicker, there are some methods that you can use. If you're using a computer, you can search for information using [Ctrl] + [F]. For mobile devices, some browsers have a "Find in page" or "Find on page" option (usually located in the top right).
If at any point during this tutorial, you see information contained in less than and greater than symbols, it is a space in which you must enter that information. For example, if you see <username>, you'd replace that with the username in the command. The symbols themselves should also be replaced and not included in your command.
Basic Target Selection
The most basic form of target selection is using a player's username/gamertag instead of a selector. When inputting a username as opposed to a selector, you do not need to use the @ symbol. If the username you are trying to input as the target contains spaces, you'll need to use quotation marks. Some examples:
You may whisper to a specific user by typing any of the below commands
/w <username> <message>
/msg <username> <message>
/tell <username> <message>
Aside from using a username/gamertag as the target, you may use what is called a "target selector". Information regarding these are below.
Selecting the nearest player (@p)
The "@p" target selector will select the nearest player entity based on where or who the command is executed from. If the command is executed from a player, the closest player will always be the player who the command is executed from. If the command is executed from a location, the closest player to that location will become the target of the command. In Bedrock Edition, the "@p" target selector will only select players who are alive. Here is an example use of this selector:
This command will whisper your given message to the nearest player to you
/w @p <message>
Note: If there are multiple users detected as the "nearest player" (multiple players are at the exact same distance from the command executor or command's execution point), the player who joined most recently will be selected.
Selecting a random player (@r)
The "@r" target selector will select a random player entity. In Bedrock Edition, the "@r" target selector can be used for non-player entities, if they're specified using a target selector filter such as type or family. Additionally, in Bedrock Edition, the "@r" target selector will only select players who are alive. Here are some example uses of this selector:
This command will whisper your given message to a randomly chosen player
/w @r <message>
This command will kill a randomly chosen entity based on the type you provide
/kill @r[type=<entity>]
Selecting all players (@a)
The "@a" target selector will select ALL player entities. This target selector will select players regardless of whether or not they are dead. This selector will target everyone unless target selector filters are used. Here are some example uses of this selector:
This command will whisper your given message to every player entity
/w @a <message>
This command will teleport every player entity to the specified coordinates
/tp @a <x> <y> <z>
Selecting all entities (@e)
The "@e" target selector will select ALL entities (this includes players) in any rendered chunks. This selector will target every entity unless target selector filters are used. Here are some example uses of this selector:
This command will kill every existing entity within any chunks that are rendered
/kill @e
This command will teleport every entity within any chunks that are rendered to the specified coordinates
/tp @e <x> <y> <z>
Selecting the command executor (@s)
The "@s" target selector will select the command executor. The command executor is the entity or command block that the command is run as but not from. If a command uses the "@s" target selector in a command block and the command doesn't specifically run as an entity, the "command executor" is the command block. Here is an example use of this selector:
Here is a command to give the command executor a single item
/give @s <item>
If a player runs this command, the command will give the specified item to that player
If a command block runs this command, the command will fail as command blocks can't receive items
Selecting the NPC command executor (@initiator)
The "@initiator" target selector is used only inside of an NPC GUI. This target selector can not be used by commands run as the player or a command block. The "@initiator" target selector will target the player who presses a button inside of an NPC GUI. Here are some example uses of this selector:
If the following command is assigned to a button inside of an NPC's GUI, the player who presses the button will receive the specified item
/give @initiator <item>
If the following command is assigned to a button inside of an NPC's GUI, the player who pressed the button will be teleported to the specified coordinates
/tp @initiator <x> <y> <z>
Positional Target Selector Filters
Positional target selector filters can be used to narrow down target selection based on location. The types of positional target selector filters include coordinates (x, y and z), distance (otherwise referred to as radius) and volume (otherwise referred to as difference values, distance values or d-values). Positional target selector filters can not be duplicated, they may only be entered once per target selector.
Coordinates (x, y, z)
A "coordinate" positional target selector filter will define a starting position in the target selector. To define a starting position using "coordinate" position target selector filters, you would enter an x-axis, y-axis or z-axis value. Please note that coordinate filters are double variables. Coordinate values can be found in the top left corner of your screen once enabled through commands (using /gamerule) or in the game settings menu. Coordinates are shown in the order of x, y and z and start at an entity's feet, not head. Distance and volume positional target selector filters as well as the count target selector filter will use the coordinate filters as a start position. You would type the coordinate filters as x=<value>, y=<value> and z=<value>. Tilde notation (~) may also be used with coordinate filters but caret notation (^) may not. Coordinate target selector filters may not be duplicated in the same target selector.
Note: In Bedrock Edition, if a user inputs an integer value, the value will automatically be centre-corrected; this means that a value such as x=0 would become x=0.5.
Examples
This teleport command defines coordinate positional filters using 0, 0, 0 as the values and a distance positional filter using a value of 5. The command will teleport all players within a 5-block spherical radius of 0, 0, 0 to the entity or command block that runs the command.
/tp @a[x=0,y=0,z=0,r=5] @s
This effect command defines coordinate positional filters using 27, 5, 19 as the values and a distance positional filter using a value of 3. The coordinate positional filters have been centre-corrected. The command will apply the speed 2 effect to all players within a 3-block spherical radius of 27.5, 5.5, 19.5 for 5 seconds when an entity or command block runs the command.
/effect @a[x=27,y=5,z=19,r=3] speed 5 1
This kill command defines coordinate positional filters using -12, 7, -25 as the values and volume positional filters all using values of 5. The coordinate positional filters have been adapted so they are not centre-corrected. The command will kill all players within a 5 by 5 by 5 cubic volume with the starting corner being at coordinates -12.0, 7.0, -25.0 and following each axis positively when an entity or command block runs the command.
/kill @a[x=-12.0,y=7.0,z=-25.0,dx=4,dy=4,dz=4]
Distance (r, rm)
A "distance" positional target selector filter will define a radius utilising a starting position in the target selector. To define a radius using "distance" positional target selector filters, you would enter an unsigned integer value to set either a radius maximum (r), radius minimum (rm) or both. Both the radius maximum (r) and radius minimum (rm) target selector filters will default to using the command executor's position or the command execution point as the starting position. The starting position can be changed using coordinate positional target selector filters. You would type the distance filters as r=<value> and rm=<value>. Neither of these distance filters can be duplicated in the same target selector. Setting a radius maximum (r) will target all of the specified entities (dependant on other target selector filters) within a spherical radius of the given unsigned integer value, centred from the target selector's starting position. Setting a radius minimum (rm) will exclude all of the specified entities (again, dependant on other target selector filters) within a spherical radius of the given unsigned integer value, which is also centred from the target selector's starting position.
Examples
This teleport command defines distance positional filters using the command's default starting position as the centre (this would be the entity or command block running the command in this specific example due to no defined coordinate positional filters). The command will teleport all players within a 5-block spherical radius centred on the starting position to the command's executor (the entity) or execution point (the command block).
/tp @a[r=5] @s
This effect command defines coordinate positional filters using 5, 7, 11 as the values (thus, setting a starting position) and distance positional filters using values of 1 (rm, radius minimum) and 3 (r, radius maximum). The coordinate positional filters have been centre-corrected. The command will apply the speed 2 effect to all players who are within a 3-block spherical radius centred on 5.5, 7.5, 11.5 for 5 seconds when an entity or command block runs the command; however, they must also not be within a 1-block spherical radius centred on the same position. This means all players within the distance created between both spherical radii will receive the effect.
/effect @a[x=5,y=7,z=11,rm=1,r=3] speed 5 1
This kill command defines coordinate positional filters using -15, -9, 21 as the values and distance positional filters using values of 7 (rm, radius minimum) and 7 (r, radius maximum). The coordinate positional filters have been adapted so they are not centre-corrected. The command will kill all players exactly 7 blocks away in a spherical radius centred on -15.0, -9.0, 21.0 when an entity or command block runs the command.
/kill @a[x=-15.0,y=-9.0,z=21.0,rm=7,r=7]
Image Example
Volume (dx, dy, dz)
A "volume" positional target selector filter (commonly referred to as "difference values", "distance values" or "d-values") will define an area by volume using the starting position in the target selector. To define a volume utilising a "volume" positional target selector filter, you would enter double values (decimals) or integer values (whole numbers). Just like when calculating volume with anything else, Minecraft will calculate the volumetric area using the formula width x height x length. You may use the "volume" positional target selector filters to create a three-dimensional space in which you can detect entities. The volumetric area will be created based on the distance you specify with your values and the starting position given in the target selector. The starting position can be modified using coordinate positional target selector filters. You would type the volume filters as dx=<value>, dy=<value> and dz=<value>. None of these volume filters can be duplicated in the same target selector. It should be noted that volumetric values start at 0, resulting in the following formula being used to calculate the value total length of area on axis - 1. If coordinate positional target selector filters are not specified, the command will utilise the default starting position, this will be the command executor's position or the command execution point unless specified otherwise. Volumetric values will accept any type of double or integer value, this includes positive and negative values. Any volumetric values left out will default to a value of 0.
Note: In Bedrock Edition, volumetric values require the entity's feet to be inside of the area to select the target.
Common Misinterpretation: Volumetric values are not a second pair of coordinates, they're distance values.
Examples
This teleport command defines volume positional filters using the command's default starting position (this would be the entity or command block running the command in this specific example due to no defined coordinate positional filters). The command will teleport all players within a 5 (dx) by 5 (dy) by 5 (dz) volumetric area (125 blocks) starting from the starting position and following each axis positively for each distance.
/tp @a[dx=4,dy=4,dz=4] @s
This effect command defines coordinate positional filters using 5, 0, 3 as the values (thus, setting a starting position) and volume positional filters using values of -2 (dx), 0 (dy) and -2 (dz). The coordinate positional filters have been centre-corrected. The command will apply the fire resistance 2 effect for 20 seconds to all players who are within a 3 (dx) by 1 (dy) by 3 (dz) volumetric area (9 blocks) starting from 5.5, 0.5, 3.5 and following the axis positively for volume values that are positive but negatively for volume values that are negative.
/effect @a[x=5,y=0,z=3,dx=-2,dy=0,dz=-2] fire_resistance 20 1
This kill command defines coordinate positional filters using 25, 0, -253 as the values (thus, setting a starting position) and volume positional filters using values of 0 (dx), 0 (dy) and 0 (dz). The coordinate positional filters have been adapted so they are not centre-corrected. The command will kill all player entities whose feet collide with the block at 25, 0, -253 exactly. This is due to the coordinate positional filters being centre-corrected and the volume positional filters creating a volume of 1 block.
/kill @a[x=25,y=0,z=-253,dx=0,dy=0,dz=0]
Image Examples
Score & Tag Target Selector Filters
Score target selector filters can be used to narrow down target selection based on an entity's score in a scoreboard objective. Tag target selector filters can be used to narrow down target selection based on the tags that an entity has. Using the scores and tag filters is the way to do this. Score arguments can't be duplicated, which means they can't be entered twice in the same target selector. You may test for multiple scores in the same filter, however. Tag target selector filters can be duplicated, allowing the choice of targeting entities with multiple tags.
Scores
A "score" target selector filter will narrow down the target selection based on score values that an entity has in one or more scoreboard objectives. To define the score target selector filter, you would need to input signed integer values (whole numbers), this is because scoreboard objectives only support integers and not double values (decimals). Utilising a score target selector filter will allow you to select targets that meet a specific score, targets that have a score greater than or less than the specified score or targets that have a score between a set range. The score selector filter can't be duplicated inside of the same target selector, though you may check multiple score objectives at once in the same filter. You would type the score target selector filter with scores={<objective>=<value>}. You can edit this to check for multiple scores by typing scores={<objective>=<value>,<objective>=<value>}.
Note: In Bedrock Edition, you may test if scores do not equal the set value parameters. To do this, you would need to input an exclamation mark. You'd do this by typing scores=!{<objective>=<value>}.
Common Mistake: Scoreboard objective names are case-sensitive, meaning a scoreboard objective called "money" is unique to that of one called "Money".
Examples using specific score values
This teleport command defines a score target selector filter with a value of 5. The command will teleport all players who have a score value of 5 in the scoreboard objective "money" to the command executor or command's execution point when an entity or command block runs the command.
/tp @a[scores={money=5}] @s
This teleport command defines a score target selector filter with a value of -20. The command will teleport all players who have a score value of -20 in the scoreboard objective "Money" to the command executor or command's execution point when an entity or command block runs the command.
/tp @a[scores={Money=-20}] @s
This teleport command defines two score target selector filters with values of 5 and 1. The command will teleport all players who have a score value of 5 in the scoreboard objective "money" but also do not have a score value of 1 in the scoreboard objective "Level" to the command executor or command's execution point when an entity or command block runs the command.
/tp @a[scores={money=5,Level=!1}] @s
Examples using greater than score values
This kill command defines a score target selector filter with a value of 12 or greater. The command will kill all players who have a score value of 12 or higher in the scoreboard objective "points" when an entity or command block runs the command.
/kill @a[scores={points=12..}]
This kill command defines a score target selector filter with a value of -10 or greater. The command will kill all players who have a score value of -10 or higher in the scoreboard objective "Points" when an entity or command block runs the command.
/kill @a[scores={Points=-10..}]
This kill command defines two score target selector filters with values of 12 or greater and 3 or greater. The command will kill all players who have a score value of 12 or higher in the scoreboard objective "points" but also do not have a score value of 3 or higher in the scoreboard objective "Level" when an entity or command block runs the command.
/kill @a[scores={points=12..,Level=!3..}]
Examples using less than score values
This kill command defines a score target selector filter with a value of 15 or less. The command will kill all players who have a score value of 15 or lower in the scoreboard objective "points" when an entity or command block runs the command.
/kill @a[scores={points=..15}]
This kill command defines a score target selector filter with a value of -30 or less. The command will kill all players who have a score value of -30 or lower in the scoreboard objective "Points" when an entity or command block runs the command.
/kill @a[scores={Points=..-30}]
This kill command defines two score target selector filters with values of 5 or less and 10 or less. The command will kill all players who have a score value of 5 or lower in the scoreboard objective "points" but also do not have a score value of 10 or lower in the scoreboard objective "Level" when an entity or command block runs the command.
/kill @a[scores={points=..5,Level=!..10}]
Examples using score values between a range
This scoreboard command defines a score target selector filter for any value between 80 and 99 (includes 80 and 99 themselves). The command will change the "xp_target" scores for all players who have a score value between 80 and 99 in the scoreboard objective "current_xp" when an entity or command block runs the command. The new "xp_target" value will be 120 for those players.
/scoreboard players set @a[scores={current_xp=80..99}] xp_target 120
This scoreboard command defines a score target selector filter for any value between -30 and -1 (includes -30 and -1 themselves). The command will change the "Points" scores for all players who have a score value between -30 and -1 in the scoreboard objective "Points" when an entity or command block runs the command. The new "Points" value will be 0 for those players.
/scoreboard players set @a[scores={Points=-30..-1}] Points 0
This effect command defines two score target selector filters with ranges 50 to 100 and 1 to 25 (includes 50, 100, 1 and 25 themselves). The command will apply the wither 1 effect to all players who have a score value between 50 and 100 in the scoreboard objective "corruption" but also do not have a score value between 1 and 25 in the scoreboard objective "Level" when an entity or command block runs the command. The effect will last for 10 seconds and have a strength of 1 (wither I).
/effect @a[scores={corruption=50..100,Level=!1..25}] wither 10 0 true
Tag
A "tag" target selector filter will narrow down the target selection based on the tags than an entity has. Tags should be a single string of characters with no spaces. If you'd like a tag to have multiple words in it, you can use the snake case naming convention (e.g. two_words or this_tag_has_lots_of_words). To enter a tag target selector filter you would write tag=<string>. You may use exclusion to check if an entity does not have a tag, to do this you would add an exclamation mark, like so: tag=!<string>.
Note: You may test if an entity has multiple tags. To do this, you would need to duplicate the tag target selector filter in the target selector. You'd do this by typing tag=<string>,tag=<string>. The entity needs to have both tags in cases like this, not one or the other. You may also use exclusion when checking for multiple tags.
Common Mistake: Tag names are case-sensitive, meaning a tag called "zombie" is unique to that of one called "Zombie".
Examples
This teleport command defines a tag target selector filter called "blue_team". The command will teleport all players who have the tag "blue_team" to the command executor's position.
/tp @a[tag=blue_team] @s
This effect command defines a tag target selector filter called "safe". The command will apply the poison 3 effect to all players who do not have the tag "safe". The effect will last for 60 seconds and have a strength of 3 (poison III).
/effect @a[tag=!safe] poison 60 2
This kill command defines two tag target selector filters called "infected_zone" and "immune". The command will kill all players who have the "infected_zone" tag but also do not have the "immune" tag. Players with both the "infected_zone" and "immune" tags will be safe, the target must meet ALL of the specified tag requirements.
/kill @a[tag=infected_zone,tag=!immune]
Entity Target Selector Filters
Name target selector filters can be used to narrow down target selection based on an entity's name. Type target selection filters can be used to narrow down target selection based on a type of entity. Family target selection filters can be used to narrow down target selection based on the family an entity belongs to. You would use name, type and family filters to do this. An entity can not have more than one name or type and therefore these filters may not be entered twice in the same target selector. The family target selector filter may be duplicated to check if an entity does or does not belong in multiple families.
Anything above has been updated. Anything below this message has yet to be updated. This message is here to signify the progress made on the tutorial pages refresh project taking place and will be removed after the tutorial has been completely updated. If you'd like to help, please leave feedback via the xSnipez Games Discord server.
Name
This target selector argument filters target selection based on entity names. This argument only allows duplication when testing for inequality. This means you may only duplicate the argument when you are excluding name arguments using the !. Any duplicate arguments testing for equality will cause the target selection to fail. To enter this target selector argument you would write [name=<string>] (you can read more about strings here). To write an excluded name argument, you'd write [name=!<string>]. Names that contain spaces in them must be written inside quotation marks. "Name" arguments are not case-sensitive (meaning "player" is the same as "Player").
Example uses of the name argument
Teleport all players with the name "player" to yourself:
/tp @a[name=player] @s
Apply the Speed III effect to all players for 25 seconds who are not named "player":
/effect @a[name=!player] speed 25 2
Kill all entities that are named "delete me":
/kill @e[name="delete me"]
Type
This target selector argument filters target selection based on entity types. This argument only allows duplication when testing for inequality. This means you may only duplicate the argument when you are excluding type arguments using the !. Any duplicate arguments testing for equality will cause the target selection to fail. To enter this target selector argument you would write [type=<string>] (you can read more about strings here). To write an excluded "type" argument, you'd write [type=!<string>]. Below is a full list of every "type" argument as of Minecraft Bedrock version 1.20.41:
List of every type argument
allay, area_effect_cloud, armor_stand, arrow, axolotl, bat, bee, blaze, boat, camel, cat, cave_spider, chest_boat, chicken, cod, command_block_minecart, cow, creeper, dolphin, donkey, dragon_fireball, drowned, egg, elder_guardian, elder_guardian_ghost, ender_crystal, ender_dragon, ender_pearl, enderman, endermite, evocation_fang, evocation_illager, falling_block, fireball, fireworks_rocket, fishing_hook, fox, frog, ghast, glow_squid, goat, guardian, hoglin, hopper_minecart, horse, husk, iron_golem, item, leash_knot, lightning_bolt, lingering_potion, llama, llama_spit, magma_cube, minecart, npc, ocelot, painting, panda, parrot, phantom, pig, piglin, piglin_brute, pillager, player, polar_bear, pufferfish, rabbit, ravager, salmon, sheep, shulker, shulker_bullet, silverfish, skeleton, skeleton_horse, slime, small_fireball, sniffer, snow_golem, snowball, spider, splash_potion, squid, stray, strider, tadpole, thrown_trident, tnt, tnt_minecart, trader_llama, tropicalfish, turtle, vex, villager, villager_v2, vindicator, wandering_trader, warden, witch, wither, wither_skeleton, wither_skull, wither_skull_dangerous, wolf, xp_bottle, xp_orb, zoglin, zombie, zombie_horse, zombie_pigman, zombie_villager, zombie_villager_v2
Example uses of the type argument
Teleport all player types to coordinates 0, 0, 0:
/tp @a[type=player] 0 0 0
Apply the Jump Boost II effect to all zombie types for 15 seconds:
/effect @e[type=zombie] jump_boost 15 1
Kill all entities that are not armour stands and not skeletons:
/kill @e[type=!armor_stand,type=!skeleton]
Family
This target selector argument filters target selection based on an entity's family. This argument allows duplication. If the argument is duplicated, all arguments must be met for the target selection to be met. To enter this target selector argument you would write [family=<string>] (you can read more about strings here). You may exclude family arguments by using the !. You'd write an excluded family argument as [family=!<string>]. You may see a full list of families here.
Example uses of the family argument
Teleport all entities with the family "arthropods" to 0, 0, 0:
/tp @e[family=arthropod] 0 0 0
Apply the Instant Health I effect to all non-hostile mobs for 1 second:
/effect @e[family=mob,family=!monster] instant_health 1 0
Kill all hostile entities except the undead:
/kill @e[family=monster,family=!undead]
Entity Data Arguments
A vertical or horizontal target selector argument will narrow down the target selection using an entity's rotation following the axis. An item target selector argument will narrow down the target selection using items an entity has. This can be done by using vertical rotation, horizontal rotation and item arguments. These arguments are explained below. None of these target selector arguments can be duplicated, which means they can't be entered twice in the same target selection.
This section is very technical. Think it can be improved? Please leave a suggestion on the xSnipez Games Discord Server.
Vertical Rotation (pitch) (rx, rxm)
These target selector arguments set a maximum vertical rotation (rx) and minimum vertical rotation (rxm) for the target selector. To enter these target selector arguments, you would type them as rx=<value> and rxm=<value>. These values have a maximum of 90 (looking directly down) and a minimum of -90 (looking directly up). Vertical rotation in Minecraft is based on how high the player is looking on the y-axis. You can use maximum vertical rotation (rx) and minimum vertical rotation (rxm) to select targets based on where they are looking on the y-axis. These arguments can not be inverted using !.
Example uses of the rx and rxm arguments
Teleport all players looking directly at the horizon to 0, 0, 0:
/tp @a[rx=0,rxm=0] 0 0 0
Apply the Instant Damage II effect to all players looking between 30° and 60° for 1 second:
/effect @a[rx=60,rxm=30] instant_damage 1 1
Kill all entities looking at or above the horizon:
/kill @e[rx=0]
Horizontal Rotation (yaw) (ry, rym)
These target selector arguments set a maximum horizontal rotation (ry) and minimum horizontal rotation (rym) for the target selector. To enter these target selector arguments, you would type them as ry=<value> and rym=<value>. These values have a maximum of 180 (north) and a minimum of 0 (south). This argument is a little confusing, so I'll give a few directions and their rotation values: North (N) (180 or -180), North East (NE) (-135), East (E) (-90), South East (SE) (-45), South (S) (0), South West (SW) (45), West (W) (90) and North West (NW) (135). Horizontal rotation in Minecraft is based on which direction the player is looking on the x-axis. You can use maximum horizontal rotation (ry) and minimum horizontal rotation (rym) to select targets based on where they are looking on the x-axis. These arguments can not be inverted using !.
Example uses of the ry and rym arguments
Teleport all players facing south to 0, 0, 0:
/tp @a[ry=0,rym=0] 0 0 0
Apply the Instant Damage II effect to all players in the 90° angle between east and south for 1 second:
/effect @a[ry=0,rym=-90] instant_damage 1 1
Kill all entities that are not facing east in the 180° angle between south and north:
/kill @e[ry=180,rym=0]
Item (hasitem)
This target selector argument filters target selection based on items an entity has. This argument can not be duplicated, but you may specify multiple items in the same argument. To enter this argument with one item, you would write [hasitem={<argument>=<value>,<argument>=<value>,...}]. To enter this argument with multiple items, you would write [hasitem=[{<argument>=<value>},{<argument>=<value>}]]. The hasitem target selector argument has other arguments, or as I like to call them, "sub-arguments". These "sub-arguments" are data, item, location, quantity and slot.
The item sub-argument specifies the item in which you are trying to search for, you must enter an item ID. You would write the item argument by typing item=<item_id>.
The data sub-argument specifies the item data of the item you are trying to search for. The data argument must be a 32-bit integer value. The data argument will default to 0 if it is not specified. It should be noted that in the current version of Minecraft (1.20.41), the data argument does not work for blocks, the value is always deemed invalid and defaults back to 0. You would write the data argument by typing data=<value>.
The location sub-argument specifies the location of the item on the entity. The slot sub-argument specifies the slot in the location specified. These arguments should be written together. Writing the slot without the location argument will deem it invalid. You would write the location argument by typing location=<location>. You would write the slot argument by typing slot=<value>. See a list of valid location sub-arguments for this target selector argument below. The slot sub-argument needs to be an integer value. The slot integer value can be tested for exact matches, less than matches, greater than matches and matches between two values (see syntax below).
Finally, the quantity sub-argument specifies the amount of the specified item they must have. You can write the quantity argument by typing quantity=<value>. The quantity sub-argument needs to be an integer value. The quantity integer value can be tested for exact matches, less than matches, greater than matches and matches between two values (see syntax below). The quantity argument can also be inverted using ! to test if an entity doesn't have the amount of the item specified.
Valid location sub-arguments for the hasitem argument
slot.armor, slot.armor.chest, slot.armor.feet, slot.armor.head, slot.armor.legs, slot.chest, slot.enderchest, slot.equippable, slot.hotbar, slot.inventory, slot.saddle, slot.weapon.mainhand, slot.weapon.offhand
Syntax of the quantity sub-argument for the hasitem argument
You can test for exact matches by typing quantity=<value>
You can test for matches that are equal to or greater than by typing quantity=<value>..
You can test for matches that are equal to or less than by typing quantity=..<value>
You can test for matches between a range by typing quantity=<value>..<value>
Syntax of the slot sub-argument for the hasitem argument
You can test for exact matches by typing slot=<value>
You can test for matches that are equal to or greater than by typing slot=<value>..
You can test for matches that are equal to or less than by typing slot=..<value>
You can test for matches between a range by typing slot=<value>..<value>
Example uses of the hasitem argument
Teleport all players who have a diamond to 0, 0, 0:
/tp @a[hasitem={item=diamond}] 0 0 0
Teleport all players who have exactly 12 diamonds to 12, 0, -29:
/tp @a[hasitem={item=diamond,quantity=12}] 12 0 -29
Teleport all players who have exactly 32 diamonds or higher to 234, 0, -3:
/tp @a[hasitem={item=diamond,quantity=32..}] 234 0 -3
Teleport all players who have exactly 16 diamonds or less to 34, 0, -13:
/tp @a[hasitem={item=diamond,quantity=..16}] 34 0 -13
Teleport all players who have between 16 and 32 diamonds to 96, 0, 1:
/tp @a[hasitem={item=diamond,quantity=16..32}] 96 0 1
Apply the Haste II effect to all players that are holding a diamond pickaxe in their main hand for 5 seconds:
/effect @a[hasitem={item=diamond_pickaxe,location=slot.weapon.mainhand] haste 5 1
Kill all entities that don't have exactly 64 iron ingots:
/kill @e[hasitem={item=iron_ingot,quantity=!64}]
Kill all entities that have exactly 32 gold ingots or higher in the 5th slot of their hotbar:
/kill @e[hasitem={item=gold_ingot,quantity=32,location=slot.hotbar,slot=4}]
Kill all entities that have between 27 and 49 creeper skulls in their inventory between slots 10 and 18:
/kill @e[hasitem={item=skull,data=4,quantity=27..49,location=slot.inventory,slot=9..18}]
Kill all entities that have between 11 and 52 creeper skulls in their inventory between slots 1 and 9 and don't have exactly 34 or higher wither skeleton skulls in their hotbar between slots 2 and 8:
/kill @e[hasitem=[{item=skull,data=4,quantity=11..52,location=slot.inventory,slot=0..8},{item=skull,data=1,quantity=!34..,location=slot.hotbar,slot=1..7}]
Player Data Arguments
An experience level target selector argument will narrow down the target selection using a player's experience level. A game mode target selector argument will narrow down the target selection using a player's current game mode. A permission target selector argument will narrow down the target selection using a player's current permissions. This can be done by using experience level, gamemode and permission arguments. These arguments are explained below. Experience-level and permission arguments can not be duplicated. Only arguments testing for inequality in gamemode arguments can be duplicated, gamemode arguments testing for equality can not.
Experience Level (l, lm)
These target selector argument filters target selection based on a player's experience levels. This argument can not be duplicated, meaning it can only be used once in any given target selection. The arguments set a level maximum (l) and level minimum (lm) to search for. To enter these target selector arguments, you would type them as l=<value> and lm=<value>. It is important to note that only integer values are accepted. It should be noted that these arguments are only specific to player entities, and will naturally filter out all non-player types.
Example uses of the l and lm arguments
Teleport all players who have exactly 5 experience levels to 0, 0, 0:
/tp @a[l=5,lm=5] 0 0 0
Apply the Regeneration IV effect to all players who have between 0 and 10 experience levels for 30 seconds:
/effect @a[l=10,lm=0] regeneration 30 3
Kill all entities that have exactly 30 experience levels:
/kill @e[l=30,lm=30]
Game Mode (m)
This target selector argument filters target selection based on a player's current gamemode. This argument can only be duplicated if testing for inequality. This means you may only duplicate the argument when you are excluding m arguments using the !. Any duplicate arguments testing for equality will cause the target selection to fail. The argument checks for the player's current gamemode. To enter this target selector argument, you would type it as m=<value> or m=<string>. It is important to note that only integer and string values are accepted. There are only four gamemode types, these are survival (survival, s or 0 are accepted), creative (creative, c or 1 are accepted), adventure (adventure, a or 2 are accepted) and spectator (only spectator is accepted).
Example uses of the m argument
Teleport all players in creative to 0, 0, 0:
/tp @a[m=creative] 0 0 0
Apply the Regeneration III effect to all players who are in the adventure gamemode for 20 seconds:
/effect @a[m=a] regeneration 20 2
Kill all players in the survival gamemode:
/kill @e[m=0]
Permission (haspermission)
This target selector argument filters target selection based on a player's permissions. This argument can not be duplicated, meaning it can only be used once in any given target selection. The argument checks for the player's current camera and movement permissions. To enter this target selector argument, you would type it as [haspermission={<permission>=<state>}]. A state can be entered as enabled or disabled. A permission argument can only be camera or movement.
Example uses of the haspermission argument
Teleport all players who have no camera movement permissions to 0, 0, 0:
/tp @a[haspermission={camera=disabled}] 0 0 0
Teleport all players who have movement permissions to 0, 0, 0:
/tp @a[haspermission={movement=enabled}] 0 0 0
Kick all players who have no camera or movement permissions:
/kick @a[haspermission={camera=disabled,movement=disabled}]
Trait Arguments
A count target selector argument will narrow down the target selection using a value limit. This can be done by using a count argument. This argument is explained below. This argument can't be duplicated.
Count (c)
This target selector argument filters target selection based on a set limit. This argument can not be duplicated, meaning it can only be used once in any given target selection. The argument limits the number of entities the command effects. To enter this target selector argument, you would type it as c=<value>. It is important to note that only integer values are accepted.
Example uses of the c argument
Teleport 2 living, randomly chosen players to 0, 0, 0:
/tp @r[c=2] 0 0 0
Teleport the nearest 3 players from the command's executor to 0, 0, 0:
/tp @p[c=3] 0 0 0
Teleport the furthest 3 players from the command's executor to 0, 0, 0:
/tp @p[c=-3] 0 0 0
Kills 5 entities:
/kill @e[c=5]