What are the basics of commands in Minecraft Java Edition?

What are the basics of Minecraft commands? If I were to start from scratch, with no knowledge on the topic, what are some things that I need to know?

Intro/Arguments

Minecraft’s commands system allows players to modify the game in ways besides mining and placing blocks. Commands have different arguments that specify their behavior, such as a selected entity or specified location. For example, the `/summon` command has two arguments: an entity to summon and a location to summon it at. It is important to note that entity means not only creepers and blazes but also dropped items, snowballs, boats, arrows, etc. Knowing the `/summon` command’s arguments shows us that `/summon minecraft:wither_skeleton 0 80 0` would summon a wither skeleton at the position `0 80 0`.

Tilde and Caret Notation

This, however, is very limiting: what if we wanted to summon an entity at our current position? This is where tilde and caret notation comes in. Tilde and caret notation works like a variable, working relative to your current location. To summon a silverfish at my current location I would do `/summon minecraft:silverfish ~ ~ ~`. You can also modify these relational coordinates by putting values after each tilde. To summon a lightning bolt 1 block in the `x` direction, 2.3 blocks in the `y` direction, and -17.914 blocks in the `z` direction, I would do `/summon minecraft:lightning_bolt ~1 ~2.3 ~-17.914`. There is another type of relative coordinates which act in relation to where the entity executing the command is looking, which use carets (^ ^ ^), and adding values changes the left/right, up/down, and forwards/backwards positioning of the command. To spawn a fireball one block in front of me, I would do `/summon minecraft:fireball ^ ^ ^1`.

Note that if you use carets (`^`), the x, y, and z must all have `^`. For example, `^ ^ ^4` is valid but `5 ^ ^` and `^ ~ ^` are not.

Entity Selectors

Alongside position selectors there are also ‘variables’ for selecting entities, appropriately called entity selectors or target selectors. These start with an @ sign and a character, which stand for different categories of entities. `@s` selects the self (the entity executing the command), `@p` selects the closest player (to the executing position), `@r` selects a random player, `@a` selects all players, and `@e` selects all entities.

These entity selectors also accept various arguments, which further narrow the amount of entities selected. Some arguments are the `type`, `number`, `distance`, `name`, or `xp levels`. These selectors are useful if you want to do anything to anyone besides named players. For example, you can /kill all items on the ground by running `/kill @e[type=item]`. In that case, you are using the entity selector @e and whittling the group selected down to only those entities that are items. It is very important to note at this point that commands are case-sensitive, meaning capitalization matters. Many broken commands have been fixed by correcting a misplaced capital or lowercase letter, so make sure you are always very careful about what you are typing.

Some useful tips for using entity selector arguments:

• `distance` uses the position of an entity’s coordinates (so their feet, generally) while `dx`, `dy`, and `dz` use their hitbox. If you want to use `x`, `y`, and `z`, or `dx`, `dy`, and `dz`, be aware that you have to specify all three of them (or all six, if you use all of them together). If you don’t use `x`, `y`, and `z`, `dx`, `dy`, and `dz` assume that the `x`, `y`, and `z` are wherever the command is being executed from.

`/execute` command

This is all very useful, but how would I go about summoning a lightning bolt onto every creeper in the loaded world? The `/summon` command has no entity selector, and I don’t want to go around writing down the coordinates of every creeper that I can see. The solution is the `/execute` command, arguably the most important command in the game because it is able to change the ‘metadata’ behind a command: who’s running it, where they are, which direction they are facing, among other things. The `/execute` command has several different specifiers we can attach to it to make it run commands in different ways. One of these is the `at` specifier, which allows us to choose which entities we want to run some command at. In this case, we would want to do `/execute at @e[type=minecraft:creeper] run summon minecraft:lightning_bolt ~ ~ ~`. This would run the command “summon a lightning bolt here” from the position of all of the creepers.

It is very important to remember who is executing a command and where is the command being executed from. An example of the problems that happen when you forget this is the command `/execute at @e[type=creeper] run kill @s`. At first glance, this may seem like it will kill all creepers loaded in the world, but it won’t. It will kill the entity who runs the command, because `@s` refers to the self (kill self) and no matter where the command is executed (even if it is at the position of all creepers) `@s` still refers to whoever is running the command. A fix might appear to be `/execute positioned as @e[type=creeper] run kill @s`, but this has the same problem: the executing entity is still the entity who runs the command. To fix this, one would run `/execute as @e[type=creeper] run kill @s`. Now, `@s` refers to `@e[type=creeper]`. Of course, one could just do `/kill @e[type=creeper]`, but this is just an example problem.

