Sometimes, commands require you to know specific information about the Minecraft world. For example, the teleport command doesn’t work unless you enter the name of a player who’s currently online — this can be quite impractical in a server where several players join and leave. Fortunately, there are a few versatile substitutes for common parameters that you can use to make your commands always work.
Many commands require you to enter coordinates as parameters, which can be cumbersome. For example, if you want to target a particular location, you have to press F3, record your coordinates, find the difference between your coordinates and those of the target location, and calculate the target location.
Relative positions are often easy to calculate, and they’re useful for writing generalized commands. Essentially, if you precede a coordinate with a tilde (~), the coordinate is relative to the player or command block running the command.
For example, if you enter the coordinates ~0 ~-1 ~0, the command interprets this as the space just below the player or command block. You can even use a single tilde in place of ~0, so you can, for example, use the command fill ~ ~ ~ ~ ~1 ~ gold_block to place gold blocks on both your position and the position just above you.
Similarly, tp Isometrus ~5 ~3 ~ teleports the player Isometrus five blocks to the east and three blocks up from his original position.
Much like relative positions, selectors are used to write general and versatile commands with many applications. They allow you to target players without knowing their usernames, target several players, and even target entities of all sorts (basically any nonblock object in the world, including creepers, minecarts, and fireballs).
If you right-click a command block, the screen that appears contains the following information about using selectors:
Use "@p" to target nearest player Use "@r" to target random player Use "@a" to target all players Use "@e" to target all entities
You can use these selectors in place of any parameter that requests a target, such as a player. For example, if you use the command tp @r 0 64 0, a random player is teleported to the coordinates (0, 64, 0). However, if you type tp @e 0 64 0, every entity in the world is teleported there.
You can also add modifiers to a selector, making it target only certain entities. For example, @a[r=20] targets all players within 20 blocks, and @e[type=Zombie,c=3] targets three zombies. Every modifier takes this form:
[<argument1>=<value1>,<argument2>=<value2>,…,<last argument>=<last value>]
If you apply this modifier, the selector targets every entity for which every argument is satisfied by the value given. To explain this, check out the list of arguments you can apply, and which entities they accept.