Zombies + Binary #4

Command Blocks

As developers, things we do solve problems. They make people’s lives easier. What we do is magic, pure magic, and we should never forget that. ― Kayla Daniels

← Back | More coming soon…

Logic gates are essential for basic circuitry, but Minecraft provides a stronger component. It’s called a Command block.

Command block →

Command blocks can execute commands when given a charge. This can be a charge from a Redstone torch, switch or any other Minecraft power source.

Command interface→

Commands

There are all sorts commands which Command blocks can execute. Command blocks can execute almost anything you can type in the console. Some of the more useful commands are:

/w player_name hello friend!

This will send a private message to a player. Replace player_name with the in-game name of your friend and they’ll get the message. /give player_name redstone_block

This will give player_name 1 block of Redstone. This works for every block/item in the game. Try giving yourself stone, wool or even diamond. /setblock 1 2 3 gold_block

This will place a gold block at x=1, y=2 and z=3. These coordinates can be tricky to figure out. If you press F3 then you can see your current x, y and z coordinates roughly half-way down the mess of text. /testforblock 1 2 3 water

This will test for the presence of water at x=1, y=2 and z=3. 1 uninterrupted charge means 1 check. /testfor @a[r=5]

This will test for any/all players within a radius of 5 blocks (from the Command block). If there are any players within those 5 blocks when the Command Block receives a charge, it will emit a comparator pulse. Don’t worry about what that means just yet… /tp player_name 1 2 3

This will teleport player_name to x=1, y=2 and z=3. You can replace 1 2 3 with the name of another player to teleport player_name to them instead.

You can find a comprehensive list of commands here. You can find a comprehensive list of blocks here. Clicking on a one of the block links will take you to a dedicated page for that block. You can find the block’s command name on the right-hand info section.

Coordinates

Some of these commands include coordinates. You can find your current coordinates by pressing F3. It’s also possible to use relative coordinates.

Command blocks can use /setblock ~3 ~ ~ stone or /setblock ~-3 ~ ~ stone to place stone 3 blocks away from themselves. They can use /tp @a ~ ~5 ~ or /tp @a ~ ~-5 ~ to teleport all players 5 blocks above or below where they currently are.

The details for detection commands (like /testfor) are a bit more complex. You can find them here.

Getting Command Blocks

Command Blocks are 1 of a few blocks which do not appear in the creative inventory. You can’t craft them. The only way to get a Command block is to spawn it in, using the following command:

/give player_name command_block 1

You need to enable cheats (when you create a map) for this command to work. Command blocks are meant for experimentation and custom map mechanics.

Clocks

Sometimes all you need is a single charge pulse, to get a Command block to do something. When you want to whisper something as a player crosses a threshold, or give them some armour then a single pulse is perfect.

Other times you may want to repeat an action. This is particularly useful when you’re doing things like detecting players or blocks within a radius or at a location. These tests need to happen frequently, and to do this we need to build clocks.

Clocks used to require all sorts of Redstone hacks and were also particularly unstable. Depending on the clock, they might freeze or cause lots of server lag. These days we can use Command Blocks to make compact, efficient clocks.

Setblock Clock

The first clock we can build uses the setblock command to place and remove Redstone blocks, in an infinite loop. It looks like this:

Setblock clock →

The top Command block places a Redstone block between itself and the bottom block, using a command like:

/setblock ~ ~-1 ~ redstone_block

The bottom Command block swaps the Redstone block with any other block, using a command like:

/setblock ~ ~1 ~ stone

Once you power the top Command block, it should place the middle Redstone block, powering the bottom Command block. This means it will be removed but not before giving another charge to the top Command block. The result is about 20 placements and breaks of the middle Redstone block, within a single tick.

Ticks are the Minecraft equivalent of 1 second. 20 cycles per tick is fast!

When the Command blocks place other blocks, they emit a message both in-game and in the log files. You can disable these messages with:

/gamerule logAdminCommands false

/gamerule commandBlockOutput false

Fill Clock

You can use the same configuration to make a Fill clock. The only difference is that the top Command block will use a command like:

/fill ~ ~-1 ~ ~ ~-1 ~3 redstone_block

And the bottom Command block will use a command like:

/fill ~ ~1 ~ ~ ~1 ~3 stone

The fill command takes a series of coordinates (x1, y1, z1, x2, y2, z2) and fills it with blocks. Instead of creating and breaking 1 Redstone block, this will create and break as many blocks (in a line) as you tell it to. Each one of those Redstone blocks can supply a charge to 4 Command blocks.

Fill clock →

Using Command Blocks As Sensors

As we’ve seen, we can use Command blocks as a kind of sensor. By adding clocks, we can make those sensors gather data.

Minecraft includes another special block, called a Comparator. It has many uses, but the only one which interests us is the ability for it to read from a Command block.

I’ve placed a Command block next to the Redstone block, created by a Setblock clock. This Command block has the following command:

/testfor @a[r=5]

This command will test for any/all players within a radius of 5 blocks (from the Command block). If we add a Comparator next to the this Command block, it will emit a signal when the Command block detects a player in that radius. I’ve added a Glowstone lamp to show how the Command block reacts when I enter the radius.