-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib/server): add Lua vehicle bindings
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
local OxVehicle = lib.class('OxVehicle') | ||
|
||
function OxVehicle:__index(index) | ||
local value = OxVehicle[index] --[[@as any]] | ||
|
||
if type(value) == 'function' then | ||
self[index] = value == OxVehicle.__call and function(...) | ||
return value(self, index, ...) | ||
end or function(...) | ||
return value(self, ...) | ||
end | ||
|
||
return self[index] | ||
end | ||
|
||
return value | ||
end | ||
|
||
function OxVehicle:__call(...) | ||
return exports.ox_core:CallVehicle(self.entity, ...) | ||
end | ||
|
||
function OxVehicle:__tostring() | ||
return json.encode(self) | ||
end | ||
|
||
function OxVehicle:getCoords() | ||
return GetEntityCoords(self.entity); | ||
end | ||
|
||
function OxVehicle:getState() | ||
return Entity(self.entity).state; | ||
end | ||
|
||
for method in pairs(exports.ox_core:GetVehicleCalls() or {}) do | ||
if not OxVehicle[method] then OxVehicle[method] = OxVehicle.__call end | ||
end | ||
|
||
local function CreateVehicleInstance(vehicle) | ||
if not vehicle then return end; | ||
|
||
return OxVehicle:new(vehicle) | ||
end | ||
|
||
---@class OxServer | ||
local Ox = Ox | ||
|
||
function Ox.GetVehicle(entityId) | ||
return CreateVehicleInstance(exports.ox_core:GetVehicle(entityId)) | ||
end | ||
|
||
function Ox.GetVehicleFromNetId(netId) | ||
return CreateVehicleInstance(exports.ox_core:GetVehicleFromNetId(netId)) | ||
end | ||
|
||
function Ox.GetVehicleFromVin(vin) | ||
return CreateVehicleInstance(exports.ox_core:GetVehicleFromVin(vin)) | ||
end | ||
|
||
function Ox.CreateVehicle(data, coords, heading) | ||
return CreateVehicleInstance(exports.ox_core:CreateVehicle(data, coords, heading)); | ||
end | ||
|
||
function Ox.SpawnVehicle(dbId, coords, heading) | ||
return CreateVehicleInstance(exports.ox_core:SpawnVehicle(dbId, coords, heading)); | ||
end |