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
+60
View File
@@ -73,6 +73,25 @@ export function setup(ctx: ServerContext) {
}
},
});
// fabric's way
const { literal, argument, types } = ctx.commands;
ctx.commands.register(
literal("shout")
.describe("Says something to someone")
.requires((source) => source.player.name !== "bob")
.then(
argument("who", types.player()).then(
argument("mode", types.choice(["loud", "quiet"])).then(
argument("text", types.greedy_string()).executes((context) => {
const who = context.get<{ name: string }>("who").name;
context.source.send_message(\`\${who} \${context.get("mode")}: \${context.get("text")}\`);
}),
),
),
),
);
ctx.commands.register(literal("boom").executes(() => { throw new Error("kaboom"); }));
}
`;
@@ -254,6 +273,47 @@ Deno.test("/tps reports the loop, and mods can't take engine commands", async ()
Deno.removeSync(clash, { recursive: true });
});
Deno.test("mods build commands fabric's way: typed arguments, suggestions, requirements and aliases", async () => {
const { join, send, take, dir } = await setup();
join(2, "bob");
take(1);
const suggest = (id: number, text: string) => {
send(id, { type: "suggest", id: 7, text });
return take(id).find((m) => m.type === "suggestions");
};
send(1, { type: "chat", text: "/shout bob loud hi there" });
assertEquals(take(1).at(-1).text, "bob loud: hi there");
send(1, { type: "chat", text: "/testmod:shout alice quiet hey" });
assertEquals(take(1).at(-1).text, "alice quiet: hey");
send(1, { type: "chat", text: "/shout nobody loud hi" });
assertEquals(take(1).at(-1).text, "nobody isn't online");
send(1, { type: "chat", text: "/shout bob" });
assertEquals(take(1).at(-1).text, "Not enough: /shout <who> <mode> <text>");
assertEquals(suggest(1, "/shout b"), { type: "suggestions", id: 7, start: 7, suggestions: [{ text: "bob" }] });
assertEquals(suggest(1, "/shout bob ").suggestions.map((s: { text: string }) => s.text), ["loud", "quiet"]);
assertEquals(suggest(1, "/sh").suggestions.map((s: { text: string }) => s.text), ["shout"]);
// bob can't, so it isn't suggested to him either
assertEquals(suggest(2, "/sh").suggestions, []);
send(2, { type: "chat", text: "/shout alice loud hi" });
assertEquals(take(2).at(-1).text, "Unknown command /shout");
// a mod's bug isn't the player's fault
const error = console.error;
const logged: unknown[][] = [];
console.error = (...args: unknown[]) => logged.push(args);
send(1, { type: "chat", text: "/boom" });
console.error = error;
assertEquals(take(1).at(-1).text, "Something went wrong running /boom");
assert(String(logged[0]?.[0]).includes("[testmod]"), String(logged[0]));
// /help lists it with its usage
send(1, { type: "chat", text: "/help shout" });
assertEquals(take(1).at(-1).text, "/shout <who> <mode> <text> - Says something to someone");
Deno.removeSync(dir, { recursive: true });
});
Deno.test("container screens: filters, properties, closing, saving, and dropping what's inside", async () => {
const { game, send, take, log, dir } = await setup();
game.set_block(1, 100, 0, "testmod:coal_box");