Minecraft Blogs / Tutorial

Function Data Packs for Dummies #1 - #5 | Quickstart: Create your first datapack!

  • 11,632 views, 17 today
  • 56
  • 40
  • 15
Bertiecrafter avatar Bertiecrafter
Site Moderator
Level 67 : High Grandmaster Stack
624
This series of tutorials will teach you how to create your very own data packs, assuming you know absolutely nothing about commands. Although data packs could just contain files that replace built-in assets like resource packs, these posts will focus on adding new features and mechanics to the game. In every part I assume you've read the previous parts.

This post flies through the first five blog posts at an incredibly high speed. If you know your way around computers, JSON, common minecraft commands and want to learn by example, this post is for you. If you prefer a more guided tutorial or get curious about the detailed reasoning behind these instructions, refer to the individual parts, starting here.

Let's get started with a datapack that allows us to quickly switch between creative and spectator!


But first, what are functions, tags and data packs?

By typing a command in chat, you're giving minecraft direct instructions to do something for you. Functions are files that consist of a bunch of commands. Tags are groups that can consist of functions, blocks, entities, items and fluids. They are represented by files and any datapack can extend a tag defined elsewhere. Data packs are bundles of functions, but also loot tables, recipes and structures. Players can simply drop a data pack into their world and start using it, they won't have to mess with the individual files within the data pack.

Data packs are not mods or plug-ins. They are a bit easier to create, but are more limited.

Getting an editor and showing the logs

Download Notepad++, which is an editor that gives suggestions based on previously typed words. Very useful for referencing things you've created earlier by name in later tutorials. In the Minecraft Launcher, click Settings and tick the checkbox that says "Open output log when Minecraft: Java Edition starts". If you now launch the game, you see a second window that will list errors in function files which helps during debugging (the process of fixing bugs). Those are the same errors you would otherwise see in chat if you were to type in a broken command like "/gamemode epic".

Creating a data pack

Open the datapacks folder of a world by using the Minecraft client (Edit > Open World Folder > datapacks folder) or navigate to %APPDATA%/.minecraft/saves/<your world>/datapacks. Create a new folder and call it "Creative Utilities". Go inside the folder and create a pack.mcmeta file.

{
"pack": {
"pack_format": 7,
"description": "put some text here"
}
}

Now run the /reload command in your minecraft world followed by /datapack list. Your first datapack should show up, hover over the entry of the data pack to see the description you specified. The description can be any Minecraft JSON text component and visit this page for the latest pack_format number.

During this tutorial we'll look at multiple other JSON files with references to wiki pages. These external pages use icons to indicate the JSON type, use this page for information on the meaning behind these icons.

Creating your first function

Create a folder named "data" and create another folder inside. It's name should be something you own, like your username. The folder represents your namespace and should be globally unique, which is only possible by using a name you own. Note that you must replace "<namespace>" with the name of this folder in any of the snippets below.

Create a folder named "functions" inside that, with another folder inside. This last folder should be used to uniquely identify your functions across all data packs created by you. An abbreviation of the data pack name should be enough, so call it "crea-util". Now create a toggle-gamemode.mcfunction file with the following contents:
tag @s[gamemode=spectator] add tut_cu_go_creative
gamemode spectator @s[gamemode=creative]
gamemode creative @s[tag=tut_cu_go_creative]
tag @s[tag=tut_cu_go_creative] remove tut_cu_go_creativef

It should be located at:
.minecraft/saves/<world_name>/datapacks/Creative Utilities/data/<namespace>/functions/crea-util/toggle-gamemode.mcfunction

At the first line, the player is given a tag if in spectator mode. If we were to change the gamemode immediately, it would be changed back right away on the second line. The second and third lines swap gamemodes and the fourth line cleans up the mess we made on the first line.

Now run /reload followed by /function <namespace>:crea-util/toggle-gamemode. As long as you were in creative or spectator, it should have toggled your gamemode.

Hooking into Minecraft

There are two built-in tags: minecraft:load and minecraft:tick. By registering our function into those tags, we can have minecraft automatically run our commands instead of making the players type /function commands. The functions inside the minecraft:tick tag run 20 times a second.

Go back to the data folder. Alongside your namespace folder, create a new folder named "minecraft". Create a folder named "tags" inside that, with another folder named "functions" inside. Now create a load.json file:
{
"values": [
"<namespace>:crea-util/load"
]
}
and a tick.json file:
{
"values": [
"<namespace>:crea-util/tick"
]
}
The file names match the minecraft:load and minecraft:tick tags. The listed values inside match function files we still have to create. The exact JSON structure can be found here. Go back to the data folder again and into your <namespace>/functions/crea-util folder. Create a new load.mcfunction file with these contents:tellraw @a {"text":"Hello World!","color":"yellow","extra":[{"text":"\nThe Creative Utilities datapack is created by <your username>","color":"aqua"},{"text":"\n Run /function <namespace>:crea-util/uninstall to uninstall.","color":"gray"}]}
Usually you use the load function to setup your datapack. The tellraw command uses JSON to describe text, see this page for the full text component structure. Then create another file named tick.mcfunction with these contents:
execute as @a[x_rotation=-90] at @s run function <namespace>:crea-util/toggle-gamemode
execute as @a[x_rotation=-90] at @s run tp @s ~ ~ ~ ~ 0
The first line calls the toggle-gamemode function when you look up, the second line rotates you back to a horizontal position. Now run the /reload command. A message should be printed to chat and because the commands above are run every tick, you should be able to look up any time to switch gamemodes.

