Functions
Functions represent reusable pieces of logic callable in a datapack.
Create a function with the function
builder:
Then in game, call the function with /function my_datapack:my_function
.
The function
builder returns a FunctionArgument
object that you can reuse to call the function from other functions:
Tags
You can set the tag of the current function you're working in with the setTag
function:
This will add the function to the minecraft:load
tag.
But you have simpler builders for the most common tags:
load
tag:minecraft:load
tick
tag:minecraft:tick
This will create functions with randomly generated names, but you can also specify the name of the function:
Commands
Many common commands have convenience builders like say
, teleport
, etc.
For example:
You can also build raw command strings and execute them:
Note: This is not recommended, but can be useful for commands not yet supported by the DSL, or if you use Macros.
Available Commands
All commands from the version cited in the README are available.
Custom Commands
You can pretty easily add new commands by creating your own builders. For example, imagine you created a mod that adds a new command /my_command
that takes a player name and a message as arguments.
You can create a builder for this command like this:
Then you can use it like any other command:
For commands that take complex types as arguments, you should use the .asArg()
function inside literal()
function. For Argument types, you don't have to use this.
See the code of the repository for more examples.
Link to time
command.
Link to weather
command.
Complex Commands
Some commands are more complex and require more than just a few arguments. For example, the execute
or data
commands.
In that case, you can use complex builders that includes all the arguments of the command. But the syntax may vary depending on the command and you should definitely check the tests to see how to use them.
An example of the execute
command:
You can use predicates in the ifCondition
block to check complex conditions. See the Predicates documentation for more details.
You may also have commands where you can create "contexts".
An example of the data
command:
Macros
See Macros.
Generated Functions
The same way the load
and tick
builders generate functions with random names, the execute
builder also generates a function with a random name if you call multiple commands inside the run
block.
This will generate a function with a random name that will be called by the execute
command.
Note: The generated functions will be generated inside a folder named
generated_scopes
in thefunctions
folder. You can change the folder to whatever you want in Configuration.
Note: The generated name will have this pattern
generated_${hashCode()}
, wherehashCode()
is the hash code of the function. This means that if you use the sameexecute
builder multiple times, it will generate the same function name and reuse the same function.
Debugging
You have multiple ways to debug your functions. First, a debug
function is available, it is pretty much the same as tellraw
but always displaying the message to everyone.
You also have a debug
block for printing a log message to the console for each command you call inside the block.
This will add a command call to tellraw
command, writing the exact command generated, clicking on the text will also call the command. Example of what is generated:
The last example is a function call to startDebug()
(which is called by the debug
block), this will add log messages to the start and the end of the function, plus a log message for each command called inside the function.
You can call the command by clicking on the debug texts added.
Also running toString()
in a function will return the generated function as a string, so you can manipulate it as you want.