Tutorials/Command blocks and functions

The Command Block is a way to help support Adventure Mode, SMP automation, and custom maps. To find out more in detail, visit the Command Block page.

Singleplayer applications (SSP)
Command blocks can be used in single player to change the difficulty, change the state of the weather, or give you predesignated items. They are particularly useful for the /weather and /time set x commands, as they are only available in cheat mode otherwise. The /time set x command is especially useful, as it allows one to change the current time to a preset time at the press of a button. Command blocks can also be used to make text appear in the person's chat menu. To do this, dig 2 blocks down in the ground, place a command block at the bottom of the hole, type this into the command block: tell @p  or tell  then place a block on top and place a pressure plate on that block. If you typed @p when someone steps on it whoever stepped on it will get a message but not anybody else. If you typed in when you or any other person steps on it whoever the username is will be sent the message: @ whispers to you:  in that form of text.You can also type: tell @p and whenever someone walks over it it will say: @ whispers to you: ''. If a mob steps on the pressure plate it will say the username of the closest person to the command block, and if you want to change the difficulty then do /difficulty x and use 0-3 or the name of the difficulty (ex. /difficulty peaceful). Another way to use Command Blocks in singleplayer is to use /tp @p to warp between different bases in your world. You can use the warp by powering the command block.

Elevator
You can also make an elevator using /tp ~0 ~y ~0, where y is the vertical difference between the upper and lower elevator entrances. The elevator can also be made to tp a player to any x y z coordinate just by leaving out the ~ before the numbers. When ~ is left in the command, the normal x y z is set to 0 0 0 where the command was made, such as standing on a pressure plate or wherever the player is standing when the command block received power. If a pressure plate was at the x y z coordinates of -36 96 -12 that ran to a command block with a command of /tp @p ~3 ~4 ~5 it would tp the player to the coordinates of -33 100 -7. The same outcome can be achieved by having the command of /tp @p -34 100 -8 one block must be subtracted from the x and z axis to get to the right coordinates.

Security System for Players
Command blocks can also be used to make a security system. Just put a pressure plate which triggers a command block in front of an iron door, which will be the entrance to your house. Insert the command: /tp @p[r=3] [name=! ] 0 -100 0. The x, y, z coordinates can be different, the above is just an example. For mobs, a simple fence or lava moat will do.

Moving Sidewalk
A similar concept to the elevator but made to move a player along the x or z coordinates. This can be created by:
 * Build an obsidian strip
 * Place a command block halfway down the strip and enter this command:
 * /tp @p ~1 ~0 ~0 [r=(sidewalk length / 2)] to move along the x axis, or
 * /tp @p ~0 ~0 ~1 [r=(sidewalk length / 2)] to move along the z axis


 * Attach the block to a clock

This will make it look like and act like the person is on a moving sidewalk. It will be very noisy unless you first turn command block output off. /gamerule commandBlockOutput false

Eternal Day


Note: As of 13w24a, there is a gamerule, doDaylightCycle, which can be used to obtain eternal day by typing in the chat, "/gamerule doDaylightCycle false", and then "/time set 6000".

By using Cobweb and a minecart, you can skip nights using /time set 0. A minecart takes 31 seconds to fall through a cobweb. Therefore, by using 23 stacked cobweb with a Detector Rail and several Powered Rail will keep the sun in the sky.

Alternatively you can use an item de-spawning on a wooden pressure pad. The item de-spawns off of the pad every 5 mins, at this time it triggers a command block reset of the time to 0, also it triggers a dispenser (or dropper) to drop a new item on the wooden pad. 5 Mins is the day cycle and will trigger before mobs spawn. This method however is limited to 64x9 = 576 cycles = 48 IRL hours of gameplay with that chunk loaded. In the snapshot 13w01a and higher, hoppers are added into the game that can pick up items and deposit them into blocks with an inventory. A mob grinder can keep the dispenser full with gunpowder, arrows, and zombie flesh.

Both of these methods require the chunk to be loaded to allow the system to work.

Alternatively, you could connect the command block to a repeating redstone pulse, such as a five-clock, some type of loop, or a daylight sensor as seen on the right. To prevent this filling up the chat box, set the gamerule "commandBlockOutput" to false using the command /gamerule commandBlockOutput false. Note: This method can be used to set a variety of things by using a different command. (I.E. to have eternal sun, add another command block to the loop and use the command /weather clear)

Also, to make it toggle-able, use one of these designs, but put an AND Gate before the command block, with a lever on one side, and the mechanism on the other side.

