Difference between revisions of "Commands.exec"

From ComputerCraft Wiki
Jump to: navigation, search
(Removed misinformation. commands.exec/execAsync only allows to pass one string: the command. Only commands.<command>() where <command> is an actual MC command allows multiple parameters, including numbers and tables)
(Updated to ComputerCraft 1.71)
Line 3: Line 3:
 
|name=commands.exec
 
|name=commands.exec
 
|args={{type|string}} command
 
|args={{type|string}} command
|returns={{type|boolean}} success [<nowiki/>, {{type|string}} error]
+
|returns={{type|boolean}} success, {{type|table}} output/{{type|string}} error
 
|api=commands
 
|api=commands
 
|addon=ComputerCraft
 
|addon=ComputerCraft
|desc=Available only to [[Command Computer]]s, executes the specified [http://minecraft.gamepedia.com/Commands MineCraft command], [[os.pullEvent|yields]] until the result is determined, then returns it. If command executes successfully then it returns <var>true</var>, otherwise returns <var>false</var> + an error message. Compare [[commands.execAsync]], which ignores the result and returns immediately, without yielding.
+
|desc=Available only to [[Command Computer]]s, executes the specified [http://minecraft.gamepedia.com/Commands MineCraft command], [[os.pullEvent|yields]] until the result is determined, then returns it. If command executes successfully then it returns <var>true</var> + the output of the command as a numerically-indexed table, otherwise returns <var>false</var> + an error message as as a numerically-indexed table. Compare [[commands.execAsync]], which ignores the result and returns immediately, without yielding.
 +
<br/>
 +
In ComputerCraft 1.71+ instead of just returning <var>true</var> this function also returns the output of the command in a numerically-indexed table. This output is the same as if you would run the command in chat. Each line of the output is put in a separate index. See example below for an example of this table.<br/>
 
<br/>
 
<br/>
 
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!")''.
Line 24: Line 26:
 
   [[print]]( "There are no players around me." )
 
   [[print]]( "There are no players around me." )
 
  end
 
  end
 +
}}
 +
{{Example
 +
|desc=Prints the output after running the command "help".
 +
|code=local success, output = '''commands.exec("help")'''
 +
 +
if success then
 +
  [[print]]( textutils.serialize( output ) )
 +
end
 +
|output=Example output:
 +
{
 +
  "--- Showing help page 1 of 5 (/help <page>) ---",
 +
  "/achievement give <stat_name> [player]",
 +
  "/clear <player> [item] [data]",
 +
  "/defaultgamemode <mode>",
 +
  "/difficulty <new difficulty>",
 +
  "/effect <player> <effect> [seconds] [amplifier]",
 +
  "/enchant <player> <enchantment ID> [level]",
 +
  "Use /forge <subcommand>. Subcommands are tps, track",
 +
}
 +
&nbsp;
 
}}
 
}}
 
}}
 
}}

Revision as of 15:58, 23 February 2015


Grid Redstone.png  Function commands.exec
Available only to Command Computers, executes the specified MineCraft command, yields until the result is determined, then returns it. If command executes successfully then it returns true + the output of the command as a numerically-indexed table, otherwise returns false + an error message as as a numerically-indexed table. Compare commands.execAsync, which ignores the result and returns immediately, without yielding.


In ComputerCraft 1.71+ instead of just returning true this function also returns the output of the command in a numerically-indexed table. This output is the same as if you would run the command in chat. Each line of the output is put in a separate index. See example below for an example of this table.

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!").
Syntax commands.exec(string command)
Returns boolean success, table output/string error
Part of ComputerCraft
API commands

Examples

Grid paper.png  Example
Says 'Hello' to the player, closest to the Command Computer.
Code
commands.exec( "say @p Hello" )



Grid paper.png  Example
Checks if there are any players around the Command Computer in 2 block radius.
Code
local radius = 2
local arePlayersAround = commands.exec( "testfor @a[r=" .. radius .. "]")

if arePlayersAround then
  print( "There is one or more players around me." )
else
  print( "There are no players around me." )
end



Grid paper.png  Example
Prints the output after running the command "help".
Code
local success, output = commands.exec("help")

if success then
  print( textutils.serialize( output ) )
end
Output Example output:
{
  "--- Showing help page 1 of 5 (/help <page>) ---",
  "/achievement give <stat_name> [player]",
  "/clear <player> [item] [data]",
  "/defaultgamemode <mode>",
  "/difficulty <new difficulty>",
  "/effect <player> <effect> [seconds] [amplifier]",
  "/enchant <player> <enchantment ID> [level]",
  "Use /forge <subcommand>. Subcommands are tps, track",
}
 


Grid disk.png Commands API Functions
commands.exec - commands.execAsync - commands.list - commands.getBlockPosition - commands.getBlockInfo - commands.getBlockInfos