Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
getMods, applyMods, vehicle pathway
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Apr 5, 2023
1 parent e4e897f commit 94d0bf7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
- state.getCharacterData
- state.getCharacterPermissions
- Athena.vehicle.tuning.getMods
- Athena.vehicle.tuning.applyMods
--------------------------------------
--- Everything Below is Before April 2
--------------------------------------
Expand Down
67 changes: 67 additions & 0 deletions src/core/server/vehicle/tuning.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as alt from 'alt-server';
import * as Athena from '@AthenaServer/api';

import VehicleTuning from '@AthenaShared/interfaces/vehicleTuning';
import { VehicleState } from '@AthenaShared/interfaces/vehicleState';
import IVehicleMod from '@AthenaShared/interfaces/vehicleMod';

/**
* Applies specified properties to a vehicle in bulk.
Expand Down Expand Up @@ -70,17 +72,82 @@ export function getTuning(vehicle: alt.Vehicle): VehicleTuning {
return tuningData;
}

/**
* Apply mods to a vehicle.
*
* Automatically saves data if vehicle is non-temporary.
*
* @export
* @param {alt.Vehicle} vehicle
* @param {number} modkit
* @param {Array<IVehicleMod>} mods
* @return {*}
*/
export async function applyMods(vehicle: alt.Vehicle, modkit: number, mods: Array<IVehicleMod>) {
if (Overrides.applyMods) {
return Overrides.applyMods(vehicle, modkit, mods);
}

const currentMods = getMods(vehicle);
for (let mod of mods) {
const existingIndex = currentMods.findIndex((x) => x.id === mod.id);
if (existingIndex >= 0) {
currentMods[existingIndex].value = mod.value;
}

try {
vehicle.setMod(mod.id, mod.value);
} catch (err) {}
}

const data = Athena.document.vehicle.get(vehicle);
if (typeof data === 'undefined') {
return;
}

const tuning: VehicleTuning = {
modkit,
mods: currentMods,
};

await Athena.document.vehicle.set(vehicle, 'tuning', tuning);
}

/**
* Return all mods that are currently applied to a vehicle.
*
* @export
* @param {alt.Vehicle} vehicle
* @return {Array<IVehicleMod>}
*/
export function getMods(vehicle: alt.Vehicle): Array<IVehicleMod> {
const mods: Array<IVehicleMod> = [];

for (let i = 0; i < 67; i++) {
try {
const value = vehicle.getMod(i);
mods.push({ id: i, value });
} catch (err) {}
}

return mods;
}

interface VehicleTuningFuncs {
applyState: typeof applyState;
applyTuning: typeof applyTuning;
getTuning: typeof getTuning;
applyMods: typeof applyMods;
getMods: typeof getMods;
}

const Overrides: Partial<VehicleTuningFuncs> = {};

export function override(functionName: 'applyState', callback: typeof applyState);
export function override(functionName: 'applyTuning', callback: typeof applyTuning);
export function override(functionName: 'getTuning', callback: typeof getTuning);
export function override(functionName: 'applyMods', callback: typeof applyMods);
export function override(functionName: 'getMods', callback: typeof getMods);
/**
* Used to override vehicle tuning functionality
*
Expand Down

1 comment on commit 94d0bf7

@Fanatic191
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3 Thank you so much! <3

Please sign in to comment.