Difference between revisions of "Commands (API)"

From ComputerCraft Wiki
Jump to: navigation, search
(I'd prefer the "native" table remain undocumented)
m
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
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 [http://minecraft.gamepedia.com/Commands MineCraft commands] and gather data from the results.
+
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 [https://minecraft.gamepedia.com/Commands Minecraft commands] and gather data from the results.
  
 
==API==
 
==API==
Line 6: Line 6:
 
{{API table/row
 
{{API table/row
 
|[[commands.exec]]({{type|string}} command)
 
|[[commands.exec]]({{type|string}} command)
|{{type|boolean}} success
+
|{{type|boolean}} success, {{type|table}} output
 
|Executes the specified command, yields until the result is determined, then returns it.
 
|Executes the specified command, yields until the result is determined, then returns it.
 
|odd}}
 
|odd}}
Line 25: Line 25:
 
|[[commands.getBlockPosition]]()
 
|[[commands.getBlockPosition]]()
 
|{{type|number}} x, {{type|number}} y, {{type|number}} z
 
|{{type|number}} x, {{type|number}} y, {{type|number}} z
|Returns the MineCraft world co-ordinates of the computer running the command.
+
|Returns the Minecraft world coordinates of the computer running the command.
 
|}}
 
|}}
  
Line 33: Line 33:
 
|Returns a table containing info about the block at the specified world location. Keys are "name" (a string) and "metadata" (a number).
 
|Returns a table containing info about the block at the specified world location. Keys are "name" (a string) and "metadata" (a number).
 
|odd}}
 
|odd}}
 +
 +
{{API table/row
 +
|[[commands.getBlockInfos]]({{type|number}} x1, {{type|number}} y1, {{type|number}} z1, {{type|number}} x2, {{type|number}} y2, {{type|number}} z2)
 +
|{{type|table}} blocks info
 +
|Returns a table containing sub-tables with info about the blocks within the specified world locations. ''Added by CC 1.76''
 +
|}}
  
 
}}
 
}}

Latest revision as of 07:07, 4 August 2020

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.