Skip to content

Commit c3905a6

Browse files
committed
add some sifter helpers
1 parent 1b8b56f commit c3905a6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/sifter/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
* This plugin is part of the bricklib project.
44
*/
55

6+
import { Player } from '@minecraft/server';
67
import * as bricklib from '../bricklib/index.js';
8+
import { parseVerb } from './parser.js';
9+
import { ParseContext, ParseResult } from './state.js';
10+
import type { CmdVerb } from './types.ts';
711

812
export * from './parser.js';
913
export * from './state.js';
@@ -13,3 +17,38 @@ export type * from './types.ts';
1317
bricklib.plugin.newPlugin('sifter', () => {
1418
/* no-op */
1519
});
20+
21+
22+
/**
23+
* Parse a command definition.
24+
* @param cmdDef The parsing rules.
25+
* @param args The tokenized arguments.
26+
* @returns The parsing result.
27+
*/
28+
export function parseCommand(cmdDef: CmdVerb, args: string[]): ParseResult
29+
{
30+
const ctx = new ParseContext(args);
31+
ctx.consumeToken(); /* skip the first arg (name of the cmd) */
32+
const res = new ParseResult();
33+
parseVerb(ctx, res, cmdDef);
34+
return res;
35+
}
36+
37+
/**
38+
* Register a custom command using sifter as its parser.
39+
* @param mgr The bricklib custom command manager.
40+
* @param cmdDef The command parsing definition.
41+
* @param fn The callback function.
42+
* @returns The registered names for the cmd.
43+
*/
44+
export function registerCommand(
45+
mgr: bricklib.command.CommandManager, cmdDef: CmdVerb,
46+
fn: (args: ParseResult, src: Player) => number): string[]
47+
{
48+
let names = [cmdDef.name];
49+
if (cmdDef.aliases)
50+
names = names.concat(cmdDef.aliases);
51+
mgr.registerCommand(names,
52+
(src, args) => fn(parseCommand(cmdDef, args), src));
53+
return names;
54+
}

0 commit comments

Comments
 (0)