Using the Daylight Sensor block is a more direct approach for eternal day.



The sensor deactivates the Redstone torch which will activate the Command Block which will set the time to 80000 when there's not enough light for the sensor to power the Redstone torch across the far distance. In other words, the Command Block will set the time to Noon when it's dark enough. The light level when the command will be activated varies with the distance of the Redstone wire. The time it sets to varies with the time typed in the Command Block.



To make it, first, you place the Daylight Sensor on a open area. Then you place some Redstone wire away from it. The distance of the Redstone wire depends on your need of precious time. You can only have a max of 15 blocks of Redstone. Next, you put a block on the other end of the Redstone. Put a Redstone Torch on the block's side and a Command Block on top of the torch. Right click the command block and start with "/time set" and enter a valid time value and the end. The value depends on when you want to set the time to. 14000 is night and 8000 is noon.



This design can be used in maps to keep it a certain time of day/night. This simple contraption can only be used in Creative mode or when you enable Cheats.

There is an alternative, more compact design. If you put a comparator facing away from the sunlight detector and a 15 strength redstone line (one redstone with a torch) into the second input. Then set the comparator to subtraction mode. The comparator will activate right after noon.

Wireless redstone
Wireless Redstone can be achieved in vanilla Minecraft using command blocks and the scoreboard system. The process involves using a scoreboard variable that is searched for by a command block attached to the output of a clock circuit. A comparator is then used on the wireless output command block that will trigger when the variable is found true.

Wireless Redstone can also be achieved by using objectives instead of variables. If the output command block is set to try and remove a non-existent objective, a redstone signal can be created by adding said objective via command block or chat command. The advantage to this method over the variable method is that it is not dependent on players.

To do this, place a command block at the input and type the command: /scoreboard objectives add dummy. At the output location, place a command block, type /scoreboard objectives remove, hook it up to a clock, and put a comparator then a repeater leading out of the command block. Then just use the repeater as an output.

Starter Kits
By using m=, you can make a kit on a server/custom map that can only be obtained once! First, dig 3 squares down in the ground, then place a row of command blocks (one for each item in the kit). In the GUI, type. Do this for each item you wish to give. Make an extra command block that has the command, then string redstone across all of the command blocks (this can be done easily by sneaking and placing). Fill in the hole and place a pressure plate over it. When players walk over it, they will get the desired kit of items and it will change their gamemode so they cannot receive it anymore (unless cheats are used)

Using Command Blocks in Single Player Non-Creative Maps
By swapping gamemode it is possible to use command blocks in single player outside of creative mode. Simply swap to creative mode with the /gamemode 1 command, set the command block command to whatever you want, then swap back to /gamemode 0. The command block only needs to be set in creative, but will still function once back in normal.

It can also be used to teleport to certain areas (for ex. a far-away Stronghold, a deep mining operation, or a floating island). If it took you a very long time to get there, and if you don't mind sort-of cheating, then you can set up a teleport system. Make sure that you are on Creative mode while setting it (use the command /gamemode 1. If you don't have cheats enabled, open the LAN and set the cheats on), but you can still activate it in Survival mode. Type this command in when having the Command Block GUI open /tp [@p,r or a] x y z. Then, activate the command block with redstone (including buttons, levers, and pressure plates), and you will be sent to your far-away destination. You can set up multiple different teleports. This can also be used when doing a big project that spans over a very large area or if you are transferring chests to from an old house to a newer house.

Adventure Map Making
If one wishes to create a Single Player adventure map, do /publish to allow all other commands (see below) to be used.

Multiplayer applications (SMP)
This section details the applications of the Command Block in multiplayer.

Through, one can create teleporters for public use on a server. Can be applied to a range of scenarios. For example, one could make a system of portals in a particularly large world in which all portals link to a central hub, which contains portals to all other portal locations creating a sort of "Fast Travel Hub"
 * Teleportation/Transport

By using /tp and an external minecraft world editing program, such as MCedit one can copy a landscape, paste it in a faraway location, and change a few blocks. Wire a pressure plate or tripwire at each site, so that after a short delay to admire the scenery, the player is teleported using the /tp command in the command blocks. If a large amount of these copied and altered locations are wired so that the player teleports to them in sequence with a slight delay in between each, then this creates the illusion that the scenery around the player is moving, similarly to the techniques used in stop-motion animation. Through, one can create a lottery. Keep in mind that now,  will directly put items into one's inventory, instead of throwing them on the ground in front of the character.
 * "Stop-Motion Animation"
 * Lottery

