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
+104 -16
View File
@@ -2,11 +2,13 @@
// see "Server scripts" in MODS.md. parts not built yet throw when used, saying so
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/common/inventory.ts";
import { get_state_value, set_state_value } from "$/common/utils.ts";
import { display_name, get_state_value, set_state_value } from "$/common/utils.ts";
import type {
BlockComponent,
BlockRef,
Command,
CommandRegistry,
CommandSource,
Container,
ContainerScreenOptions,
EventSignal,
@@ -19,6 +21,21 @@ import type {
ServerContext,
} from "$/common/mod_api/server.ts";
import { ModLoadError } from "$/common/mod_loader.ts";
import {
argument,
block as block_argument,
boolean,
choice,
CommandDispatcher,
CommandError,
CommandNode,
greedy_string,
integer,
item,
literal,
number,
word,
} from "$/common/commands.ts";
import { AIR_ID } from "$/common/protocol.ts";
import type { GameServer } from "./game_server.ts";
import type { OpenScreen, ServerPlayer } from "./player.ts";
@@ -83,7 +100,10 @@ interface Timer {
export class ModRuntime {
block_components = new Map<string, { mod: string; component: BlockComponent }>();
item_components = new Map<string, { mod: string; component: ItemComponent }>();
commands = new Map<string, { mod: string; name: string; command: Command }>();
// every command, the engine's and mods', see common/commands.ts
commands = new CommandDispatcher<ServerCommandSource>();
// who registered each command name: "engine" or a mod's id
command_owners = new Map<string, string>();
before: Before = {
block_break: new Signal(),
@@ -192,6 +212,32 @@ export class ModRuntime {
}
}
// adds a command, and for mods the same command as /mod:name so it can always be told apart
register_command(owner: string, node: CommandNode<ServerCommandSource>) {
if (!/^[a-z0-9_]+$/.test(node.name)) {
throw new Error(`command name "${node.name}" must be a-z, 0-9 and _`);
}
const existing = this.command_owners.get(node.name);
if (existing) {
throw new Error(
existing === "engine"
? `/${node.name} belongs to the engine`
: `/${node.name} is also registered by ${existing}`,
);
}
this.commands.register(node);
this.command_owners.set(node.name, owner);
if (owner !== "engine") {
const alias = new CommandNode<ServerCommandSource>(`${owner}:${node.name}`);
alias.children.push(...node.children);
alias.command = node.command;
alias.requirement = node.requirement;
alias.description = node.description;
this.commands.root.then(alias);
this.command_owners.set(alias.name, owner);
}
}
// components on a block, with their params from its json
components_of(block_id: string): { mod: string; id: string; component: BlockComponent; params: unknown }[] {
const block = EverythingRegistry.get<{ components?: Record<string, unknown> }>("blocks", block_id);
@@ -309,23 +355,39 @@ export class ModRuntime {
},
},
commands: {
register: (name, command) => {
register: (name_or_node: string | CommandNode<CommandSource>, command?: Command) => {
setup_only("commands");
if (!/^[a-z0-9_]+$/.test(name)) {
throw new ModLoadError(mod, `command name "${name}" must be a-z, 0-9 and _`);
const node = typeof name_or_node === "string"
? simple_command(name_or_node, command!)
: name_or_node as unknown as CommandNode<ServerCommandSource>;
try {
this.register_command(mod, node as CommandNode<ServerCommandSource>);
} catch (e) {
throw new ModLoadError(mod, (e as Error).message);
}
if (["give", "tps", "time"].includes(name)) {
throw new ModLoadError(mod, `/${name} belongs to the engine`);
}
const existing = this.commands.get(name);
if (existing) {
throw new ModLoadError(mod, `/${name} is also registered by ${existing.mod}`);
}
const entry = { mod, name, command };
this.commands.set(name, entry);
this.commands.set(`${mod}:${name}`, entry);
},
},
literal: (name) => literal<CommandSource>(name),
argument: (name, type) => argument(name, type),
types: {
integer,
number,
boolean,
word,
greedy_string,
choice,
item: () => item_argument(),
block: () => block_argument(display_name_of_block),
player: () => ({
parse: (text: string) => {
const found = game().players().find((p) => p.name === text);
if (!found) throw new CommandError(`${text} isn't online`);
return this.player(found);
},
suggest: (partial: string) =>
game().players().filter((p) => p.name.startsWith(partial)).map((p) => ({ text: p.name })),
}),
},
} as CommandRegistry,
events: {
before: map_signals(this.before, mod),
after: map_signals(this.after, mod),
@@ -587,6 +649,32 @@ function not_yet_object<T>(mod: string, name: string, where: string): T {
const MAX_CONTAINER_SIZE = 256;
// who ran a command, as the engine sees it: what mods see plus the engine's player
export interface ServerCommandSource extends CommandSource {
server_player: ServerPlayer;
}
// register(name, { run }): the words after the name go to run, split on spaces
function simple_command(name: string, command: Command): CommandNode<ServerCommandSource> {
const run = (args: string[], source: ServerCommandSource) => command.run(args, source.player);
return literal<ServerCommandSource>(name)
.describe(command.description)
.executes((context) => run([], context.source))
.then(
argument<string, ServerCommandSource>("args", greedy_string()).executes((context) =>
run(context.get<string>("args").split(/\s+/).filter((word) => word.length > 0), context.source)
),
);
}
export function item_argument() {
return item((id) => display_name(id));
}
function display_name_of_block(id: string) {
return display_name(id);
}
// the engine's item stacks as mods see them: { id, count, data }
export function item_api(stack: EngineItemStack): ItemStack {
return {