Skip to content
This repository was archived by the owner on Nov 3, 2024. It is now read-only.

The mc language

SnaveSutit edited this page Nov 25, 2020 · 11 revisions

Contexts

  • Folder context refers to where functions and directories can be defined.
  • Function context refers to syntax inside of functions where commands can be written.
  • Macro context refers to the folder context inside of .mcm files
#!foo.mc
# Folder context
dir {
    # Folder context
    function bar {
        # Function context
        say Hello
    }
}
# Folder context
function bas {
    # Function context
    say World!
}

Functions

You can create functions using the function keyword:

function foo {
    say I am called via `function mc_file_name:foo`
}

Functions can only be defined within Folder context

Directories

You can create sub directories using the dir keyword:

dir foo {
    function bar {
        say I am called via `function mc_file_name:foo/bar`
    }
    dir bar {
        function bas {
            say I am called via `function mc_file_name:foo/bar/bas`
        }
    }
}

Directories can only be defined within Folder context

Function Blocks

Function blocks are used to define groups of commands that will be wrapped up into a function on compile-time.

Using the block keyword will define a block and inline the generated function call:

block {
    scoreboard players set @s value 1
    tellraw @s {"score":{"objective":"value","name":"@s"}}
}
# When this compiles it will inline something similar to this:
# function mc_file_name:__generated__/block/0

# The block keyword can be omitted, but recommended for readability in most cases.
# This will compile the same as above
{
    scoreboard players set @s value 1
    tellraw @s {"score":{"objective":"value","name":"@s"}}
}

Function blocks can also be defined at the end of execute commands:

execute if score @s value matches 1 run block {
    tellraw @s "Value is 1"
}
# When this compiles it will inline something similar to this:
# execute if score @s value matches 1 run function mc_file_name:__generated__/execute/0

# Again the block keyword can be omitted:
execute if score @s value matches 1 run {
    tellraw @s "Value was 1"
    scoreboard players set @s value 0
}

Keep in mind that if you do not omit the block keyword after an execute command it will always define a function, even if the block only contains a single command. If omitted and only 1 command is inside the block instead of making a function it will just append that command to the end of the execute command. For example:

execute if score @s value matches 1 run {
    tellraw @s "Value is 1"
}

Would compile to execute if score @s value matches 1 run tellraw @s "Value was 1"

Function blocks can also be named using the name keyword:

block {
    name foo
    say hi
}

This allows for manual naming to remove the __generated__ folders from your datapack if you so choose.

Function blocks can only be defined and used within Function context

Clocks

Clocks are an easy way of running a function every x amount of time. For instance you could create a clock that runs every 5 seconds like this:

clock 5s {
    say I run every 5 seconds!
}

The time argument takes a schedule command time input (5s, 10t, 1d)

Clocks can only be defined and used within Folder context

While, Async While, and Until

while

The while loop can be used to run a set of commands while a condition (defined as an execute subcommand chain) is true. It will halt the execution of the function until the loop exits:

scoreboard players set #i 0
while(if score #i value matches ..10) {
    tellraw @a [{"text":"loop "},{"score":{"name":"#i","objective":"value"}}]
    scoreboard players add #i value 1
}
say I will run once the while loop has ended!

This will loop until the value of #i is greater than 10, and then it will exit the loop and continue running the function

async while

The async while loop will run a set of commands every x amount of time until it's condition is true without halting the execution of the function.

scoreboard players set #i value 0
async while(if score #i value matches ..10,5t) {
    scoreboard players add #i value 1
    tellraw @a [{"text":"loop "},{"score":{"name":"#i","objective":"value"}}]
}
say I will run immediately after the first while loop

The async while loop can also have a finally function block defined that will execute it's contents once the loop exits:

scoreboard players set #i value 0
async while(if score #i value matches ..10,5t) {
    scoreboard players add #i value 1
    tellraw @a [{"text":"loop "},{"score":{"name":"#i","objective":"value"}}]
} finally {
    say I will run once the loop exits!
}
say I will run immediately after the first while loop

while, async while, and until can only be defined and used within Function context

Clone this wiki locally