- 11,789 views, 3 today
772
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 simple (primitive) value types are:
By combining complex values with simple values you can create an entire object structure full of information. The first all-containing value is called the "root".
Lucky for you, you don't have to worry about any of this. You just have to be able to understand the icons used by the Minecraft Wiki and the range limitations that go with them. The wiki pages might specify additional restrictions for each value in any JSON file. They're hard to miss if you ever read up on one though. Have a look here for an up to date list of value types. Ignore the different writing styles shown there, they don't apply to JSON. A number in JSON is always just a line of digits with possibly a dot somewhere in-between.
Don't try to remember all of them. Just be aware of the icons and , since you're likely to try a value outside their ranges if you're not careful.
The syntax is simple: /tellraw <player> <json>
You can find the JSON structure here.
The icons you see represent the value types. Explanation of the icons can be found here.
According to the wiki the JSON is either a string, an array or an object. Which means that this is correct:
We didn't accomplish much with this though. Let's have a look at the object. If you look at where it says "The base chat component object", you'll see an icon in front. That icon means that it's an object. Below that line you have all the keys written out and the icons tell you the value type.
Because the root is an object, we must start our JSON structure with:
Now if we look at the "text" key you'll see that its icon is a page with lines. This means that it requires a string value. Using this information we can create this command:
Have a look at all the other possible keys. For example, you can see that the "bold" key requires a boolean. So if you want to display bold text, you'd use this:
The style keys apply to the entire text in that object. If you want multiple styles in the same command, use the "extra" key (which is an array type). Note that all children inherit the styling of their parent unless overridden. Example:
I don't recommend using the array version, because it doesn't behave the way it looks. Have a look at this command:
You'd expect the second word to be white, since it has no color tag. However, the command is translated into:
Which causes the second word to be green as well, since it inherits style properties from its parent. See how this can be confusing?
Lastly, you can use "\n" to insert new lines. This allows you to have multi-line /tellraw commands.
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.
Try sending a hyperlink to yourself or a friend using /tellraw and the wiki page. The hyperlink must be colored aqua and underlined. If you hover over it, it should show a description (no style required). If you click it, it should open this website: https://theuselessweb.com/
If the command turns out to be too long for chat, you can give yourself a command block (/give @s command_block), put the command in there and slap a button on it.
Subscribe if you want to get notified of new posts.
Where is JSON used?
No, it's not a person. It's a standardised definition language that allows passing what programmers call "objects" from one application to another in a human readable format. It's used all over the web. Since it was originally developed for use with JavaScript, JSON stands for "JavaScript Object Notation". In minecraft, it's used in technical files and any commands that change or display text. It's also a great start for understanding NBT. That's another definition language based on JSON, but only a tiny bit more difficult.How do I write JSON?
JSON is just plain text written according to rules (syntax). The entirety of a JSON file is just 1 value. If you were to put a simple value in it, like a number, you can't put anything else in that JSON file. This is why the top-level value is often a complex one, allowing more values to be put inside. Each of the examples below is valid JSON.The simple (primitive) value types are:
- A number with a dot as decimal separator. Examples:
4
Or3.5
- A string, also described as a piece of text surrounded by quotes. Example:
"Hi, how are you doing?"
- A boolean (case sensitive). Examples:
true
orfalse
- Nothing. Keep in mind that this is different from 0, " " or false. This value is not used in Minecraft, it's just listed here for completeness. Example:
null
- Array: It's a list of values. These values could be of any type, but usually are of the same type. Each value is separated by a comma. You're not allowed to place a comma after the last value. Example:
[
"This is an array of values",
50,
true,
null,
[
"Remember, an array is also a value"
]
] - Object or Compound: It's a collection of string keys and values. Once again, the values can be of any type, the key/value pairs are separated by commas and you can't have a comma behind the last pair. Example:
{
"my favourite number": 15,
"my pet": {
"name": "Max",
"type": "dog"
},
"my favourite food": [
"pizza",
"cake"
]
}
By combining complex values with simple values you can create an entire object structure full of information. The first all-containing value is called the "root".
Why do some types have multiple icons?
JavaScript does not differentiate between different kinds of numbers, which is why JSON (created for JavaScript) doesn't either. This isn't a problem for light pieces of code that JavaScript was made for. However, a complex piece of software like a game needs optimisations. By splitting up a "number" into different types with different ranges, less bytes can be used if they aren't needed. Carefully choosing the amount of bytes needed for a number can save both RAM and disk space.Lucky for you, you don't have to worry about any of this. You just have to be able to understand the icons used by the Minecraft Wiki and the range limitations that go with them. The wiki pages might specify additional restrictions for each value in any JSON file. They're hard to miss if you ever read up on one though. Have a look here for an up to date list of value types. Ignore the different writing styles shown there, they don't apply to JSON. A number in JSON is always just a line of digits with possibly a dot somewhere in-between.
Icon | Name | Range |
Byte | Whole number from -128 to 127 | |
Short | Whole number from -32,768 to 32,767 | |
Integer | Whole number from -2,147,483,648 to 2,147,483,647 | |
Long | Whole number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | |
Float | These are the only types that allow decimals. A float is less precise than a double, which means a float can represent less numbers than a double within the same range. It's barely noticeable though, so just forget about it. The ranges are also insane. ±34 followed by 37 zeros for a float and ±17 followed by 307 zeros for a double. | |
Double | ||
Byte Array | A list of bytes | |
Integer Array | A list of integers | |
Long Array | A list of longs | |
List | A list of anything, not just numbers. The wiki will tell you what's expected. |
Don't try to remember all of them. Just be aware of the icons and , since you're likely to try a value outside their ranges if you're not careful.
Using JSON in the tellraw command
You might have heard of /say. It broadcasts a message but it doesn't have much functionality. The text is always white and it's always prefixed with the name of the executioner (a player name, "@" for command blocks and "Server" for functions/console). Tellraw allows you to print text messages in color, many different styles and you can even make it change its content based on certain conditions. It's one of the many places where JSON is used.The syntax is simple: /tellraw <player> <json>
You can find the JSON structure here.
The icons you see represent the value types. Explanation of the icons can be found here.
According to the wiki the JSON is either a string, an array or an object. Which means that this is correct:
/tellraw <player> "Hey there!"
Hey there!
We didn't accomplish much with this though. Let's have a look at the object. If you look at where it says "The base chat component object", you'll see an icon in front. That icon means that it's an object. Below that line you have all the keys written out and the icons tell you the value type.
Because the root is an object, we must start our JSON structure with:
/tellraw <player> {}
Now if we look at the "text" key you'll see that its icon is a page with lines. This means that it requires a string value. Using this information we can create this command:
/tellraw <player> {"text":"Hey there!"}
Hey there!
Have a look at all the other possible keys. For example, you can see that the "bold" key requires a boolean. So if you want to display bold text, you'd use this:
/tellraw <player> {"text":"LOOK AT ME","bold":true}
LOOK AT ME
The style keys apply to the entire text in that object. If you want multiple styles in the same command, use the "extra" key (which is an array type). Note that all children inherit the styling of their parent unless overridden. Example:
/tellraw <player> {"text":"Base Text ","bold":true,"color":"red","extra":[
{"text":"Child Text ","color":"yellow"},
{"text":"Child Text"}
]}
Base Text Child Text Child Text
I don't recommend using the array version, because it doesn't behave the way it looks. Have a look at this command:
/tellraw <player> [{"text":"hey","color":"dark_green"},{"text":" there"}]
hey there
You'd expect the second word to be white, since it has no color tag. However, the command is translated into:
/tellraw <player> {"text":"hey","color":"dark_green","extra":[{"text":" there"}]}
hey there
Which causes the second word to be green as well, since it inherits style properties from its parent. See how this can be confusing?
Lastly, you can use "\n" to insert new lines. This allows you to have multi-line /tellraw commands.
/tellraw {"text":"Hey there!\nHow are you doing?"}
Hey there!
How are you doing?
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.Try sending a hyperlink to yourself or a friend using /tellraw and the wiki page. The hyperlink must be colored aqua and underlined. If you hover over it, it should show a description (no style required). If you click it, it should open this website: https://theuselessweb.com/
If the command turns out to be too long for chat, you can give yourself a command block (/give @s command_block), put the command in there and slap a button on it.
What's next?
Next up we're going to create our first datapack. It won't do much yet, but you need to know the basics in order to do something awesome.Subscribe if you want to get notified of new posts.
Tags |
4 Update Logs
Updated data type icons : by Bertiecrafter 10/24/2020 7:06:44 amOct 24th, 2020
The wiki used new icons, so I made this blog display the updated versions.
LOAD MORE LOGS
tools/tracking
4435043
6
function-data-packs-for-dummies-2-who-s-json
Create an account or sign in to comment.
Trial and error is how I solved it with the help of MC Wiki and WikiHow (wikis are underrated).