Provides the functionality for registering custom commands.

Custom Command APIs are released as part of Scripting API 2.0 Beta in Minecraft v1.21.80. Here's a video from Mojang that covers the custom command APIs.

Constructors

Methods

  • Parameters

    Returns void

    Registers a custom command that when executed triggers a script callback.

    This function can't be called in read-only mode.

    This function can be called in early-execution mode.

    // This sample is from Mojang, see an updated sample at
    // https://github.com/microsoft/minecraft-scripting-samples/tree/main/custom-commands
    import {
    system,
    StartupEvent,
    CommandPermissionLevel,
    CustomCommand,
    CustomCommandParamType,
    CustomCommandStatus,
    CustomCommandOrigin,
    CustomCommandResult,
    world,
    Entity,
    Vector3,
    } from "@minecraft/server";

    system.beforeEvents.startup.subscribe((init: StartupEvent) => {
    const dirtsterCommand: CustomCommand = {
    name: "creator:dirtster",
    description: "Adds some dirt, ster",
    permissionLevel: CommandPermissionLevel.GameDirectors,
    mandatoryParameters: [
    { type: CustomCommandParamType.Location, name: "dirtLocation" },
    ],
    };
    init.customCommandRegistry.registerCommand(dirtsterCommand, dirtster);
    });

    function dirtster(
    origin: CustomCommandOrigin,
    loc: Vector3,
    ): CustomCommandResult {
    world.sendMessage("Lets get dirty!");

    system.run(() => {
    const dim = world.getDimension("overworld");

    dim.setBlockType(loc, "minecraft:dirt");

    // it's a mini dirt pyramid
    dim.setBlockType(
    { x: loc.x + 2, y: loc.y + 1, z: loc.z },
    "minecraft:dirt",
    );
    dim.setBlockType(
    { x: loc.x - 2, y: loc.y + 1, z: loc.z },
    "minecraft:dirt",
    );
    dim.setBlockType(
    { x: loc.x + 1, y: loc.y + 1, z: loc.z },
    "minecraft:dirt",
    );
    dim.setBlockType(
    { x: loc.x - 1, y: loc.y + 1, z: loc.z },
    "minecraft:dirt",
    );
    dim.setBlockType(
    { x: loc.x, y: loc.y + 1, z: loc.z },
    "minecraft:dirt",
    );

    dim.setBlockType(
    { x: loc.x + 1, y: loc.y + 2, z: loc.z },
    "minecraft:dirt",
    );
    dim.setBlockType(
    { x: loc.x - 1, y: loc.y + 2, z: loc.z },
    "minecraft:dirt",
    );
    dim.setBlockType(
    { x: loc.x, y: loc.y + 2, z: loc.z },
    "minecraft:dirt",
    );

    dim.setBlockType(
    { x: loc.x, y: loc.y + 3, z: loc.z },
    "minecraft:dirt",
    );
    }); //

    return {
    status: CustomCommandStatus.Success,
    };
    }
    // This sample is from Mojang, see an updated sample at
    // https://github.com/microsoft/minecraft-scripting-samples/tree/main/custom-commands
    import {
    system,
    StartupEvent,
    CommandPermissionLevel,
    CustomCommand,
    CustomCommandParamType,
    CustomCommandStatus,
    CustomCommandOrigin,
    CustomCommandResult,
    world,
    } from "@minecraft/server";

    system.beforeEvents.startup.subscribe((init: StartupEvent) => {
    const helloCommand: CustomCommand = {
    name: "creator:hellocustomcommand",
    description: "Celebration super party hello",
    permissionLevel: CommandPermissionLevel.Any,
    optionalParameters: [
    { type: CustomCommandParamType.Integer, name: "celebrationSize" },
    ],
    };
    init.customCommandRegistry.registerCommand(
    helloCommand,
    helloCustomCommand,
    );
    });

    function helloCustomCommand(
    origin: CustomCommandOrigin,
    celebrationSize?: number,
    ): CustomCommandResult {
    world.sendMessage("Hello Custom Command!");
    const player = origin.sourceEntity;

    if (celebrationSize && player) {
    system.run(() => {
    player.dimension.createExplosion(player.location, celebrationSize);
    });
    }

    return {
    status: CustomCommandStatus.Success,
    };
    }
    // This sample is from Mojang, see an updated sample at
    // https://github.com/microsoft/minecraft-scripting-samples/tree/main/custom-commands
    import {
    system,
    StartupEvent,
    CommandPermissionLevel,
    CustomCommand,
    CustomCommandParamType,
    CustomCommandStatus,
    CustomCommandOrigin,
    CustomCommandResult,
    world,
    Entity,
    } from "@minecraft/server";

    system.beforeEvents.startup.subscribe((init: StartupEvent) => {
    const partyCommand: CustomCommand = {
    name: "creator:party",
    description: "Cause selected entities to party",
    permissionLevel: CommandPermissionLevel.GameDirectors,
    mandatoryParameters: [
    {
    type: CustomCommandParamType.EntitySelector,
    name: "partyParticipants",
    },
    ],
    };
    init.customCommandRegistry.registerCommand(partyCommand, party);
    });

    function party(
    origin: CustomCommandOrigin,
    entities: Entity[],
    ): CustomCommandResult {
    world.sendMessage("Entity party!");

    system.run(() => {
    for (const entity of entities) {
    entity.applyImpulse({ x: 0, y: 1, z: 0 });
    entity.dimension.spawnParticle(
    "minecraft:ominous_spawning_particle",
    entity.location,
    );
    }
    }); //

    return {
    status: CustomCommandStatus.Success,
    };
    }
  • Parameters

    • name: string
    • values: string[]

    Returns void

    Registers a custom command enum.

    This function can't be called in read-only mode.

    This function can be called in early-execution mode.

    import {
    system,
    StartupEvent,
    CustomCommand,
    CommandPermissionLevel,
    CustomCommandParamType,
    CustomCommandOrigin,
    CustomCommandResult,
    CustomCommandStatus,
    world,
    } from "@minecraft/server";

    system.beforeEvents.startup.subscribe((init: StartupEvent) => {
    const dimensionsEnum: string[] = ["overworld", "nether", "the_end"];
    const switchDimensionCommand: CustomCommand = {
    name: "jayly:switchdimension",
    description: "Switch dimension",
    permissionLevel: CommandPermissionLevel.GameDirectors,
    mandatoryParameters: [
    { type: CustomCommandParamType.Enum, name: "jayly:dimension" },
    ],
    };
    // registerEnum() must be called before registerCommand()
    init.customCommandRegistry.registerEnum("jayly:dimension", dimensionsEnum);
    init.customCommandRegistry.registerCommand(
    switchDimensionCommand,
    switchDimensionsCommand,
    );
    });

    function switchDimensionsCommand(
    origin: CustomCommandOrigin,
    dimensionId: string,
    ): CustomCommandResult {
    const entity = origin.sourceEntity;
    if (!entity)
    return {
    status: CustomCommandStatus.Failure,
    message: "No entity found",
    };

    system.run(() => {
    entity.teleport(entity.location, {
    dimension: world.getDimension(dimensionId),
    });
    });

    return {
    status: CustomCommandStatus.Success,
    message: `Teleported ${entity.typeId.replace("minecraft:", "")} to ${dimensionId}`,
    };
    }