MDL Examples

This page contains working examples of MDL features.

Basic Hello World

A simple datapack that says hello when loaded:

pack "hello" "A simple hello world datapack" 82;
namespace "hello";

function hello:main {
    say "Hello, Minecraft!";
    tellraw @a {"text":"Welcome to my datapack!","color":"green"};
}

on_load hello:main;

Logical Operators and Precedence

pack "logicalops" "Generated by MDL CLI" 82;
namespace "logicalops";

// Variables (default scope @s)
var num a = 0;
var num b = 0;

// Main function
function logicalops:main {
    say "Hello from logicalops!";
    exec logicalops:add<@a>;
}

function logicalops:init {
    a = 1;
    b = 2;
}

function logicalops:add {
    var result = $a$ + $b$;
    say "Result: $result$";
}

function logicalops:subtract {
    var result = $a$ - $b$;
    say "Result: $result$";
}

function logicalops:multiply {
    var result = $a$ * $b$;
    say "Result: $result$";
}

function logicalops:divide {
    var result = $a$ / $b$;
    say "Result: $result$";
}

function logicalops:andop {
    if $a$ > 0 && $b$ > 0 {
        say "Both are greater than 0";
    } else {
        say "At least one is not greater than 0";
    }
}

function logicalops:orop {
    if $a$ > 0 || $b$ > 0 {
        say "At least one is greater than 0";
    } else {
        say "Both are not greater than 0";
    }
}

function logicalops:notop {
    if !$a$ > 0 {
        say "a is not greater than 0";
    } else {
        say "a is greater than 0";
    }
}

function logicalops:whileloop {
    a = 0;
    while $a$ < 10 {
        say "a is less than 10";
        a = $a$ + 1;
    }
}

function logicalops:scheduledwhileloop {
    // the scheduledwhile is just like the while loop, but it is scheduled to run every tick. This is useful for large calculations that would exceed the command recursion limit.
    a = 0;
    scheduledwhile $a$ < 10 {
        say "a is less than 10";
        a = $a$ + 1;
    }
}

function logicalops:complexops {
    var c = 5;
    var d = 10;
    var e = 15;
    var f = 20;
    var g = 2;
    var h = 0;
    h = $h$ - 1;

    var result = ($c$ + $d$ - $e$ * $f$ / $g$) * $h$;

    if !$result$ == 135 {
        say "Result is not 135, there is an issue!";
    } else {
        say "Result is -135, everything is good!";
    }
}

on_load logicalops:main;

Raw Commands

Using raw Minecraft commands:

pack "raw" "Raw command example" 82;
namespace "raw";

function raw:custom {
    // Use raw Minecraft commands via single-line raw blocks
    $!raw effect give @s minecraft:speed 10 1 raw!$
    $!raw particle minecraft:explosion ~ ~ ~ 1 1 1 0 10 raw!$
    $!raw playsound minecraft:entity.player.levelup player @s ~ ~ ~ 1 1 raw!$
}

on_load raw:custom;

Function Macros

Demonstrates macro lines and passing macro data to functions:

pack "macros" "Function macro examples" 82;
namespace "macros";

// Target function using a macro line with $(name)
function macros:greeter {
    $say "Hello $(name)"
    say "Done";
}

// Callers using inline JSON and with-clause
function macros:callers {
    // Inline JSON compound (prefer single quotes outside)
    exec macros:greeter '{name:"Alex"}';

    // Pull compound from a data source via with-clause
    exec macros:greeter with storage macros:ctx player.info;
}

on_load macros:callers;

Fan-out Execution Pattern

Run a function once per matching entity using an outer exec scope, then use default @s inside:

pack "fanout" "Fan-out example" 82;
namespace "fanout";

var num visits<@s> = 0;

function fanout:increment {
    // Runs per-entity; scope defaults to @s inside
    visits = $visits$ + 1;
    say "Visits: $visits$";
}

// Fan out to all players on load
on_load fanout:increment<@a>;

Real-World Example: BetterWorldEdit

For a comprehensive real-world example of MDL in action, check out BetterWorldEdit - a powerful world editing datapack built entirely with MDL!

This project demonstrates:

  • Complex variable management and scoping
  • Large-scale command generation
  • Real-world datapack architecture

You can download and test the latest version from the releases page.