API Specstable
2 min read time

Documentation

Commands

Registering and managing commands through PixelScript.

apiscript

Commands

Commands are everything in the Minecraft world, and have first-class support in PixelScript.

All scripts have the registerCommand, registerTabCompleter and registerCommandAlias functions available globally, which can be used to register commands and their aliases. Commands registered are directly tied to the script, and will be automatically unregistered when the script is unloaded. These commands are directly inserted into the server's command map, and have the ability to override existing commands if needed.

Registering commands with registerCommand(command: string, executor: (sender: CommandSender, args: string[]) => void): void

Registers a command with the given name and executor function. The executor function will be called whenever the command is executed, with the sender and arguments passed in.

Simple example of registering a command:

const GameMode = $.GameMode; const gameModes = { 'survival': GameMode.SURVIVAL, 'creative': GameMode.CREATIVE, 'adventure': GameMode.ADVENTURE, 'spectator': GameMode.SPECTATOR, '0': GameMode.SURVIVAL, '1': GameMode.CREATIVE, '2': GameMode.ADVENTURE, '3': GameMode.SPECTATOR } registerCommand('gamemode', (sender, args) => { if (!(sender instanceof $.Player)) { sender.sendRichMessage('<red>This command can only be run by a player!</red>'); return; } if (!sender.hasPermission('command.gamemode') || !sender.isOp()) { sender.sendRichMessage('<red>You do not have permission to use this command.</red>'); return; } if (args.length === 0) { sender.sendMessage('Usage: /gamemode <mode>'); return; } const mode = args[0].toLowerCase(); const mappedMode = gameModes[mode]; if (!mappedMode) { sender.sendRichMessage('<red>Unknown gamemode: ' + mode + '.</red>'); return; } sender.setGameMode(mappedMode); sender.sendRichMessage('<green>Your gamemode has been set to ' + mode + '.</green>'); });

Registering tab completers with registerTabCompleter(command: string, completer: (sender: CommandSender, args: string[]) => string[]): void

Registers a tab completer for the given command. The completer function will be called whenever the player presses tab while typing the command, and should return an array of possible completions. Simple example of registering a tab completer:

registerTabCompleter('gamemode', (sender, args) => { const completions = []; if (args.length === 1) { const modes = ['survival', 'creative', 'adventure', 'spectator', '0', '1', '2', '3']; const input = args[0].toLowerCase(); for (const mode of modes) { if (mode.startsWith(input)) { completions.push(mode); } } } return completions; });

Registering command aliases with registerCommandAlias(command: string, aliases: string | string[]): void

Registers one or more aliases for the given command. When a player executes the alias, it will be treated as if they executed the original command. Simple example of registering command aliases:

registerCommandAlias('gamemode', ['gm', 'gmode']);
Documentation in early stages. Not meant for public consumption.