Minecraft / Blogs

Making a datapack with VS Code

Datapack with VS Code

  • 7 diamonds
  • 181 views, 6 today
  • 0 favorites
  • 7
  • 0
  • comment1
  • playlist_add
  • share
  • more_horiz
avatar ShelLuser
Level 38 : Artisan Engineer
Hi gang!


I'll be very honest with you guys: I am biased as heck ;)

Thing is: I've been coding Java for a very long time now and my preferred tool for that has always been NetBeans. I've been using that critter ever since I grabbed version 4.1, now over 12 years ago, so it's fair to say that I have a bit of history with it. Unfortunately many things changed recently and for offtopic reasons I began to dislike it. A few weeks back I (re)discovered VS Code and that was the beginning of the end for me. In the mean time I got rid of NetBeans and I've been using VS Code for pretty much everything which involved editing code files.

Editing a .reg file (Windows registry file)? VS Code has a formatting plugin for it to make my life easier. Editing batch files? No problem. Heck, as mentioned above: I'm even using it to code, build, debug and deploy Java programs and it. just. works..

And did yo know it also supports JSON formatting? Why is that important, you ask? Because Minecraft heavily relies on that as well!

But first things first...

What is VS Code?

VS Code editor

VS Code is short for Visual Studio Code, and it's an editor developed by none other than Microsoft and specifically designed to edit code files. In other words: it's a tool for programming. Now, don't let the mention of Microsoft startle you: the project is fully open source (you can find its GitHub repository here), free to use and available for all major platforms (Windows, MacOS & Linux).

And honestly? It's actually pretty good!

What makes this program stand out from the rest is its small size (it only eats up 195Mb of diskspace) which automatically results in a quick startup time. But despite its small size it packs one heck of a punch thanks to a severe collection of available extensions. There's a very dedicated community around this project and they provide support for just about everything.

And yes: including Minecraft!

But why would you want to use this?

Simple: have you ever been here before? You want to generate a book. So after looking at some samples which explain the right formatting you open notepad and get to work. Once done you open Minecraft, paste in your code and... it doesn't work?

# This is how it all begins...give @s minecraft:written_book{tag:["hb"],generation:1,author:"§b§oNotch",title:§4§lThe Forbidden One",pages:["[{\"selector\":\"@p\"},\"...\",{\"text\":\"\\n\\nIf there is power to gain, Thy shall find it in the Dark Lords domain.\\n\\nThe strongest passage must though build, For this is the portal which cannot be killed.\",\"color\":\"gold\",\"bold\":true,\"italic\":true,hoverEvent:{\"action\":\"show_text\",\"value\":\"Herobrine is watching..\"}}]","{\"text\":\"Oh foolish mortal, know what thy seek, For His minions are everything, most certainly not weak.\\n\\nLord Herobrine; your powers are plenty, The number of thy portal: it has been dubbed 20.\",\"color\":\"gold\",\"bold\":true,\"italic\":true}","[{\"text\":\"Summon Herobrine!\",\"color\":\"dark_red\",\"bold\":true,clickEvent:{\"action\":\"run_command\",\"value\":\"/function herobrine:summon\"},hoverEvent:{\"action\":\"show_text\",\"value\":\"Beware...\"}},{\"text\":\"\\n\\nReset game\",\"color\":\"green\",\"bold\":true,clickEvent:{\"action\":\"run_command\",\"value\":\"/function herobrine:reset\"},hoverEvent:{\"action\":\show_text\",\"value\":\"Start over.\"}}]","\"This is my first 'game datapack', I hope you'll enjoy.\\n\\nWith special thanks to AyanamiKun for helping me test this!\\n\\nIf you're having issues be sure to enable cheat codes!\\n\\n--- ShelLuser\""]}give @s minecraft:obsidian{tag:["hb"],display:{Name:"{\"text\":\"Portal component\",\"color\":\"dark_gray\",\"italic\":true}"}} 20

So where's the error? Good luck finding it in that huge wall of text! :)

And the solution? Why, VS Code of course!

Error spotting with VS Code

Load in the file and make sure to tell VS Code to utilize JSON formatting. And as you can see in the screenshot above something will stick out in no time. I added a backslash to 'escape' a quote but then apparently forgot the actual quote itself. How stupid ;)

It isn't perfect though!

As you can see above support is a bit limited when it comes to function files, that's because VS Code doesn't support this format on its own so I had to tell it to treat this as a JSON file. Unfortunately the JSON formatting is only used in parts of the actual code, some of it are simple Minecraft commands. And that doesn't always work properly.

There are some extensions available which provide support for specific Minecraft files (such as Minecraft JSON Schemes, which is pretty decent) but all of them have their own share of issues and limitations. For example: even though a 'Minecraft function' can be used as a file type it makes the previous syntax highlighting option fully unusable:

Code shown as Minecraft function file

This is the same file as before, now formatted as "Minecraft function". As you can see the entire last section of the command is treated as one huge comment, which seriously defeats the purpose of using VS Code for this. So sometimes you have to step in yourself.

Let's get started!

