Skip to content

Commit dd81777

Browse files
committed
refactor: extract bricklib.args into sift
1 parent 3c78af7 commit dd81777

File tree

7 files changed

+30
-15
lines changed

7 files changed

+30
-15
lines changed

src/bricklib/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export const VERSION = '0.1.0-beta';
77

88
/* modules */
99
export { default as config } from './config.js';
10-
export * as args from './args/index.js';
1110
export * as command from './command.js';
1211
export * as database from './database.js';
1312
export * as events from './events.js';

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { FormCancelationReason } from '@minecraft/server-ui';
22
import * as bricklib from './bricklib/index.js';
33
import * as gatepass from './gatepass/index.js';
4+
import * as sift from './sift/index.js';
45

56
/* load plugins */
6-
bricklib.plugin.loadPlugin('gatepass');
7+
const load = bricklib.plugin.loadPlugin;
8+
load('gatepass');
9+
load('sift');
710

811

912
const mgr = new bricklib.command.CommandManager();
@@ -23,12 +26,12 @@ const def = {
2326
args: [
2427
{
2528
id: 'text',
26-
type: bricklib.args.parsers.variadic(bricklib.args.parsers.string()),
29+
type: sift.parsers.variadic(sift.parsers.string()),
2730
}
2831
]
2932
};
3033

31-
mgr.registerCommand(...bricklib.args.makeCommand(def, (args, src) => {
34+
mgr.registerCommand(...sift.makeCommand(def, (args, src) => {
3235
gatepass.assertPermission('chat.echo', src);
3336
src.sendMessage(args.text.join(' '));
3437
return 0;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type * as bricklib from '../bricklib/index.js';
12
import type { ArgTokenStream } from './tokens.js';
2-
import type { RawString } from '../rawtext.js';
33

44
/**
55
* A command argument parser.
@@ -17,7 +17,7 @@ export type ParseResult<T extends {} = Record<PropertyKey, any>> = T;
1717
export interface CmdArgument<T = any>
1818
{
1919
id: PropertyKey,
20-
help?: string | RawString,
20+
help?: string | bricklib.rawtext.RawString,
2121
name?: string,
2222

2323
type: TypeParser<T>,
@@ -33,7 +33,7 @@ export interface CmdArgument<T = any>
3333
export interface CmdOption
3434
{
3535
id: PropertyKey,
36-
help?: string | RawString,
36+
help?: string | bricklib.rawtext.RawString,
3737

3838
long?: string[],
3939
short?: string,
@@ -46,7 +46,7 @@ export interface CmdOption
4646
export interface CmdVerb
4747
{
4848
id: PropertyKey,
49-
help?: string | RawString,
49+
help?: string | bricklib.rawtext.RawString,
5050

5151
name: string,
5252
aliases?: string[],
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1+
/**
2+
* Sift -- A flexible command parser for bricklib.
3+
* This plugin is part of the bricklib project.
4+
*/
5+
6+
/* TODO: help-text gen, cmd builder, and more type parsers... */
7+
8+
import * as bricklib from '../bricklib/index.js';
19
import { Player } from '@minecraft/server';
2-
import { CommandCallback, CommandManager } from '../command.js';
310
import { parseVerb } from './parser.js';
411
import { ArgTokenStream } from './tokens.js';
512
import type { CmdVerb, ParseResult } from './defs.ts';
613

14+
715
export type * from './defs.ts';
816
export * from './parser.js';
917
export * from './tokens.js';
1018
export * as parsers from './types.js';
1119

20+
bricklib.plugin.newPlugin('sift', () => {
21+
/* no-op */
22+
});
23+
24+
1225
/**
1326
* Parse a custom command.
1427
* @param def The command parsing definition.
@@ -25,25 +38,25 @@ export function parseCommand(def: CmdVerb, args: string[]): ParseResult
2538

2639
/**
2740
* Make a command def that you can pass to
28-
* {@link CommandManager.registerCommand}.
41+
* {@link bricklib.command.CommandManager.registerCommand}.
2942
* @param def The command definition.
3043
* @param fn The callback.
3144
* @returns Array of args.
3245
* @example
3346
* ```ts
34-
* import * as bricklib from './bricklib/index.js';
47+
* import * as sift from './sift/index.js';
3548
* const def = {
3649
* id: 'echo',
3750
* name: 'echo',
3851
* args: [
3952
* {
4053
* id: 'text',
41-
* type: bricklib.args.parsers.string(),
54+
* type: sift.parsers.string(),
4255
* }
4356
* ]
4457
* };
4558
*
46-
* mgr.registerCommand(...bricklib.args.makeCommand(def, (args, src) => {
59+
* mgr.registerCommand(...sift.makeCommand(def, (args, src) => {
4760
* src.sendMessage(args.text);
4861
* return 0;
4962
* }));
@@ -52,9 +65,9 @@ export function parseCommand(def: CmdVerb, args: string[]): ParseResult
5265
export function makeCommand<T = any>(
5366
def: CmdVerb,
5467
fn: (args: ParseResult<T>, src: Player) => number
55-
): [string[], CommandCallback]
68+
): [string[], bricklib.command.CommandCallback]
5669
{
57-
const out: [string[], CommandCallback] = [null, null];
70+
const out: [string[], bricklib.command.CommandCallback] = [null, null];
5871
out[0] = [def.name];
5972
if (def.aliases)
6073
out[0] = out[0].concat(def.aliases);

0 commit comments

Comments
 (0)