775
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.
The tags we're going to be looking at are: "minecraft:tick" and "minecraft:load". The first set of functions is executed after every tick (20 times per second) and the second set of functions is executed after every /reload.
Let's start by creating a function named "tick" and a function named "load". Technically these functions could have any name, but it's easy to spot them in a larger list of functions if you call them that way. Their contents can simply be /say, /tellraw or /title commands. Test if they output something by manually running "/function <namespace>:path/to/function/tick" and one similar for "load" in the minecraft chat.
Now open up the "data" folder of your datapack. Create a new folder named "minecraft", with a "tags" folder inside. This last folder has another folder called "functions" inside. As you could have guessed, the "minecraft" folder causes the tags to be in the minecraft namespace (the part before the colon in the tag names above). Now create a "load.json". Use this format for it's contents. You should know how to write JSON by now. Make sure to include the namespaced id of the function in the values array. The "namespaced id" is the function namespace and path to the load function, the same piece of text you would put into the /function command. Now run /reload and check if the load function you made is fired.
Create a tick.json file in the same folder as load.json. The contents are roughly the same, but in the new tick.json file you should be referring to the tick function. After reloading, see if chat receives output 20 times a second. You might not want chat output 20 times a second forever, so you might want to clear the contents of the tick function after.
Absolutely! Just put the following command anywhere in your data pack.
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.
This data pack will make time run backwards. Make sure that the data pack freezes time using the /gamerule command and displays a nice coloured message in chat saying that the data pack is loaded with a little bit of credit to you, using the minecraft:load tag. Also print the function that should be called if the player would want to uninstall the data pack. Inside the uninstall function, undo the changes applied to the gamerule. You don't have to take the previous value into account, just make time tick on uninstall regardless of what the setting was before. Every tick, use the "/time add" command to add just below 1 day every tick. This creates the illusion that you are removing some value from time, even though there is no "/time remove" command. The same trick is used to make these water droplets fall upwards!
Subscribe if you want to get notified of new posts.
Getting (even more) hooked into Minecraft
Last time you learned how to write a function, but you were probably wondering why all those datapacks are able to do so much magic without ever having to type a single /function command. Data packs hook into Minecraft game events to create their own game cycle and call functions based on certain conditions. These hooks follow a simple mechanism: Call all functions in certain tags and allow any data pack creator to register a function in those tags. Minecraft doesn't know what functions will be in the tag during development and you don't know which tags are called at what times unless documented.The tags we're going to be looking at are: "minecraft:tick" and "minecraft:load". The first set of functions is executed after every tick (20 times per second) and the second set of functions is executed after every /reload.
Let's start by creating a function named "tick" and a function named "load". Technically these functions could have any name, but it's easy to spot them in a larger list of functions if you call them that way. Their contents can simply be /say, /tellraw or /title commands. Test if they output something by manually running "/function <namespace>:path/to/function/tick" and one similar for "load" in the minecraft chat.
Now open up the "data" folder of your datapack. Create a new folder named "minecraft", with a "tags" folder inside. This last folder has another folder called "functions" inside. As you could have guessed, the "minecraft" folder causes the tags to be in the minecraft namespace (the part before the colon in the tag names above). Now create a "load.json". Use this format for it's contents. You should know how to write JSON by now. Make sure to include the namespaced id of the function in the values array. The "namespaced id" is the function namespace and path to the load function, the same piece of text you would put into the /function command. Now run /reload and check if the load function you made is fired.
Create a tick.json file in the same folder as load.json. The contents are roughly the same, but in the new tick.json file you should be referring to the tick function. After reloading, see if chat receives output 20 times a second. You might not want chat output 20 times a second forever, so you might want to clear the contents of the tick function after.
What about the uninstall function?
There is no official uninstall tag. However, it's best to provide a function to clean up any mess that the load command makes. For example the function could remove any scoreboard objectives or reset gamerules. Make sure to inform the player on load that this function exists.
Can I create my own tags?
Absolutely! Just put the following command anywhere in your data pack./function #<namespace>:<path-to-function>
For example, you can run /function #minecraft:load to call all load functions. Anyone using your data pack to extend theirs could register a function in your custom tag using the method described above. Note that you only have to write JSON for registering functions in tags, you don't have to define any new tags in your data pack using some kind of JSON file. Using function tags is one way of creating an API, a set of connection points that other data pack developers can hook into.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.
This data pack will make time run backwards. Make sure that the data pack freezes time using the /gamerule command and displays a nice coloured message in chat saying that the data pack is loaded with a little bit of credit to you, using the minecraft:load tag. Also print the function that should be called if the player would want to uninstall the data pack. Inside the uninstall function, undo the changes applied to the gamerule. You don't have to take the previous value into account, just make time tick on uninstall regardless of what the setting was before. Every tick, use the "/time add" command to add just below 1 day every tick. This creates the illusion that you are removing some value from time, even though there is no "/time remove" command. The same trick is used to make these water droplets fall upwards!
What's next?
Next up we're going to look selector variables/arguments and relative locations.Subscribe if you want to get notified of new posts.
Tags |
tools/tracking
4473023
6
function-data-packs-for-dummies-4-let-s-play-tag-load-tick-uninstall-and-other-functions
Create an account or sign in to comment.
The .json files contain arrays (lists) that can contain multiple function names. So even though you can only have 1 tick and 1 load .json file, you can make each file list/reference multiple function files. Each function file can also call multiple other function files using the "function" command (yes, the same command you use to run functions in chat, can also be used inside other functions!).
See one of my other comments for an example tag .json file.
{
"values": [
"<namespace>:[path/to/function/]load"
]
}
For example:
{
"values": [
"bertiecrafter:my_data_pack/load"
]
}