@nativewrappers/common 0.0.98 → 0.0.100

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 CHANGED
@@ -8,6 +8,7 @@ interface ParameterTypes {
8
8
  interface Parameter {
9
9
  name: string;
10
10
  type: keyof ParameterTypes;
11
+ defaultValue?: any;
11
12
  help?: string;
12
13
  optional?: boolean;
13
14
  }
@@ -18,6 +19,27 @@ type MappedParameters<T extends Parameter[]> = {
18
19
  raw: string;
19
20
  };
20
21
  type CommandHandler<T extends Parameter[]> = (args: MappedParameters<T>) => void | Promise<void>;
22
+ /**
23
+ * ```typescript
24
+ * new Command(["do", "deleteobjects"], async ({source, radius}) => {
25
+ * const entities = Prop.AllProps();
26
+ * const ply = new Player(source);
27
+ * const pos = ply.Ped.Position
28
+ * for (const ent of entities) {
29
+ * // if they're outside of the range of our specified radius just continue to next
30
+ * if (ent.Position.distance(pos) > radius) continue;
31
+ * ent.delete();
32
+ * }
33
+ * }, "Deletes all objects in the specified range", [
34
+ * {
35
+ * name: "radius",
36
+ * type: "number",
37
+ * help: "The radius to delete the entities in",
38
+ * defaultValue: 5.0
39
+ * }
40
+ * ] as const, "group.moderator")
41
+ * ```
42
+ */
21
43
  export declare class Command<T extends Parameter[] = Parameter[]> {
22
44
  #private;
23
45
  readonly name: string | string[];
package/Command.js CHANGED
@@ -49,7 +49,9 @@ class Command {
49
49
  if (params) {
50
50
  for (const parameter of params) {
51
51
  if (parameter.type) {
52
- parameter.help = parameter.help ? `${parameter.help} (type: ${parameter.type})` : `(type: ${parameter.type})`;
52
+ const defaultString = parameter.defaultValue !== void 0 ? `[default: ${parameter.defaultValue}]` : "";
53
+ const typeString = `[type: ${parameter.type}] ${defaultString}`;
54
+ parameter.help = parameter.help ? `${parameter.help} ${typeString}` : `${typeString}`;
53
55
  }
54
56
  }
55
57
  }
@@ -87,26 +89,36 @@ class Command {
87
89
  const result = this.params.every((param, index) => {
88
90
  const arg = args[index];
89
91
  let value = arg;
90
- switch (param.type) {
91
- case "number":
92
- value = +arg;
93
- break;
94
- case "string":
95
- value = !Number(arg) ? arg : false;
96
- break;
97
- case "playerId":
98
- $SERVER: {
99
- value = arg === "me" ? source2 : +arg;
100
- if (!value || !DoesPlayerExist(value.toString())) value = void 0;
101
- }
102
- $CLIENT: {
103
- value = arg === "me" ? GetPlayerServerId(PlayerId()) : +arg;
104
- if (!value || GetPlayerFromServerId(value) === -1) value = void 0;
105
- }
106
- break;
107
- case "longString":
108
- value = raw.substring(raw.indexOf(arg));
109
- break;
92
+ const hasDefaultValue = param.defaultValue !== void 0;
93
+ const hasArg = typeof arg === "string";
94
+ if (!hasArg && hasDefaultValue) {
95
+ value = param.defaultValue;
96
+ } else {
97
+ switch (param.type) {
98
+ case "number":
99
+ if (hasDefaultValue && !hasArg) {
100
+ value = param.defaultValue;
101
+ } else {
102
+ value = +arg;
103
+ }
104
+ break;
105
+ case "string":
106
+ value = !Number(arg) ? arg : false;
107
+ break;
108
+ case "playerId":
109
+ $SERVER: {
110
+ value = arg === "me" ? source2 : +arg;
111
+ if (!value || !DoesPlayerExist(value.toString())) value = void 0;
112
+ }
113
+ $CLIENT: {
114
+ value = arg === "me" ? GetPlayerServerId(PlayerId()) : +arg;
115
+ if (!value || GetPlayerFromServerId(value) === -1) value = void 0;
116
+ }
117
+ break;
118
+ case "longString":
119
+ value = raw.substring(raw.indexOf(arg));
120
+ break;
121
+ }
110
122
  }
111
123
  if (value === void 0 && (!param.optional || param.optional && arg)) {
112
124
  return Citizen.trace(
package/decors/Events.js CHANGED
@@ -15,8 +15,20 @@ function Exports(exportName) {
15
15
  throw new Error("Exports does not work on private methods, please mark the method as public");
16
16
  }
17
17
  context.addInitializer(function() {
18
- exports(exportName, (...args) => {
19
- return originalMethod.call(this, ...args);
18
+ exports(exportName, async (...args) => {
19
+ try {
20
+ return await originalMethod.call(this, ...args);
21
+ } catch (err) {
22
+ REMOVE_EVENT_LOG: {
23
+ if (!GlobalData.EnablePrettyPrint) return;
24
+ console.error("------- EXPORT ERROR --------");
25
+ console.error(`Call to ${exportName} errored`);
26
+ console.error(`Data: ${JSON.stringify(args)}`);
27
+ console.error(`Error: ${err}`);
28
+ console.error("------- END EXPORT ERROR --------");
29
+ }
30
+ throw err;
31
+ }
20
32
  });
21
33
  });
22
34
  }, "actualDecorator");
@@ -28,9 +40,9 @@ function Event(eventName) {
28
40
  throw new Error("Event does not work on private methods, please mark the method as public");
29
41
  }
30
42
  context.addInitializer(function() {
31
- on(eventName, (...args) => {
43
+ on(eventName, async (...args) => {
32
44
  try {
33
- return originalMethod.call(this, ...args);
45
+ return await originalMethod.call(this, ...args);
34
46
  } catch (e) {
35
47
  REMOVE_EVENT_LOG: {
36
48
  if (!GlobalData.EnablePrettyPrint) return;
@@ -52,7 +64,7 @@ function NetEvent(eventName, remoteOnly = true) {
52
64
  throw new Error("NetEvent does not work on private methods, please mark the method as public");
53
65
  }
54
66
  context.addInitializer(function() {
55
- onNet(eventName, (...args) => {
67
+ onNet(eventName, async (...args) => {
56
68
  const src = source;
57
69
  try {
58
70
  $CLIENT: {
@@ -60,7 +72,7 @@ function NetEvent(eventName, remoteOnly = true) {
60
72
  return;
61
73
  }
62
74
  }
63
- return originalMethod.call(this, ...args);
75
+ return await originalMethod.call(this, ...args);
64
76
  } catch (e) {
65
77
  REMOVE_NET_EVENT_LOG: {
66
78
  if (!GlobalData.EnablePrettyPrint) return;
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  ],
9
9
  "license": "MIT",
10
10
  "type": "module",
11
- "version": "0.0.98",
11
+ "version": "0.0.100",
12
12
  "repository": {
13
13
  "type": "git",
14
14
  "url": "https://github.com/nativewrappers/nativewrappers.git"