@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/src/client/localPed.ts
CHANGED
|
@@ -1,169 +1,169 @@
|
|
|
1
|
-
import { createLogger } from "../shared";
|
|
2
|
-
import { requestAnimDict, requestModel } from "./streaming";
|
|
3
|
-
import { Zones } from "./zones";
|
|
4
|
-
|
|
5
|
-
const log = createLogger(GetCurrentResourceName());
|
|
6
|
-
|
|
7
|
-
export interface LocalPedOptions {
|
|
8
|
-
/** Model name or hash. */
|
|
9
|
-
model: string | number;
|
|
10
|
-
coords: { x: number; y: number; z: number; heading: number };
|
|
11
|
-
/** `coords.z` is the floor (panel points): the ped stands on it. Otherwise z is the ped's own root. Default true. */
|
|
12
|
-
ground?: boolean;
|
|
13
|
-
/** A scenario it plays (`WORLD_HUMAN_CLIPBOARD`), or an animation loop. */
|
|
14
|
-
scenario?: string;
|
|
15
|
-
anim?: { dict: string; clip: string };
|
|
16
|
-
/** Spawned when the player comes this close, removed past it (default 50 m). */
|
|
17
|
-
distance?: number;
|
|
18
|
-
/**
|
|
19
|
-
* Attached to an entity (a bed, a vehicle, another ped) at a bone with an offset and rotation; `entity` may be a
|
|
20
|
-
* function read at each spawn (an entity found from a network id). Not frozen while attached. `coords` stays where
|
|
21
|
-
* the ped is created and the distance is measured from.
|
|
22
|
-
*/
|
|
23
|
-
attach?: {
|
|
24
|
-
entity: number | (() => number);
|
|
25
|
-
/** Bone index (`GetEntityBoneIndexByName`, `GetPedBoneIndex`); default 0, the entity's root. */
|
|
26
|
-
bone?: number;
|
|
27
|
-
offset?: { x: number; y: number; z: number };
|
|
28
|
-
rotation?: { x: number; y: number; z: number };
|
|
29
|
-
};
|
|
30
|
-
/** Sitting in a vehicle seat (-1 driver, 0 front passenger...); `vehicle` may be a function read at each spawn. */
|
|
31
|
-
seat?: { vehicle: number | (() => number); seat: number };
|
|
32
|
-
/** After the ped appears (target options, props) and before it goes. */
|
|
33
|
-
onSpawn?: (ped: number) => void;
|
|
34
|
-
onDespawn?: (ped: number) => void;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** One zone set for every local ped of this resource: peds exist only while their player is near. */
|
|
38
|
-
const zones = new Zones();
|
|
39
|
-
let nextId = 0;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* A ped only this client sees (a shopkeeper, a job NPC): created when the player comes near, deleted when they leave,
|
|
43
|
-
* frozen, invincible and deaf to events. Never networked, so strict entity lockdown does not matter.
|
|
44
|
-
*/
|
|
45
|
-
export class LocalPed {
|
|
46
|
-
private readonly id = `ped:${++nextId}`;
|
|
47
|
-
private handle = 0;
|
|
48
|
-
private wanted = false;
|
|
49
|
-
|
|
50
|
-
constructor(private readonly options: LocalPedOptions) {
|
|
51
|
-
const { x, y, z } = options.coords;
|
|
52
|
-
zones.add(this.id, {
|
|
53
|
-
shape: {
|
|
54
|
-
type: "sphere",
|
|
55
|
-
center: { x, y, z },
|
|
56
|
-
radius: options.distance ?? 50,
|
|
57
|
-
},
|
|
58
|
-
interval: 1000,
|
|
59
|
-
onEnter: () => void this.spawn(),
|
|
60
|
-
onExit: () => this.despawn(),
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** The ped while it exists, else 0. */
|
|
65
|
-
get entity(): number {
|
|
66
|
-
return this.handle;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** Deletes the ped for good. */
|
|
70
|
-
remove(): void {
|
|
71
|
-
zones.remove(this.id);
|
|
72
|
-
this.despawn();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
private async spawn(): Promise<void> {
|
|
76
|
-
this.wanted = true;
|
|
77
|
-
const o = this.options;
|
|
78
|
-
try {
|
|
79
|
-
const hash = await requestModel(o.model);
|
|
80
|
-
if (o.anim) await requestAnimDict(o.anim.dict);
|
|
81
|
-
// The player may have left while the model loaded.
|
|
82
|
-
if (!this.wanted || this.handle) return;
|
|
83
|
-
const { x, y, z, heading } = o.coords;
|
|
84
|
-
const ped = CreatePed(
|
|
85
|
-
4,
|
|
86
|
-
hash,
|
|
87
|
-
x,
|
|
88
|
-
y,
|
|
89
|
-
o.ground === false ? z : z - 1,
|
|
90
|
-
heading,
|
|
91
|
-
false,
|
|
92
|
-
false,
|
|
93
|
-
);
|
|
94
|
-
SetModelAsNoLongerNeeded(hash);
|
|
95
|
-
if (!this.place(ped)) FreezeEntityPosition(ped, true);
|
|
96
|
-
SetEntityInvincible(ped, true);
|
|
97
|
-
SetBlockingOfNonTemporaryEvents(ped, true);
|
|
98
|
-
SetPedCanRagdoll(ped, false);
|
|
99
|
-
if (o.scenario) TaskStartScenarioInPlace(ped, o.scenario, 0, true);
|
|
100
|
-
else if (o.anim)
|
|
101
|
-
TaskPlayAnim(
|
|
102
|
-
ped,
|
|
103
|
-
o.anim.dict,
|
|
104
|
-
o.anim.clip,
|
|
105
|
-
8,
|
|
106
|
-
8,
|
|
107
|
-
-1,
|
|
108
|
-
1,
|
|
109
|
-
0,
|
|
110
|
-
false,
|
|
111
|
-
false,
|
|
112
|
-
false,
|
|
113
|
-
);
|
|
114
|
-
this.handle = ped;
|
|
115
|
-
o.onSpawn?.(ped);
|
|
116
|
-
} catch (error) {
|
|
117
|
-
log.error(`local ped ${o.model} could not be created`, error);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** Puts the new ped into its seat or attaches it; false when it stands free (no target entity right now). */
|
|
122
|
-
private place(ped: number): boolean {
|
|
123
|
-
const read = (e: number | (() => number)) =>
|
|
124
|
-
typeof e === "function" ? e() : e;
|
|
125
|
-
const { seat, attach } = this.options;
|
|
126
|
-
if (seat) {
|
|
127
|
-
const vehicle = read(seat.vehicle);
|
|
128
|
-
if (!vehicle || !DoesEntityExist(vehicle)) return false;
|
|
129
|
-
SetPedIntoVehicle(ped, vehicle, seat.seat);
|
|
130
|
-
return true;
|
|
131
|
-
}
|
|
132
|
-
if (!attach) return false;
|
|
133
|
-
const entity = read(attach.entity);
|
|
134
|
-
if (!entity || !DoesEntityExist(entity)) return false;
|
|
135
|
-
const o = attach.offset ?? { x: 0, y: 0, z: 0 };
|
|
136
|
-
const r = attach.rotation ?? { x: 0, y: 0, z: 0 };
|
|
137
|
-
AttachEntityToEntity(
|
|
138
|
-
ped,
|
|
139
|
-
entity,
|
|
140
|
-
attach.bone ?? 0,
|
|
141
|
-
o.x,
|
|
142
|
-
o.y,
|
|
143
|
-
o.z,
|
|
144
|
-
r.x,
|
|
145
|
-
r.y,
|
|
146
|
-
r.z,
|
|
147
|
-
false,
|
|
148
|
-
false,
|
|
149
|
-
false,
|
|
150
|
-
false,
|
|
151
|
-
2,
|
|
152
|
-
true,
|
|
153
|
-
);
|
|
154
|
-
return true;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
private despawn(): void {
|
|
158
|
-
this.wanted = false;
|
|
159
|
-
if (!this.handle) return;
|
|
160
|
-
const ped = this.handle;
|
|
161
|
-
this.handle = 0;
|
|
162
|
-
this.options.onDespawn?.(ped);
|
|
163
|
-
DeleteEntity(ped);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
on("onResourceStop", (resource: string) => {
|
|
168
|
-
if (resource === GetCurrentResourceName()) zones.clear();
|
|
169
|
-
});
|
|
1
|
+
import { createLogger } from "../shared";
|
|
2
|
+
import { requestAnimDict, requestModel } from "./streaming";
|
|
3
|
+
import { Zones } from "./zones";
|
|
4
|
+
|
|
5
|
+
const log = createLogger(GetCurrentResourceName());
|
|
6
|
+
|
|
7
|
+
export interface LocalPedOptions {
|
|
8
|
+
/** Model name or hash. */
|
|
9
|
+
model: string | number;
|
|
10
|
+
coords: { x: number; y: number; z: number; heading: number };
|
|
11
|
+
/** `coords.z` is the floor (panel points): the ped stands on it. Otherwise z is the ped's own root. Default true. */
|
|
12
|
+
ground?: boolean;
|
|
13
|
+
/** A scenario it plays (`WORLD_HUMAN_CLIPBOARD`), or an animation loop. */
|
|
14
|
+
scenario?: string;
|
|
15
|
+
anim?: { dict: string; clip: string };
|
|
16
|
+
/** Spawned when the player comes this close, removed past it (default 50 m). */
|
|
17
|
+
distance?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Attached to an entity (a bed, a vehicle, another ped) at a bone with an offset and rotation; `entity` may be a
|
|
20
|
+
* function read at each spawn (an entity found from a network id). Not frozen while attached. `coords` stays where
|
|
21
|
+
* the ped is created and the distance is measured from.
|
|
22
|
+
*/
|
|
23
|
+
attach?: {
|
|
24
|
+
entity: number | (() => number);
|
|
25
|
+
/** Bone index (`GetEntityBoneIndexByName`, `GetPedBoneIndex`); default 0, the entity's root. */
|
|
26
|
+
bone?: number;
|
|
27
|
+
offset?: { x: number; y: number; z: number };
|
|
28
|
+
rotation?: { x: number; y: number; z: number };
|
|
29
|
+
};
|
|
30
|
+
/** Sitting in a vehicle seat (-1 driver, 0 front passenger...); `vehicle` may be a function read at each spawn. */
|
|
31
|
+
seat?: { vehicle: number | (() => number); seat: number };
|
|
32
|
+
/** After the ped appears (target options, props) and before it goes. */
|
|
33
|
+
onSpawn?: (ped: number) => void;
|
|
34
|
+
onDespawn?: (ped: number) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** One zone set for every local ped of this resource: peds exist only while their player is near. */
|
|
38
|
+
const zones = new Zones();
|
|
39
|
+
let nextId = 0;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A ped only this client sees (a shopkeeper, a job NPC): created when the player comes near, deleted when they leave,
|
|
43
|
+
* frozen, invincible and deaf to events. Never networked, so strict entity lockdown does not matter.
|
|
44
|
+
*/
|
|
45
|
+
export class LocalPed {
|
|
46
|
+
private readonly id = `ped:${++nextId}`;
|
|
47
|
+
private handle = 0;
|
|
48
|
+
private wanted = false;
|
|
49
|
+
|
|
50
|
+
constructor(private readonly options: LocalPedOptions) {
|
|
51
|
+
const { x, y, z } = options.coords;
|
|
52
|
+
zones.add(this.id, {
|
|
53
|
+
shape: {
|
|
54
|
+
type: "sphere",
|
|
55
|
+
center: { x, y, z },
|
|
56
|
+
radius: options.distance ?? 50,
|
|
57
|
+
},
|
|
58
|
+
interval: 1000,
|
|
59
|
+
onEnter: () => void this.spawn(),
|
|
60
|
+
onExit: () => this.despawn(),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The ped while it exists, else 0. */
|
|
65
|
+
get entity(): number {
|
|
66
|
+
return this.handle;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Deletes the ped for good. */
|
|
70
|
+
remove(): void {
|
|
71
|
+
zones.remove(this.id);
|
|
72
|
+
this.despawn();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private async spawn(): Promise<void> {
|
|
76
|
+
this.wanted = true;
|
|
77
|
+
const o = this.options;
|
|
78
|
+
try {
|
|
79
|
+
const hash = await requestModel(o.model);
|
|
80
|
+
if (o.anim) await requestAnimDict(o.anim.dict);
|
|
81
|
+
// The player may have left while the model loaded.
|
|
82
|
+
if (!this.wanted || this.handle) return;
|
|
83
|
+
const { x, y, z, heading } = o.coords;
|
|
84
|
+
const ped = CreatePed(
|
|
85
|
+
4,
|
|
86
|
+
hash,
|
|
87
|
+
x,
|
|
88
|
+
y,
|
|
89
|
+
o.ground === false ? z : z - 1,
|
|
90
|
+
heading,
|
|
91
|
+
false,
|
|
92
|
+
false,
|
|
93
|
+
);
|
|
94
|
+
SetModelAsNoLongerNeeded(hash);
|
|
95
|
+
if (!this.place(ped)) FreezeEntityPosition(ped, true);
|
|
96
|
+
SetEntityInvincible(ped, true);
|
|
97
|
+
SetBlockingOfNonTemporaryEvents(ped, true);
|
|
98
|
+
SetPedCanRagdoll(ped, false);
|
|
99
|
+
if (o.scenario) TaskStartScenarioInPlace(ped, o.scenario, 0, true);
|
|
100
|
+
else if (o.anim)
|
|
101
|
+
TaskPlayAnim(
|
|
102
|
+
ped,
|
|
103
|
+
o.anim.dict,
|
|
104
|
+
o.anim.clip,
|
|
105
|
+
8,
|
|
106
|
+
8,
|
|
107
|
+
-1,
|
|
108
|
+
1,
|
|
109
|
+
0,
|
|
110
|
+
false,
|
|
111
|
+
false,
|
|
112
|
+
false,
|
|
113
|
+
);
|
|
114
|
+
this.handle = ped;
|
|
115
|
+
o.onSpawn?.(ped);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
log.error(`local ped ${o.model} could not be created`, error);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Puts the new ped into its seat or attaches it; false when it stands free (no target entity right now). */
|
|
122
|
+
private place(ped: number): boolean {
|
|
123
|
+
const read = (e: number | (() => number)) =>
|
|
124
|
+
typeof e === "function" ? e() : e;
|
|
125
|
+
const { seat, attach } = this.options;
|
|
126
|
+
if (seat) {
|
|
127
|
+
const vehicle = read(seat.vehicle);
|
|
128
|
+
if (!vehicle || !DoesEntityExist(vehicle)) return false;
|
|
129
|
+
SetPedIntoVehicle(ped, vehicle, seat.seat);
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
if (!attach) return false;
|
|
133
|
+
const entity = read(attach.entity);
|
|
134
|
+
if (!entity || !DoesEntityExist(entity)) return false;
|
|
135
|
+
const o = attach.offset ?? { x: 0, y: 0, z: 0 };
|
|
136
|
+
const r = attach.rotation ?? { x: 0, y: 0, z: 0 };
|
|
137
|
+
AttachEntityToEntity(
|
|
138
|
+
ped,
|
|
139
|
+
entity,
|
|
140
|
+
attach.bone ?? 0,
|
|
141
|
+
o.x,
|
|
142
|
+
o.y,
|
|
143
|
+
o.z,
|
|
144
|
+
r.x,
|
|
145
|
+
r.y,
|
|
146
|
+
r.z,
|
|
147
|
+
false,
|
|
148
|
+
false,
|
|
149
|
+
false,
|
|
150
|
+
false,
|
|
151
|
+
2,
|
|
152
|
+
true,
|
|
153
|
+
);
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private despawn(): void {
|
|
158
|
+
this.wanted = false;
|
|
159
|
+
if (!this.handle) return;
|
|
160
|
+
const ped = this.handle;
|
|
161
|
+
this.handle = 0;
|
|
162
|
+
this.options.onDespawn?.(ped);
|
|
163
|
+
DeleteEntity(ped);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
on("onResourceStop", (resource: string) => {
|
|
168
|
+
if (resource === GetCurrentResourceName()) zones.clear();
|
|
169
|
+
});
|
package/src/client/location.ts
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
import type { Vec3 } from "../shared/vec";
|
|
2
|
-
import { cache } from "./cache";
|
|
3
|
-
|
|
4
|
-
export interface LocationName {
|
|
5
|
-
street: string;
|
|
6
|
-
/** The crossing street, when there is one. */
|
|
7
|
-
crossing?: string;
|
|
8
|
-
/** The area's label (`Vinewood Hills`), "" where the game has none. */
|
|
9
|
-
zone: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/** The street, crossing and zone names at a point (default the local ped). */
|
|
13
|
-
export function locationName(coords?: Vec3): LocationName {
|
|
14
|
-
const at = coords ?? coordsOfPed();
|
|
15
|
-
const [streetHash, crossingHash] = GetStreetNameAtCoord(at.x, at.y, at.z);
|
|
16
|
-
const crossing =
|
|
17
|
-
crossingHash !== 0 ? GetStreetNameFromHashKey(crossingHash) : "";
|
|
18
|
-
const zone = GetLabelText(GetNameOfZone(at.x, at.y, at.z));
|
|
19
|
-
return {
|
|
20
|
-
street: streetHash !== 0 ? GetStreetNameFromHashKey(streetHash) : "",
|
|
21
|
-
crossing: crossing || undefined,
|
|
22
|
-
zone: zone !== "NULL" ? zone : "",
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function coordsOfPed(): Vec3 {
|
|
27
|
-
const [x = 0, y = 0, z = 0] = GetEntityCoords(cache.ped, false);
|
|
28
|
-
return { x, y, z };
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* A vehicle's display name (`Sultan RS`) from a model name, hash or vehicle entity; the model name when the game has
|
|
33
|
-
* no label for it (add-on models without a text entry).
|
|
34
|
-
*/
|
|
35
|
-
export function vehicleLabel(vehicle: string | number): string {
|
|
36
|
-
const model =
|
|
37
|
-
typeof vehicle === "number" &&
|
|
38
|
-
DoesEntityExist(vehicle) &&
|
|
39
|
-
IsEntityAVehicle(vehicle)
|
|
40
|
-
? GetEntityModel(vehicle)
|
|
41
|
-
: vehicle;
|
|
42
|
-
const hash = typeof model === "number" ? model : GetHashKey(model);
|
|
43
|
-
const name = GetDisplayNameFromVehicleModel(hash);
|
|
44
|
-
const label = GetLabelText(name);
|
|
45
|
-
if (label !== "NULL" && label !== "") return label;
|
|
46
|
-
if (name !== "" && name !== "CARNOTFOUND") return name;
|
|
47
|
-
return typeof model === "string" ? model : String(hash);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** The ground height under x / y, probing down from `z` (default 1000): null while the collision there is not loaded. */
|
|
51
|
-
export function groundZ(x: number, y: number, z = 1000): number | null {
|
|
52
|
-
const [found, ground] = GetGroundZFor_3dCoord(x, y, z, false);
|
|
53
|
-
return found ? ground : null;
|
|
54
|
-
}
|
|
1
|
+
import type { Vec3 } from "../shared/vec";
|
|
2
|
+
import { cache } from "./cache";
|
|
3
|
+
|
|
4
|
+
export interface LocationName {
|
|
5
|
+
street: string;
|
|
6
|
+
/** The crossing street, when there is one. */
|
|
7
|
+
crossing?: string;
|
|
8
|
+
/** The area's label (`Vinewood Hills`), "" where the game has none. */
|
|
9
|
+
zone: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** The street, crossing and zone names at a point (default the local ped). */
|
|
13
|
+
export function locationName(coords?: Vec3): LocationName {
|
|
14
|
+
const at = coords ?? coordsOfPed();
|
|
15
|
+
const [streetHash, crossingHash] = GetStreetNameAtCoord(at.x, at.y, at.z);
|
|
16
|
+
const crossing =
|
|
17
|
+
crossingHash !== 0 ? GetStreetNameFromHashKey(crossingHash) : "";
|
|
18
|
+
const zone = GetLabelText(GetNameOfZone(at.x, at.y, at.z));
|
|
19
|
+
return {
|
|
20
|
+
street: streetHash !== 0 ? GetStreetNameFromHashKey(streetHash) : "",
|
|
21
|
+
crossing: crossing || undefined,
|
|
22
|
+
zone: zone !== "NULL" ? zone : "",
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function coordsOfPed(): Vec3 {
|
|
27
|
+
const [x = 0, y = 0, z = 0] = GetEntityCoords(cache.ped, false);
|
|
28
|
+
return { x, y, z };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A vehicle's display name (`Sultan RS`) from a model name, hash or vehicle entity; the model name when the game has
|
|
33
|
+
* no label for it (add-on models without a text entry).
|
|
34
|
+
*/
|
|
35
|
+
export function vehicleLabel(vehicle: string | number): string {
|
|
36
|
+
const model =
|
|
37
|
+
typeof vehicle === "number" &&
|
|
38
|
+
DoesEntityExist(vehicle) &&
|
|
39
|
+
IsEntityAVehicle(vehicle)
|
|
40
|
+
? GetEntityModel(vehicle)
|
|
41
|
+
: vehicle;
|
|
42
|
+
const hash = typeof model === "number" ? model : GetHashKey(model);
|
|
43
|
+
const name = GetDisplayNameFromVehicleModel(hash);
|
|
44
|
+
const label = GetLabelText(name);
|
|
45
|
+
if (label !== "NULL" && label !== "") return label;
|
|
46
|
+
if (name !== "" && name !== "CARNOTFOUND") return name;
|
|
47
|
+
return typeof model === "string" ? model : String(hash);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The ground height under x / y, probing down from `z` (default 1000): null while the collision there is not loaded. */
|
|
51
|
+
export function groundZ(x: number, y: number, z = 1000): number | null {
|
|
52
|
+
const [found, ground] = GetGroundZFor_3dCoord(x, y, z, false);
|
|
53
|
+
return found ? ground : null;
|
|
54
|
+
}
|
package/src/client/netEvent.ts
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import { createLogger } from "../shared";
|
|
2
|
-
|
|
3
|
-
const log = createLogger(GetCurrentResourceName());
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Whether the net event being handled came from the server (read it before the first `await`). On a client only
|
|
7
|
-
* events from the server carry a numeric source: 65535 for a normal event, 0 for a latent one (the client
|
|
8
|
-
* registers the server as reassembly target 0); a local emit by any resource carries a string.
|
|
9
|
-
*/
|
|
10
|
-
export const fromServer = (): boolean => typeof globalThis.source === "number";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A net event the server sends: the only way an ai-* client listens to the server (never bare `onNet`). Calls that
|
|
14
|
-
* did not come from the server (another script's local emit) are dropped. A handler that takes arguments gets them
|
|
15
|
-
* only through `args` (`(name, { args }, handler)`; `null` drops the call). A thrown error is logged.
|
|
16
|
-
*/
|
|
17
|
-
export function onServerEvent(name: string, handler: () => unknown): void;
|
|
18
|
-
export function onServerEvent<const A extends readonly unknown[]>(
|
|
19
|
-
name: string,
|
|
20
|
-
options: { args: (raw: unknown[]) => A | null },
|
|
21
|
-
handler: (...args: A) => unknown,
|
|
22
|
-
): void;
|
|
23
|
-
export function onServerEvent(
|
|
24
|
-
name: string,
|
|
25
|
-
first:
|
|
26
|
-
(() => unknown) | { args: (raw: unknown[]) => readonly unknown[] | null },
|
|
27
|
-
second?: (...args: readonly unknown[]) => unknown,
|
|
28
|
-
): void {
|
|
29
|
-
const [handler, parse] =
|
|
30
|
-
typeof first === "function" ? [first, null] : [second, first.args];
|
|
31
|
-
onNet(name, (...raw: unknown[]) => {
|
|
32
|
-
if (!fromServer() || !handler) return;
|
|
33
|
-
const args = parse ? parse(raw) : [];
|
|
34
|
-
if (!args)
|
|
35
|
-
return log.warn(`event ${name} from the server had invalid arguments`);
|
|
36
|
-
Promise.resolve()
|
|
37
|
-
.then(() => handler(...args))
|
|
38
|
-
.catch((error: unknown) => log.error(`event ${name} failed`, error));
|
|
39
|
-
});
|
|
40
|
-
}
|
|
1
|
+
import { createLogger } from "../shared";
|
|
2
|
+
|
|
3
|
+
const log = createLogger(GetCurrentResourceName());
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Whether the net event being handled came from the server (read it before the first `await`). On a client only
|
|
7
|
+
* events from the server carry a numeric source: 65535 for a normal event, 0 for a latent one (the client
|
|
8
|
+
* registers the server as reassembly target 0); a local emit by any resource carries a string.
|
|
9
|
+
*/
|
|
10
|
+
export const fromServer = (): boolean => typeof globalThis.source === "number";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A net event the server sends: the only way an ai-* client listens to the server (never bare `onNet`). Calls that
|
|
14
|
+
* did not come from the server (another script's local emit) are dropped. A handler that takes arguments gets them
|
|
15
|
+
* only through `args` (`(name, { args }, handler)`; `null` drops the call). A thrown error is logged.
|
|
16
|
+
*/
|
|
17
|
+
export function onServerEvent(name: string, handler: () => unknown): void;
|
|
18
|
+
export function onServerEvent<const A extends readonly unknown[]>(
|
|
19
|
+
name: string,
|
|
20
|
+
options: { args: (raw: unknown[]) => A | null },
|
|
21
|
+
handler: (...args: A) => unknown,
|
|
22
|
+
): void;
|
|
23
|
+
export function onServerEvent(
|
|
24
|
+
name: string,
|
|
25
|
+
first:
|
|
26
|
+
(() => unknown) | { args: (raw: unknown[]) => readonly unknown[] | null },
|
|
27
|
+
second?: (...args: readonly unknown[]) => unknown,
|
|
28
|
+
): void {
|
|
29
|
+
const [handler, parse] =
|
|
30
|
+
typeof first === "function" ? [first, null] : [second, first.args];
|
|
31
|
+
onNet(name, (...raw: unknown[]) => {
|
|
32
|
+
if (!fromServer() || !handler) return;
|
|
33
|
+
const args = parse ? parse(raw) : [];
|
|
34
|
+
if (!args)
|
|
35
|
+
return log.warn(`event ${name} from the server had invalid arguments`);
|
|
36
|
+
Promise.resolve()
|
|
37
|
+
.then(() => handler(...args))
|
|
38
|
+
.catch((error: unknown) => log.error(`event ${name} failed`, error));
|
|
39
|
+
});
|
|
40
|
+
}
|