Lastly, create a uninstall.mcfunction file with the following contents:
tellraw @a {"text":"Successfully uninstalled Creative Utilities!","color":"aqua"}
datapack disable "file/Creative Utilities"
datapack disable "file/Creative Utilities.zip"
Although uninstall functions aren't an official thing, I recommend creating them to clean up any mess your datapack made in the load function. If you do provide an uninstall function, make sure to tell the players on load that they should use a /function command to do an uninstall before removing the data pack.

Challenge Yourself

If you followed this tutorial without looking at the file contents, you probably have no idea what the commands do or why it works. Don't worry though, that's the nature of a quickstart guide. If you know a little about commands, maybe you can try adding extra features to the datapack. Regardless, try to understand what everything in the data pack means. Play around and see how modifications affect its behaviour. Maybe even search for information on parts you don't understand.

Awesome! What's next?

If this quickstart guide left you with a lot of questions, feel free to have a look at the five individual parts, starting here. If you're ready to continue, have a look at the Quality Control section that I left out in part 4. Part 6 will be about target selectors, which are the components starting with @ in the commands above and allow you to dynamically select players and other entities. Examples of relative coordinates are the squigly lines (~) in the tick function.

Subscribe if you want to get notified of new posts.

Function Data Packs for Dummies #1 - #5 | Quickstart: Create your first datapack!
Function Data Packs for Dummies #1 - #5 | Quickstart: Create your first datapack!
Tags

2 Update Logs

Added a section about a recommended editor and opening the log window : by Bertiecrafter 07/15/2020 12:00:50 pmJul 15th, 2020

See title.
This is also to match the new information in blog #3.
LOAD MORE LOGS

Create an account or sign in to comment.

1
02/03/2022 1:57 pm
Level 26 : Expert Pokemon
SprainedSpark89
SprainedSpark89 avatar
is the data pack json and minecraft json do the same or no?
1
02/03/2022 2:08 pm
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
Eh, I don't really understand your question. Each file in this tutorial has it's own purpose. Read the individual parts (From part 1 to part 5) if you don't understand this and would like to read everything in more detail.
1
02/03/2022 3:08 pm
Level 26 : Expert Pokemon
SprainedSpark89
SprainedSpark89 avatar
what i mean is if you go to the version folder then click on a version folder there is 2 files 1 is jar and the other is json the jar is where the assets, textures, and the other jsons are stored but is the JSON in the folder do almost the same thing as the data pack json?
1
02/04/2022 1:40 am
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
No, I don't think so. The .json in the versions folde seems to specify at which URL certain assets can be found. The .json in the root of the data pack simply describes the data pack (version + description).
1
08/06/2020 4:21 pm
Level 1 : New Miner
hardcoremc007
hardcoremc007 avatar
how do I reinstall after uninstalling
3
08/06/2020 5:23 pmhistory
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
There is no actual uninstalling going on. An uninstall function is implemented by the creator of the datapack themselves, cleaning up anything that's created in the load or other functions. For example, I always remove scoreboard objectives and run a /datapack disable command to deactivate the data pack. To "reinstall", the user can simply do /reload to fire the load function again, which should create everything needed for the other functions. If you add the /datapack disable command to the uninstall function, the user would have to /datapack enable the pack first, before doing /reload.

So in short, all functions are completely implemented by you. They do nothing you don't tell them to do. So do whatever you want in the uninstall function, but it will most likely consist of cleaning stuff up. How to "reinstall" will depend on what you did during the uninstall.
4
08/06/2020 6:55 pmhistory
Level 1 : New Miner
hardcoremc007
hardcoremc007 avatar
ooh
thanks for the tutorials and I'm sorry for taking up so much of your time today
1
07/09/2020 3:07 am
Level 24 : Expert Pixel Puncher
Cybear_Tron
Cybear_Tron avatar
PLZ TELL ANY OTHER WAY FOR TGGLE
3
07/09/2020 4:06 amhistory
Level 67 : High Grandmaster Stack
Bertiecrafter
Bertiecrafter avatar
The 4 commands needed to toggle are the bare minimum. If it only had two commands, the second one would set the game mode of players who already switched. I guess you could use 3 commands if you did this:
Creative -> Adventure
Spectator -> Creative
Adventure -> Spectator
But there is no benefit to that.

Now for triggering, you could use anything. There are several target selector arguments (we used x_rotation here), you could just have the user write out the /function command or you can use the scoreboard to detect certain behaviour or the /trigger command. Refer to the blog posts about the scoreboard for more info.
2
07/09/2020 11:31 pm
Level 24 : Expert Pixel Puncher
Cybear_Tron
Cybear_Tron avatar
I changed the code and set it as that when a person in spectator will look downwards he/she will be in creative, thnx :)
Planet Minecraft

Website

© 2010 - 2022
www.planetminecraft.com

Welcome