Skip to content

Plugin Structure

Lifecycle

Every plugin has three phases:

Server starts

 Root script code runs       ← setDefaults(), global variables

   onEnable() is called      ← register events and commands

   [Server is running]

   onDisable() is called     ← save config, cleanup

 Server closes

Important

events.on() and commands.register() must be called inside onEnable(), never at the root level of the script. The root level runs before the system is ready to receive registrations.

Available global variables

VariableTypeDescription
pluginobjectPlugin metadata
consoleobjectServer logger
eventsobjectRegister event listeners
commandsobjectRegister commands
configobjectPlugin YAML config
setTimeoutfunctionRun code after a delay
setIntervalfunctionRun code repeatedly
clearIntervalfunctionCancel an interval

plugin

js
plugin.name        // "MyPlugin"
plugin.version     // "1.0.0"
plugin.author      // "YourName"
plugin.dataFolder  // "plugins/my-plugin"

Full template

js
// =========================================
// My Plugin — Dragonfly Script API
// =========================================

console.log("Loading: " + plugin.name + " v" + plugin.version);

// Default configuration
// config.yml is created at plugins/my-plugin/config.yml
config.setDefaults({
    "prefix":       "§a[MyPlugin]§r",
    "welcome":      "Welcome to the server!",
    "max-players":  20
});

// =========================================
// Lifecycle
// =========================================

function onEnable() {
    var prefix  = config.getString("prefix", "§f[?]");
    var welcome = config.getString("welcome", "Welcome");

    events.on("PlayerJoin", function(event) {
        var player = event.getPlayer();
        player.sendMessage(prefix + " " + welcome);
    });

    commands.register("hello", "Say hello", function(player, args) {
        player.sendMessage(prefix + " Hello, " + player.getName() + "!");
    });

    console.log("Plugin enabled with prefix: " + prefix);
}

function onDisable() {
    config.save();
    console.log("Plugin disabled.");
}

// Export (required)
module = {
    onEnable:  onEnable,
    onDisable: onDisable
};

Colors in messages

Minecraft Bedrock uses § followed by a code to apply colors:

CodeColor/Format
§aGreen
§cRed
§eYellow
§fWhite
§7Gray
§bAqua
§dMagenta
§rReset
§lBold
§oItalic
js
player.sendMessage("§aGreen §cRed §eYellow §r Normal");

Dragonfly Script API