Minecraft / Blogs

Function Data Packs for Dummies #4 | Let's play tag! (Load, tick, uninstall and other functions)

  • 5
  • 1
  • comment1
  • playlist_add
  • share
  • more_horiz
avatar Bertiecrafter
Level 43 : Master Robot
This series of tutorials will teach you how to create your very own datapacks, assuming you know absolutely nothing about commands. Datapacks can contain a number of things, but only functions and tags will be covered in these posts. In every part I assume you've read the previous parts.

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. 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. Now do the same for tick. You might not want chat output 20 times a second, so you might want to clear the contents of the tick function after testing it works.

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.


1 reply

01/15/2020 12:11 amhistory
Level 43 : Master Robot
Thank you Cyprezz, PMC, MasterKiloRen99 and Luracasmus for the diamonds! =)

Planet Minecraft



© 2010 - 2020