Skip to content

Commit

Permalink
fix(server/vehicle): store vehicles on resource stop
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Feb 21, 2024
1 parent 736e25a commit 07cdcd5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
6 changes: 3 additions & 3 deletions server/player/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'player/loading';
import 'player/events';
import 'player/commands';
import './loading';
import './events';
import './commands';
import { OxPlayer } from './class';
import { GetCharIdFromStateId } from './db';

Expand Down
7 changes: 5 additions & 2 deletions server/vehicle/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export class OxVehicle extends ClassInterface {
const vehicle = this.members[id];

if (!resource || resource === vehicle.script) {
if (vehicle.owner || vehicle.group) parameters.push(vehicle.#getSaveData());
if (vehicle.owner || vehicle.group) {
vehicle.#stored = 'impound';
parameters.push(vehicle.#getSaveData());
}

vehicle.despawn();
}
Expand All @@ -111,7 +114,7 @@ export class OxVehicle extends ClassInterface {
id?: number,
vin?: string,
owner?: number,
group?: string,
group?: string
) {
super();
this.entity = entity;
Expand Down
10 changes: 1 addition & 9 deletions server/vehicle/events.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { OxVehicle } from './class';

on('onResourceStop', (resource: string) => {
if (resource !== 'ox_core') return;

const vehicles = OxVehicle.getAll();

for (const id in vehicles) {
vehicles[id].despawn();
}
});
on('onResourceStop', (resource: string) => OxVehicle.saveAll(resource));

on('entityRemoved', (entityId: number) => {
const vehicle = OxVehicle.get(entityId);
Expand Down
10 changes: 6 additions & 4 deletions server/vehicle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { OxVehicle } from './class';
import { CreateNewVehicle, GetStoredVehicleFromId, IsPlateAvailable, VehicleRow } from './db';
import { GetVehicleData } from '../../common/vehicles';
import { DEBUG } from '../../common/config';
import type { Dict } from 'types';

import './class';
import './commands';
import type { Dict } from 'types';
import './events';

if (DEBUG) import('./parser');

Expand All @@ -20,7 +21,8 @@ export async function CreateVehicle(
properties?: Dict<any>;
}),
coords?: number | number[] | { x: number; y: number; z: number },
heading?: number
heading?: number,
invokingScript = GetInvokingResource()
) {
if (typeof data === 'string') data = { model: data };

Expand Down Expand Up @@ -68,7 +70,6 @@ export async function CreateVehicle(
if (typeof coords === 'number') coords = GetEntityCoords(coords);
else if (typeof coords === 'object' && !Array.isArray(coords)) coords = [coords.x || 0, coords.y || 0, coords.z || 0];

const invokingScript = GetInvokingResource();
const entity = coords
? CreateVehicleServerSetter(data.model, networkType, coords[0], coords[1], coords[2], heading || 90)
: 0;
Expand Down Expand Up @@ -122,13 +123,14 @@ export async function CreateVehicle(
}

export async function SpawnVehicle(id: number, coords: number | number[], heading?: number) {
const invokingScript = GetInvokingResource();
const vehicle = await GetStoredVehicleFromId(id);

if (!vehicle) return;

vehicle.data = JSON.parse(vehicle.data as any);

return await CreateVehicle(vehicle, coords, heading);
return await CreateVehicle(vehicle, coords, heading, invokingScript);
}

exports('CreateVehicle', CreateVehicle);
Expand Down

0 comments on commit 07cdcd5

Please sign in to comment.