Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: utils to lib and remove house garages #36

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Lua.diagnostics.globals": [
"lib",
"cache",
"QBX",
"qbx",
"MySQL"
]
}
50 changes: 12 additions & 38 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local config = require 'config.client'
local sharedConfig = require 'config.shared'
local VEHICLES = exports.qbx_core:GetVehiclesByName()
local lasthouse = nil

local function doCarDamage(currentVehicle, vehicle)
local engine = vehicle.engine + 0.0
Expand Down Expand Up @@ -73,8 +72,8 @@ local function getStateLabel(state)
end

local function displayVehicleInfo(vehicle, garageName, garageInfo)
local engine = math.round(vehicle.engine / 10)
local body = math.round(vehicle.body / 10)
local engine = qbx.math.round(vehicle.engine / 10)
local body = qbx.math.round(vehicle.body / 10)
local engineColor = getProgressColor(engine)
local bodyColor = getProgressColor(body)
local fuelColor = getProgressColor(vehicle.fuel)
Expand All @@ -85,7 +84,7 @@ local function displayVehicleInfo(vehicle, garageName, garageInfo)
{
title = 'Information',
icon = 'circle-info',
description = ('Name: %s\nPlate: %s\nStatus: %s\nImpound Fee: $%s'):format(vehLabel, vehicle.plate, stateLabel, CommaValue(vehicle.depotprice)),
description = ('Name: %s\nPlate: %s\nStatus: %s\nImpound Fee: $%s'):format(vehLabel, vehicle.plate, stateLabel, lib.math.groupdigits(vehicle.depotprice)),
readOnly = true,
},
{
Expand Down Expand Up @@ -116,7 +115,7 @@ local function displayVehicleInfo(vehicle, garageName, garageInfo)
options[#options + 1] = {
title = 'Take out',
icon = 'fa-truck-ramp-box',
description = '$'..CommaValue(vehicle.depotprice),
description = '$'..lib.math.groupdigits(vehicle.depotprice),
event = 'qb-garages:client:TakeOutDepot',
arrow = true,
args = {
Expand Down Expand Up @@ -204,14 +203,14 @@ RegisterNetEvent('qb-garages:client:takeOutGarage', function(data)
return
end

local netId = lib.callback.await('qb-garage:server:spawnvehicle', false, data.vehicle, data.garageInfo.type == 'house' and data.garageInfo.coords or data.garageInfo.spawn, sharedConfig.takeOut.warpInVehicle)
local timeout = 100
while not NetworkDoesEntityExistWithNetworkId(netId) and timeout > 0 do
Wait(10)
timeout -= 1
end
local netId = lib.callback.await('qb-garage:server:spawnvehicle', false, data.vehicle, data.garageInfo.spawn)

local veh = lib.waitFor(function()
if NetworkDoesEntityExistWithNetworkId(netId) then
return NetToVeh(netId)
end
end)

local veh = NetToVeh(netId)
if veh == 0 then
exports.qbx_core:Notify('Something went wrong spawning the vehicle', 'error')
return
Expand All @@ -220,17 +219,14 @@ RegisterNetEvent('qb-garages:client:takeOutGarage', function(data)
SetVehicleFuelLevel(veh, data.vehicle.fuel)
doCarDamage(veh, data.vehicle)
TriggerServerEvent('qb-garage:server:updateVehicleState', 0, data.vehicle.plate, data.garageName)
TriggerEvent('vehiclekeys:client:SetOwner', data.vehicle.plate)

if not sharedConfig.takeOut.engineOff then
SetVehicleEngineOn(veh, true, true, false)
end

Wait(500)
end)

local function parkVehicle(vehicle, garageName, garageInfo)
local plate = GetPlate(vehicle)
local plate = qbx.getVehiclePlate(vehicle)
if GetVehicleNumberOfPassengers(vehicle) ~= 1 then
local owned = lib.callback.await('qb-garage:server:checkOwnership', false, plate, garageInfo.type, garageName, QBX.PlayerData.gang.name)
if not owned then
Expand Down Expand Up @@ -361,28 +357,6 @@ local function createGarages()
end
end

RegisterNetEvent('qb-garages:client:setHouseGarage', function(house, hasKey)
if sharedConfig.houseGarages[house] then
if lasthouse ~= house then
--[[if lasthouse then
destroyZone('hmarker', lasthouse)
end]]--
if hasKey and sharedConfig.houseGarages[house].coords.x then
createZones(house, sharedConfig.houseGarages[house])
lasthouse = house
end
end
end
end)

RegisterNetEvent('qb-garages:client:houseGarageConfig', function(garageConfig)
sharedConfig.houseGarages = garageConfig
end)

RegisterNetEvent('qb-garages:client:addHouseGarage', function(house, garageInfo)
sharedConfig.houseGarages[house] = garageInfo
end)

AddEventHandler('QBCore:Client:OnPlayerLoaded', function()
createGarages()
end)
Expand Down
22 changes: 11 additions & 11 deletions config/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ return {

---@class GarageConfig
---@field label string -- Label for the garage
---@field coords vector4 -- Coordinates for taking out a vehicle
---@field size vector3 -- Size of the zone
---@field coords vector4 -- Coordinates for the garage
---@field size vector3 -- Size of the garage
---@field spawn vector4 -- Coordinates where the vehicle will spawn
---@field showBlip boolean? -- Enable or disable the blip
---@field blipName string? -- Name of the blip
---@field blipSprite number? -- Sprite for the blip
---@field blipColor number? -- Color for the blip
---@field type public | job | gang | depot -- Type of garage
---@field vehicle car | air | sea | all -- Vehicle type
---@field job string? -- Job / Gang name
---@type table<garageName, GarageConfig>
---@field showBlip boolean -- Enable or disable the blip
---@field blipName string -- Name of the blip
---@field blipSprite number -- Sprite for the blip
---@field blipColor number -- Color for the blip
---@field type 'public'|'job'|'gang'|'depot' -- Type of garage
---@field vehicle 'car'|'air'|'sea'|'all' -- Vehicle type
---@field job string -- Job / Gang name

---@type table<string, GarageConfig>

-- Public Garages
motelgarage = {
Expand Down
5 changes: 1 addition & 4 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version '1.0.0'

shared_scripts {
'@ox_lib/init.lua',
'@qbx_core/modules/utils.lua',
'@qbx_core/modules/lib.lua',
'@qbx_core/shared/locale.lua',
'locales/en.lua',
'locales/*.lua',
Expand All @@ -28,8 +28,5 @@ files {
'config/shared.lua',
}

dependency 'ox_lib'

provide 'qb-garages'
lua54 'yes'
use_experimental_fxv2_oal 'yes'
21 changes: 12 additions & 9 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,27 @@ end

lib.callback.register('qb-garage:server:checkOwnership', checkOwnership)

lib.callback.register('qb-garage:server:spawnvehicle', function (source, vehInfo, coords, warp)
local plate = vehInfo.plate
local vehProps = {}
lib.callback.register('qb-garage:server:spawnvehicle', function (source, vehInfo, coords)
local props = {}

local result = MySQL.query.await('SELECT mods FROM player_vehicles WHERE plate = ?', {plate})
local result = MySQL.query.await('SELECT mods FROM player_vehicles WHERE plate = ?', {vehInfo.plate})
if result[1] then
vehProps = json.decode(result[1].mods)
props = json.decode(result[1].mods)
end

local netId = SpawnVehicle(source, vehInfo.vehicle, coords, warp, vehProps)
local netId = qbx.spawnVehicle({ spawnSource = coords, model = vehInfo.vehicle, props = props})

local veh = NetworkGetEntityFromNetworkId(netId)
SetVehicleNumberPlateText(veh, plate)

SetVehicleNumberPlateText(veh, vehInfo.plate)

if sharedConfig.takeOut.doorsLocked then
SetVehicleDoorsLocked(veh, 2)
end

outsideVehicles[plate] = {netID = netId, entity = veh}
TriggerClientEvent('vehiclekeys:client:SetOwner', source, vehInfo.plate)

outsideVehicles[vehInfo.plate] = {netID = netId, entity = veh}
return netId
end)

Expand Down Expand Up @@ -142,7 +145,7 @@ RegisterNetEvent('qb-garage:server:updateVehicleState', function(state, plate, g
if not carInfo then return end

local vehCost = VEHICLES[carInfo.vehicle].price
local newPrice = math.round(vehCost * (config.impoundFee.percentage / 100))
local newPrice = qbx.math.round(vehCost * (config.impoundFee.percentage / 100))
if config.impoundFee.enable then
if carInfo.depotprice ~= newPrice then
MySQL.update('UPDATE player_vehicles SET state = ?, depotprice = ? WHERE plate = ?', {state, newPrice, plate})
Expand Down
Loading