Get Started
Installation
First, make sure you have Python 3 installed. You can check by running the command python --version
. If it returns an error you can download it from here or using your package manager.
Create a directory for your datapack. For a simple datapack this can be directly in your world's datapacks folder. (e.g. ~/.minecraft/saves/<world>/datapacks/<your-datapack-name>
) Or anywhere else on the file system. Then navigate inside it
Create your project's Python environment and activate it. (This step is not strictly required but its a good practice!)
Install the mcpy
package
Initialize the project. Hit enter to use default values
Open up the src/pack.py
file and inspect the lines. The concepts should look familiar (E.g. namespaces, .mcfunction
files, and JSON
files are written)
@datapack
def simple_pack():
with namespace("simple_datapack"):
with directory("api/greetings"):
@mcfunction
def say_hello():
yield "say Hello!"
with namespace("minecraft"):
@functions
def load():
yield {"values": ["simple_datapack:api/greetings/say_hello"]}
Building
Build your pack's data/
files with the following. Optionally, add -w
or --watch
to continuously build on file changes
Open the Minecraft World with the datapack we just created and reload datapacks using the /reload
command.
That's it! You should see the hello message upon reload.
Writing a Datapack
First, create a src/pack.py
file in your datapack directory or use python -m mcpy init
to get a starter file.
In this file, Mcpy will look for a function with a @datapack
decorator. This function will define your datapack and all of its files.
from mcpy import (datapack, mcfunction, functions)
@datapack
def my_pack():
# define datapack here
...
Before defining any files, a namespace must be defined (e.g. pack/data/<namespace>/functions/foo.mcfunction
). Use the namespace
context manager to descend into a namespace directory.
Now that a namespace is created, an mcfunction file can be created by defining a function decorated with @mcfunction
.
@datapack
def my_pack():
with namespace('my.pack'):
@mcfunction
def say_hello():
yield 'say Hello!'
In the example above, the .mcfunction
file generated will have the name say_hello
, taken from the decorated function. Similarly, the content will be taken from the yielded command strings or written using the write
function.
(Additionally, the mcpy.cmd
module can be used to simplify usage of common commands)
Now that the mcfunction is defined. We can call it when the datapack loads using the minecraft:load
tag. Function tag files are defined in a similar manner as above.
@datapack
def my_pack():
with namespace('my.pack'):
@mcfunction
def say_hello():
yield 'say Hello!'
with namespace('minecraft'):
@functions
def load():
yield {
'values': [ str(say_hello) ]
}
Now the datapack can be built from it's root directory (e.i. with the pack.mcmeta
in it).
The data
directory will now be populated with the expected files and the datapack directory can be copied into a world. Alternatively, an output directory can be specified that the compiled datapack will be copied into. Using an output directory is required when including dependency datapacks. (See the How to Include Dependencies guide for details)
The last step is to open the Minecraft world and run the /reload
command to see the changes take effect.