By rigging a clock to a command block and using  a whole bunch, you can force a player to stay at a certain place. If you do this it would be advisable to either turn off commands in your chat settings or do. Using the "Range" and "Minimum Range" parameters, you can have it teleport players back only when they're a certain distance away.
 * Jails

Alternatively, giving the player certain status effects at high levels using the  command can mimic the effect of a jail. Use  to keep the player from moving, and   to keep the player from breaking things. If you really want to kick it up a notch, poison the player (use one of the previous two commands, but replace the first number with 19).

By using the parameter, only players in Creative will be affected by the command block.
 * Admin Only

If you place a command block connected to a redstone pulse you can set  in a command block one can create announcements that could play even when the admin/owner wasn't there.
 * Fun anti-creative mode trap

By using, you can scan people for items. If you want to remove certain items, use. For example, on a server that doesn't want griefing you could have a clock connected to a command block set to.
 * Security Scan

Using, the command block can report the nearest player. If the command block is placed after a finish line, it will say the finishers in order.
 * Race Referee

Creating a "Creative Only" zone is possible through command blocks, but it is complex and more of a case-by-case basis. The basic idea would be to create a "fence" of command block affected zones so that, once a player flies in creative through it, they would be changed to survival or adventure mode. A "World Guard" zone would be a lot simpler, just have a command block do. To prevent people pressing the button a second time and again getting their kit you can make two command blocks activated in order: 1.
 * Creative Area/World Guard

Although you are not directly able to obtain colored/bold/italic text when typing into the Command Block GUI, through McEdit filters you can edit the text to have those properties just like how plug-ins change chat text properties on servers. SethBling's McEdit Filter: ColourCommandBlockText
 * Special Colourful Words

When a player walks over a pressure plate, it can trigger a command block with /say @p.
 * Alarm System

Using coordinate and maximum parameters you can teleport everyone within a specific area to another specific area.
 * Remove Players

If a certain player has been very nice,regular,or helpful to the server but not quite meeting the requirements to be promoted to an admin, you could have a room full of buttons connected to Command Blocks with helpful effects, like giving the helpful player a stack of diamonds or a bedrock block, then have it connected to another command block, sending them to the spawn point. (It is VERY important this room is reinforced with bedrock, hidden, and you can only get in by teleporting to its co-ordinates to avoid abuse).
 * Reward Room

(Command:  and   (to start or stop a thunderstorm))  Assuming only admins can get to it, a weather machine can be quite useful, you can turn rain on and off at will. One of the more helpful uses of this technique would be to use a combination of villagers and some form of redstone machinery to create and automatic rain disabler, this would allow you to shut off the rain almost immediately without needing to use the command, or to have an admin present. It can also be used to create, and/or end, a scenario where it is more advantageous to have rain as opposed to sunlight, and vice versa; such as during a forest fire, or when a thunderstorm is spawning monsters.
 * Weather Machine

with tp @p and give @p, you could make an obstacle course or maze that when finished will give a player a prize, and will teleport them back to spawn or some other area.
 * Obstacle Course prizes

By making two similar places on one map, you can have a button activate a command block to tp you from one to another, while also changing the time of day. This can be used to look like time travel.
 * Time Machine

By using a condition such as  hooked to a short clock and one or more TNT blocks via a comparator, the command block can act as an undetectable proximity mine against all players except the placing player.
 * Proximity Mine

By using a condition such as, the command block can detect a door's owner within a certain radius and open a door for them, and only for them.
 * Secure Doors

As of the snapshot 13w36a, you can put NBT tags in the /give command. You can obtain an infinite stack of blocks by typing in a command block.
 * Infinite items in survival

Application in custom maps
This section details the applications of the Command Block in custom maps.

One map that takes advantage of a lot of the features of the Command Block is SethBling's TF2 Map "Dustbowl" in Minecraft.

http://www.youtube.com/watch?v=_f2brU8hBWc&feature=plcp

Another example is the Hypixel's Gladiator Arena in which the fighting system is almost only based on the Command Block.

http://www.youtube.com/watch?v=jryJkWcqmzM

Sometimes it's important in a map to keep the command blocks silent, may it be to keep the magic of the suddenly appearing items in the inventory, or because the channel telling that the player was teleported, it can break the mood of a map. To keep them silent (except when you ask them to speak), just use the.
 * Silent Command Block

Using pressure plates or tripwire, make areas (like a castle for instance) a different difficulty (for this instance I will use peaceful). the doors could have tripwire hooked to a command block that changes ones difficulty to peaceful, creating a sort of "Safe Haven". one could also do it the other way around, creating a more dangerous zone, such as arenas having the difficulty of hard, rather than normal or easy.
 * Safe Haven/Arena Fight Zone

