Minecraft:Commands/function: Difference between revisions
More actions
imported>Kestrer m Add link |
Fix template calls: add MC/ prefix |
||
| (One intermediate revision by the same user not shown) | |||
| Line 4: | Line 4: | ||
* 2 {{only|java|short=1}} | * 2 {{only|java|short=1}} | ||
* 1 {{only|bedrock|short=1}} | * 1 {{only|bedrock|short=1}} | ||
| others = | | others = Cheat only{{only|bedrock|short=1}} | ||
}} | }} | ||
| Line 10: | Line 10: | ||
== Usage == | == Usage == | ||
{{ | {{MC/In|java}}, runs a function or all functions in a tag from a data pack. | ||
{{ | {{MC/In|bedrock}}, runs a [[Minecraft:Function (Bedrock Edition)|function]] from a [[Minecraft:behavior pack]]. | ||
{{ | {{MC/In|java}}, the behavior of {{MC/Cmd|return run function ...}} (or {{MC/Cmd|return run execute ... run function ...}}) is different with a direct call to the {{MC/Cmd|link=none|function}} command. If {{MC/Cmd|return run function ...}} runs multiple functions, execution stops after first {{MC/Cmd|link=none|return}} in any of the functions. If a direct call to the {{MC/Cmd|function ...|link=none}} runs multiple functions, all of the functions are executed, and the return values are added up as the <code>result</code> value of the {{MC/Cmd|function ...|link=none}} command. | ||
== Syntax == | == Syntax == | ||
| Line 22: | Line 22: | ||
: <code>function <name> <arguments></code> | : <code>function <name> <arguments></code> | ||
: <code>function <name> with (block <sourcePos>|entity <source>|storage <source>) [<path>]</code> | : <code>function <name> with (block <sourcePos>|entity <source>|storage <source>) [<path>]</code> | ||
:: Runs a function or functions in a tag, with arguments for | :: Runs a function or functions in a tag, with arguments for macros. | ||
* '''Bedrock Edition''' | * '''Bedrock Edition''' | ||
| Line 29: | Line 29: | ||
== Arguments == | == Arguments == | ||
{{ | {{MC/Argument|name|function|name: filepath|CommandFilePath}} | ||
: Specifies the function(s) to execute. | : Specifies the function(s) to execute. | ||
: {{ | : {{MC/Arg desc|je=function|be=commandfilepath}} | ||
{{ | {{MC/El|je|short=1}}: {{MC/Argument|arguments|nbt_compound_tag}} | ||
: Specifies arguments for macro functions in a compound NBT tag. | : Specifies arguments for macro functions in a compound NBT tag. | ||
: {{Arg desc|je=nbt_compound_tag}} | : {{MC/Arg desc|je=nbt_compound_tag}} | ||
{{ | {{MC/El|je|short=1}}: {{MC/Argument|sourcePos|block_pos}} | ||
: The position of the target [[Minecraft:block entity]] whose NBT is to be used. | : The position of the target [[Minecraft:block entity]] whose NBT is to be used. | ||
: {{ | : {{MC/Arg desc|je=block_pos}} | ||
{{ | {{MC/El|je|short=1}}: {{MC/Argument|source|entity}} (in <code>entity <target></code> mode) | ||
: Specifies an entity whose NBT is to be used. | : Specifies an entity whose NBT is to be used. | ||
: {{ | : {{MC/Arg desc|je=entity|amount=single|type=entitie}} | ||
{{ | {{MC/El|je|short=1}}: {{MC/Argument|source|resource_location}} (in <code>storage <target></code> mode) | ||
: Specifies a | : Specifies a storage whose NBT is to be used. | ||
: {{ | : {{MC/Arg desc|je=resource_location}} | ||
{{ | {{MC/El|je|short=1}}: {{MC/Argument|path|nbt_path}} | ||
: Specifies the NBT path. Should be a path of a single compound tag. | : Specifies the NBT path. Should be a path of a single compound tag. | ||
: {{ | : {{MC/Arg desc|je=nbt_path}} | ||
== Result == | == Result == | ||
| Line 66: | Line 66: | ||
{{Result table|The tag at <code><path></code> is not a compound tag.}} | {{Result table|The tag at <code><path></code> is not a compound tag.}} | ||
{{Result table|cmd=/... <arguments><br>/... with ..|With the provided arguments, any macro line in the function(s) becomes an unparseable command.}} | {{Result table|cmd=/... <arguments><br>/... with ..|With the provided arguments, any macro line in the function(s) becomes an unparseable command.}} | ||
{{Result table|cmd=|Only one function is specified, and in it there's no {{ | {{Result table|cmd=|Only one function is specified, and in it there's no {{MC/Cmd|return}} command being executed.|[[Minecraft:Commands#Result|Void]]}} | ||
{{Result table|Only one function is specified, and in it there's a {{ | {{Result table|Only one function is specified, and in it there's a {{MC/Cmd|return}} command being executed and returning a failure.|fail}} | ||
{{Result table|Multiple functions are specified, and in all of the functions, there is no {{ | {{Result table|Multiple functions are specified, and in all of the functions, there is no {{MC/Cmd|return}} command being executed.|Void}} | ||
{{Result table|succeed}} | {{Result table|succeed}} | ||
{{Result table}} | {{Result table}} | ||
== Output == | == Output == | ||
{{ | {{MC/In|java}}, for {{MC/Cmd|return run execute store ... run function ...}}: | ||
{{Output table}} | {{MC/Output table}} | ||
{{Output table|edition=je|On fail|N/A|0|0}} | {{MC/Output table|edition=je|On fail|N/A|0|0}} | ||
{{Output table|On void|N/A|unchanged|unchanged}} | {{MC/Output table|On void|N/A|unchanged|unchanged}} | ||
{{Output table|On success|N/A|1|the return value of the first | {{Output table|On success|N/A|1|the return value of the first non-void function<br>(Execution stops after first {{MC/Cmd|link=none|return}} in any of the specified functions)}} | ||
{{Output table}} | {{MC/Output table}} | ||
{{ | {{MC/In|java}}, otherwise: | ||
{{Output table}} | {{MC/Output table}} | ||
{{Output table|edition=je|On fail|0|0|0}} | {{MC/Output table|edition=je|On fail|0|0|0}} | ||
{{Output table|On void|0|unchanged|unchanged}} | {{MC/Output table|On void|0|unchanged|unchanged}} | ||
{{Output table|On success|1|1|the sum of the return values of all the | {{MC/Output table|On success|1|1|the sum of the return values of all the non-void functions}} | ||
{{Output table}} | {{MC/Output table}} | ||
{{ | {{MC/In|be}}: | ||
{{Output table|onlybe=1}} | {{MC/Output table|onlybe=1}} | ||
{{Output table|edition=be|On fail|0}} | {{MC/Output table|edition=be|On fail|0}} | ||
{{Output table|On success|1}} | {{MC/Output table|On success|1}} | ||
{{Output table}} | {{MC/Output table}} | ||
== Examples == | == Examples == | ||
* {{ | * {{MC/In|java}}: | ||
** To run a function located at <code>data/custom/function/example/test.mcfunction</code> in a loaded data pack: {{ | ** To run a function located at <code>data/custom/function/example/test.mcfunction</code> in a loaded data pack: {{MC/Cmd|function custom:example/test|link=none}} | ||
** To run all functions in a function | ** To run all functions in a function tag located at <code>data/custom/tags/function/example/test.json</code>: {{MC/Cmd|function #custom:example/test|link=none}} | ||
** To run a function if there is a sheep within a radius of 2 blocks: <code>/execute if entity @e[type=sheep,distance=..2] run function custom:example/test</code> | ** To run a function if there is a sheep within a radius of 2 blocks: <code>/execute if entity @e[type=sheep,distance=..2] run function custom:example/test</code> | ||
** To run a function unless the executor is a player: <code>/execute unless entity @s[type=player] run function custom:example/test</code> | ** To run a function unless the executor is a player: <code>/execute unless entity @s[type=player] run function custom:example/test</code> | ||
| Line 103: | Line 103: | ||
=== ''Java Edition'' === | === ''Java Edition'' === | ||
{{HistoryTable | {{HistoryTable | ||
|{{HistoryLine|java}} | |{{MC/HistoryLine|java}} | ||
|{{HistoryLine||1.12|dev=pre1|Added {{ | |{{HistoryLine||1.12|dev=pre1|Added {{MC/Cmd|function|link=none}}.}} | ||
|{{HistoryLine|||dev=pre4|Added new arguments to the {{ | |{{HistoryLine|||dev=pre4|Added new arguments to the {{MC/Cmd|function|link=none}} command: <code>[if{{!}}unless] [selector]</code>}} | ||
|{{HistoryLine|||dev=pre6|Skipped functions (when a conditional fails) are now considered failures when used in commands.}} | |{{MC/HistoryLine|||dev=pre6|Skipped functions (when a conditional fails) are now considered failures when used in commands.}} | ||
|{{HistoryLine||1.13|dev=17w49b|{{ | |{{HistoryLine||1.13|dev=17w49b|{{MC/Cmd|function|link=none}} now accept function tags as the argument.|Removed {{cd|[if{{!}}unless]}} arguments in favor of {{cmd|execute [if{{!}}unless]}}.}} | ||
|{{HistoryLine||1.20.2|dev=23w31a|Added function macro arguments.}} | |{{MC/HistoryLine||1.20.2|dev=23w31a|Added function macro arguments.}} | ||
|{{HistoryLine||1.20.3|dev=23w41a|{{ | |{{HistoryLine||1.20.3|dev=23w41a|{{MC/Cmd|link=none|function}} command no longer returns the number of commands executed to better accommodate new {{MC/Cmd|return}} command.}} | ||
|{{HistoryLine|||dev=23w44a|If {{ | |{{HistoryLine|||dev=23w44a|If {{MC/Cmd|link=none|function <''function tag''>}} runs multiple functions in combination with {{MC/Cmd|link=none|return run}}, execution stops after first {{MC/Cmd|link=none|return}} in any of the functions.|A single call to the {{MC/Cmd|link=none|function}} command always returns when run with {{MC/Cmd|link=none|return run}}. For example, {{MC/Cmd|link=none|return run execute ''[split context]'' run function ''<some function with conditional return>''}} always returns after processing the first context.}} | ||
|{{HistoryLine||1.20.5|dev=23w51a|Fixed a bug that <code>/function</code> executing a function with <code>/return fail</code> has a <code>success</code> value of 1.<ref>{{bug|MC-267193|||fixed}}</ref><ref>{{bug|MC-268160}}</ref>}} | |{{HistoryLine||1.20.5|dev=23w51a|Fixed a bug that <code>/function</code> executing a function with <code>/return fail</code> has a <code>success</code> value of 1.<ref>{{bug|MC-267193|||fixed}}</ref><ref>{{bug|MC-268160}}</ref>}} | ||
}} | }} | ||
| Line 116: | Line 116: | ||
=== ''Bedrock Edition'' === | === ''Bedrock Edition'' === | ||
{{HistoryTable | {{HistoryTable | ||
|{{HistoryLine|bedrock}} | |{{MC/HistoryLine|bedrock}} | ||
|{{HistoryLine||1.8.0|dev=beta 1.8.0.8|Added {{ | |{{HistoryLine||1.8.0|dev=beta 1.8.0.8|Added {{MC/Cmd|function|link=none}}.}} | ||
}} | }} | ||
== See also == | == See also == | ||
* {{ | * {{MC/Cmd|tag}} – To run function in batch, every tick, or on server (re)load. | ||
== References == | == References == | ||
{{Reflist}} | {{MC/Reflist}} | ||
== External links == | == External links == | ||
| Line 130: | Line 130: | ||
== Navigation == | == Navigation == | ||
{{Navbox commands}} | {{MC/Navbox commands}} | ||
de:Befehl/function | |||
es:Comandos/function | |||
ja:コマンド/function | |||
ko:명령어/function | |||
pt:Comandos/function | |||
ru:Команды консоли/function | |||
uk:Команди консолі/function | |||
zh:命令/function | |||
Latest revision as of 20:10, 9 April 2026
Runs commands found in the corresponding function file(s) with command execution context (executor, execution position, angles, etc) of the /function command itself.
Usage
in Template:Editions, runs a function or all functions in a tag from a data pack.
in Template:Editions, runs a function from a Minecraft:behavior pack.
in Template:Editions, the behavior of
- REDIRECT Template:Command
Template:Redr (or
- REDIRECT Template:Command
Template:Redr) is different with a direct call to the
- REDIRECT Template:Command
Template:Redr command. If
- REDIRECT Template:Command
Template:Redr runs multiple functions, execution stops after first
- REDIRECT Template:Command
Template:Redr in any of the functions. If a direct call to the
- REDIRECT Template:Command
Template:Redr runs multiple functions, all of the functions are executed, and the return values are added up as the result value of the
- REDIRECT Template:Command
Template:Redr command.
Syntax
- Java Edition
function <name>- Runs a function or functions in a tag.
function <name> <arguments>function <name> with (block <sourcePos>|entity <source>|storage <source>) [<path>]- Runs a function or functions in a tag, with arguments for macros.
- Bedrock Edition
function <name: filepath>- Runs a function.
Arguments
Template:El: <name>: function
Template:El: name: filepath: CommandFilePath
- Specifies the function(s) to execute.
- {{#vardefine:onlyje|Template:IN,}}{{#vardefine:onlybe| Template:IN,}}{{#vardefine:je-arg|function}}{{#vardefine:be-arg|function}}Template:Error{{#vardefine:onlyje|}}{{#vardefine:onlybe|}}{{#vardefine:je-arg|}}{{#vardefine:be-arg|}}
- REDIRECT Template:Edition
Template:Rcat: <arguments>: nbt_compound_tag
- Specifies arguments for macro functions in a compound NBT tag.
- {{#vardefine:onlyje|}}{{#vardefine:onlybe| Template:IN,}}{{#vardefine:je-arg|nbt_compound_tag}}{{#vardefine:be-arg|}}Template:Error{{#vardefine:onlyje|}}{{#vardefine:onlybe|}}{{#vardefine:je-arg|}}{{#vardefine:be-arg|}}
- REDIRECT Template:Edition
Template:Rcat: <sourcePos>: block_pos
- The position of the target Minecraft:block entity whose NBT is to be used.
- {{#vardefine:onlyje|}}{{#vardefine:onlybe| Template:IN,}}{{#vardefine:je-arg|block_pos}}{{#vardefine:be-arg|}}Template:Error{{#vardefine:onlyje|}}{{#vardefine:onlybe|}}{{#vardefine:je-arg|}}{{#vardefine:be-arg|}}
- REDIRECT Template:Edition
Template:Rcat: <source>: entity (in entity <target> mode)
- Specifies an entity whose NBT is to be used.
- {{#vardefine:onlyje|}}{{#vardefine:onlybe| Template:IN,}}{{#vardefine:je-arg|entity}}{{#vardefine:be-arg|}}Template:Error{{#vardefine:onlyje|}}{{#vardefine:onlybe|}}{{#vardefine:je-arg|}}{{#vardefine:be-arg|}}
- REDIRECT Template:Edition
Template:Rcat: <source>: resource_location (in storage <target> mode)
- Specifies a storage whose NBT is to be used.
- {{#vardefine:onlyje|}}{{#vardefine:onlybe| Template:IN,}}{{#vardefine:je-arg|resource_location}}{{#vardefine:be-arg|}}Template:Error{{#vardefine:onlyje|}}{{#vardefine:onlybe|}}{{#vardefine:je-arg|}}{{#vardefine:be-arg|}}
- REDIRECT Template:Edition
Template:Rcat: <path>: nbt_path
- Specifies the NBT path. Should be a path of a single compound tag.
- {{#vardefine:onlyje|}}{{#vardefine:onlybe| Template:IN,}}{{#vardefine:je-arg|nbt_path}}{{#vardefine:be-arg|}}Template:Error{{#vardefine:onlyje|}}{{#vardefine:onlybe|}}{{#vardefine:je-arg|}}{{#vardefine:be-arg|}}
Result
Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table Template:Result table
Output
in Template:Editions, for
- REDIRECT Template:Command
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-table-lines|-2}}{{#vardefine:output-table-lines-now|}}{{#vardefine:command-num-now|}}{{#vardefine:command-line-now|}}{{#vardefine:commands-num|}}{{#vardefine:output-edition-num-now|}}{{#vardefine:output-edition-line-now|}}{{#vardefine:output-editions-num|}}{{#vardefine:je-outputs-dis|}}
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:situation-{{#var:output-table-lines}}|On fail}}{{#vardefine:output1-{{#var:output-table-lines}}|N/A}}{{#vardefine:output2-{{#var:output-table-lines}}|0}}{{#vardefine:output3-{{#var:output-table-lines}}|0}}{{#vardefine:command-row-{{#var:commands-num}}|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-editions-num|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-edition-{{#var:output-editions-num}}|je}}{{#vardefine:output-edition-row-{{#var:output-editions-num}}|1}} {{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:situation-{{#var:output-table-lines}}|On void}}{{#vardefine:output1-{{#var:output-table-lines}}|N/A}}{{#vardefine:output2-{{#var:output-table-lines}}|unchanged}}{{#vardefine:output3-{{#var:output-table-lines}}|unchanged}}{{#vardefine:command-row-{{#var:commands-num}}|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-edition-row-{{#var:output-editions-num}}|Expression error: Unrecognized punctuation character "{".}} Template:Output table
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-table-lines|-2}}{{#vardefine:output-table-lines-now|}}{{#vardefine:command-num-now|}}{{#vardefine:command-line-now|}}{{#vardefine:commands-num|}}{{#vardefine:output-edition-num-now|}}{{#vardefine:output-edition-line-now|}}{{#vardefine:output-editions-num|}}{{#vardefine:je-outputs-dis|}}
in Template:Editions, otherwise:
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-table-lines|-2}}{{#vardefine:output-table-lines-now|}}{{#vardefine:command-num-now|}}{{#vardefine:command-line-now|}}{{#vardefine:commands-num|}}{{#vardefine:output-edition-num-now|}}{{#vardefine:output-edition-line-now|}}{{#vardefine:output-editions-num|}}{{#vardefine:je-outputs-dis|}}
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:situation-{{#var:output-table-lines}}|On fail}}{{#vardefine:output1-{{#var:output-table-lines}}|0}}{{#vardefine:output2-{{#var:output-table-lines}}|0}}{{#vardefine:output3-{{#var:output-table-lines}}|0}}{{#vardefine:command-row-{{#var:commands-num}}|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-editions-num|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-edition-{{#var:output-editions-num}}|je}}{{#vardefine:output-edition-row-{{#var:output-editions-num}}|1}} {{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:situation-{{#var:output-table-lines}}|On void}}{{#vardefine:output1-{{#var:output-table-lines}}|0}}{{#vardefine:output2-{{#var:output-table-lines}}|unchanged}}{{#vardefine:output3-{{#var:output-table-lines}}|unchanged}}{{#vardefine:command-row-{{#var:commands-num}}|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-edition-row-{{#var:output-editions-num}}|Expression error: Unrecognized punctuation character "{".}} {{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:situation-{{#var:output-table-lines}}|On success}}{{#vardefine:output1-{{#var:output-table-lines}}|1}}{{#vardefine:output2-{{#var:output-table-lines}}|1}}{{#vardefine:output3-{{#var:output-table-lines}}|the sum of the return values of all the non-void functions}}{{#vardefine:command-row-{{#var:commands-num}}|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-edition-row-{{#var:output-editions-num}}|Expression error: Unrecognized punctuation character "{".}}
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-table-lines|-2}}{{#vardefine:output-table-lines-now|}}{{#vardefine:command-num-now|}}{{#vardefine:command-line-now|}}{{#vardefine:commands-num|}}{{#vardefine:output-edition-num-now|}}{{#vardefine:output-edition-line-now|}}{{#vardefine:output-editions-num|}}{{#vardefine:je-outputs-dis|}}
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-table-lines|-2}}{{#vardefine:output-table-lines-now|}}{{#vardefine:command-num-now|}}{{#vardefine:command-line-now|}}{{#vardefine:commands-num|}}{{#vardefine:output-edition-num-now|}}{{#vardefine:output-edition-line-now|}}{{#vardefine:output-editions-num|}}{{#vardefine:je-outputs-dis|}}
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:situation-{{#var:output-table-lines}}|On fail}}{{#vardefine:output1-{{#var:output-table-lines}}|0}}{{#vardefine:output2-{{#var:output-table-lines}}|N/A}}{{#vardefine:output3-{{#var:output-table-lines}}|N/A}}{{#vardefine:command-row-{{#var:commands-num}}|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-editions-num|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-edition-{{#var:output-editions-num}}|be}}{{#vardefine:output-edition-row-{{#var:output-editions-num}}|1}} {{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:situation-{{#var:output-table-lines}}|On success}}{{#vardefine:output1-{{#var:output-table-lines}}|1}}{{#vardefine:output2-{{#var:output-table-lines}}|N/A}}{{#vardefine:output3-{{#var:output-table-lines}}|N/A}}{{#vardefine:command-row-{{#var:commands-num}}|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-edition-row-{{#var:output-editions-num}}|Expression error: Unrecognized punctuation character "{".}}
{{#vardefine:output-table-lines|Expression error: Unrecognized punctuation character "{".}}{{#vardefine:output-table-lines|-2}}{{#vardefine:output-table-lines-now|}}{{#vardefine:command-num-now|}}{{#vardefine:command-line-now|}}{{#vardefine:commands-num|}}{{#vardefine:output-edition-num-now|}}{{#vardefine:output-edition-line-now|}}{{#vardefine:output-editions-num|}}{{#vardefine:je-outputs-dis|}}
Examples
- in Template:Editions:
- To run a function located at
data/custom/function/example/test.mcfunctionin a loaded data pack:
- To run a function located at
- REDIRECT Template:Command
- To run all functions in a function tag located at
data/custom/tags/function/example/test.json:
- To run all functions in a function tag located at
- REDIRECT Template:Command
- To run a function if there is a sheep within a radius of 2 blocks:
/execute if entity @e[type=sheep,distance=..2] run function custom:example/test - To run a function unless the executor is a player:
/execute unless entity @s[type=player] run function custom:example/test - To run a function macro with arguments a=42, b="example":
/function custom:example/test {a: 42, b: "example"} - To run a function macro with arguments from storage
custom:storage:/function custom:example/test with custom:storage
- To run a function if there is a sheep within a radius of 2 blocks:
History
Java Edition
Bedrock Edition
See also
- REDIRECT Template:Command
Template:Redr – To run function in batch, every tick, or on server (re)load.
References
<references group="">
</references>
External links
de:Befehl/function es:Comandos/function ja:コマンド/function ko:명령어/function pt:Comandos/function ru:Команды консоли/function uk:Команди консолі/function zh:命令/function