So, the first thing you want to do is get & install Visual Studio code. You can download it from the main website here. During the installation it asks you if you want to associate it with supported files and / or with opening directories. I suggest turning on the option to use it on directories but keep the option to associate it with files deselected. This will allow you to determine for yourself when you want to use this (if you want to change something you can always right click on the file, find the option "Open with ..." and from there select either the editor or 'other' after which you can select it.

Next: make sure you have a Minecraft world to use. In my example I'm going to be using "snapworld" which is a world I use to test snapshots. Start Minecraft, click "Singleplayer" and then click on the world you're going to use. Click "Edit" and then click "Open World Folder". Note that this option is only available with 1.13 and up. For older versions you'd have to open your file manager yourself and then go to %appdata%\.minecraft\saves.

Once you have your world folder open check that it has a "datapacks" folder. If not then create it. After that right click on the folder and select "Open with VS Code", this will start the editor. You should now see something like this:


So, what is a datapack? Basically nothing more but a directory with a specific identification file in it (called pack.mcmeta), then a sub directory called data and finally one (or more) other sub directories which define the so called namespace.

First things first: pack.mcmeta.

Setting up the datapack

So first we're going to make a new subdirectory. Click on the "New folder' icon behind "Datapacks" and let's name it MyDataPack. Then either click on MyDataPack and press control-n (for 'new file') or right click and select the option "New file". A new tab will be opened called "Untitled-1". Well, that's no good so press control-s (to save), open the MyDataPack folder and then enter the file name to use: pack.mcmeta. Now 2 things can happen. You could get a warning which tells you that VS Code has found an extension in the marketplace which could be useful for this file. Or VS Code already recognizes the type and will turn it into a JSON file.

If you follow the link to the marketplace you'll probably get directed to Minecraft JSON Schemes. If not try to find it & install it by clicking the small 'install' option behind the name. After that click on the 'pack.mcmeta' tab again to re-open your file.

Start by typing an opening curly bracket: { and you'll notice that VS Code automatically adds a closing bracket for you. This way you can never lose track of opening and closing pairs. Press enter Then type: "pack:"{ and press enter again. If you installed the plugin then the code completion window will probably appear and fill out pack:{} for you. Just hit enter to make that happen.

Now we need 2 entries: pack_format and description. Start typing pack and then press control-space to bring up the code completion window. It should list pack_format and immediately fill out 1 for you. Just press tab to use this value, add a comma and then hit enter.

Entering the description

Here you can see what it looks like when I'm entering 'description'. Press tab and enter a nice description.

Step one is complete!

Adding a name space and our first function

Now we need to add the data folder which will contain the namespace(s) we're going to use. Right click on 'MyDataPack' and select 'New folder'. Name this 'data'. Then right click on 'data' and add another folder. Lets call this one 'code'. Now we're ready to actually add some contents. As I mentioned in the header we're going to add a function, so click on 'code' to create one more folder. Call it "functions".

Now we're getting somewhere. Right click on the 'functions' folder and select the 'new file' option. Name the file: helloworld.mcfunction. Now we're ready to add the actual code:

say Hello Minecraft world!

Here's what things should look like:

Our HelloWorld function

Now we're ready to test this stuff out. Make sure that you saved all the files. If a file has unsaved changes then you'll notice a small ball behind its name in the tab. Then go back to Minecraft and if you already loaded the world then use the /reload command (you obviously need to have cheat codes enabled!).

Testing our new function

Hello world!

Now to test it. Enter the /function command and if you did everything right then it should already list our new function. Press tab to select it, then enter to start it. Hello Minecraft world!

So now for the cool part ;)

Go back to VS Code, click file and then select "Save workspace as...". Place the file in a location where you can easily find it and give it a good name. For example: "My first datapack".

Why this is so cool you wonder?

Simple really: the next time you want to continue working on your datapack all you have to do is open that workspace file you just created. No more messing in Minecraft with editing a world or using your OS file manager. Instead you open this file, VS Code will be started and you'll be taken right back where you left off.

For more information...

Despite its small size VS Code is actually an extremely flexible and customizable program. Therefor don't try to learn the whole thing in one day, you'll never going to succeed. Take it one step at a a time.

Some general points which might be useful to know:
  • Not every option is listed in a menu. Press control-shift-p to bring up the command palette and simply start typing the thing(s) you're looking for.
  • All tabs in the sidebar can be accessed using hotkeys. Just hover your mouse over the icons and it will show you which combination you can use. Examples are: control-shift-e to open the editor pane, control-shift--x to open the eXtensions pane (also called "marketplace") and control-shift-g to open the source control page (powered by Git).
  • Also: control-b will open or close the sidebar.
  • If you want to change the default settings: press control-shift-p, then enter 'settings', the option should become visible. You can also use the cogwheel icon in the lower left corner.
  • If you're interested to learn more then I highly recommend checking out these introduction videos.
  • Keep in mind that VS Code can do a whole lot more than just editing Minecraft data files ;)

And there you have it!

I hope this was useful for some of you!

This is definitely a new and quick way to start working on datapacks.

Comments : 1

star Login or register to post a comment.

Show Comments

1 - 1 of 1

  • Nitrox Nova
  • Level 28
  • Expert Robot
  • February 18, 2019, 12:03 pm
This is so detailed and well explained. Thank you for taking the time to share it!

1 - 1 of 1

Show Comments

Planet Minecraft



© 2010 - 2019