Improve commands

This commit is contained in:
2026-09-27 00:38:55 -03:00
parent 535bd36d91
commit 8c7ca92891
13 changed files with 1095 additions and 109 deletions
+30 -1
View File
@@ -1,5 +1,8 @@
// what server scripts get, see "Server scripts" in MODS.md
import type { EventSignal, Face, Id, ItemStack, ModInfo, Position } from "./shared.ts";
import type { ArgumentType, CommandNode } from "../commands.ts";
export type { ArgumentType, CommandContext, CommandNode, Suggestion } from "../commands.ts";
export type { EventSignal, Face, Id, ItemStack, ModInfo, Position } from "./shared.ts";
@@ -58,8 +61,16 @@ export interface ComponentRegistry {
register_item<P>(id: Id, component: ItemComponent<P>): void;
}
// commands
// commands, fabric's (brigadier's) way: see common/commands.ts and "Commands" in MODS.md
// who ran a command
export interface CommandSource {
readonly player: Player;
// answers them in chat
send_message(text: string): void;
}
// the simple way: the words after the name, split on spaces
export interface Command {
description: string;
usage: string;
@@ -67,7 +78,25 @@ export interface Command {
}
export interface CommandRegistry {
// a tree of literal() and argument() nodes, like fabric's dispatcher.register(literal("heal").then(...))
register(node: CommandNode<CommandSource>): void;
register(name: string, command: Command): void;
literal(name: string): CommandNode<CommandSource>;
argument<T>(name: string, type: ArgumentType<T, CommandSource>): CommandNode<CommandSource>;
// argument types. they parse their text and suggest what could go there as players type
types: {
integer(min?: number, max?: number): ArgumentType<number, CommandSource>;
number(min?: number, max?: number): ArgumentType<number, CommandSource>;
boolean(): ArgumentType<boolean, CommandSource>;
word(): ArgumentType<string, CommandSource>;
greedy_string(): ArgumentType<string, CommandSource>;
choice<T extends string>(options: readonly T[]): ArgumentType<T, CommandSource>;
// an item or block id. without a namespace, whichever has that name
item(): ArgumentType<Id, CommandSource>;
block(): ArgumentType<Id, CommandSource>;
// an online player's name
player(): ArgumentType<Player, CommandSource>;
};
}
// events