Skip to content

Commit

Permalink
refactor(es_extended/server/classes/vehicle)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenshiin13 committed Nov 25, 2024
1 parent f3fe180 commit dda11d9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 83 deletions.
106 changes: 23 additions & 83 deletions [core]/es_extended/server/classes/vehicle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,55 @@
---@field setOwner fun(self:VehicleClass, owner:string):nil
---@field despawn fun(self:VehicleClass):nil

---@type table<number, VehicleData>
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)
end
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)
Expand All @@ -81,86 +78,29 @@ 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
TriggerEvent("esx:vehicleDespawned", self.plate)

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
2 changes: 2 additions & 0 deletions [core]/es_extended/server/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Core.DatabaseConnected = false
Core.playersByIdentifier = {}

Core.vehicleTypesByModel = {}
---@type table<number, VehicleData>
Core.vehicles = {}

RegisterNetEvent("esx:onPlayerSpawn", function()
ESX.Players[source].spawned = true
Expand Down
57 changes: 57 additions & 0 deletions [core]/es_extended/server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dda11d9

Please sign in to comment.