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.
Some of these variables or arguments are based on location. Since datapacks are by default located at worldspawn and this tutorial series didn't cover how to relocate yet, you'll be limited to using them in chat or command blocks only for now.
Let's start with the target selector variables:
Let's look at a few important target selector arguments:
A common mistake is translating "All players within 5 blocks" as "@a[distance=5]". This will never target anyone, because it's impossible for anyone to be exactly 5.0000(...) blocks out. The best approximation for "All players 5 blocks out" would be "@a[distance=4.5..5.5]" and "All playerse within 5 blocks" would translate to "@a[distance=..5]".
As you might have figured out, @p and @r are shortcuts for @a[limit=1,sort=nearest] and @a[limit=1,sort=random]. Also @a is a shortcut for @e[type=player]. One last, but very useful example:
Whenever you publish your data pack, you don't know who is going to use it or at what coordinates it's going to be used. The previous section covered how to select the players, now let's have a look at locations.
There are 3 versions of coordinates:
Both x y z and ~dx ~dy ~dz coordinates can be mixed (~4 100 ~-1), because they're on the same grid. You can't mix anything with ^left ^up ^forwards, because "~3 100 ^2" does not guarantee a block can be found meeting all of these requirements:
- 3 blocks along the x axis
- at y = 100
- 2 blocks forwards
You know how I said that every programmer's first thing should be printing "Hello World"? Scrap that, it's EXPLOSIVE ARROWS, because it was one of the first things I made and if you don't like explosives, you're not a real gamer. This data pack will use relative locations and as I said, these tutorials didn't cover relocating away from the default location in datapacks. Also this datapack uses NBT, which is another advanced subject that will get covered much later in this tutorial series, so I will give you pre-made commands and you need to fix them instead.
First, familiarize yourself with the /particle command. Figure out what each argument does. You can just use the chat window to run the commands. Hint, use a speed of 0.0001 to make sure you can actually see the particles without them poofing away instantly.
Then, get yourself a flame bow and place these commands into the tick function of your data pack:
- Replace @_ with the right target selector variable.
- Add a target selector argument to all commands to only select arrows. Leave the nbt arguments alone.
- Replace "say fire" with "particle ...." in order to spawn a trail of flame particles 1 block behind the arrow. Start with an amount of 2, because this amount will be spawned 20 times a second! Also, you won't have to do any calculations for the the positioning, because if you wouldn't specify an offset (e.g. ~ ~ ~), you're already at the location of the arrow. All you have to do is make it spawn 1 block behind the arrow, regardless of the direction of the arrow.
- Replace "say Boom!" with "summon ....." to summon tnt. Make sure to test if the kill command actually deletes the arrow once landed first, to avoid blowing up your PC with 20 TNTs a second instead.
Subscribe if you want to get notified of new posts.
Target Selectors
Say goodbye to putting your name in function commands. As a datapack creator, you're expected to target players dynamically. This means you always apply your commands to the right players, regardless of amount or location. Target selectors consist of two parts: The target selector variable and target selector arguments. Let's say you start with all entities, this includes players, mobs, but also items, flying snowballs and falling sand blocks. Both the variable and arguments filter this set of entities, but the variable is required while the arguments are not. It's possible that after applying both filters 0 entities are targetted, this is allowed and it will simply prevent the command from being executed.Some of these variables or arguments are based on location. Since datapacks are by default located at worldspawn and this tutorial series didn't cover how to relocate yet, you'll be limited to using them in chat or command blocks only for now.
Let's start with the target selector variables:
@e - Everything
@a - All players
@r - Random player
@p - The nearest player
@s - The entity executing the command / function
For example, "/give @a minecraft:diamond" gives everyone online a diamond. Next we're going to look at target selector arguments. You can find all of them here. The full syntax is:@<variable>[<argument>=<value>,<argument>=<value>, ...]
Some arguments also allow =! which means "is not equal to".Let's look at a few important target selector arguments:
distance - Only target players within the given distance. Accepts a range, which can be written as: [min]..[max] or <exact value>.
sort - Accepts nearest, furthest, random and arbitrary. Arbitrary means "do not sort" and is the default. Do not use arbitrary for random selections.
limit - Limits the result set to the specified number
type - Only accept entities of this type
nbt - Match the actual data inside an entity. Will be explained in a future tutorial.
A common mistake is translating "All players within 5 blocks" as "@a[distance=5]". This will never target anyone, because it's impossible for anyone to be exactly 5.0000(...) blocks out. The best approximation for "All players 5 blocks out" would be "@a[distance=4.5..5.5]" and "All playerse within 5 blocks" would translate to "@a[distance=..5]".
As you might have figured out, @p and @r are shortcuts for @a[limit=1,sort=nearest] and @a[limit=1,sort=random]. Also @a is a shortcut for @e[type=player]. One last, but very useful example:
/kill @e[type=!player] - Kill everything except players. Useful in datapack testing worlds if you made a mess. Keep in mind that this also kills things that survival players enjoy having around, like pets and item frames, so use it carefully on servers.
Relative Locations
Whenever you publish your data pack, you don't know who is going to use it or at what coordinates it's going to be used. The previous section covered how to select the players, now let's have a look at locations. There are 3 versions of coordinates:
x y z - Absolute, the well-known way of writing coordinates. Each number is a point on the x, y or z axis.
~dx ~dy ~dz - Relative, grid based. Each number indicates an offset on the x, y or z axis.
^-db ^dn ^dt - Relative, head based. The syntax is a pathetic attempt at linking it's functionality to science, a so called TNB frame. Simply said, it's: ^left ^up ^forwards. This does take all rotations into account, so "forward" while looking down means you will actually go down.
Examples:/tp @s 24 0 34 - Teleport to 24 0 34
/tp @s ~2 ~ ~ - Move along the x axis, 2 blocks.
/tp @s ^ ^ ^3 - Move 3 blocks forwards.
Both x y z and ~dx ~dy ~dz coordinates can be mixed (~4 100 ~-1), because they're on the same grid. You can't mix anything with ^left ^up ^forwards, because "~3 100 ^2" does not guarantee a block can be found meeting all of these requirements:
- 3 blocks along the x axis
- at y = 100
- 2 blocks forwards
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.You know how I said that every programmer's first thing should be printing "Hello World"? Scrap that, it's EXPLOSIVE ARROWS, because it was one of the first things I made and if you don't like explosives, you're not a real gamer. This data pack will use relative locations and as I said, these tutorials didn't cover relocating away from the default location in datapacks. Also this datapack uses NBT, which is another advanced subject that will get covered much later in this tutorial series, so I will give you pre-made commands and you need to fix them instead.
First, familiarize yourself with the /particle command. Figure out what each argument does. You can just use the chat window to run the commands. Hint, use a speed of 0.0001 to make sure you can actually see the particles without them poofing away instantly.
Then, get yourself a flame bow and place these commands into the tick function of your data pack:
execute as @_[nbt={inGround:0b},nbt=!{Fire:-1s}] at @s run say fire
execute as @_[nbt={inGround:1b},nbt=!{Fire:-1s}] at @s run say Boom!
kill @_[nbt={inGround:1b},nbt=!{Fire:-1s}]
Now try to apply the following fixes:- Replace @_ with the right target selector variable.
- Add a target selector argument to all commands to only select arrows. Leave the nbt arguments alone.
- Replace "say fire" with "particle ...." in order to spawn a trail of flame particles 1 block behind the arrow. Start with an amount of 2, because this amount will be spawned 20 times a second! Also, you won't have to do any calculations for the the positioning, because if you wouldn't specify an offset (e.g. ~ ~ ~), you're already at the location of the arrow. All you have to do is make it spawn 1 block behind the arrow, regardless of the direction of the arrow.
- Replace "say Boom!" with "summon ....." to summon tnt. Make sure to test if the kill command actually deletes the arrow once landed first, to avoid blowing up your PC with 20 TNTs a second instead.
What's next?
Next up we're going to look at the scoreboard, a magical place where numbers are generated and stored.Subscribe if you want to get notified of new posts.
Tags |
tools/tracking
4474923
6
function-data-packs-for-dummies-5-who-what-where-but-how-player-selectors-and-relative-coordinates
Create an account or sign in to comment.
Target all players in a sphere or box that don't have a tag. In the function, do whatevever you want and add the tag.
#tick.mcfunction
execute positioned <arena_x> <arena_y> <arena_z> as @a[distance=..10,tag=!in_arena] at @s run function bertiecrafter:my_pack/enter_arena
execute positioned <arena_x> <arena_y> <arena_z> as @a[distance=10..,tag=in_arena] at @s run function bertiecrafter:my_pack/leave_arena
#distance can be replaced by x,y,z,dx,dy,dz parameters to target a box.
#Although you'll have to be very creative targeting players NOT in that box for the leave arena function.
#enter_arena.mcfunction
# Do whatever you want here
tag @s add in_arena
#leave_arena.mcfunction
# Do whatever you want here
tag @s remove in_arena