Functions allow players to run lists of commands using text files with the extension .mcfunction. It is recommended to use UTF-8 encoding (without BOM) for function files to prevent any problems.
Usage
To utilize functions, the text files must first be placed into a top-level folder within [WORLD_NAME]/data/functions[upcoming]. For single player, the world folders can be found in .minecraft/saves. For multiplayer, functions must be placed in every world folder that wants to utilize them. The name of the new top-level folder will be the namespace used when running the functions inside. Functions can also be placed into subfolders within the top-level folder.
For example, running the function custom:example/test will refer to the file located at functions/custom/example/test.mcfunction. If the namespace is left out when trying to call a function, it will default to the minecraft: namespace. Using a custom namespace is recommended in order to prevent unintended behavior in the case of future additions to the default namespace.[1]
Functions, being text files, are easily modifiable and less likely than command blocks to induce lag when used in large quantities. If a function is modified or added, using the /reload command will reload the function files from disk. This allows Minecraft to recognize any changes to the function files, without the need to quit and re-enter the world.
Upon successfully running a function, a message will display in the chat: Executed [amount] command(s) from function '[function file directory]'. The successful output of the commands inside a function cannot be measured with a comparator (although the same effect could be accomplished with the use of /stats and /scoreboard commands).
Function syntax
Testing the function system. Three /tellraw messages and one /give command were used in this simple function.
Within the .mcfunction file, one valid command is placed per line, without the usual forward slash (/). Players can add comments within the function text file by preceding them with a #.
Individual commands in functions can be longer than the 32,500 character limit in command blocks but the total number of commands run inside a function will still obey /gamerule maxCommandChainLength, which is 65,536 commands by default; any commands beyond this limit will be ignored.
Running a function
Functions will run all of their commands in a single tick and functions called from within other functions will also run their commands in the same tick as their parent.
Position changes that happen within a function will not affect the relative coordinates used in other commands within that function (or within its child functions) until the next iteration, regardless of the order of commands. The /execute command circumvents this.
For example:
tp @s ~ ~5 ~ setblock ~ ~-1 ~ emerald_block execute @s ~ ~ ~ setblock ~ ~-1 ~ diamond_block
When called through a player this function will teleport that player 5 blocks up, place an emerald block one block below their original position before the teleport, and place a diamond block one block below their new position after the teleport. This behavior does not affect position arguments within selectors, which will always test for the current position at the time of execution.
There are several methods of running a function file in-game:
Commands
- Allows players to run a function once.
- The commands in the function are run through the entity or command block that ran the
/functioncommand. - The optional arguments
ifandunlesswill make the function only run when theselectorwas found or not found in the world respectively. - Usage:
/function <name>
- Runs a function every tick.
- The commands in the function are run through the server.
- The coordinates from which the "server" executes commands are (0, 0, 0).
- If multiple functions need to be looped, it is possible to insert function commands into this looped function file, put function commands in those functions, etc.
- Usage:
/gamerule gameLoopFunction <namespace:path/to/function/file> - To stop the loop just point gameLoopFunction to a non-existing function:
/gamerule gameLoopFunction -, this value is also the default.
Advancements
Advancements can run a function once as a reward for completing them. The commands in the function are run through the player who completed the advancement.
Reward functions are called within advancement JSON files using the following format:
{
"rewards": {
"function": "namespace:path/to/function/file"
}
}
Tags
Functions can be grouped together using tags in data packs. These tags can then be called to run all the functions inside that tag with /function #(namespace):tag.
Functions tagged in minecraft:tick will automatically run every tick at the beginning of the tick. Functions tagged in minecraft:load will run after (re)loading the datapack.
History
| release | |||||
|---|---|---|---|---|---|
1.12{{Extension DPL}}<ul><li>[[Obsidian Boat|Obsidian Boat]]<br/>{{for|other uses|Obsidian (disambiguation)}} {{for|other kinds of boats|Boat (disambiguation)}}
{{Joke feature}}
{{ItemEntity
|title=Obsidian Boat
|image=Obsidian Boat.png
|image2=Obsidian Boat (item).png
|stackable=No
|renewable=Yes
}}
The '''obsidian boat''' was a joke [[boat]] variant released in an April Fools' joke version.
== Obtaining ==
=== Crafting ===
{{crafting
|A2 = Obsidian |C2 = Obsidian
|A3 = Obsidian |B3 = Obsidian |C3 = Obsidian
|Output= Obsidian Boat
|ignoreusage=1
}}
== Usage ==
Obsidian boats sank in [[water]] instead of floating. They also sank in lava like other boats but did not get destroyed in it.
== Sounds ==
Obsidian boats used the Friendly Creatures sound category for entity-dependent sound events, although did not have any sounds of their own.
== Data values ==
=== ID ===
{{ID table
|shownumericids=y
|showforms=y
|generatetranslationkeys=java
|displayname=Obsidian Boat
|spritetype=item
|nameid=obsidian_boat
|id=432
|form=item
|translationkey=item.obsidian_boat.name
|foot=1}}
{{ID table
|shownumericids=y
|generatetranslationkeys=java
|displayname=Obsidian Boat
|spritetype=entity
|nameid=boat
|id=41
|translationkey=entity.Boat.name
|foot=1}}
=== Entity data ===
{{/ED}}
== History ==
{{History|java}}
{{History||1.10|snap=15w14a|link=Java Edition 15w14a|[[File:Obsidian_Boat_(item).png|32px]] [[File:Obsidian Boat.png|32px]] Added the obsidian boat.}}
{{History|foot}}
== Issues ==
Obsidian boat is a joke feature and as such issues relating to them cannot be fixed.
{{Items}}
{{Entities}}
{{Jokes}}
[[Category:Joke items]]
[[Category:Joke entities]]
[[it:Barca di ossidiana]]
[[ja:黒曜石のボート]]
[[pt:Bote de obsidiana]]
[[uk:Обсидіановий човен]]
[[zh:15w14a]]</li><li>[[:Category:Dyes|Category:Dyes]]<br/>These are items that can be used to dye sheep, wool blocks, or combined to make other dyes.
[[Category:Items]]
[[es:Categoría:Tintes]]
[[fr:Catégorie:Teinture]]
[[hu:Kategória:Festékek]]
[[zh:Category:染料]]</li></ul> | pre1 | Added functions. | |||
| pre3 | Commands are no longer allowed to begin with a / (forward slash) | ||||
| Comments can now only be preceded with #; using // is no longer allowed | |||||
| pre4 | Added new arguments to the /function command: [if|unless] [selector] | ||||
| pre6 | Skipped functions (when a conditional fails) are now considered failures when used in commands. | ||||
| upcoming | |||||
1.13{{Extension DPL}}<ul><li>[[:Category:Planned items|Category:Planned items]]<br/>All pages relating to items that have been officially announced, but are not yet in the game.
__EXPECTUNUSEDCATEGORY__
[[Category:Items]]
[[Category:Planned]]
[[pt:Itens planejados]]</li><li>[[Le Tricolore|Le Tricolore]]<br/>{{Exclusive|Java}}
{{Joke feature}}
{{Item
| image =
| renewable = Yes
| stackable = Yes (64)
}}
'''Le Tricolore''' (French for ''The Tricolour'') is a joke item from [[Java Edition 23w13a_or_b]].
== Obtaining ==
One le tricolore and one [[la baguette]] is given to all players when [[French mode]] gets activated. Deactivating the mode will remove le tricolore from the player's inventory (but not la baguette; [[container]]s such as [[chest]]s are not affected).
Le Tricolore is not available in the creative inventory, but can be obtained by using the {{cmd|give}} command.
== Data values ==
=== ID ===
{{ID table
|showforms=y
|generatetranslationkeys=java
|displayname=Le Tricolore
|spritetype=item
|nameid=le_tricolore
|form=item
|foot=1}}
== History ==
{{History|java}}
{{History||23w13a or b|[[File:Le Tricolore.png|32px]] Added Le Tricolore.}}
{{History|foot}}
{{Items}}
{{Jokes}}
[[Category:Joke items]]
[[ja:Le Tricolore]]
[[pt:Le Tricolore]]</li></ul> | 17w43a | Custom functions have been moved into data packs. | |||
| 17w45a | Functions are now completely parsed and cached on load. | ||||
| 17w49b | Function can now be tagged. | ||||
Functions tagged in minecraft:tick now run every tick in the beginning of the tick. | |||||
Issues
Issues relating to "Function (Java Edition)" are maintained on the bug tracker. Report issues there.
References
- ↑ "Until vanilla adds something with the same name, then it won't work for you! Or if someone else has to work with your stuff, then confusing!" – @Dinnerbone (Nathan Adams) on X, June 21, 2017