This is a piece of code to create customs commands easier in mcbe.
First you need to import the commands handler into your main script file (ex: index.js)
file : index.js
import './commands-builder/index';
Then, create a new file for the command, and make sur you create a new instance of it with the new keyword :
file : ./commands/ban.command.ts
import { Player } from "@minecraft/server";
import Command from "commands-builder/command";
export default class BanCommand extends Command {
public readonly name : string = 'ban'
public readonly description : string = 'This is a ban command!'
public readonly alliases : Array<string> = ['b']
public readonly operatorOnly: boolean = true;
// TAG NEEDED TO EXECUTE THE COMMAND
public readonly permissions : Array<string> = ['admin', 'moderator']
public onExecute(sender : Player, args : Array<string>) : void {
sender.sendMessage(`§cYou banned ${args.join(' ')}!`)
}
}
To finish, each time you create a new command in a new file, you need to register the file path like so:
file : ./commands-builder/commands.register.ts
export const REGISTERED_COMMANDS = [
'./ban.command', // path in the curretn directory (ex : "./admin/ban.command" or "/admin/claim-command" or "shop-command")
'/help.command',
]
In your command class you can set the command name, description, alliases and also permissions. Permissions is an array of tag that the player needed to execute the command. In the command onExecute function you can use the sender and the command args.
You can add prefixes you wanted in the commands config file.
By default their is only 2 prefixes '!' and '?'.
file : ./commands-builder/commands.config.ts
export const COMMANDS_CONFIG = {
prefixes : ['!', '?'],
helpCommand : ['help', 'aide'],
helpCommandsByPage : 5
}
This help show all registered commands.
You can configure how many commands are display each page in the ./commands-builder/commands.config.ts
.
This help command is almost the same as vanilla one, so it's translate for each languages (for green text).