Difference between revisions of "Commands (API)"

From ComputerCraft Wiki
Jump to: navigation, search
m (renamed a return value)
m
Line 35: Line 35:
 
==Notes==
 
==Notes==
 
*Most commands returned by [[commands.list]]() are also available as functions, such that ''commands.exec(command)'' could also be written as ''commands.command(par1, par2, ...)'' - eg, ''commands.exec("msg @p Hello world!")'' becomes ''commands.msg("@p Hello world!")''.
 
*Most commands returned by [[commands.list]]() are also available as functions, such that ''commands.exec(command)'' could also be written as ''commands.command(par1, par2, ...)'' - eg, ''commands.exec("msg @p Hello world!")'' becomes ''commands.msg("@p Hello world!")''.
*There is a 1000 command limit per tick. After executing 1000 commands, you should [[os.pullEvent|yield]] before executing any more commands.
+
*ComputerCraft enforces a limit of 1000 commands per server tick on Command Computers. Consider [[os.pullEvent|yielding]] manually every so often (eg, sleeping for a second) when making large amounts of [[commands.execAsync]] calls.
  
 
[[Category:APIs]]
 
[[Category:APIs]]

Revision as of 23:42, 16 February 2015

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 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 co-ordinates 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).

Notes

  • Most commands returned by commands.list() are also available as functions, such that commands.exec(command) could also be written as commands.command(par1, par2, ...) - eg, commands.exec("msg @p Hello world!") becomes commands.msg("@p Hello world!").
  • 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.