diff --git a/[core]/es_extended/server/classes/vehicle.lua b/[core]/es_extended/server/classes/vehicle.lua index ce15c6fcf..0c2438f6c 100644 --- a/[core]/es_extended/server/classes/vehicle.lua +++ b/[core]/es_extended/server/classes/vehicle.lua @@ -21,47 +21,44 @@ ---@field setOwner fun(self:VehicleClass, owner:string):nil ---@field despawn fun(self:VehicleClass):nil ----@type table -local vehicles = {} ----@type VehicleClass -local vehicleClass = { +Core.vehicleClass = { plate = "", getNetId = function(self) - assert(vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") + assert(Core.vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") - return vehicles[self.plate].netId + return Core.vehicles[self.plate].netId end, getEntity = function(self) - assert(vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") + assert(Core.vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") - return vehicles[self.plate].entity + return Core.vehicles[self.plate].entity end, getPlate = function(self) - assert(vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") + assert(Core.vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") - return vehicles[self.plate].plate + return Core.vehicles[self.plate].plate end, getModelHash = function(self) - assert(vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") + assert(Core.vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") - return vehicles[self.plate].modelHash + return Core.vehicles[self.plate].modelHash end, getProps = function(self) - assert(vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") + assert(Core.vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") - return vehicles[self.plate].props + return Core.vehicles[self.plate].props end, getOwner = function(self) - assert(vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") + assert(Core.vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") - return vehicles[self.plate].owner + return Core.vehicles[self.plate].owner end, setPlate = function(self, plate, apply) - assert(vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") + assert(Core.vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") assert(type(plate) == "string", "Expected 'plate' to be a string") - local vehicle = vehicles[self.plate] + local vehicle = Core.vehicles[self.plate] if apply and vehicle.owner then SetVehicleNumberPlateText(vehicle.entity, plate) MySQL.prepare("UPDATE `owned_vehicles` SET `plate` = ? WHERE `plate` = ? AND `owner` = ?", plate, vehicle.plate, vehicle.owner) @@ -69,10 +66,10 @@ local vehicleClass = { vehicle.plate = plate end, setProps = function(self, props, apply) - assert(vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") + assert(Core.vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") assert(type(props) == "table", "Expected 'props' to be a table") - local vehicle = vehicles[self.plate] + local vehicle = Core.vehicles[self.plate] if apply and vehicle.owner then Entity(vehicle.entity).state:set("VehicleProperties", props, true) MySQL.prepare("UPDATE `owned_vehicles` SET `vehicle` = ? WHERE `plate` = ?", json.encode(props), vehicle.plate) @@ -81,22 +78,22 @@ local vehicleClass = { vehicle.props = props end, setOwner = function(self, owner) - assert(vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") + assert(Core.vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") assert(type(owner) == "string", "Expected 'owner' to be a string") - local vehicle = vehicles[self.plate] + local vehicle = Core.vehicles[self.plate] if (vehicle.owner == owner) then return end MySQL.prepare("UPDATE `owned_vehicles` SET `owner` = ? WHERE `plate` = ?", owner, vehicle.plate) - vehicles[self.plate].owner = owner + Core.vehicles[self.plate].owner = owner end, despawn = function(self) - assert(vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") + assert(Core.vehicles[self.plate] ~= nil, "Attempted to access invalid vehicle") - local vehicle = vehicles[self.plate] + local vehicle = Core.vehicles[self.plate] if DoesEntityExist(vehicle.entity) then DeleteEntity(vehicle.entity) end @@ -104,63 +101,6 @@ local vehicleClass = { MySQL.prepare("UPDATE `owned_vehicles` SET `stored` = 1 WHERE `plate` = ?", vehicle.plate) - vehicles[self.plate] = nil + Core.vehicles[self.plate] = nil end, } - ----@param plate string ----@param entity number ----@param model string|number ----@param props table ----@param owner string ----@return VehicleClass -function ESX.CreateExtendedVehicle(plate, entity, model, props, owner) - assert(type(plate) == "string", "Expected 'plate' to be a string") - assert(type(entity) == "number", "Expected 'entity' to be a number") - assert(type(model) == "string" or type(model) == "number", "Expected 'model' to be a string or number") - assert(type(props) == "table", "Expected 'props' to be a table") - assert(type(owner) == "string", "Expected 'owner' to be a string") - assert(DoesEntityExist(entity) == true, "Entity does not exist") - assert(GetEntityType(entity) == 2, "Entity is not a vehicle") - - if vehicles[plate] then - local obj = table.clone(vehicleClass) - obj.plate = plate - return obj - end - - if type(model) ~= "number" then - model = GetHashKey(model) - end - - local netId = NetworkGetNetworkIdFromEntity(entity) - ---@type VehicleData - local vehicle = { - plate = plate, - entity = entity, - netId = netId, - modelHash = model, - props = props, - owner = owner, - } - vehicles[netId] = vehicle - - local obj = table.clone(vehicleClass) - obj.plate = plate - TriggerEvent("esx:vehicleSpawned", obj) - MySQL.prepare("UPDATE `owned_vehicles` SET `stored` = 0 WHERE `plate` = ?", plate) - - return obj -end - ----@param plate string ----@return VehicleClass|nil -function ESX.GetVehicleFromPlate(plate) - assert(type(plate) == "string", "Expected 'plate' to be a string") - - if vehicles[plate] then - local obj = table.clone(vehicleClass) - obj.plate = plate - return obj - end -end diff --git a/[core]/es_extended/server/common.lua b/[core]/es_extended/server/common.lua index edbc39529..f49f91c26 100644 --- a/[core]/es_extended/server/common.lua +++ b/[core]/es_extended/server/common.lua @@ -12,6 +12,8 @@ Core.DatabaseConnected = false Core.playersByIdentifier = {} Core.vehicleTypesByModel = {} +---@type table +Core.vehicles = {} RegisterNetEvent("esx:onPlayerSpawn", function() ESX.Players[source].spawned = true diff --git a/[core]/es_extended/server/functions.lua b/[core]/es_extended/server/functions.lua index 617af16cb..05539f6dc 100644 --- a/[core]/es_extended/server/functions.lua +++ b/[core]/es_extended/server/functions.lua @@ -370,6 +370,63 @@ function ESX.GetIdentifier(playerId) return identifier and identifier:gsub("license:", "") end +---@param plate string +---@param entity number +---@param model string|number +---@param props table +---@param owner string +---@return VehicleClass +function ESX.CreateExtendedVehicle(plate, entity, model, props, owner) + assert(type(plate) == "string", "Expected 'plate' to be a string") + assert(type(entity) == "number", "Expected 'entity' to be a number") + assert(type(model) == "string" or type(model) == "number", "Expected 'model' to be a string or number") + assert(type(props) == "table", "Expected 'props' to be a table") + assert(type(owner) == "string", "Expected 'owner' to be a string") + assert(DoesEntityExist(entity) == true, "Entity does not exist") + assert(GetEntityType(entity) == 2, "Entity is not a vehicle") + + if Core.vehicles[plate] then + local obj = table.clone(Core.vehicleClass) + obj.plate = plate + return obj + end + + if type(model) ~= "number" then + model = GetHashKey(model) + end + + local netId = NetworkGetNetworkIdFromEntity(entity) + ---@type VehicleData + local vehicle = { + plate = plate, + entity = entity, + netId = netId, + modelHash = model, + props = props, + owner = owner, + } + Core.vehicles[netId] = vehicle + + local obj = table.clone(Core.vehicleClass) + obj.plate = plate + TriggerEvent("esx:vehicleSpawned", obj) + MySQL.prepare("UPDATE `owned_vehicles` SET `stored` = 0 WHERE `plate` = ?", plate) + + return obj +end + +---@param plate string +---@return VehicleClass|nil +function ESX.GetVehicleFromPlate(plate) + assert(type(plate) == "string", "Expected 'plate' to be a string") + + if Core.vehicles[plate] then + local obj = table.clone(Core.vehicleClass) + obj.plate = plate + return obj + end +end + ---@param model string|number ---@param player number ---@param cb function