Minecraft Blogs / Tutorial

Function Data Packs for Dummies #3 | Creating a new data pack and your first function

  • 5,882 views, 61 today
  • 44
  • 15
  • 24
Bertiecrafter avatar Bertiecrafter
Level 61 : High Grandmaster Cyborg
337
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.

Getting an editor

Although the default text editor that comes with your computer or laptop can be used to create data packs, I recommend a more professional editor like Notepad++. Although it won't give you any smart suggestions or auto complete, it still provides you with suggestions based on the words you typed earlier. This can come in handy later on in the tutorial series, when you need to refer to something that you have created earlier by name. Visual Studio Code is an editor often used by programmers, which is also fine if you already have it, but a little bit much if you plan on using it just for Minecraft data packs. This editor does have extensions that claim to have smart suggestions, but they are not at a stage that makes them worth downloading yet.

A new world, a new data pack

Open up Minecraft and create a world if you want to. Click a world, then click "Edit" and "Open World Folder". Go back to the single player menu and join the world. The folder containing the world should have opened up. Go into "datapacks" and create a new folder. The name should represent what you're going to create. Next up we're going to create the pack.mcmeta file. It turns our now empty folder into a valid data pack and gives Minecraft some other information.

​For Windows Users
It's important to turn on file extensions if you are on Windows. They tell programs how files should be read. Other operating systems read the file content to know what to do with them and therefore don't hide the extensions for safety. In order to make file extensions visible, go to the View tab in the files/folders window (it's called "the Explorer") and check "File name extensions" in the "Show/Hide" category. You should see a dot followed by a couple of characters appear behind every file. These characters represent the extension.

Create a file called pack.mcmeta in the folder you've just created. You can do this by opening up a text editor, like Notepad and then clicking "File" > "Save as...". You must make sure to select "All Files" in the save as drop-down menu below the text box for the file name. If you do not do this, the file will be saved as pack.mcmeta.txt instead of simply pack.mcmeta. Another way is to right click the empty folder > New > Text Document and change the name (including extension) right away. I recommend the second way, because you would spot it faster if your extension is wrong.
For Mac/Linux Users
Start by opening a text editor like TextEdit. If you're using this application, make sure to go to Format > Make Plain Text every time you create a new file. Then click "File" > "Save as..." and after entering the full file name (e.g. "pack.mcmeta"), uncheck the box that says "If no extension is provided, use ".txt"". Another way is to right click the empty folder > New Text File and change the name right away. I recommend the second way, because you would spot it faster if your extension is wrong. The "extension" is the part behind the last dot in the file name. In this case it's "mcmeta".

Use right-click and "Get Info" to view the full file name. If it says "pack.mcmeta.txt", it's wrong.

Now fill that file with the JSON below. Any content between braces should be indented with 4 extra spaces if you want to format it nicely.

File Contents

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

The fun thing is that the description key has the same JSON structure as the tellraw command, so with knowledge from the previous tutorial you can do something really fancy there if you want to. You should also know already that a simple string is permitted as well. The text is only displayed if you hover over the datapack entry in a command we'll get to soon, so don't spend much time on it.

The pack_format number is 5 for 1.15.2, have a look here for the latest pack_format number. Alternatively, you can copy the value from the pack.mcmeta file in .minecraft/versions/<version>/<version>.jar (open with 7-Zip, WinRAR or other zip software).

Now go back to your world and do "/reload" followed by "/datapack list". You should see your folder in the list, in green. If it's not in the list, your pack.mcmeta has a mistake. If it's in the list, but red, use "/datapack enable file/<folder name>" to enable it. If you hover over it, it should show the description you specified.

Your first function

Functions are awesome. They allow you to turn thoughts into magic and you can look cool by calling your "list of instructions" an "ALGORITHM".

In order for every data pack to be combined with any other data pack, every function must be unique in the whole wide world. Sounds impossible, but we can try. Start with creating a "data" folder alongside the pack.mcmeta file. Then create a folder in the "data" folder and give it a name that you own. An easy one is your Minecraft username, but feel free to use a bought domain name. This folder will become your "namespace". Programmers have namespaces too and they make sure they are unique in the very same way.

Now create a "functions" folder inside your namespace folder. By using a namespace you own, you can prevent data packs from other users colliding with yours. However, it doesn't prevent one of your data packs from colliding with another one of yours. In order to avoid this, we're not going to create a function file right away. Create a folder in the "functions" folder first and give it a name that should distinguish this pack from others. Usually a shortened version (no spaces) of the data pack name should be enough.

Now we can create our first function file. Make sure that it has "mcfunction" as extension. You can do it the same way you created the "mcmeta" file before. The path to the file should look like this:
.minecraft/saves/<world_name>/datapacks/<data_pack_name>/data/<namespace>/functions/<datapackname>/<function_name>.mcfunction

Just like a real programmer, it's tradition to output "Hello world!" as the first thing you do in every new language you learn. Remember, function files are just lists of commands, so at the first line you can write your say or tellraw command right away. Make sure to omit the leading /. Any lines starting with # are ignored and can be used for comments. Once you got your command in place, you can simply do /reload and then /function <namespace>:<datapackname>/<functionname> (Don't put ".mcfunction" at the end of it)

