From d95712ea8deb6484774cd960630360cba0b5fc78 Mon Sep 17 00:00:00 2001 From: Accipiter Nisus Date: Mon, 22 Sep 2025 09:37:34 +0200 Subject: [PATCH] GH-27: Added command flags. Moved priority to be held by Command instead of passed separately to Room.addCommand. --- docs/documentation.md | 75 ++++++++++++++++++++++++++++++++++++- examples/intercom_inside.ts | 4 +- examples/vip_list.ts | 13 +++++-- lib/env.ts | 2 +- lib/host.ts | 48 ++++++++++++++++++++++-- lib/types/host/index.d.ts | 30 ++++++++++++++- 6 files changed, 158 insertions(+), 14 deletions(-) diff --git a/docs/documentation.md b/docs/documentation.md index 3ec8482..e593f4d 100644 --- a/docs/documentation.md +++ b/docs/documentation.md @@ -298,6 +298,7 @@ export function onResponse(     [type Timestamp](#type-timestamp) [Enums](#enums)     [enum CharState](#enum-charstate) +    [enum CommandFlag](#enum-commandflag)     [enum ExitIcon](#enum-exiticon)     [enum ExitNav](#enum-exitnav)     [enum IdleLevel](#enum-idlelevel) @@ -314,6 +315,9 @@ export function onResponse(         [method useExit](#method-cmdaction-useexit)     [class Command](#class-command)         [method field](#method-command-field) +        [method setPriority](#method-command-setpriority) +        [method setRestricted](#method-command-setrestricted) +        [method setUnlisted](#method-command-setunlisted)         [method json](#method-command-json)     [class ExitAction](#class-exitaction)         [method useExit](#method-exitaction-useexit) @@ -586,6 +590,20 @@ States that a character may have. --- +

enum CommandFlag

+ +```ts +const enum CommandFlag { + None = 0, + Restricted = 1, + Unlisted = 2, +} +``` + +Command flag. + +--- +

enum ExitIcon

```ts @@ -793,6 +811,8 @@ Creates a new instance of the [Command](#class-command) class.

class Command properties

+* `priority` (u32) +* `flags` (u32) * `pattern` (string) * `desc` (string) @@ -817,6 +837,58 @@ Sets the definition for a command field. * ([Command](#class-command)): This instance, allowing method chaining. +--- + +

method Command.setPriority

+ +```ts +setPriority(priority: u32): Command +``` + +Sets command priority. + +

Parameters

+ +* `priority` (u32): Priority for sort order (descending) and when two or more +commands match the same input. Higher priority is selected first. + +

Returns

+ +* ([Command](#class-command)): This instance, allowing method chaining. + + +--- + +

method Command.setRestricted

+ +```ts +setRestricted(): Command +``` + +Sets the command as restricted, only accessible to character able to edit +the room. + +

Returns

+ +* ([Command](#class-command)): This instance, allowing method chaining. + + +--- + +

method Command.setUnlisted

+ +```ts +setUnlisted(): Command +``` + +Sets the command as unlisted, not showing up in the interface. It can +still be used, and will be listed using `list commands`. + +

Returns

+ +* ([Command](#class-command)): This instance, allowing method chaining. + + ---

method Command.json

@@ -2612,8 +2684,7 @@ See also: [Writing scripts - Custom commands](https://github.com/mucklet/mucklet * `keyword` (string): Keyword for the command. * `cmd` ([Command](#class-command)): Command to add. -* `priority` (u32): Priority for sort order (descending) and when two or -more commands match the same input. Higher priority is selected first. +* `priority` (u32): Deprecated: Use Command.setPriority instead. --- diff --git a/examples/intercom_inside.ts b/examples/intercom_inside.ts index 1a56a8d..39bb4cd 100644 --- a/examples/intercom_inside.ts +++ b/examples/intercom_inside.ts @@ -36,8 +36,8 @@ function updateOutside(): void { export function onActivate(): void { // Add the "turn on intercom" and "turn off intercom" commands. // Priority (20 and 10) is used to sort "on" before "off". - Room.addCommand("on", new Command("turn on intercom", onHelp), 20) - Room.addCommand("off", new Command("turn off intercom", offHelp), 10) + Room.addCommand("on", new Command("turn on intercom", onHelp).setPriority(20)) + Room.addCommand("off", new Command("turn off intercom", offHelp).setPriority(10)) // Start listening to messages from the outside room script. Script.listen([outside]) // Update the outside room with the current intercom status. diff --git a/examples/vip_list.ts b/examples/vip_list.ts index 34471be..c872246 100644 --- a/examples/vip_list.ts +++ b/examples/vip_list.ts @@ -24,16 +24,21 @@ export function onActivate(): void { Room.addCommand( "add", new Command("add vip ", "Add a character to the VIP list.") - .field("Character", new Field.Char("Character to add.")), + .field("Character", new Field.Char("Character to add.")) + .setRestricted() // Only let room owner edit list + .setPriority(30), ) Room.addCommand( "remove", new Command("remove vip ", "Remove a character from the VIP list.") - .field("Character", new Field.Char("Character to remove.")), + .field("Character", new Field.Char("Character to remove.")) + .setRestricted() // Only let room owner edit list + .setPriority(20), ) Room.addCommand( "list", - new Command("list vip", "List all VIP characters."), + new Command("list vip", "List all VIP characters.") + .setPriority(10), ) // Listen for script requests. Script.listen() @@ -120,7 +125,7 @@ export function onCommand(addr: string, cmdAction: CmdAction): void { // onRequest is called when another script sends a request to this script. export function onRequest(addr: string, request: Request): void { if (request.topic == "isVip") { - // Parse the data passed as arguments. It should be the charId. + // Parse the data passed as arguments. It should be the character ID. const charId = request.parseData() // If the charId exists in the store, the character is a VIP. const isVip = Store.getString(charId) != null diff --git a/lib/env.ts b/lib/env.ts index f44bda5..2c31a01 100644 --- a/lib/env.ts +++ b/lib/env.ts @@ -54,7 +54,7 @@ export declare namespace Room { export function unlistenExit(exitId: string | null): boolean @external("env", "room.addCommand") - export function addCommand(key: string, cmd: string, priority: u32): void + export function addCommand(key: string, cmd: string, priority: u32, flags: u32): void @external("env", "room.removeCommand") export function removeCommand(key: string): boolean diff --git a/lib/host.ts b/lib/host.ts index cea687c..6320653 100644 --- a/lib/host.ts +++ b/lib/host.ts @@ -343,6 +343,12 @@ export const enum ExitIcon { In = 11, Out = 12, } +/** Command flag. */ +export const enum CommandFlag { + None = 0, + Restricted = 1, + Unlisted = 2, +} // @ts-expect-error @inline @@ -1002,6 +1008,9 @@ export interface CommandField { */ export class Command { private fieldDefs: Map = new Map(); + public priority: u32 = 0; + public flags: u32 = 0; + /** * Creates a new instance of the {@link Command} class. */ @@ -1027,6 +1036,37 @@ export class Command { return this; } + /** + * Sets command priority. + * @param priority Priority for sort order (descending) and when two or more + * commands match the same input. Higher priority is selected first. + * @returns This instance, allowing method chaining. + */ + setPriority(priority: u32): Command { + this.priority = priority; + return this; + } + + /** + * Sets the command as restricted, only accessible to character able to edit + * the room. + * @returns This instance, allowing method chaining. + */ + setRestricted(): Command { + this.flags |= CommandFlag.Restricted; + return this; + } + + /** + * Sets the command as unlisted, not showing up in the interface. It can + * still be used, and will be listed using `list commands`. + * @returns This instance, allowing method chaining. + */ + setUnlisted(): Command { + this.flags |= CommandFlag.Unlisted; + return this; + } + /** * Converts the command into a JSON structure. */ @@ -1463,11 +1503,13 @@ export namespace Room { * * @param keyword - Keyword for the command. * @param cmd - Command to add. - * @param priority - Priority for sort order (descending) and when two or - * more commands match the same input. Higher priority is selected first. + * @param priority - Deprecated: Use Command.setPriority instead. */ export function addCommand(keyword: string, cmd: Command, priority: u32 = 0): void { - return room_binding.addCommand(keyword, cmd.json(), priority); + if (cmd.priority != 0) { + priority = cmd.priority; + } + return room_binding.addCommand(keyword, cmd.json(), priority, cmd.flags); } /** diff --git a/lib/types/host/index.d.ts b/lib/types/host/index.d.ts index dae90d3..0f0e801 100644 --- a/lib/types/host/index.d.ts +++ b/lib/types/host/index.d.ts @@ -329,6 +329,12 @@ declare const enum ExitIcon { In = 11, Out = 12 } +/** Command flag. */ +declare const enum CommandFlag { + None = 0, + Restricted = 1, + Unlisted = 2 +} /** * CmdAction is a command action triggered by a character. */ @@ -704,6 +710,8 @@ declare class Command { pattern: string; desc: string; private fieldDefs; + priority: u32; + flags: u32; /** * Creates a new instance of the {@link Command} class. */ @@ -715,6 +723,25 @@ declare class Command { * @returns This instance, allowing method chaining. */ field(key: string, def: CommandField): Command; + /** + * Sets command priority. + * @param priority Priority for sort order (descending) and when two or more + * commands match the same input. Higher priority is selected first. + * @returns This instance, allowing method chaining. + */ + setPriority(priority: u32): Command; + /** + * Sets the command as restricted, only accessible to character able to edit + * the room. + * @returns This instance, allowing method chaining. + */ + setRestricted(): Command; + /** + * Sets the command as unlisted, not showing up in the interface. It can + * still be used, and will be listed using `list commands`. + * @returns This instance, allowing method chaining. + */ + setUnlisted(): Command; /** * Converts the command into a JSON structure. */ @@ -1043,8 +1070,7 @@ declare namespace Room { * * @param keyword - Keyword for the command. * @param cmd - Command to add. - * @param priority - Priority for sort order (descending) and when two or - * more commands match the same input. Higher priority is selected first. + * @param priority - Deprecated: Use Command.setPriority instead. */ function addCommand(keyword: string, cmd: Command, priority?: u32): void; /**