|
| 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 | +; |
0 commit comments