NBT Data

This is all very well and good, but what if we want to do something more complicated, such as give a random player an unbreakable golden sword with Fire Aspect II which has the bolded, non-italic, dark-red name “Flame Boi” and eats non-gamers? This is where NBT comes in. NBT stands for Named Binary Tags, and it essentially allows you to set certain attributes of items, blocks, entities, etc. beyond just their location. NBT data consists of tags which have a name and a value. Sometimes, a tag may have multiple or nested values, in which case a list `[]` (series of values) or compound `{}` (tags within a tag) will be required. A full list of NBT tags is on this site, but right now we will just use the ones required for the sword. To give the sword, we would use

``````/give @r minecraft:golden_sword{Enchantments:[{id:"minecraft:fire_aspect",lvl:2}],Unbreakable:1,display:{Name:"{\"text\":\"Flame Boi\",\"color\":\"dark_red\",\"italic\":\"false\",\"bold\":\"true\"}",Lore:["{\"text\":\"This sword eats non-gamers.\"}"]}} 1
``````

To break it down, `Enchantments:[]` is a list of enchantments, each of which is a compound tag (enclosed with {}) with multiple, comma separated modifiers. The `id:""` tag is a String (series of characters) so it’s enclosed in quotes, while the `level:#`, being just an integer, doesn’t need any quotes. The golden sword’s `Unbreakable:#` tag is set to 1 so it lasts long enough to actually kill something, and the `display:{}` tag carries all of the information needed to display the item in the inventory, such as the name and lore. The `Name:""` tag is actually a String (if you want to color it and change the font type) and because of this it gets formatted in a special way. Because quotes by themselves are interpreted as containing Strings, they need to be “escaped” with a backslash so they actually get sent in as part of the `Name:""` String. This means that without the backslashes, our `Name:""` command looks like this: `{"text":"Flame Boi","color":"dark_red","italic":"false","bold":"true"}`, which is much more decipherable. Finally, `Lore:[]` is a list of lines of lore to display, where each line (delineated by `{something something something}`) also needs to have escaped quotes so it can be interpreted properly.

A very helpful command for learning about NBT tags is `/data get entity @s SelectedItem`. It tells you all of the NBT tags that the item that you’re currently holding has.
That includes custom names, colors, enchantments, etc.
To use it you can craft, find, or `/give` yourself the item that you want to use, rename and enchant it with an anvil and then hold it. Run `/data get entity @s SelectedItem` to get all of the tags that the item has that you may need to `/give` yourself an identical item.

A helpful keyboard shortcut is F3+I, it will copy a `/setblock` or `/summon` command into your clipboard that you can use to set a block, or summon an entity that will be identical to the one you’re looking at, including all NBT-tags and block states.
You can copy that command into a text editor with Ctrl+C to learn a lot about NBT tags. If you want a `/summon` command for a horse with blue leather armor, you can find or `/summon` a horse, give it blue leather armor, then look at it, press F3+I and copy the command into a command block, no changes required. You won’t need all of the NBT tags in most cases, you can make the command shorter by removing excess NBT tags. Make sure to test the command a few times in the process to make sure you didn’t break anything.
The keyboard shortcuts provided here are for Windows users, they may be different for mac, linux, or other systems

Conclusion

If you’ve read through this and still have a question on how a command works or where to put an NBT tag or something similar, be sure to read the wiki about that command, or this website which has the data types in NBT, this website which lists the NBT tags in Minecraft, this website which contains information on player NBT, or this website which has information on chunk NBT.

NBT is very similar to JSON, so several links which may help explain JSON are this, this, this, this, and this. Don’t worry if you don’t have any programming knowledge/experience or if it is too technical for you: JSON is related to NBT in structure but you don’t need to know about server-client data interchange or anything like that to use NBT. This is a good JSON validator, however be wary: JSON and NBT are not the same, so valid nbt such as `{Enchantments:[{id:"minecraft:infinity",lvl:1}],Unbreakable:1,display:{Name:"{\"text\":\"Epic Pearl\",\"color\":\"light_purple\"}",Lore:["{\"text\":\"Yaw yeet\"}"]}}` will be flagged as incorrect JSON because the String identifiers (`id`, `lvl`, etc.) are not enclosed with quotation marks. It’s better to figure out why something is broken by hand (tracing braces/brackets, retyping it, checking capitalization…) than be misled by a JSON checker which operates by slightly different rules than NBT. However, `/tellraw` and `/title` use strict JSON (which can be found here) so the above tool is very useful for helping create those types of commands.