/
vehiclesDespawnOnLeave.ts
85 lines (67 loc) · 2.05 KB
/
vehiclesDespawnOnLeave.ts
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
import * as alt from 'alt-server';
import * as Athena from '@AthenaServer/api';
import { Character } from '@AthenaShared/interfaces/character';
/**
* THIS IS A DEFAULT SYSTEM.
* IF YOU WANT TO DISABLE IT, MAKE A PLUGIN AND DISABLE IT THROUGH:
* `Athena.systems.defaults.x.disable()`
*
* DO NOT APPEND ANY ADDITIONAL DATA TO THIS SYSTEM.
* COPY THE CODE AND REMAKE IT AS A PLUGIN IF YOU WANT TO MAKE CHANGES.
*/
const SYSTEM_NAME = 'Vehicles Despawn on Leave';
let enabled = true;
const Internal = {
init() {
if (!enabled) {
return;
}
Athena.player.events.on('player-disconnected', Internal.processPlayer);
alt.log(`~lc~Default System: ~g~${SYSTEM_NAME}`);
},
async processPlayer(player: alt.Player, document: Character) {
let removedVehicles = 0;
if (typeof document === 'undefined') {
return;
}
const owner = document._id.toString();
for (let vehicle of [...alt.Vehicle.all]) {
const vehData = Athena.document.vehicle.get(vehicle);
if (typeof vehData === 'undefined') {
continue;
}
if (vehData.owner !== owner) {
continue;
}
if (vehData.doNotDespawn) {
continue;
}
await Athena.vehicle.controls.update(vehicle);
const vehicleId = vehicle.id;
try {
vehicle.destroy();
removedVehicles += 1;
} catch (err) {
console.log(err);
}
Athena.document.vehicle.unbind(vehicleId);
}
alt.log(`~y~Removed ${removedVehicles} vehicles owned by ${document.name} on disconnect.`);
},
};
/**
* Disable vehicles despawning when a player leaves.
*
*
* #### Example
* ```ts
* Athena.systems.defaults.vehiclesDespawnOnLeave.disable();
* ```
*
*
*/
export function disable() {
enabled = false;
alt.log(`~y~Default ${SYSTEM_NAME} Turned Off`);
}
Athena.systems.plugins.addCallback(Internal.init);