Command Block

Command Blocks are blocks that run commands when activated by a redstone current, adding vast functionality to adventure maps.

Command Block placement is intended only for map building. They can therefore only be obtained using the command, and can only be edited successfully by operators in Creative Mode on multiplayer servers. They are otherwise never spawned, cannot be crafted, and don't appear in the usual Creative Mode inventory.

To be able to use Command Blocks in multiplayer, the following values must be set in the server.properties file:


 * must be set to
 * must be set to  or above (default is 4)

It is not possible to break Command Blocks in Survival Mode, and explosions do not damage them in any mode. They can't be pushed by pistons, but can be destroyed by Ender Dragons.

Usage
The Command Block can execute useful commands, such as giving items or experience to a player. To change the command executed by the block, simply right-click it, and the GUI will open.

In place of player names, it is possible to use multiple different variables:

Comparators can be used to determine whether or not a command block's command succeeded, as well as the number of times it has succeeded in a single execution. More information on this behavior can be found here, and command success criteria can be found in the Command article.

When using commands, treat them as if entered from the console - you always need to specify the target. For example, to change the mode to Survival for the nearest player type. Note that you will have to be in Creative mode to change the command associated with the block.

The commandBlockOutput gamerule does not affect the command block using the and,  and  commands.

The command block sends commands through the console in singleplayer and multiplayer, though commands can still be prefixed with a "/".

For innovative, practical uses of the Command Block, view Tutorials/Command Block.

Examples

 * The player can make a kind of world guard using the command block. (e.g. code: )
 * Using the radius argument with a teleporter hooked up to a clock circuit can be used to create an exclusion zone ("forcefield") around an x y z coordinate (which should not be centered on the command block itself, otherwise it will be difficult to edit or disable it later). Simply program it to teleport any player within a certain range to a further away point, or just back 10 meters using relative coordinates. Note that such a forcefield wouldn't know which direction you were approaching from, and may result in pulling a player in before pushing them back out the other side.
 * The commands, , and will cause the command block to say user name of the nearest player in the message.
 * You can also clear players inventories, to prevent griefing and/or cheating:

Spawners
Command Blocks can create spawners using the setblock command, eg.: Anything that counts as an entity can be spawned (except a player), which includes all hostile, neutral and passive mobs, as well as animals, minecarts, fireballs, arrows, snowballs, ender pearls, custom thrown potions, fireworks, primed TNT, falling sand or gravel, paintings and item frames, experience orbs, and even dropped items of any type.

Pig/Cow/Chicken spawners can be placed in the butcher's backyard in a village to create a constant meat supply. E.g.: Note that the spawner in the last code example will spawn a chicken riding a pig. There doesn't seem to be a limit to how many levels of riders you can use (e.g.: a chicken riding a pig riding a cow riding a horse riding a silverfish riding a villager ("say that's a mighty odd hat you have on Farmer Brown")).

Please note that if you want to use the RequiredPlayerRange tag you have to use the MaxNearbyEntities tag as well. Same deal with MinSpawnDelay and MaxSpawnDelay. Lastly, note that the y value used in both of those spawner example codes was ~-1, which (assuming you're not flying) will put the spawner in the surface level of the ground at your feet.

If you don't want to target a specific player, you can use,  , or   instead of. Keep in mind that you can use @p (which means "closest player") in player issued commands as an easy to type 2 character replacement for your own name as the closest player to you will always be you.

Booby traps
The most lethal booby trap would be a command block set to teleport someone into the void.

As of 1.8 you can do in a command block below a block with a pressure plate to make a multi-use landmine that kills all nearby entities upon stepping on it (including players, item frames, and paintings)

Note that booby traps that use @p can be dangerous due to the fact that non-player entities could trigger them (is something like a pressure plate is used), resulting in the teleportation of the nearest player, no matter how far away they are and regardless of the fact that they haven't actually stepped on the plate. To get around this, you can use the command as an invisible pressure plate. Hook a clock circuit up to a command block programmed with, with the coordinates changed as appropriate. Note that the range (65536) is an intentional integer overflow, which is currently the only way to get a 1 block area for range. Note that as of 1.8 a range of zero can now be specified. Mojang also made range 65536 actually check that big an area (which presumably means they changed the number of bits the number is in, resulting in a higher max number).

Teleporters
It is possible to make teleporters using the command block. (e.g. code: ) Taking it one step further, a teleporting station can be made, with multiple button-activated teleporters that each teleport to different locations. Scouting and testing teleportation destinations first is advisable, to make sure players don't end up stuck inside blocks or in other unintended places. If you appear in ground while testing coordinates, use to get out (may require multiple uses).

If you use the teleport command with the command block, you can use relative coordinates in the destination coordinates by placing an  in front of a Coordinate (e.g. code:  This would teleport the player 8 blocks into the air).

Commands that use coordinates (e.g. or ) add 0.5 to whole numbers when no decimal follows. This is so you appear centered over the block you appear on instead of at the edge or corner of it. For example, the number -33 would execute as -32.5, and 187 as 187.5. To prevent this behavior, add .0 (25.3, 90.0) after them, as these values are executed left as-is.

Game Rules
Game Rules allow players and command blocks to adjust several base settings. If no change is specified, it will display what the game rule's value is. The player can also input things other than "true" or "false", but they will not be recognized.

Disabled or Unavailable Commands
The following commands cannot be used in a command block for various reasons:

* Some commands, such as,  , and   would be expected to require a player as the executor, but work in command blocks and the server console. The command block's "name" defaults to "@", but can be renamed with an anvil.

** Seed's output can be read from the Command Block's "Previous output" window. However as it only allows 1 line, commands like help or list will only show the first line of the output.

Arguments
After using a target (, ,  , or  ) you can optionally use arguments. This is how you use the arguments: NOTE: Make sure you do NOT add a space between arguments or commas.

Example:

Alternatively, the coordinates and maximum (but not minimum!) radius can be provided more concisely as long as they're the first (up to four) arguments without keys, for instance:

is identical to the previous example.

Because the argument 'c' uses players from the end of a list first when negative,  may be used to select the farthest player instead of the nearest one.

finds only one Arrow in the search radius (100).