Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 73 additions & 2 deletions docs/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -586,6 +590,20 @@ States that a character may have.

---

<h3 id="enum-commandflag">enum CommandFlag</h3>

```ts
const enum CommandFlag {
None = 0,
Restricted = 1,
Unlisted = 2,
}
```

Command flag.

---

<h3 id="enum-exiticon">enum ExitIcon</h3>

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

<h4 id="class-command-properties">class Command properties</h4>

* `priority` <i>(u32)</i>
* `flags` <i>(u32)</i>
* `pattern` <i>(string)</i>
* `desc` <i>(string)</i>

Expand All @@ -817,6 +837,58 @@ Sets the definition for a command field.
* <i>([Command](#class-command))</i>: This instance, allowing method chaining.


---

<h3 id="method-command-setpriority">method Command.setPriority</h3>

```ts
setPriority(priority: u32): Command
```

Sets command priority.

<h4>Parameters</h4>

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

<h4>Returns</h4>

* <i>([Command](#class-command))</i>: This instance, allowing method chaining.


---

<h3 id="method-command-setrestricted">method Command.setRestricted</h3>

```ts
setRestricted(): Command
```

Sets the command as restricted, only accessible to character able to edit
the room.

<h4>Returns</h4>

* <i>([Command](#class-command))</i>: This instance, allowing method chaining.


---

<h3 id="method-command-setunlisted">method Command.setUnlisted</h3>

```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`.

<h4>Returns</h4>

* <i>([Command](#class-command))</i>: This instance, allowing method chaining.


---

<h3 id="method-command-json">method Command.json</h3>
Expand Down Expand Up @@ -2612,8 +2684,7 @@ See also: [Writing scripts - Custom commands](https://github.com/mucklet/mucklet

* `keyword` <i>(string)</i>: Keyword for the command.
* `cmd` <i>([Command](#class-command))</i>: Command to add.
* `priority` <i>(u32)</i>: Priority for sort order (descending) and when two or
more commands match the same input. Higher priority is selected first.
* `priority` <i>(u32)</i>: Deprecated: Use Command.setPriority instead.


---
Expand Down
4 changes: 2 additions & 2 deletions examples/intercom_inside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 9 additions & 4 deletions examples/vip_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ export function onActivate(): void {
Room.addCommand(
"add",
new Command("add vip <Character>", "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 <Character>", "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()
Expand Down Expand Up @@ -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<string>()
// If the charId exists in the store, the character is a VIP.
const isVip = Store.getString(charId) != null
Expand Down
2 changes: 1 addition & 1 deletion lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 45 additions & 3 deletions lib/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1002,6 +1008,9 @@ export interface CommandField {
*/
export class Command {
private fieldDefs: Map<string, string> = new Map<string, string>();
public priority: u32 = 0;
public flags: u32 = 0;

/**
* Creates a new instance of the {@link Command} class.
*/
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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);
}

/**
Expand Down
30 changes: 28 additions & 2 deletions lib/types/host/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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;
/**
Expand Down