If you see "Hello World!" appear in chat, it's a success! Keep in mind that I won't tell you do a /reload after every data pack edit in the next tutorials anymore, you'll have to remember this yourself.

Challenge Yourself

After every tutorial, I'll include a section where you are challenged to apply what you've learned. I recommend you playing with what you've learned, it helps you getting familiar with new concepts and be able to find solutions to problems. You don't have to do exactly what's written below, you can always challenge yourself in a different way.

Create a mob challenge that admins could do for times when there are no hackers to catch. It should /clear the inventory, /give the player armor + a weapon and spawn like 10-20 zombies or skeletons. You can do this last part by simply doing "/summon zombie" or "/summon skeleton" a bunch of times. It's okay to put a player name in the function file for now. You'll learn how to dynamically select players in another tutorial.

What's next?

Next up we're going to look at how to quickly fix problems as well as making sure that your data pack is of a high quality.
Subscribe if you want to get notified of new posts.

Function Data Packs for Dummies #3 | Creating a new data pack and your first function
Function Data Packs for Dummies #3 | Creating a new data pack and your first function
Tags

4 Update Logs

Update #4 : 05/15/2021 6:38:03 pmMay 15th

Talked to djmushyjp for a bit to discover a couple difficulties for Mac users that I didn't know about as a Windows user. I modified the text in the "For Mac/Linux Users" spoiler to hopefully fix these issues.
LOAD MORE LOGS

Create an account or sign in to comment.

