Commands (API)

From ComputerCraft Wiki
Jump to: navigation, search

Only available to the fabled Command Computer (itself only available to ops in creative mode, running CC 1.7 or later), the commands API allows your system to directly execute Minecraft commands and gather data from the results.

API

Grid disk.png  commands (API)
Function Return values Description
commands.exec(string command) boolean success, table output Executes the specified command, yields until the result is determined, then returns it.
commands.execAsync(string command) number taskID Executes the specified command, but doesn't yield. Queues a "task_complete" event after the command is executed.
commands.list() table commands Returns a numerically indexed table filled with strings representing acceptable commands for commands.exec() / commands.execAsync().
commands.getBlockPosition() number x, number y, number z Returns the Minecraft world coordinates of the computer running the command.
commands.getBlockInfo(number x, number y, number z) table block info Returns a table containing info about the block at the specified world location. Keys are "name" (a string) and "metadata" (a number).
commands.getBlockInfos(number x1, number y1, number z1, number x2, number y2, number z2) table blocks info Returns a table containing sub-tables with info about the blocks within the specified world locations. Added by CC 1.76


commands.<command>()/commands.async.<command>()

Every command returned by commands.list() is available as commands.command and commands.async.command. For example, instead of writing this:

commands.exec("say Hi")

...you can write:

commands.say("Hi")

Same with async:

commands.execAsync("say Hi")
-- same as:
commands.async.say("Hi")

On top of that you can pass to them multiple parameters including strings, numbers, booleans and tables. All those parameters are serialized if necessary (such as tables) and concatenated into one string. All these functions will run the same command with the same parameters:

commands.exec("tellraw @p {text:'Hello!',bold:true}")

commands.tellraw("@p {text:'Hello!',bold:true}") -- one or more parameters may be put into one string

commands.tellraw("@p", "{text:'Hello!',bold:true}") -- or you can separate them to different parameters

commands.tellraw("@p", {text="Hello!", bold=true}) -- you can also pass a table directly to it

The same applies for commands.async.<command>().

All the commands inside commands.<command>()/commands.async.<command>() are also case insensitive. Running commands.TellRAW( ... ) is the same as running commands.tellraw( ... ).

Notes

  • ComputerCraft enforces a limit of 1000 commands per server tick on Command Computers. Consider yielding manually every so often (eg, sleeping for a second) when making large amounts of commands.execAsync calls.