@nativewrappers/common 0.0.60 → 0.0.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Command.d.ts +1 -0
- package/Command.js +89 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +1 -1
package/Command.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/Command.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { GlobalData } from "./GlobalData";
|
|
2
|
+
const commands = [];
|
|
3
|
+
if (GlobalData.IS_SERVER) {
|
|
4
|
+
on("playerJoining", () => emitNet("chat:addSuggestions", source, commands));
|
|
5
|
+
}
|
|
6
|
+
class Command {
|
|
7
|
+
name;
|
|
8
|
+
help;
|
|
9
|
+
params;
|
|
10
|
+
#handler;
|
|
11
|
+
constructor(name, help, handler, params, restricted = true) {
|
|
12
|
+
this.name = name;
|
|
13
|
+
this.help = help;
|
|
14
|
+
this.params = params;
|
|
15
|
+
const commandHandler = (source, args, raw) => this.call(source, args, raw);
|
|
16
|
+
this.#handler = handler;
|
|
17
|
+
this.name = `/${name}`;
|
|
18
|
+
typeof name === "string"
|
|
19
|
+
? RegisterCommand(name, commandHandler, !!restricted)
|
|
20
|
+
: name.forEach((name) => RegisterCommand(name, commandHandler, !!restricted));
|
|
21
|
+
if (params) {
|
|
22
|
+
params.forEach((param) => {
|
|
23
|
+
if (param.type)
|
|
24
|
+
param.help = param.help
|
|
25
|
+
? `${param.help} (type: ${param.type})`
|
|
26
|
+
: `(type: ${param.type})`;
|
|
27
|
+
});
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
if (GlobalData.IS_SERVER) {
|
|
30
|
+
commands.push(this);
|
|
31
|
+
return emitNet("chat:addSuggestions", -1, this);
|
|
32
|
+
}
|
|
33
|
+
emit("chat:addSuggestion", this);
|
|
34
|
+
}, 100);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
mapArguments(source, args, raw) {
|
|
38
|
+
const mapped = {
|
|
39
|
+
source,
|
|
40
|
+
raw,
|
|
41
|
+
};
|
|
42
|
+
if (!this.params)
|
|
43
|
+
return mapped;
|
|
44
|
+
const result = this.params.every((param, index) => {
|
|
45
|
+
const arg = args[index];
|
|
46
|
+
let value = arg;
|
|
47
|
+
switch (param.type) {
|
|
48
|
+
case "number":
|
|
49
|
+
value = +arg;
|
|
50
|
+
break;
|
|
51
|
+
case "string":
|
|
52
|
+
value = !Number(arg) ? arg : false;
|
|
53
|
+
break;
|
|
54
|
+
case "playerId":
|
|
55
|
+
if (GlobalData.IS_SERVER) {
|
|
56
|
+
value = arg === "me" ? source : +arg;
|
|
57
|
+
if (!value || !DoesPlayerExist(value.toString()))
|
|
58
|
+
value = undefined;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
value = arg === "me" ? GetPlayerServerId(PlayerId()) : +arg;
|
|
62
|
+
if (!value || GetPlayerFromServerId(value) === -1)
|
|
63
|
+
value = undefined;
|
|
64
|
+
}
|
|
65
|
+
break;
|
|
66
|
+
case "longString":
|
|
67
|
+
value = raw.substring(raw.indexOf(arg));
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
if (value === undefined && (!param.optional || (param.optional && arg))) {
|
|
71
|
+
return Citizen.trace(`^1command '${raw.split(" ")[0] || raw}' received an invalid ${param.type} for argument ${index + 1} (${param.name}), received '${arg}'^0`);
|
|
72
|
+
}
|
|
73
|
+
mapped[param.name] = value;
|
|
74
|
+
return true;
|
|
75
|
+
});
|
|
76
|
+
return result ? mapped : null;
|
|
77
|
+
}
|
|
78
|
+
async call(source, args, raw = args.join(" ")) {
|
|
79
|
+
const parsed = this.mapArguments(source, args, raw);
|
|
80
|
+
if (!parsed)
|
|
81
|
+
return;
|
|
82
|
+
try {
|
|
83
|
+
await this.#handler(parsed);
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
Citizen.trace(`^1command '${raw.split(" ")[0] || raw}' failed to execute!^0\n${err.message}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED