@aifivem/lib 0.6.0 → 0.7.0
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/package.json +3 -3
- package/src/client/action.ts +38 -38
- package/src/client/callback.ts +70 -70
- package/src/client/controls.ts +73 -73
- package/src/client/entity.ts +15 -15
- package/src/client/headshot.ts +26 -26
- package/src/client/localPed.ts +169 -169
- package/src/client/location.ts +54 -54
- package/src/client/netEvent.ts +40 -40
- package/src/client/nuiScreen.ts +87 -87
- package/src/client/players.ts +34 -34
- package/src/client/raycast.ts +48 -48
- package/src/client/shot.ts +29 -29
- package/src/client/vehicleProps.ts +107 -107
- package/src/fivem/callbackNames.ts +12 -12
- package/src/fivem/locale.ts +57 -57
- package/src/server/callback.ts +112 -112
- package/src/server/dataFile.ts +81 -81
- package/src/server/netEvent.ts +58 -58
- package/src/server/position.ts +20 -20
- package/src/server/rateLimit.ts +53 -53
- package/src/shared/format.ts +26 -26
- package/src/shared/grid.ts +20 -20
- package/src/shared/index.ts +151 -151
- package/src/shared/lang.ts +39 -39
- package/src/shared/read.ts +60 -60
- package/src/shared/strict.ts +70 -70
- package/src/shared/vec.ts +19 -19
- package/src/shared/vehicleProps.ts +138 -138
- package/src/shared/wait.ts +27 -27
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aifivem/lib",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Helpers every ai-core resource uses: logging, exports, rate limits, database tables.",
|
|
5
5
|
"license": "GPL-3.0-only",
|
|
6
6
|
"repository": {
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"access": "public"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@aifivem/shared": "^0.
|
|
26
|
-
"@aifivem/mysql": "^0.
|
|
25
|
+
"@aifivem/shared": "^0.6.0",
|
|
26
|
+
"@aifivem/mysql": "^0.2.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@citizenfx/client": "latest",
|
package/src/client/action.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import { loadLocaleTexts } from "../fivem/locale";
|
|
2
|
-
import { isActionOk, replyError } from "../shared/action";
|
|
3
|
-
import { triggerServerCallback } from "./callback";
|
|
4
|
-
import { notify } from "./notify";
|
|
5
|
-
|
|
6
|
-
/** This resource's lang table, read on the first refusal. */
|
|
7
|
-
let texts: Record<string, string> | null = null;
|
|
8
|
-
|
|
9
|
-
export interface CallActionOptions {
|
|
10
|
-
/** Lang key shown when the reply is missing, malformed, timed out or the call was refused by the gate. */
|
|
11
|
-
fallback: string;
|
|
12
|
-
/** ms before the call counts as failed (default the callback timeout). */
|
|
13
|
-
timeout?: number;
|
|
14
|
-
/** No notification on a refusal (the caller shows the error itself). */
|
|
15
|
-
silent?: boolean;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Runs an action on the server (`onClientCallback` answering an `ActionResult`). A refusal, a timeout or a gate
|
|
20
|
-
* refusal shows the error's text from this resource's lang file as an error notification. Resolves with the ok
|
|
21
|
-
* reply (extra fields unchecked: parse the ones you read), null otherwise.
|
|
22
|
-
*/
|
|
23
|
-
export async function callAction(
|
|
24
|
-
name: string,
|
|
25
|
-
args: unknown[] = [],
|
|
26
|
-
options: CallActionOptions,
|
|
27
|
-
): Promise<({ ok: true } & Record<string, unknown>) | null> {
|
|
28
|
-
const reply = await triggerServerCallback(name, args, options.timeout).catch(
|
|
29
|
-
() => null,
|
|
30
|
-
);
|
|
31
|
-
if (isActionOk(reply)) return reply;
|
|
32
|
-
if (!options.silent) {
|
|
33
|
-
const key = replyError(reply, options.fallback) ?? options.fallback;
|
|
34
|
-
texts ??= loadLocaleTexts();
|
|
35
|
-
notify(texts[key] ?? key, "error");
|
|
36
|
-
}
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
1
|
+
import { loadLocaleTexts } from "../fivem/locale";
|
|
2
|
+
import { isActionOk, replyError } from "../shared/action";
|
|
3
|
+
import { triggerServerCallback } from "./callback";
|
|
4
|
+
import { notify } from "./notify";
|
|
5
|
+
|
|
6
|
+
/** This resource's lang table, read on the first refusal. */
|
|
7
|
+
let texts: Record<string, string> | null = null;
|
|
8
|
+
|
|
9
|
+
export interface CallActionOptions {
|
|
10
|
+
/** Lang key shown when the reply is missing, malformed, timed out or the call was refused by the gate. */
|
|
11
|
+
fallback: string;
|
|
12
|
+
/** ms before the call counts as failed (default the callback timeout). */
|
|
13
|
+
timeout?: number;
|
|
14
|
+
/** No notification on a refusal (the caller shows the error itself). */
|
|
15
|
+
silent?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Runs an action on the server (`onClientCallback` answering an `ActionResult`). A refusal, a timeout or a gate
|
|
20
|
+
* refusal shows the error's text from this resource's lang file as an error notification. Resolves with the ok
|
|
21
|
+
* reply (extra fields unchecked: parse the ones you read), null otherwise.
|
|
22
|
+
*/
|
|
23
|
+
export async function callAction(
|
|
24
|
+
name: string,
|
|
25
|
+
args: unknown[] = [],
|
|
26
|
+
options: CallActionOptions,
|
|
27
|
+
): Promise<({ ok: true } & Record<string, unknown>) | null> {
|
|
28
|
+
const reply = await triggerServerCallback(name, args, options.timeout).catch(
|
|
29
|
+
() => null,
|
|
30
|
+
);
|
|
31
|
+
if (isActionOk(reply)) return reply;
|
|
32
|
+
if (!options.silent) {
|
|
33
|
+
const key = replyError(reply, options.fallback) ?? options.fallback;
|
|
34
|
+
texts ??= loadLocaleTexts();
|
|
35
|
+
notify(texts[key] ?? key, "error");
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
package/src/client/callback.ts
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DEFAULT_CALLBACK_TIMEOUT,
|
|
3
|
-
requestEvent,
|
|
4
|
-
responseEvent,
|
|
5
|
-
} from "../fivem/callbackNames";
|
|
6
|
-
import { createLogger } from "../shared";
|
|
7
|
-
import { fromServer } from "./netEvent";
|
|
8
|
-
|
|
9
|
-
const resource = GetCurrentResourceName();
|
|
10
|
-
const log = createLogger(resource);
|
|
11
|
-
|
|
12
|
-
interface Pending {
|
|
13
|
-
resolve: (value: unknown) => void;
|
|
14
|
-
reject: (error: Error) => void;
|
|
15
|
-
timer: ReturnType<typeof setTimeout>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const pending = new Map<number, Pending>();
|
|
19
|
-
let nextId = 0;
|
|
20
|
-
|
|
21
|
-
onNet(responseEvent(resource), (id: unknown, ok: unknown, result: unknown) => {
|
|
22
|
-
if (!fromServer()) return;
|
|
23
|
-
const entry = typeof id === "number" ? pending.get(id) : undefined;
|
|
24
|
-
if (!entry) return;
|
|
25
|
-
pending.delete(id as number);
|
|
26
|
-
clearTimeout(entry.timer);
|
|
27
|
-
if (ok === true) entry.resolve(result);
|
|
28
|
-
else entry.reject(new Error("server callback failed"));
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Asks the server (`onClientCallback(name, ...)` there) and resolves with its answer. The answer is unknown:
|
|
33
|
-
* parse it. Rejects when the server reports an error or does not answer within `timeoutMs`.
|
|
34
|
-
*/
|
|
35
|
-
export function triggerServerCallback(
|
|
36
|
-
name: string,
|
|
37
|
-
args: unknown[] = [],
|
|
38
|
-
timeoutMs = DEFAULT_CALLBACK_TIMEOUT,
|
|
39
|
-
): Promise<unknown> {
|
|
40
|
-
const id = ++nextId;
|
|
41
|
-
return new Promise((resolve, reject) => {
|
|
42
|
-
const timer = setTimeout(() => {
|
|
43
|
-
pending.delete(id);
|
|
44
|
-
reject(new Error(`server callback ${name} timed out`));
|
|
45
|
-
}, timeoutMs);
|
|
46
|
-
pending.set(id, { resolve, reject, timer });
|
|
47
|
-
emitNet(requestEvent(name), resource, id, ...args);
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** Answers the server's `triggerClientCallback(name, source, ...)`. Arguments are unknown: parse before use. */
|
|
52
|
-
export function onServerCallback(
|
|
53
|
-
name: string,
|
|
54
|
-
handler: (...args: unknown[]) => unknown,
|
|
55
|
-
): void {
|
|
56
|
-
onNet(
|
|
57
|
-
requestEvent(name),
|
|
58
|
-
async (caller: unknown, id: unknown, ...args: unknown[]) => {
|
|
59
|
-
if (!fromServer() || typeof caller !== "string" || typeof id !== "number")
|
|
60
|
-
return;
|
|
61
|
-
try {
|
|
62
|
-
const result = await handler(...args);
|
|
63
|
-
emitNet(responseEvent(caller), id, true, result ?? null);
|
|
64
|
-
} catch (error) {
|
|
65
|
-
log.error(`callback ${name} failed`, error);
|
|
66
|
-
emitNet(responseEvent(caller), id, false, null);
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
);
|
|
70
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_CALLBACK_TIMEOUT,
|
|
3
|
+
requestEvent,
|
|
4
|
+
responseEvent,
|
|
5
|
+
} from "../fivem/callbackNames";
|
|
6
|
+
import { createLogger } from "../shared";
|
|
7
|
+
import { fromServer } from "./netEvent";
|
|
8
|
+
|
|
9
|
+
const resource = GetCurrentResourceName();
|
|
10
|
+
const log = createLogger(resource);
|
|
11
|
+
|
|
12
|
+
interface Pending {
|
|
13
|
+
resolve: (value: unknown) => void;
|
|
14
|
+
reject: (error: Error) => void;
|
|
15
|
+
timer: ReturnType<typeof setTimeout>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const pending = new Map<number, Pending>();
|
|
19
|
+
let nextId = 0;
|
|
20
|
+
|
|
21
|
+
onNet(responseEvent(resource), (id: unknown, ok: unknown, result: unknown) => {
|
|
22
|
+
if (!fromServer()) return;
|
|
23
|
+
const entry = typeof id === "number" ? pending.get(id) : undefined;
|
|
24
|
+
if (!entry) return;
|
|
25
|
+
pending.delete(id as number);
|
|
26
|
+
clearTimeout(entry.timer);
|
|
27
|
+
if (ok === true) entry.resolve(result);
|
|
28
|
+
else entry.reject(new Error("server callback failed"));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Asks the server (`onClientCallback(name, ...)` there) and resolves with its answer. The answer is unknown:
|
|
33
|
+
* parse it. Rejects when the server reports an error or does not answer within `timeoutMs`.
|
|
34
|
+
*/
|
|
35
|
+
export function triggerServerCallback(
|
|
36
|
+
name: string,
|
|
37
|
+
args: unknown[] = [],
|
|
38
|
+
timeoutMs = DEFAULT_CALLBACK_TIMEOUT,
|
|
39
|
+
): Promise<unknown> {
|
|
40
|
+
const id = ++nextId;
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
const timer = setTimeout(() => {
|
|
43
|
+
pending.delete(id);
|
|
44
|
+
reject(new Error(`server callback ${name} timed out`));
|
|
45
|
+
}, timeoutMs);
|
|
46
|
+
pending.set(id, { resolve, reject, timer });
|
|
47
|
+
emitNet(requestEvent(name), resource, id, ...args);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Answers the server's `triggerClientCallback(name, source, ...)`. Arguments are unknown: parse before use. */
|
|
52
|
+
export function onServerCallback(
|
|
53
|
+
name: string,
|
|
54
|
+
handler: (...args: unknown[]) => unknown,
|
|
55
|
+
): void {
|
|
56
|
+
onNet(
|
|
57
|
+
requestEvent(name),
|
|
58
|
+
async (caller: unknown, id: unknown, ...args: unknown[]) => {
|
|
59
|
+
if (!fromServer() || typeof caller !== "string" || typeof id !== "number")
|
|
60
|
+
return;
|
|
61
|
+
try {
|
|
62
|
+
const result = await handler(...args);
|
|
63
|
+
emitNet(responseEvent(caller), id, true, result ?? null);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
log.error(`callback ${name} failed`, error);
|
|
66
|
+
emitNet(responseEvent(caller), id, false, null);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
);
|
|
70
|
+
}
|
package/src/client/controls.ts
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
import { TickLoop } from "./tickLoop";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Disabled controls of this resource, one per-frame job for all of them: each reason (`cuffed`, `seatbelt`, `menu`)
|
|
5
|
-
* adds its controls and removes them when it ends; the job runs only while any reason is active.
|
|
6
|
-
*/
|
|
7
|
-
class Controls {
|
|
8
|
-
private readonly sets = new Map<
|
|
9
|
-
string,
|
|
10
|
-
{ group: number; controls: readonly number[] }
|
|
11
|
-
>();
|
|
12
|
-
/** Reasons that disable every control, each with the controls it keeps. */
|
|
13
|
-
private readonly all = new Map<string, readonly number[]>();
|
|
14
|
-
/** Flat [group, control, group, control...] of every active reason, rebuilt on change (nothing allocated per frame). */
|
|
15
|
-
private flat: number[] = [];
|
|
16
|
-
/** Controls every `disableAll` reason keeps (and no `disable` reason takes). */
|
|
17
|
-
private kept: number[] = [];
|
|
18
|
-
private readonly tick = new TickLoop(() => {
|
|
19
|
-
if (this.all.size > 0) {
|
|
20
|
-
DisableAllControlActions(0);
|
|
21
|
-
DisableAllControlActions(1);
|
|
22
|
-
DisableAllControlActions(2);
|
|
23
|
-
const kept = this.kept;
|
|
24
|
-
for (let i = 0; i < kept.length; i++)
|
|
25
|
-
EnableControlAction(0, kept[i] ?? 0, true);
|
|
26
|
-
}
|
|
27
|
-
const flat = this.flat;
|
|
28
|
-
for (let i = 0; i < flat.length; i += 2)
|
|
29
|
-
DisableControlAction(flat[i] ?? 0, flat[i + 1] ?? 0, true);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
/** Disables `controls` (in input group `group`, default 0) every frame until `enable(reason)`. */
|
|
33
|
-
disable(reason: string, controls: readonly number[], group = 0): void {
|
|
34
|
-
this.sets.set(reason, { group, controls });
|
|
35
|
-
this.rebuild();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Disables every control (input groups 0-2) every frame until `enable(reason)`, except `except` (group 0: camera
|
|
40
|
-
* look, push to talk...). Read the disabled ones with `IsDisabledControlPressed` (noclip, free camera).
|
|
41
|
-
*/
|
|
42
|
-
disableAll(reason: string, except: readonly number[] = []): void {
|
|
43
|
-
this.all.set(reason, except);
|
|
44
|
-
this.rebuild();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
enable(reason: string): void {
|
|
48
|
-
const a = this.sets.delete(reason);
|
|
49
|
-
const b = this.all.delete(reason);
|
|
50
|
-
if (a || b) this.rebuild();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
isDisabled(reason: string): boolean {
|
|
54
|
-
return this.sets.has(reason) || this.all.has(reason);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private rebuild(): void {
|
|
58
|
-
this.flat = [];
|
|
59
|
-
const taken = new Set<number>();
|
|
60
|
-
for (const { group, controls } of this.sets.values())
|
|
61
|
-
for (const c of controls) {
|
|
62
|
-
this.flat.push(group, c);
|
|
63
|
-
if (group === 0) taken.add(c);
|
|
64
|
-
}
|
|
65
|
-
const lists = [...this.all.values()];
|
|
66
|
-
this.kept = (lists[0] ?? []).filter(
|
|
67
|
-
(c) => !taken.has(c) && lists.every((list) => list.includes(c)),
|
|
68
|
-
);
|
|
69
|
-
this.tick.set(this.flat.length > 0 || this.all.size > 0);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export const controls = new Controls();
|
|
1
|
+
import { TickLoop } from "./tickLoop";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Disabled controls of this resource, one per-frame job for all of them: each reason (`cuffed`, `seatbelt`, `menu`)
|
|
5
|
+
* adds its controls and removes them when it ends; the job runs only while any reason is active.
|
|
6
|
+
*/
|
|
7
|
+
class Controls {
|
|
8
|
+
private readonly sets = new Map<
|
|
9
|
+
string,
|
|
10
|
+
{ group: number; controls: readonly number[] }
|
|
11
|
+
>();
|
|
12
|
+
/** Reasons that disable every control, each with the controls it keeps. */
|
|
13
|
+
private readonly all = new Map<string, readonly number[]>();
|
|
14
|
+
/** Flat [group, control, group, control...] of every active reason, rebuilt on change (nothing allocated per frame). */
|
|
15
|
+
private flat: number[] = [];
|
|
16
|
+
/** Controls every `disableAll` reason keeps (and no `disable` reason takes). */
|
|
17
|
+
private kept: number[] = [];
|
|
18
|
+
private readonly tick = new TickLoop(() => {
|
|
19
|
+
if (this.all.size > 0) {
|
|
20
|
+
DisableAllControlActions(0);
|
|
21
|
+
DisableAllControlActions(1);
|
|
22
|
+
DisableAllControlActions(2);
|
|
23
|
+
const kept = this.kept;
|
|
24
|
+
for (let i = 0; i < kept.length; i++)
|
|
25
|
+
EnableControlAction(0, kept[i] ?? 0, true);
|
|
26
|
+
}
|
|
27
|
+
const flat = this.flat;
|
|
28
|
+
for (let i = 0; i < flat.length; i += 2)
|
|
29
|
+
DisableControlAction(flat[i] ?? 0, flat[i + 1] ?? 0, true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/** Disables `controls` (in input group `group`, default 0) every frame until `enable(reason)`. */
|
|
33
|
+
disable(reason: string, controls: readonly number[], group = 0): void {
|
|
34
|
+
this.sets.set(reason, { group, controls });
|
|
35
|
+
this.rebuild();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Disables every control (input groups 0-2) every frame until `enable(reason)`, except `except` (group 0: camera
|
|
40
|
+
* look, push to talk...). Read the disabled ones with `IsDisabledControlPressed` (noclip, free camera).
|
|
41
|
+
*/
|
|
42
|
+
disableAll(reason: string, except: readonly number[] = []): void {
|
|
43
|
+
this.all.set(reason, except);
|
|
44
|
+
this.rebuild();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
enable(reason: string): void {
|
|
48
|
+
const a = this.sets.delete(reason);
|
|
49
|
+
const b = this.all.delete(reason);
|
|
50
|
+
if (a || b) this.rebuild();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
isDisabled(reason: string): boolean {
|
|
54
|
+
return this.sets.has(reason) || this.all.has(reason);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private rebuild(): void {
|
|
58
|
+
this.flat = [];
|
|
59
|
+
const taken = new Set<number>();
|
|
60
|
+
for (const { group, controls } of this.sets.values())
|
|
61
|
+
for (const c of controls) {
|
|
62
|
+
this.flat.push(group, c);
|
|
63
|
+
if (group === 0) taken.add(c);
|
|
64
|
+
}
|
|
65
|
+
const lists = [...this.all.values()];
|
|
66
|
+
this.kept = (lists[0] ?? []).filter(
|
|
67
|
+
(c) => !taken.has(c) && lists.every((list) => list.includes(c)),
|
|
68
|
+
);
|
|
69
|
+
this.tick.set(this.flat.length > 0 || this.all.size > 0);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const controls = new Controls();
|
package/src/client/entity.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { waitFor } from "../shared/wait";
|
|
2
|
-
|
|
3
|
-
/** The local entity of a network id once it exists on this client, or 0 after `timeoutMs` (default 5 s). */
|
|
4
|
-
export async function waitForEntity(
|
|
5
|
-
netId: number,
|
|
6
|
-
timeoutMs = 5000,
|
|
7
|
-
): Promise<number> {
|
|
8
|
-
return waitFor(
|
|
9
|
-
() =>
|
|
10
|
-
NetworkDoesEntityExistWithNetworkId(netId)
|
|
11
|
-
? NetToEnt(netId) || undefined
|
|
12
|
-
: undefined,
|
|
13
|
-
timeoutMs,
|
|
14
|
-
).catch(() => 0);
|
|
15
|
-
}
|
|
1
|
+
import { waitFor } from "../shared/wait";
|
|
2
|
+
|
|
3
|
+
/** The local entity of a network id once it exists on this client, or 0 after `timeoutMs` (default 5 s). */
|
|
4
|
+
export async function waitForEntity(
|
|
5
|
+
netId: number,
|
|
6
|
+
timeoutMs = 5000,
|
|
7
|
+
): Promise<number> {
|
|
8
|
+
return waitFor(
|
|
9
|
+
() =>
|
|
10
|
+
NetworkDoesEntityExistWithNetworkId(netId)
|
|
11
|
+
? NetToEnt(netId) || undefined
|
|
12
|
+
: undefined,
|
|
13
|
+
timeoutMs,
|
|
14
|
+
).catch(() => 0);
|
|
15
|
+
}
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* The local entity of a network id right now, or 0 (not on this client, or not of `type`: 1 ped, 2 vehicle, 3 object;
|
package/src/client/headshot.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import { waitFor } from "../shared";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Registers a headshot of a ped and resolves once the game rendered it: the handle and its texture name (a
|
|
5
|
-
* `https://nui-img/<txd>/<txd>` picture), or null after `timeoutMs`. Call `UnregisterPedheadshot(handle)` when it is
|
|
6
|
-
* no longer shown.
|
|
7
|
-
*/
|
|
8
|
-
export async function pedHeadshot(
|
|
9
|
-
ped: number,
|
|
10
|
-
transparent = false,
|
|
11
|
-
timeoutMs = 5000,
|
|
12
|
-
): Promise<{ handle: number; txd: string } | null> {
|
|
13
|
-
if (!DoesEntityExist(ped)) return null;
|
|
14
|
-
const handle = transparent
|
|
15
|
-
? RegisterPedheadshotTransparent(ped)
|
|
16
|
-
: RegisterPedheadshot(ped);
|
|
17
|
-
const ready = await waitFor(
|
|
18
|
-
() => (IsPedheadshotReady(handle) && IsPedheadshotValid(handle)) || undefined,
|
|
19
|
-
timeoutMs,
|
|
20
|
-
).catch(() => false);
|
|
21
|
-
if (!ready) {
|
|
22
|
-
UnregisterPedheadshot(handle);
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
return { handle, txd: GetPedheadshotTxdString(handle) };
|
|
26
|
-
}
|
|
1
|
+
import { waitFor } from "../shared";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Registers a headshot of a ped and resolves once the game rendered it: the handle and its texture name (a
|
|
5
|
+
* `https://nui-img/<txd>/<txd>` picture), or null after `timeoutMs`. Call `UnregisterPedheadshot(handle)` when it is
|
|
6
|
+
* no longer shown.
|
|
7
|
+
*/
|
|
8
|
+
export async function pedHeadshot(
|
|
9
|
+
ped: number,
|
|
10
|
+
transparent = false,
|
|
11
|
+
timeoutMs = 5000,
|
|
12
|
+
): Promise<{ handle: number; txd: string } | null> {
|
|
13
|
+
if (!DoesEntityExist(ped)) return null;
|
|
14
|
+
const handle = transparent
|
|
15
|
+
? RegisterPedheadshotTransparent(ped)
|
|
16
|
+
: RegisterPedheadshot(ped);
|
|
17
|
+
const ready = await waitFor(
|
|
18
|
+
() => (IsPedheadshotReady(handle) && IsPedheadshotValid(handle)) || undefined,
|
|
19
|
+
timeoutMs,
|
|
20
|
+
).catch(() => false);
|
|
21
|
+
if (!ready) {
|
|
22
|
+
UnregisterPedheadshot(handle);
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return { handle, txd: GetPedheadshotTxdString(handle) };
|
|
26
|
+
}
|