Namespaces

Variants

Share

Share
Views
Actions

Command Block

From Minecraft Wiki
(Redirected from Command block)
Jump to: navigation, search
Command Block
Command Block.png
Command Block
Type

Tile Entity

Physics

No

Transparency

No

Luminance

No

Blast resistance

18,000,000

Tool

None

Renewable

No

Stackable

Yes (64)

Flammable

No

First appearance

12w32a

Tile Entity ID

Control

Data value

dec: 137 hex: 89 bin: 10001001

Name

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 /give <player> command_block <amount>, 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:

  • enable-command-block must be set to true
  • op-permission-level must be set to 2 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.

Contents

[edit] Usage

The Command Block can execute useful commands, such as giving items or experience to a player.[1] 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:

Variable Function
@p Nearest player
@r Random player
@a All players
@e All Entities

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 /gamemode survival @p. 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 /say and /me, /tell and /tellraw commands.

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

The text limit for a command block is 32k.

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

[edit] Examples

See also: Tutorials/Command Block
  • The player can make a kind of world guard using the command block. (e.g. code: /gamemode <mode:[0|1|2]> @a[r=<radius>])
  • 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 /say Hello, @p, /tell @p Hello, @p, and /me Hello, @p 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: /clear <player> <item> <data-value>

[edit] Spawners

Command Blocks can create spawners using the setblock command, eg.: /setblock ~1 ~-1 ~0 mob_spawner 0 replace {EntityId:Zombie,SpawnData:{Equipment:[{id:276,Count:1}]}} 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.: /setblock ~1 ~-1 ~0 mob_spawner 0 replace {EntityId:Chicken,MaxNearbyEntities:10,RequiredPlayerRange:16,SpawnCount:6,SpawnRange:2,MinSpawnDelay:50,MaxSpawnDelay:200,SpawnData:{Riding:{id:"Pig"}}} 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 @a, @p, or @r instead of <player>. 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.

[edit] 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 /kill @e[r=2] 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 (if 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 /testfor command as an invisible pressure plate. Hook a clock circuit up to a command block programmed with /testfor @p[x=50,y=64,z=46,r=65536], 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).

[edit] Teleporters

It is possible to make teleporters using the command block. (e.g. code: /tp @p x y z) 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 /tp @p ~0 ~8 ~0 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: /tp @p ~0 ~8 ~0 This would teleport the player 8 blocks into the air).

Commands that use coordinates (e.g. /tp or /spawnpoint) 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.

[edit] 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.

/gamerule <rule:[commandBlockOutput|doFireTick|doMobLoot|doMobSpawning|doTileDrops|keepInventory|mobGriefing]> (<enabled:[true|false]>)

Gamerule Effect
commandBlockOutput Enables/disables text output of command block commands to console
doDaylightCycle Enables/disables day/night cycle
doFireTick Enables/disables fire updates (no fire spread or dissipation)
doMobLoot Enables/disables mob drops
doMobSpawning Enables/disables the spawning of mobs (with the exception of spawn eggs and mob spawners)
doTileDrops Enables/disables blocks dropping items when broken (includes TNT destroying blocks)
keepInventory Enables/disables keeping inventory on death
mobGriefing Enables/disables creepers, ghasts, and Wither blowing up blocks, endermen picking up blocks, zombies breaking doors, and certain mobs picking up weapons/armor
naturalRegeneration Allows/Disallows player to naturally regenerate health (if their hunger bar is high enough)
logAdminCommands Enables/disables the "<player> issued server command: /<command>" message shown in the server console
showDeathMessages Enables/disables chat messages when a player dies

[edit] Disabled or Unavailable Commands

The following commands cannot be used in a command block for various reasons:

Category Reason for Unavailability Commands
Disabled for server security These commands are likely disabled due to limited use in command blocks and the potential for severe, unintended consequences on servers. ban, ban-ip, deop, kick, op, pardon, pardon-ip, save-all, save-off, save-on, stop, whitelist add/remove/on/off/reload, debug, publish
Disabled for unknown reasons These commands do not bear unintended consequences, but still fail in command blocks. banlist, whitelist list
Available, but shows in the previous output bar** These commands output messages to their user only, not to public chat. help, seed, list, scoreboard objectives/players/teams list

*Some commands, such as /me, /tell, and /say 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.

[edit] Arguments

After using a target (@a, @p, @r, or @e) you can optionally use arguments. This is how you use the arguments: @<selector>[<argument>=<value>,<argument>=<value>]
NOTE: Make sure you do NOT add a space between arguments or commas.

Argument Function
x X coordinate for the search area, being the center for radial selection and the starting corner for cubic selection. Note, this must be an integer.
y Y coordinate for the search area, being the center for radial selection and the starting corner for cubic selection. Note, this must be an integer.
z Z coordinate for the search area, being the center for radial selection and the starting corner for cubic selection. Note, this must be an integer.
dx X coordinate offset to define a rectangular search area. Note, this cannot be negative.
dy Y coordinate offset to define a rectangular search area. Note, this cannot be negative.
dz Z coordinate offset to define a rectangular search area. Note, this cannot be negative.
r Maximum search radius.
rm Minimum search radius.
rx Maximum x rotation.
rxm Minimum x rotation.
ry Maximum y rotation.
rym Minimum y rotation.
m A player's game mode. -1=All, 0=Survival, 1=Creative, 2=Adventure, 3=Spectator
c Number of players (or entities). If negative, uses players (or entities) from the end of the list first.
l Maximum experience level of players.
lm Minimum experience level of players.
score_name For the objective "name", the maximum score this player has.
score_name_min For the objective "name", the minimum score this player has.
team Checks if player is in the specified team. Inserting an '!' before the team name checks only for players not on this team. Providing no team name allows for checking for all players without a team.
name Checks for players or entities with this name. Using =! instead of = checks only for players/entities who do not have this name. (Example: name=!Notch)
type Checks for entities with this entity type. (Example: type=EntityHorse) This can also be used with =! (described above).

Example: @p[x=1,y=30,z=26,r=10,rm=2,m=1,c=3,l=25,lm=3]

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:

@p[1,30,26,10,rm=2,m=1,c=3,l=25,lm=3,score_Kills_min=1,score_Deaths=2,team=Red]

is identical to the previous example.

Because the argument 'c' uses players from the end of a list first when negative, @p[c=-1] may be used to select the farthest player instead of the nearest one.

@e[type=Arrow,r=100,c=1]

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

If r is specified along with dx, dy and/or dz, only entities within the overlap of both search regions will be found.

[edit] Video

[edit] History

Official release
27 July 2012
Jeb began tweeting of a new block he had created to start expanding Adventure mode. He began posting pictures, ideas and hints of what the block will be able to do.
31 July 2012
During Notch's AMA he hinted the block may be in 1.4.
1.4.2 12w32a Added Command Block.
1.5 13w03a Command block success can now power a redstone comparator.
13w04a Command blocks renamed with an anvil now use their name instead of @ in the chat.
1.6.1 13w19a Command Blocks are no longer mineable in survival, and are also not damaged by explosions except by Blue Wither Skulls.
1.7.2 13w37a Added a bar that shows the previous output. Also increased the character limit from 256 to 16,369 when inserting a command in-game. Using a third-party editor allows you to reach 32,767 characters.
13w39a Added Minecart with Command Block.
Upcoming
1.8 14w07a Dispensers now places command blocks and runs the preset command if there's one, instead of shooting it out.
See also: Commands#History

[edit] Issues

Issues relating to "Command Block" are maintained on the issue tracker. Report issues there.

[edit] Gallery

[edit] See also

[edit] References