serenity-command-builder
Version:
A simple command builder for SerenityJS.
95 lines (88 loc) • 3.05 kB
TypeScript
import { Plugin, PluginEvents, PluginPriority } from '@serenityjs/plugins';
import { Dimension, Entity, World, CommandResponse } from '@serenityjs/core';
type CommandParameters = {
[key: string]: any;
};
type MappedParameters<T> = {
[P in keyof T]: T[P] extends new (...args: any[]) => any ? InstanceType<T[P]> : T[P];
};
type ExecuteCallback<T extends CommandParameters> = (world: World, origin: Dimension | Entity, params: MappedParameters<T>) => CommandResponse;
type FailureCallback = (origin: Dimension | Entity) => void;
/**
* Used for class oriented overload creation.
*
* This class handles the creation of overloads that can be used in the command builder.
*/
declare class CommandOverload<T extends CommandParameters> {
/**
* The parameters/arguments available for this overload.
*/
readonly parameters: T;
/**
* Callback to execute when this overload is ran.
*/
execute: ExecuteCallback<T>;
constructor(parameters: T);
/**
* Sets the callback function to execute when this overload is ran.
* @param callback Function to run.
*/
onCallback(callback: ExecuteCallback<T>): this;
}
/**
* Used for class oriented command creation.
*
* This class handles the creation and registration of new commands.
*/
declare class CommandBuilder {
private static readonly unregisteredCommands;
private static readonly registeredCommands;
private name;
private description;
private overloads;
private permissions;
private debug;
onFail: FailureCallback;
/**
* @param name Name and ID that will be used to access your command in-game.
* @param description Short description that will show up alongside your command in-game.
*/
constructor(name: string, description: string);
/**
* Controls the use cases/executions of your command.
*/
addOverload(overload: CommandOverload<any>): this;
/**
* Optionally limits usage of this command to users with matching permission strings.
*/
setPermissions(permissions: string[]): this;
/**
* Optionally identifies the command as a debug command (shows up blue in-game).
*/
setDebug(debug: boolean): this;
/**
* Optionally runs this function if a matching overload cannot be found.
*/
setOnFail(callback: FailureCallback): this;
/**
* Registers your command to Serenity.
*/
register(category?: string): void;
/**
* Registers all commands through server command registry.
*/
static registerAll(plugin: Plugin, world: World): void;
}
/**
* @Plugin
*/
declare class CommandBuilderPlugin extends Plugin implements PluginEvents {
readonly priority: PluginPriority;
readonly CommandBuilder: typeof CommandBuilder;
readonly CommandOverload: typeof CommandOverload;
constructor();
onInitialize(): void;
onStartUp(): void;
}
declare const _default: CommandBuilderPlugin;
export { CommandBuilder, CommandBuilderPlugin, CommandOverload, _default as default };