1
04/29/2021 1:09 pm
Level 1 : New Miner
djmushyjp
djmushyjp avatar
please help me, I have been at this for some hours, I don't see anything wrong, I have gone over the post multiple times ad looked at the comments and have no idea what is wrong, I didn't even get past /datapacks list. it never showed up. the extension is fine, the code is fine. the version number is fine, there is nothing wrong with mc it self I don't think. I'm stuck and with no way out.
1
05/15/2021 6:38 pm
Level 61 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
Fixed after some messaging :P (See the new contents of the "For Mac/Linux Users" spoiler)
1
04/29/2021 1:16 pmhistory
Level 61 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
Hey! No worries, just join my discord (I'll add a button/link at the bottom of the submission above), find me and send me what you've got. I'll have a look :P
1
01/10/2021 12:57 pm
Level 31 : Artisan Pixel Painter
Dunk__
Dunk__ avatar
Need help! I followed this page and when I ran the command it sead unknown function. what sold I do?
2
01/10/2021 4:29 pm
Level 61 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
Look at the logs (see last paragraph on how to enable them) and see if it tells you in red if you wrote something wrong (usually in the first few lines of the error message). Also make sure you did /reload and check the tab auto-complete on /function, perhaps you misspelled it.
1
01/02/2021 9:42 amhistory
Level 8 : Apprentice Miner
DaveBob
DaveBob avatar
Need help! So I created a datapack and followed everything on this page. I created 2 MCFUNCTION files: "test" and "fight". "fight" would trigger the event listed in the challenge and "test" would run "tellraw @e "Hello world!". Now here's the weird thing: Minecraft autocompletes the "fight" function and it runs just fine. However upon attempting to run "test", the game would dismiss it as an unknown function even though it's in the same folder and the same file type as "fight". Any idea as to why this file apparently doesn't exist according to the game?
1
01/02/2021 10:25 am
Level 8 : Apprentice Miner
DaveBob
DaveBob avatar
Nevermind, it was because I targeted @e instead of @a. Good tutorial though!
2
01/02/2021 11:53 am
Level 61 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
Glad you figured it out! Make sure you have the log window opened up (see last section), it would probably tell you that /tellraw can only target players or something.
2
10/01/2020 7:45 am
Level 1 : New Explorer
Adityaku
Adityaku avatar
Thanks, you are best, I gave you Diamond.
2
10/01/2020 7:44 amhistory
Level 1 : New Explorer
Adityaku
Adityaku avatar
okay, I did a mistake, that's why I was not able to create more than one function. luckily I solved it
3
10/01/2020 3:16 pmhistory
Level 61 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
You should reply to your previous comment if you want to add anything to it. It reads nicer and it keeps messages grouped =)
Glad you managed to get it to work!
2
10/01/2020 6:54 am
Level 1 : New Explorer
Adityaku
Adityaku avatar
how to create more than one functions?
for example, one function will give you stone sword and another function will give iron sword?
2
10/01/2020 6:09 am
Level 1 : New Explorer
Adityaku
Adityaku avatar
I tried it, it really works 👍🏼
2
10/01/2020 5:03 am
Level 1 : New Explorer
Adityaku
Adityaku avatar
nice, but a bit HARD
1
05/07/2020 5:09 pm
Level 43 : Master Theorist
Chimerabot
Chimerabot avatar
I have the right path to the file, an (almost definitely) unique namespace, and the correct JSON in the function, but the command still claims that it "Expected whitespace to end one argument, but found trailing data". What do I do?
3
05/07/2020 5:32 pm
Level 61 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
That means you didn't enter the correct amount of arguments. Search the minecraft wiki for the right syntax or try tab autocomplete. Maybe you forgot quotes around a value with a space in it, making it appear like multiple arguments.
2
05/07/2020 5:15 pm
Level 43 : Master Theorist
Chimerabot
Chimerabot avatar
aagh
7
05/07/2020 10:48 pm
Level 43 : Master Theorist
Chimerabot
Chimerabot avatar
I found a VERY important detail you may want to add: all folders, the namespace, the function name, etc. cannot contain any capital letters. I have no idea why, but the /function command won't work without it. I found it on Mojang's bug page here.
3
06/14/2020 11:12 pm
Level 1 : New Miner
Rhaast77
Rhaast77 avatar
You saved my life, i've been trying to understand what was going wrong for hours.
1
06/17/2020 10:21 pm
Level 43 : Master Theorist
Chimerabot
Chimerabot avatar
Same thing happened to me.
1
04/24/2020 12:13 am
Level 1 : New Explorer
Mincecraf
Mincecraf avatar
i cant find the pack.mcmeta what do i do
1
04/24/2020 12:26 am
Level 1 : New Explorer
Mincecraf
Mincecraf avatar
Nevermind I got it
2
04/24/2020 3:04 am
Level 61 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
For future readers, you'll have to create the file yourself. The file contents are in a spoiler in this blog post.
3
01/20/2020 12:36 amhistory
Level 61 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
Thank you MasterKiloRen99, Mr_Nobody1166, Luracasmus, qo101412, daitallica, PMC, Wundercroft, FeedFrex, Chimerabot, Mincecraf, neanterrtall, iZezzin, bisgusting, Rogue64, Nattehine, Halim006, PixieMax, sanonasu, Atolite, ShinyDragon96, cam_reno, TofuChild36, EpicNecromancer, Adityaku, User3271279G, Aysim, Yeetabix420, Vellaris, foxisgamer1212, Bozzi, Flashbulb, Cybear_Tron, MegaMushroomJack, jeanluccy, A_Bread_Boii, RoBoo1194, Stevek11, Drew1, halfdeer1, MrHiglin, Bozzl, SUPERIONtheKnight, BlazingObsidian and MLG_Sand for the diamonds!
Planet Minecraft

Website

© 2010 - 2021
www.planetminecraft.com

Welcome