Skip to content

Commit

Permalink
feat: GetVehicleClass server export (#566)
Browse files Browse the repository at this point in the history
* feat: GetVehicleModel export

* fix: check for empty cache

* fix: lint

* remove unused meta table
  • Loading branch information
Manason authored Sep 17, 2024
1 parent 674864a commit bd4ed96
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,14 @@ CreateThread(function()
end
end
end)

lib.callback.register('qbx_core:client:getVehicleClasses', function()
local models = GetAllVehicleModels()
local classes = {}
for i = 1, #models do
local model = models[i]
local class = GetVehicleClassFromName(model)
classes[model] = class
end
return classes
end)
21 changes: 21 additions & 0 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ QBX.Player_Buckets = {}
QBX.Entity_Buckets = {}
QBX.UsableItems = {}

---@alias Model number
---@alias VehicleClass integer see https://docs.fivem.net/natives/?_0x29439776AAA00A62
---@type table<Model, VehicleClass>
local vehicleClasses = {}

---Caches the vehicle classes the first time this is called by getting the data from a random client.
---Returns nil if there is no cache and no client is connected to get the data from.
---@param model number
---@return VehicleClass?
function GetVehicleClass(model)
if #vehicleClasses == 0 then
local players = GetPlayers()
if #players == 0 then return end
local playerId = players[math.random(#players)]
vehicleClasses = lib.callback.await('qbx_core:client:getVehicleClasses', playerId)
end
return vehicleClasses[model]
end

exports('GetVehicleClass', GetVehicleClass)

---@return table<string, Vehicle>
function GetVehiclesByName()
return QBX.Shared.Vehicles
Expand Down

0 comments on commit bd4ed96

Please sign in to comment.