Using parameter , you can affect those who are in Creative, or survival with parameter.
 * Detecting Cheaters

This is a simple one, just have a system of tp-ing command blocks at the entrance/exit of a place, and you can build the interior somewhere else, allowing you to cheat space restriction. (example of use: you want interior walls to be wool, but exterior walls to be wood (normally 2m thick walls). You can make the outside of the house be 8x10 m, and the inside of the house be 8x10 m.) Or, have the inside of the house 20x20m instead!
 * It's bigger on the inside

By using the  command, one can easily make a button-activated checkpoint.
 * Save game

As said before, there is a command that makes the player able to keep their inventory upon death:. Simply connect this to a pressure pad before a parkour level over lava and you don't have to use any kind of chest minecart (especially useful when the player has it's full inventory, which is bigger than a single chest).
 * Lava parkours

It is true that the  command is really useful because you don't have to wait for the night to create a checkpoint, but what if you want to have a BUD switch connected to the bed or something of the sort? Then, you just need to have a pressure plate (it's more discreet) connected to a block with the  command, and it will set the time to night so the player can sleep.
 * Still want a bed?

Let's say your map starts on a cold stormy night and when the player starts, for some random reason, there's a bright sun outside! Or what if you want to start on a bright day and the rain decides to join along? Fear not, for there is a weather command. Just use the  command with, as input, ,   or. Along with this, you can also use  with as input a number:   is sunrise,   is noon,   is dusk and   is midnight.
 * Time and weather following the scenario

Create a hostile mob (so it disappears when switching to Peaceful) that never despawns if the player is far away (use third-party software to do so) and place it on a pressure plate. Make sure it will keep it pressed infinitely, so make sure it can't jump or walk away. Make a redstone circuit that causes a command block to activate when input from pressure plate ceases to exist. Place a message inside the block that informs the player on chat that the map has been broken and must be redownloaded. This way, switching to Peaceful will cause the mob to vanish and activate the message. You can even make a teleportation command block so that a cheater is banished from the map to a small room without a way out as soon as he switches to Peaceful.
 * Detecting Players who switched to Peaceful


 * Talking Adventure Map(featuring an easy Quest System!)

The  command is really useful because it makes a text appear into the chat. It's maybe one of the greatest use of the Command Block as you don't need silly signs or long books to show the game rules. Just put a bunch of command blocks in lines with repeaters set to two or three ticks (so the player isn't spammed) and you can easily remove a chest of books or a lot of signs.

Another use of the command is talking NPCs and other mobs. Imagine you enter a butchery and you want the butcher to say something. Before, you needed to use signs or books, using some room in a butchery and putting objects that sometimes seemed random, but now... You just have to use the  command and the butcher will talk. Maybe a pig behind the counter will even beg for mercy! Let's take another example: you are in a factory and an alarm suddenly rings and says that it's going to explode. Having it in the chat, especially if the text is long, keeps the stressfull mood; the same thing works for bomb timers. The player will get more into the map's mood if the chat displays '10...' '9...' '8...' also.

Last but not least, it makes it easier to create a Quest System. Let's say the player gets in a butchery. He activates through a pressure plate connected to a RS NOR Latch (so it can't be activated twice) and a command block with the  command in which the butcher asks for the player's help: he needs to kill a pig (shame on him). The player gets to the pig that is standing on a stone pressure plate, that turns off when the pig is dead. So, when the player gets back to the butchery, he activates another circuit containing yet another RS NOR Latch and two command blocks with two commands. First, the  command in which the butcher thanks the player, and other one, the   command, so the player gets the reward. Along with that, don't forget to use the @p command so the butcher says the player's name. For example, if the command block is set to do this:, the channel will say: "[@]: Thanks, . You helped me a lot killing that pig." This makes the map way cooler and looks like it's made for the player as it says his/her name! You could also change [@] to [Butcher] by renaming the /say command block using an anvil.

By using  and , which can be enployed to build structures, fill chests, and spawn previously unavailable entities. The syntax for the  and
 * Making a Store

Along with that, the upcoming command ' will permit you to construct automatically structures; It will also make it possible to make traps with disappearing floors and sudden walls.

(Thank you HeroOfTime500 for being in the minecart)

The setblock command will also allow you to do this but only with normal rails without third party programs like mcedit

Further Resources
Feare_104's Command Block Tutorials