You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/routes/guide/getting-started/+page.svx
+63-40Lines changed: 63 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ description:
6
6
step-by-step tutorial will help you understand the basics of datapack
7
7
creation, even if you have little coding knowledge. Start enhancing your
8
8
Minecraft experience today!
9
-
version: 1.21.8
9
+
version: 1.21.10
10
10
---
11
11
12
12
<script lang="ts">
@@ -65,19 +65,25 @@ or more difficult to work with. https://vscode.dev/
65
65
66
66
The first step when creating a datapack is to **make a new world** to test it
67
67
in. Go ahead and do that now - make sure that your world has **cheats enabled**
68
-
and it is set to **creative mode**. Once you've created the world, the next step
69
-
is to find and open your world folder:
68
+
and it is set to **creative mode**. In the world, you will need to run the `/datapack create` command -
69
+
it will create your datapack folder as well as the necessary metadata for you, which saves you a bunch of time.
70
+
71
+
For example, to make a datapack with the ID `ExampleDatapack` and the
72
+
description "This is an example datapack", you would run the following command:
73
+
74
+
```
75
+
/datapack create ExampleDatapack "This is an example datapack"
76
+
```
77
+
78
+
Once you've created your empty datapack, the next step is to find and open the
79
+
world folder:
70
80
71
81
1. Save and quit your world
72
82
2. Go to your world in the Singleplayer menu and hit "Edit"
73
83
3. Press "Open World Folder"
74
84
75
-
The folder that opens contains all the data in your Minecraft world. You should
76
-
see that there's lots of other subfolders in this folder. Find the one which is
77
-
called `datapacks`, and open it. This folder is where the world's datapacks are
78
-
stored. Create a folder - this will be the root folder of your datapack project.
79
-
The name doesn't matter. I'm just going to call it `Example Datapack`. **Open
80
-
this new folder**. Inside this folder is where all the data starts from.
85
+
The folder that opens contains your Minecraft world's data. There will be loads of subfolders in
86
+
this folder: find the one which is called `datapacks`, and open it. This folder is where the world's datapacks are stored. If you used `/datapack create`, you should see your datapack's folder here already. (If not, you'll have to create it yourself. This is explained in the "NOTE" box below)
81
87
82
88
:::tip
83
89
@@ -88,15 +94,16 @@ datapack will be _so much easier_!
88
94
89
95
:::
90
96
91
-
The first file we want to create is the `pack.mcmeta` file. This file will tell
92
-
Minecraft that the folder is a datapack, as well as holding the basic
93
-
information of the pack. **Create `pack.mcmeta`, and put this inside it**:
97
+
The datapack folder will contain `pack.mcmeta` and a folder called `data`.
98
+
`pack.mcmeta` tells Minecraft that the folder is a datapack,
99
+
as well as containing the basic information about the pack, such
100
+
as what versions it works in. `/datapack create` made this file automatically, but you can open the file like normal and view it if you want:
94
101
95
-
```json
102
+
```json:pack.mcmeta
96
103
{
97
104
"pack": {
98
-
"description": "DATAPACK NAME HERE",
99
-
"pack_format": 81
105
+
"description": "This is an example datapack",
106
+
"pack_format": 88
100
107
}
101
108
}
102
109
```
@@ -106,8 +113,18 @@ If you're interested, here's what this file means:
106
113
- `pack` is an object containing the metadata of the file.
107
114
- `description` is the name of your datapack, usually along with a short
108
115
description of what your pack does.
109
-
- `pack_format` tells Minecraft what versions this datapack works in. `81` is
110
-
the latest for 1.21.8
116
+
- `pack_format` tells Minecraft what versions this datapack works in. `88`
117
+
(or 88.0) is the latest for 1.21.10
118
+
119
+
120
+
121
+
:::note
122
+
123
+
If you're in a version earlier than 1.21.6, you may notice that `/datapack create` doesn't
124
+
exist. You will have to create your datapack folder (inside `datapacks`) yourself. Just make
125
+
a folder, and then inside that folder, create `pack.mcmeta` (as shown above) and the `data` folder.
126
+
127
+
:::
111
128
112
129
## Writing your first function
113
130
@@ -123,18 +140,18 @@ as an entity (or as the Server) and at a position
123
140
Let's get started by writing one simple function. In traditional programming
124
141
fashion, let's make a function which sends "Hello World" to chat.
125
142
126
-
1. Create a folder in your datapack called `data`.
143
+
1. Open your empty `data` folder.
127
144
2. In the `data` folder, create a new folder. This folder is your
128
145
**namespace** - it will contain all the data specific to your datapack.
129
146
Usually this needs to have a **unique name** (one which other people would
130
147
not have picked), but for this tutorial just call it `example`.
131
148
3. In the `example` folder, create a folder called `function`. This folder will
132
-
contain all the **mcfunction** files.
149
+
contain all the **.mcfunction** files.
133
150
134
151
You should now have a folder structure that looks something like this:
@@ -146,7 +163,7 @@ due to some pesky name changes Mojang introduced in a recent update.
146
163
147
164
Once you're sure that's correct, you can start writing the actual function.
148
165
149
-
In the `function` folder, create an empty file called `hello_world.mcfunction`.
166
+
In the `function` folder, create an empty text file called `hello_world.mcfunction`.
150
167
Open this file with any text editor (we recommend Visual Studio Code)
151
168
152
169
Inside `hello_world.mcfunction`, put the following:
@@ -159,9 +176,15 @@ title @s title "Hello World!"
159
176
give @s diamond
160
177
```
161
178
162
-
It's that simple! Once you **save that file**, **rejoin your world** and then
163
-
run `/reload`, you should be able to use the following command to run the
164
-
function:
179
+
:::info
180
+
181
+
Commands in functions shouldn't have the default slash `/` symbol which in-world
182
+
commands have! Having the `/` before a command will make the mcfunction invalid
183
+
and fail when it tries to run
184
+
185
+
:::
186
+
187
+
It's that simple! Once you **save that file**, and then run `/reload` (or if that doesn't work, rejoin your world), you should be able to use the following command to run the function:
165
188
166
189
```mcfunction
167
190
/function example:hello_world
@@ -211,7 +234,12 @@ Minecraft "run this command every tick" (every tick = 20 times per second)
211
234
1. In the `data` folder, create the `minecraft` folder.
212
235
2. In the `minecraft` folder, create a `tags` folder
213
236
3. In the `tags` folder, create a `function` folder
214
-
4. In the new `function` folder, create a new file: `tick.json`
237
+
4. In the new `function` folder, create a new text file: `tick.json`
| `@e` | **All Entities** - By itself, this selector will select every single entity which is currently loaded in the world |
289
+
| `@e` | **All Entities** - By itself, this selector will select every single entity which is currently loaded in the world. |
262
290
| `@s` | **This Entity** - This selector will select the entity which is currently running the command. For example, if I run a command as @s, then it will run the commands as me. |
263
291
| `@a` | **All Players** - This selects all currently online players. (All players are always loaded in the world). |
264
292
| `@r` | **Random Player** - This selects one random player. |
@@ -282,7 +310,7 @@ basic ones which you might see quite often:
282
310
| `[type=minecraft:arrow]` | **Entity Type** - This criteria will narrow down the selection to only include entities of a certain type. For good pack optimisation, you should use this often. |
283
311
| `[distance=..5]` | **Distance to entity** - This criteria will only include entities based on their distance to their distance to the current position. `..` can be used to represent a greater/less than symbol - for example, `..5` means less than 5 blocks away. |
284
312
| `[limit=2]` | **Limit selection** - Using the `limit` criteria, you can make specify a max amount of entities to be selected. This is often used in conjunction with the `sort` criteria. For example, `@e[sort=nearest,limit=1]` would select the nearest entity of any type. |
285
-
| `[nbt={key:value}]` | **Entity Data** - This criteria will select entities if their NBT (entity data) matches a pattern. |
313
+
| `[nbt={key:value}]` | **Entity Data** - This criteria will select entities if their NBT (entity data) matches a pattern. (note: if you do this loads of times, it might start to be a bit laggy!) |
286
314
287
315
### How are we going to use them?
288
316
@@ -295,10 +323,10 @@ This target selector can be used in an `execute` command to run a command as the
295
323
entity. Try putting this in your `loop.mcfunction`:
296
324
297
325
```mcfunction:loop.mcfunction
298
-
execute as @e[type=arrow,nbt={inGround:1b}] at @s run say I'm an arrow, I'm in the ground!
326
+
execute as @e[type=arrow,nbt={inGround:1b}] run say I'm an arrow, I'm in the ground!
299
327
```
300
328
301
-
Reload your datapack and fire an arrow at at a block. You should see that your
329
+
`/reload` your datapack and fire an arrow at at a block. You should see that your
302
330
chat is spammed with messages, meaning that we successfully selected arrows in
303
331
the ground, and executed a command as them!
304
332
@@ -308,33 +336,28 @@ Now we can move onto the fun part! We have a simple command which will run
308
336
another command as any arrow in the ground. We only need to do two things with
309
337
this command:
310
338
339
+
- Get the arrow's position
311
340
- Spawn an explosion at the position of the arrow
312
341
- Kill the arrow so that it only explodes once
313
342
314
-
To create an explosion in Minecraft, we can simply summon a TNT entity. When a
315
-
TNT entity is summoned, it will explode instantly.
343
+
To create an explosion in Minecraft, we can simply summon a TNT entity that has an instant fuse length - we can do this by summoning `tnt` with the data `{fuse:0}` at the position of the arrow. We can do this using the `execute at` command, which tells the game that the `summon` command should be ran at the position of the entity, instead of at the world spawn (because all commands are run at a position in the world). This way, when we summon an entity at `~ ~ ~`, the game knows that it's referring to the position of the arrow. *This sounds complicated, but in reality, its a really easy and intuitive system when you understand it :P*
316
344
317
345
```mcfunction
318
-
execute as @e[type=arrow,nbt={inGround:1b}] at @s run summon tnt
346
+
execute at @e[type=arrow,nbt={inGround:1b}] run summon tnt ~ ~ ~ {fuse:0}
319
347
```
320
348
321
349
Then, we just need to copy this command and make it kill the arrow after the
322
350
explosion has happened. This is as simple as it sounds - we can use `/kill` to
323
351
remove the arrow. Your finished `loop.mcfunction` should look like this:
324
352
325
353
```mcfunction:loop.mcfunction
326
-
execute as @e[type=arrow,nbt={inGround:1b}] at @s run summon tnt
354
+
execute as @e[type=arrow,nbt={inGround:1b}] at @s run summon tnt ~ ~ ~ {fuse:0}
327
355
kill @e[type=arrow,nbt={inGround:1b}]
328
356
```
329
357
330
-
:::info
358
+
:::note
331
359
332
-
In reality, this method isn't the best one to use. Instead of having two
333
-
commands for this, you can just execute a function as the arrow which will
334
-
summon the TNT at @s's position, and then kill @s. By only using the @e selector
335
-
once and then referring to @s, you remove extra stress from the computer. For
336
-
now, it doesn't matter - this guide is designed to give you a better idea of the
337
-
logic rather than the little details.
360
+
In reality, this method might be a bit laggy if there are a lot of `arrow`s in the world. A better system would involve creating a function which referenced `@s` (the "current" entity) instead of `@e[...]`, and then runnning that function as all `arrow`s in the ground. This way, we only use `@e[...]` once, which is a lot more efficient. However, for the purposes of this tutorial, it doesn't matter at all.
0 commit comments