Skip to content

Commit

Permalink
refactor(vehicle): use ox_lib properties handling
Browse files Browse the repository at this point in the history
I'm done with props - the community can debug the problem now.
  • Loading branch information
thelindat committed Jul 14, 2024
1 parent 2edb79e commit 7f4817c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 101 deletions.
46 changes: 0 additions & 46 deletions client/vehicle/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
cache,
getVehicleProperties,
onServerCallback,
setVehicleProperties,
waitFor,
} from '@overextended/ox_lib/client';
import { Vector3 } from '@nativewrappers/fivem';
Expand Down Expand Up @@ -45,47 +43,3 @@ AddStateBagChangeHandler('initVehicle', '', async (bagName: string, key: string,
SetVehicleOnGroundProperly(entity);
setTimeout(() => Entity(entity).state.set(key, null, true));
});

AddStateBagChangeHandler('vehicleProperties', '', async (bagName: string, key: string, value: any) => {
if (!value) return DEBUG && console.info(`removed ${key} state from ${bagName}`);

const entity = await waitFor(
async () => {
const entity = GetEntityFromStateBagName(bagName);
DEV: console.info(key, entity);

if (entity) return entity;
},
'failed to get entity from statebag name',
10000
);

if (!entity) return;

const status = await new Promise((resolve) => {
let i = 0;
let interval: CitizenTimer;

interval = setInterval(() => {
i++;
const doesEntityExist = DoesEntityExist(entity);

if (i % 5) console.info(`Attempting to set ${bagName} on entity ${entity} (${i})`);

if (i > 100 || !doesEntityExist || !Entity(entity).state[key]) {
resolve(doesEntityExist ? 0 : i > 100 ? 1 : 2);
return clearInterval(interval);
}

setVehicleProperties(entity, value);
}, 200);
});

console.info(
status
? status === 1
? `Failed to set ${bagName} on entity ${entity} (timed out)`
: `Set ${bagName} on entity ${entity}`
: `Failed to set ${bagName} on entity ${entity} (entity does not exist)`
);
});
2 changes: 1 addition & 1 deletion common/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { checkDependency } from '@overextended/ox_lib/';
import type { OxGroupPermissions } from 'types';

if (!checkDependency('ox_lib', '3.20.0', true)) throw new Error(`Failed dependency check.`);
if (!checkDependency('ox_lib', '3.24.0', true)) throw new Error(`Failed dependency check.`);

export function LoadDataFile(file: string) {
return JSON.parse(LoadResourceFile('ox_core', `/common/data/${file}.json`));
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
"devDependencies": {
"@citizenfx/client": "latest",
"@citizenfx/server": "latest",
"@types/node": "^20.14.2",
"@types/node": "^20.14.10",
"esbuild": "^0.20.2",
"prettier": "^3.3.1",
"prettier": "^3.3.3",
"tsc-alias": "^1.8.10",
"typescript": "^5.4.5"
"typescript": "^5.5.3"
},
"dependencies": {
"@nativewrappers/fivem": "^0.0.28",
"@overextended/ox_lib": "^3.22.2",
"@overextended/ox_lib": "^3.24.0",
"mariadb": "^3.3.1"
},
"engines": {
Expand Down
69 changes: 34 additions & 35 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 2 additions & 15 deletions server/vehicle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { OxVehicle } from './class';
import { CreateNewVehicle, GetStoredVehicleFromId, IsPlateAvailable, VehicleRow } from './db';
import { GetVehicleData } from '../../common/vehicles';
import { DEBUG } from '../../common/config';

import './class';
import './commands';
import './events';
import { VehicleProperties } from '@overextended/ox_lib';
import { VehicleProperties, setVehicleProperties } from '@overextended/ox_lib/server';
import { VectorFromBuffer } from '../../common';

if (DEBUG) import('./parser');
Expand Down Expand Up @@ -121,19 +120,7 @@ export async function CreateVehicle(
const state = vehicle.getState();

state.set('initVehicle', true, true);
state.set('vehicleProperties', metadata.properties, true);

let interval: CitizenTimer;

interval = setInterval(() => {
if (GetVehicleNumberPlateText(entity) !== '') {
console.log(`Set vehicleProperties for entity ${entity}`);
state.set('vehicleProperties', null, true);
return clearInterval(interval);
}

if (!DoesEntityExist(entity)) clearInterval(interval);
}, 200);
setVehicleProperties(entity, metadata.properties)

return vehicle;
}
Expand Down

0 comments on commit 7f4817c

Please sign in to comment.