forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
104 lines (100 loc) · 3.8 KB
/
index.ts
File metadata and controls
104 lines (100 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Script example for ScriptAPI
// Author: Jayly <https://github.com/JaylyDev>
// Project: https://github.com/JaylyDev/ScriptAPI
import { CommandResult, Dimension, Entity, world, Player } from "@minecraft/server";
/**
* Main class for custom command functions, with the player that execute
* this command with additional arguments split in an iterable iterator
* string array.
*/
export class Command {
private __player: Player;
public argv: IterableIterator<string>;
public get player(): Player { return this.__player; }
public get argv0 (): string { return this.argv.next().value; };
constructor (argv: string[], player: Player) {
this.argv = (function* () { for (let arg of argv) yield arg; })();
this.__player = player;
};
};
/**
* Contains a method that lets you run console commands within
* Minecraft.
*/
// tslint:disable-next-line:no-unnecessary-class
export class Commands {
/**
* @remarks
* Runs a particular command synchronously from the context.
* @param commandString
* Command to run. Note that command strings should not start
* with slash.
* @param target
* Target to be used as context for the command to run
* within.
* @returns
* For commands that return data, returns a CommandResult with
* an indicator of command results.
* @throws This function can throw errors.
*/
static run(commandString: string, target: Dimension | Entity = world.getDimension("overworld")): CommandResult {
if (target instanceof Dimension || Entity) return target.runCommand(commandString);
else throw TypeError("Native type conversion failed");
};
/**
* @remarks
* Runs a particular command asynchronously from the context.
* Where possible - and especially for
* long-running operations - you should use runCommandAsync
* over runCommand.
* @param commandString
* Command to run. Note that command strings should not start
* with slash.
* @param target
* Target to be used as context for the command to run
* within.
* @returns
* For commands that return data, returns a CommandResult with
* an indicator of command results.
* @throws This function can throw errors.
*/
static async runAsync(commandString: string, target: Dimension | Entity = world.getDimension("overworld")): Promise<CommandResult> {
if (target instanceof Dimension || Entity) return await target.runCommandAsync(commandString);
else throw TypeError("Native type conversion failed");
};
/**
* @remarks
* Registers a new custom command. This command will become
* available in Minecraft via [prefix][command].
* @param prefix
* The prefix of this specific command. (Case sensitive)
* @param command
* Name of this specific command. (Case sensitive)
* @param commandFunction
* Implementation of the command function.
* @throws
* This function can throw error: You are not allow to register a new slash command.
* @example example1.js
* ```typescript
* Commands.register("!", "test", function (arg) {
* arg.player.runCommandAsync(`say ${arg.argv0} ${JSON.stringify([...arg.argv])}`);
* });
* ```
*/
public static register (prefix: string, command: string, commandFunction: (arg: Command) => void): void {
if (prefix.startsWith("/")) throw Error ("Unable to register slash commands.");
world.beforeEvents.chatSend.subscribe((arg) => {
var argv = arg.message.split(/(".*?"|[^"\s]+)+(?=\s*|\s*$)/g).filter( e => e.trim().length > 0);
if (argv[0] === `${prefix}${command}`) {
arg.cancel = true;
try {
commandFunction(new Command(argv, arg.sender));
} catch (err) {
let { statusMessage } = JSON.parse(err);
console.error(err);
arg.sender.sendMessage(`§c${statusMessage}`);
};
};
});
};
};