Skip to content

Commit aa48dac

Browse files
committed
make target param optional
1 parent 98c7db5 commit aa48dac

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

scripts/commands/index.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Dimension, Entity, world } from "mojang-minecraft";
2+
/**
3+
* Contains a method that lets you run console commands within
4+
* Minecraft.
5+
*/
6+
// tslint:disable-next-line:no-unnecessary-class
7+
export class Commands {
8+
/**
9+
* @remarks
10+
* Runs a particular command from the context.
11+
* @param commandString
12+
* Command to run. Note that command strings should not start
13+
* with slash.
14+
* @param target
15+
* Target to be used as context for the command to run
16+
* within.
17+
* @returns For commands that return data, returns a JSON structure with command response values.
18+
* @throws This function can throw errors.
19+
* @example commands.js
20+
* ```typescript
21+
* Commands.run("say You got a new high score!");
22+
* Commands.run("scoreboard players set @p score 10", world.getDimension("overworld"));
23+
* ```
24+
*/
25+
static run(commandString, target = world.getDimension("overworld")) {
26+
if (target instanceof Dimension || Entity)
27+
return target.runCommand(commandString);
28+
else
29+
throw TypeError("Native type conversion failed");
30+
}
31+
;
32+
/**
33+
* @remarks
34+
* Runs a particular command asynchronously from the context.
35+
* Where possible - and especially for
36+
* long-running operations - you should use runCommandAsync
37+
* over runCommand.
38+
* @param commandString
39+
* Command to run. Note that command strings should not start
40+
* with slash.
41+
* @param target
42+
* Target to be used as context for the command to run
43+
* within.
44+
* @returns
45+
* For commands that return data, returns a CommandResult with
46+
* an indicator of command results.
47+
* @throws This function can throw errors.
48+
*/
49+
static async runAsync(commandString, target = world.getDimension("overworld")) {
50+
if (target instanceof Dimension || Entity)
51+
return target.runCommandAsync(commandString);
52+
else
53+
throw TypeError("Native type conversion failed");
54+
}
55+
;
56+
}
57+
;

scripts/commands/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CommandResult, Dimension, Entity } from "mojang-minecraft";
1+
import { CommandResult, Dimension, Entity, world } from "mojang-minecraft";
22

33
/**
44
* Contains a method that lets you run console commands within
@@ -19,11 +19,11 @@ export class Commands {
1919
* @throws This function can throw errors.
2020
* @example commands.js
2121
* ```typescript
22-
* Commands.run("say You got a new high score!", World.getDimension("overworld"));
23-
* Commands.run("scoreboard players set @p score 10", World.getDimension("overworld"));
22+
* Commands.run("say You got a new high score!");
23+
* Commands.run("scoreboard players set @p score 10", world.getDimension("overworld"));
2424
* ```
2525
*/
26-
static run(commandString: string, target: Dimension | Entity): any {
26+
static run(commandString: string, target: Dimension | Entity = world.getDimension("overworld")): any {
2727
if (target instanceof Dimension || Entity) return target.runCommand(commandString);
2828
else throw TypeError("Native type conversion failed");
2929
};
@@ -45,7 +45,7 @@ export class Commands {
4545
* an indicator of command results.
4646
* @throws This function can throw errors.
4747
*/
48-
static async runAsync(commandString: string, target: Dimension | Entity): Promise<CommandResult> {
48+
static async runAsync(commandString: string, target: Dimension | Entity = world.getDimension("overworld")): Promise<CommandResult> {
4949
if (target instanceof Dimension || Entity) return target.runCommandAsync(commandString);
5050
else throw TypeError("Native type conversion failed");
5151
};

0 commit comments

Comments
 (0)