From 9de7454eb7f5d7b32be14d7d9699bc4c825d9ca7 Mon Sep 17 00:00:00 2001 From: David Malchin Date: Thu, 11 Jan 2024 22:46:40 +0200 Subject: [PATCH] refactor: use lib over utils --- bridge/qb/client/events.lua | 2 +- bridge/qb/client/functions.lua | 460 ++++++++++++++++++++++++++++----- bridge/qb/server/functions.lua | 104 +++++++- bridge/qb/server/main.lua | 14 +- bridge/qb/shared/main.lua | 90 +++++-- client/character.lua | 4 +- client/events.lua | 5 +- client/main.lua | 2 +- config/server.lua | 4 +- fxmanifest.lua | 3 +- server/commands.lua | 14 +- server/events.lua | 8 +- server/main.lua | 2 +- 13 files changed, 598 insertions(+), 114 deletions(-) diff --git a/bridge/qb/client/events.lua b/bridge/qb/client/events.lua index d99a98086..7f353346e 100644 --- a/bridge/qb/client/events.lua +++ b/bridge/qb/client/events.lua @@ -8,7 +8,7 @@ end) RegisterNetEvent('QBCore:Client:VehicleInfo', function(info) local vehicle = NetworkGetEntityFromNetworkId(info.netId) - local plate = GetPlate(vehicle) + local plate = qbx.getVehiclePlate(vehicle) local hasKeys = config.hasKeys() local data = { diff --git a/bridge/qb/client/functions.lua b/bridge/qb/client/functions.lua index 138e774ab..f5912967e 100644 --- a/bridge/qb/client/functions.lua +++ b/bridge/qb/client/functions.lua @@ -11,29 +11,84 @@ function functions.GetPlayerData(cb) cb(QBX.PlayerData) end ----@deprecated use GetCoordsFromEntity from imports/utils.lua -functions.GetCoords = GetCoordsFromEntity +---@deprecated use the GetEntityCoords and GetEntityHeading natives directly +---@param entity number +---@return vector4 +functions.GetCoords = function(entity) + local coords = GetEntityCoords(entity) + return vec4(coords.x, coords.y, coords.z, GetEntityHeading(entity)) +end ---@deprecated use https://overextended.dev/ox_inventory/Functions/Client#search -functions.HasItem = HasItem +---@param items string | string[] +---@param amount integer +---@return boolean +functions.HasItem = function(items, amount) + amount = amount or 1 + local count = exports.ox_inventory:Search('count', items) + if type(items) == 'table' and type(count) == 'table' then + for _, v in pairs(count) do + if v < amount then + return false + end + end + return true + end + return count >= amount +end -- Utility ----@deprecated use DrawText2D from imports/utils.lua +---@deprecated use qbx.drawText2d from modules/lib.lua +---@param x number +---@param y number +---@param width number +---@param height number +---@param scale number +---@param r integer +---@param g integer +---@param b integer +---@param a integer +---@param text string functions.DrawText = function(x, y, width, height, scale, r, g, b, a, text) - DrawText2D(text, vec2(x, y), width, height, scale, 4, r, g, b, a) + qbx.drawText2d({ + coords = vec2(x, y), + width = width, + height = height, + scale = scale, + color = vec4(r, g, b, a), + text = text, + }) end ----@deprecated use DrawText3D from imports/utils.lua +---@deprecated use qbx.drawText3d from modules/lib.lua +---@param x number +---@param y number +---@param z number +---@param text string functions.DrawText3D = function(x, y, z, text) - DrawText3D(text, vec3(x, y, z), 0.35, 4, 255, 255, 255, 215) + qbx.drawText3d({ + coords = vec3(x, y, z), + text = text, + }) end ---@deprecated use lib.requestAnimDict from ox_lib functions.RequestAnimDict = lib.requestAnimDict ----@deprecated use PlayAnim from imports/utils.lua -functions.PlayAnim = PlayAnim +---@deprecated use PlayAnim from modules/lib.lua +---@async +---@param animDict string +---@param animName string +---@param upperbodyOnly? boolean +---@param duration? integer +functions.PlayAnim = function(animDict, animName, upperbodyOnly, duration) + local flags = upperbodyOnly and 16 or 0 + local runTime = duration or -1 + lib.requestAnimDict(animDict) + TaskPlayAnim(cache.ped, animDict, animName, 8.0, 3.0, runTime, flags, 0.0, false, false, true) + RemoveAnimDict(animDict) +end ---@deprecated use lib.requestModel from ox_lib functions.LoadModel = lib.requestModel @@ -86,56 +141,189 @@ end -- Getters ----@deprecated use GetVehicles from imports/utils.lua -functions.GetVehicles = GetVehicles +---@param pool string +---@param ignoreList? integer[] +---@return integer[] +local function getEntities(pool, ignoreList) + ignoreList = ignoreList or {} + local ents = GetGamePool(pool) + local entities = {} + local ignoreMap = {} + for i = 1, #ignoreList do + ignoreMap[ignoreList[i]] = true + end + + for i = 1, #ents do + local entity = ents[i] + if not ignoreMap[entity] then + entities[#entities + 1] = entity + end + end + return entities +end + +---@param entities integer[] +---@param coords vector3? +---@return integer closestObj +---@return number closestDistance +local function getClosestEntity(entities, coords) + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords or GetEntityCoords(cache.ped) + local closestDistance = -1 + local closestEntity = -1 + for i = 1, #entities do + local entity = entities[i] + local entityCoords = GetEntityCoords(entity) + local distance = #(entityCoords - coords) + if closestDistance == -1 or closestDistance > distance then + closestEntity = entity + closestDistance = distance + end + end + return closestEntity, closestDistance +end ----@deprecated use GetObjects from imports/utils.lua -functions.GetObjects = GetObjects +---@deprecated use the GetGamePool('CVehicle') native directly +functions.GetVehicles = function() + return GetGamePool('CVehicle') +end ----@deprecated use GetPlayersInScope from imports/utils.lua -functions.GetPlayers = GetPlayersInScope +---@deprecated use the GetGamePool('CObject') native directly +functions.GetObjects = function() + return GetGamePool('CObject') +end + +---@deprecated use the GetActivePlayers native directly +functions.GetPlayers = function() + return GetActivePlayers() +end ----@deprecated use GetPeds from imports/utils.lua -functions.GetPeds = GetPeds +---@deprecated use the GetGamePool('CPed') native directly +---@param ignoreList? integer[] +functions.GetPeds = function(ignoreList) + return getEntities('CPed', ignoreList) +end ----@deprecated use GetClosestPed from imports/utils.lua +---@deprecated use lib.getClosestPed from ox_lib ---Use GetClosestPlayer if wanting to ignore non-player peds -functions.GetClosestPed = GetClosestPed +---@param coords? vector3 +---@param ignoreList? integer[] +functions.GetClosestPed = function(coords, ignoreList) + return getClosestEntity(getEntities('CPed', ignoreList), coords) +end ----@deprecated use IsWearingGloves from imports/utils.lua -functions.IsWearingGloves = IsWearingGloves +---@deprecated use qbx.isWearingGloves from modules/lib.lua +functions.IsWearingGloves = function() + return qbx.isWearingGloves() +end ----@deprecated use GetClosestPlayer from imports/utils.lua -functions.GetClosestPlayer = GetClosestPlayer +---@deprecated use lib.getClosestPlayer from ox_lib +---@param coords? vector3 +functions.GetClosestPlayer = function(coords) + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords or GetEntityCoords(cache.ped) + local playerId, _, playerCoords = lib.getClosestPlayer(coords, 5.0, false) + local closestDistance = playerCoords and #(playerCoords - coords) or nil + return playerId, closestDistance +end ----@deprecated use GetPlayersFromCoords from imports/utils.lua -functions.GetPlayersFromCoords = GetPlayersFromCoords +---@deprecated use lib.getNearbyPlayers from ox_lib +---@param coords? vector3 +---@param distance? number +functions.GetPlayersFromCoords = function(coords, distance) + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords or GetEntityCoords(cache.ped) + local players = lib.getNearbyPlayers(coords, distance or 5.0, true) ----@deprecated use GetClosestVehicle from imports/utils.lua -functions.GetClosestVehicle = GetClosestVehicle + -- This is for backwards compatability as beforehand it only returned the PlayerId, where Lib returns PlayerPed, PlayerId and PlayerCoords + for i = 1, #players do + players[i] = players[i].id + end ----@deprecated use GetClosestObject from imports/utils.lua -functions.GetClosestObject = GetClosestObject + return players +end + +---@deprecated use lib.getClosestVehicle from ox_lib +---@param coords? vector3 +functions.GetClosestVehicle = function(coords) + return getClosestEntity(GetGamePool('CVehicle'), coords) +end + +---@deprecated use lib.getClosestObject from ox_lib +---@param coords? vector3 +functions.GetClosestObject = function(coords) + return getClosestEntity(GetGamePool('CObject'), coords) +end ----@deprecated use GetClosestBone from imports/utils.lua -functions.GetClosestBone = GetClosestBone +---@deprecated use the GetWorldPositionOfEntityBone native and calculate distance directly +---@param entity integer +---@param list integer[] | {id: integer}[] bones +---@return integer | {id: integer} | {id: integer, type: string, name: string} +---@return vector3 boneCoords +---@return number boneDistance +functions.GetClosestBone = function(entity, list) + local playerCoords = GetEntityCoords(cache.ped) + + ---@type integer | {id: integer} | {id: integer, type: string, name: string}, vector3, number + local bone, coords, distance + for _, element in pairs(list) do + local boneCoords = GetWorldPositionOfEntityBone(entity, element.id or element) + local boneDistance = #(playerCoords - boneCoords) + if not coords or distance > boneDistance then + bone = element + coords = boneCoords + distance = boneDistance + end + end + if not bone then + bone = {id = GetEntityBoneIndexByName(entity, "bodyshell"), type = "remains", name = "bodyshell"} + coords = GetWorldPositionOfEntityBone(entity, bone.id) + distance = #(coords - playerCoords) + end + return bone, coords, distance +end ----@deprecated use GetBoneDistance from imports/utils.lua -functions.GetBoneDistance = GetBoneDistance +---@deprecated use the GetWorldPositionOfEntityBone native and calculate distance directly +---@param entity integer +---@param boneType integer +---@param bone string | integer +---@return number distance +functions.GetBoneDistance = function(entity, boneType, bone) + local boneIndex = boneType == 1 and GetPedBoneIndex(entity, bone --[[@as integer]]) or GetEntityBoneIndexByName(entity, bone --[[@as string]]) + local boneCoords = GetWorldPositionOfEntityBone(entity, boneIndex) + local playerCoords = GetEntityCoords(cache.ped) + return #(playerCoords - boneCoords) +end ----@deprecated use AttachProp from imports/utils.lua -functions.AttachProp = AttachProp +---@deprecated use the AttachEntityToEntity native directly +---@param ped integer +---@param model string | integer +---@param boneId integer +---@param x number +---@param y number +---@param z number +---@param xR number +---@param yR number +---@param zR number +---@param vertex boolean +---@return integer prop +functions.AttachProp = function(ped, model, boneId, x, y, z, xR, yR, zR, vertex) + local modelHash = type(model) == 'string' and joaat(model) or model + local bone = GetPedBoneIndex(ped, boneId) + lib.requestModel(modelHash) + local prop = CreateObject(modelHash, 1.0, 1.0, 1.0, true, true, false) + AttachEntityToEntity(prop, ped, bone, x, y, z, xR, yR, zR, true, true, false, true, not vertex and 2 or 0, true) + SetModelAsNoLongerNeeded(modelHash) + return prop +end -- Vehicle ----@deprecated call server function CreateVehicle instead from imports/utils.lua. +---@deprecated use server-side qbx.spawnVehicle from modules/lib.lua. ---@param model string|number ---@param cb? fun(vehicle: number) ---@param coords? vector4 player position if not specified ---@param isnetworked? boolean defaults to true ---@param teleportInto boolean teleport player to driver seat if true function functions.SpawnVehicle(model, cb, coords, isnetworked, teleportInto) - coords = type(coords) == 'table' and vec4(coords.x, coords.y, coords.z, coords.w or GetEntityHeading(cache.ped)) or coords or GetCoordsFromEntity(cache.ped) + coords = type(coords) == 'table' and vec4(coords.x, coords.y, coords.z, coords.w or GetEntityHeading(cache.ped)) or coords or functions.GetCoords(cache.ped) model = type(model) == 'string' and joaat(model) or model if not IsModelInCdimage(model) then return end @@ -153,17 +341,32 @@ function functions.SpawnVehicle(model, cb, coords, isnetworked, teleportInto) if cb then cb(veh) end end ----@deprecated use DeleteVehicle from imports/utils.lua -functions.DeleteVehicle = DeleteVehicle +---@deprecated use qbx.deleteVehicle from modules/lib.lua +---@param vehicle integer +functions.DeleteVehicle = function(vehicle) + qbx.deleteVehicle(vehicle) +end ----@deprecated use GetPlate from imports/utils.lua -functions.GetPlate = GetPlate +---@deprecated use qbx.getVehiclePlate from modules/lib.lua +---@param vehicle integer +functions.GetPlate = function(vehicle) + if not vehicle or vehicle == 0 then return end + return qbx.getVehiclePlate(vehicle) +end ----@deprecated use GetVehicleDisplayName from imports/utils.lua -functions.GetVehicleLabel = GetVehicleDisplayName +---@deprecated use qbx.getVehicleDisplayName from modules/lib.lua +---@param vehicle integer +functions.GetVehicleLabel = function(vehicle) + if not vehicle or vehicle == 0 then return end + return qbx.getVehicleDisplayName(vehicle) +end ----@deprecated use IsVehicleSpawnClear from imports/utils.lua -functions.SpawnClear = IsVehicleSpawnClear +---@deprecated use lib.getNearbyVehicles from ox_lib +functions.SpawnClear = function(coords, radius) + local veh = getClosestEntity(GetGamePool('CVehicle'), coords) + local distance = #(GetEntityCoords(veh) - coords) + return distance <= radius +end ---@deprecated use lib.getVehicleProperties from ox_lib function functions.GetVehicleProperties(vehicle) @@ -261,7 +464,7 @@ function functions.SetVehicleProperties(vehicle, props) props.modLightbar = props.modLightbar or props.modKit49 props.modRoofLivery = props.modRoofLivery or props.liveryRoof - + --- lib.setVehicleProperties copied and pasted from Overextended below so that we can remove the error so that setting properties is best effort if not DoesEntityExist(vehicle) then error(("Unable to set vehicle properties for '%s' (entity does not exist)"): @@ -621,26 +824,165 @@ end ---@deprecated use lib.requestNamedPtfxAsset from ox_lib functions.LoadParticleDictionary = lib.requestNamedPtfxAsset ----@deprecated use StartParticleAtCoord from imports/utils.lua -functions.StartParticleAtCoord = StartParticleAtCoord +---@deprecated use ParticleFx natives directly +---@param dict string +---@param ptName string +---@param looped boolean +---@param coords? vector3 defaults to player position +---@param rot vector3 +---@param scale? number defaults to 1.0 +---@param alpha? number defaults to 1.0 +---@param color? {r: number, g: number, b: number} +---@param duration? integer ms +---@return integer +functions.StartParticleAtCoord = function(dict, ptName, looped, coords, rot, scale, alpha, color, duration) + coords = type(coords) == 'table' and vec3(coords.x, coords.y, coords.z) or coords or GetEntityCoords(cache.ped) + + lib.requestNamedPtfxAsset(dict) + UseParticleFxAssetNextCall(dict) + SetPtfxAssetNextCall(dict) + local particleHandle + if looped then + particleHandle = StartParticleFxLoopedAtCoord(ptName, coords.x, coords.y, coords.z, rot.x, rot.y, rot.z, scale or 1.0, false, false, false, false) + if color then + SetParticleFxLoopedColour(particleHandle, color.r, color.g, color.b, false) + end + SetParticleFxLoopedAlpha(particleHandle, alpha or 10.0) + if duration then + Wait(duration) + StopParticleFxLooped(particleHandle, false) + end + else + SetParticleFxNonLoopedAlpha(alpha or 1.0) + if color then + SetParticleFxNonLoopedColour(color.r, color.g, color.b) + end + StartParticleFxNonLoopedAtCoord(ptName, coords.x, coords.y, coords.z, rot.x, rot.y, rot.z, scale or 1.0, false, false, false) + end + return particleHandle +end ----@deprecated use StartParticleOnEntity from imports/utils.lua -functions.StartParticleOnEntity = StartParticleOnEntity +---@deprecated use ParticleFx natives directly +---@param dict string +---@param ptName string +---@param looped boolean +---@param entity integer +---@param bone? string | number +---@param offset vector3 +---@param rot vector3 +---@param scale? number defaults to 1.0 +---@param alpha? number defaults to 1.0 +---@param color? {r: number, b: number, g: number} +---@param evolution? {name: string, amount: number} +---@param duration? integer ms +---@return number? +functions.StartParticleOnEntity = function(dict, ptName, looped, entity, bone, offset, rot, scale, alpha, color, evolution, duration) + lib.requestNamedPtfxAsset(dict) + UseParticleFxAssetNextCall(dict) + local particleHandle = nil + ---@cast bone number + local pedBoneIndex = bone and GetPedBoneIndex(entity, bone) or 0 + ---@cast bone string + local nameBoneIndex = bone and GetEntityBoneIndexByName(entity, bone) or 0 + local entityType = GetEntityType(entity) + local boneID = entityType == 1 and (pedBoneIndex ~= 0 and pedBoneIndex) or (looped and nameBoneIndex ~= 0 and nameBoneIndex) + if looped then + if boneID then + particleHandle = StartParticleFxLoopedOnEntityBone(ptName, entity, offset.x, offset.y, offset.z, rot.x, rot.y, rot.z, boneID, scale or 1.0, false, false, false) + else + particleHandle = StartParticleFxLoopedOnEntity(ptName, entity, offset.x, offset.y, offset.z, rot.x, rot.y, rot.z, scale or 1.0, false, false, false) + end + if evolution then + SetParticleFxLoopedEvolution(particleHandle, evolution.name, evolution.amount, false) + end + if color then + SetParticleFxLoopedColour(particleHandle, color.r, color.g, color.b, false) + end + SetParticleFxLoopedAlpha(particleHandle, alpha or 1.0) + if duration then + Wait(duration) + StopParticleFxLooped(particleHandle, false) + end + else + SetParticleFxNonLoopedAlpha(alpha or 1.0) + if color then + SetParticleFxNonLoopedColour(color.r, color.g, color.b) + end + if boneID then + StartParticleFxNonLoopedOnPedBone(ptName, entity, offset.x, offset.y, offset.z, rot.x, rot.y, rot.z, boneID, scale or 1.0, false, false, false) + else + StartParticleFxNonLoopedOnEntity(ptName, entity, offset.x, offset.y, offset.z, rot.x, rot.y, rot.z, scale or 1.0, false, false, false) + end + end + return particleHandle +end ----@deprecated use GetStreetNameAtCoords from imports/utils.lua -functions.GetStreetNametAtCoords = GetStreetNameAtCoords +---@deprecated use qbx.getStreetName from modules/lib.lua +---@param coords vector3 +---@return { main: string, cross: string } +functions.GetStreetNametAtCoords = function(coords) + return qbx.getStreetName(coords) +end ----@deprecated use GetZoneAtCoords from imports/utils.lua -functions.GetZoneAtCoords = GetZoneAtCoords +---@deprecated use qbx.getZoneName from modules/lib.lua +---@param coords vector3 +---@return string +functions.GetZoneAtCoords = function(coords) + return qbx.getZoneName(coords) +end ----@deprecated use GetCardinalDirection from imports/utils.lua -functions.GetCardinalDirection = GetCardinalDirection +---@deprecated use qbx.getCardinalDirection from modules/lib.lua +functions.GetCardinalDirection = function(entity) + entity = entity or cache.ped + if not entity or not DoesEntityExist(entity) then + return 'Entity does not exist' + end + + return qbx.getCardinalDirection(entity) +end + +---@class BridgeCurrentTime +---@field formattedMin string +---@field formattedHour integer +---@field ampm 'AM' | 'PM' +---@field min number +---@field hour number ---@deprecated use GetCurrentTime from imports/utils.lua -functions.GetCurrentTime = GetCurrentTime +---@return BridgeCurrentTime +functions.GetCurrentTime = function() + local obj = {} + obj.min = GetClockMinutes() + obj.hour = GetClockHours() + + if obj.hour <= 12 then + obj.ampm = 'AM' + elseif obj.hour >= 13 then + obj.ampm = 'PM' + obj.formattedHour = obj.hour - 12 + end ----@deprecated use GetGroundZCoord from imports/utils.lua -functions.GetGroundZCoord = GetGroundZCoord + if obj.min <= 9 then + obj.formattedMin = ('0%s'):format(obj.min) + end + + return obj +end + +---@deprecated use the GetGroundZFor_3dCoord native directly +---@param coords vector3 +---@return vector3? +functions.GetGroundZCoord = function(coords) + if not coords then return end + + local retval, groundZ = GetGroundZFor_3dCoord(coords.x, coords.y, coords.z, false) + if retval then + return vec3(coords.x, coords.y, groundZ) + end + + lib.print.verbose('Couldn\'t find Ground Z Coordinates given 3D Coordinates:', coords) + return coords +end ---Text box popup for player which dissappears after a set time. ---@param text table|string text of the notification diff --git a/bridge/qb/server/functions.lua b/bridge/qb/server/functions.lua index b29128cc6..227a56e50 100644 --- a/bridge/qb/server/functions.lua +++ b/bridge/qb/server/functions.lua @@ -4,8 +4,13 @@ local functions = {} local createQbExport = require 'bridge.qb.shared.export-function' ----@deprecated -functions.GetCoords = GetCoordsFromEntity +---@deprecated use the GetEntityCoords and GetEntityHeading natives directly +---@param entity number +---@return vector4 +functions.GetCoords = function(entity) + local coords = GetEntityCoords(entity) + return vec4(coords.x, coords.y, coords.z, GetEntityHeading(entity)) +end ---@deprecated use the native GetPlayerIdentifierByType? functions.GetIdentifier = GetPlayerIdentifierByType @@ -21,7 +26,7 @@ function functions.GetPlayers() return sources end ----@deprecated use SpawnVehicle from imports/utils.lua +---@deprecated use qbx.spawnVehicle from modules/lib.lua ---@return number? function functions.SpawnVehicle(source, model, coords, warp) local ped = GetPlayerPed(source) @@ -40,9 +45,16 @@ function functions.SpawnVehicle(source, model, coords, warp) return veh end ----@deprecated use SpawnVehicle from imports/utils.lua +---@deprecated use qbx.spawnVehicle from modules/lib.lua function functions.CreateVehicle(source, model, _, coords, warp) - local netId = SpawnVehicle(source, model, coords, warp) + model = type(model) == 'string' and joaat(model) or (model --[[@as integer]]) + local ped = GetPlayerPed(source) + + local netId = qbx.spawnVehicle({ + model = model, + spawnSource = coords or ped, + warp = warp and ped or nil, + }) return NetworkGetEntityFromNetworkId(netId) end @@ -54,19 +66,87 @@ function functions.UseItem(source, item) exports['qb-inventory']:UseItem(source, item) end ----@deprecated use KickWithReason from imports/utils.lua -functions.Kick = KickWithReason +local discordLink = GetConvar('qbx:discordlink', 'discord.gg/qbox') +---@deprecated use setKickReason or deferrals for connecting players, and the DropPlayer native directly otherwise +---@param source Source +---@param reason string +---@param setKickReason? fun(reason: string) +---@param deferrals? Deferrals +functions.Kick = function(source, reason, setKickReason, deferrals) + reason = '\n' .. reason .. '\n🔸 Check our Discord for further information: ' .. discordLink + if setKickReason then + setKickReason(reason) + end + CreateThread(function() + if deferrals then + deferrals.update(reason) + Wait(2500) + end + if source then + DropPlayer(source --[[@as string]], reason) + end + for _ = 0, 4 do + while true do + if source then + if GetPlayerPing(source --[[@as string]]) >= 0 then + break + end + Wait(100) + CreateThread(function() + DropPlayer(source --[[@as string]], reason) + end) + end + end + Wait(5000) + end + end) +end + +---@deprecated check for license usage directly yourself +---@param license string +---@return boolean +functions.IsLicenseInUse = function(license) + local players = GetPlayers() + + for _, player in pairs(players) do + local plyLicense2 = GetPlayerIdentifierByType(player --[[@as string]], 'license2') + local plyLicense = GetPlayerIdentifierByType(player --[[@as string]], 'license') + if plyLicense2 == license or plyLicense == license then + return true + end + end ----@deprecated use IsLicenseInUse from imports/utils.lua -functions.IsLicenseInUse = IsLicenseInUse + return false +end -- Utility functions ---@deprecated use https://overextended.dev/ox_inventory/Functions/Server#search -functions.HasItem = HasItem +---@param source Source +---@param items string | string[] +---@param amount? integer +---@return boolean +functions.HasItem = function(source, items, amount) + amount = amount or 1 + local count = exports.ox_inventory:Search(source, 'count', items) + if type(items) == 'table' and type(count) == 'table' then + for _, v in pairs(count) do + if v < amount then + return false + end + end + return true + end + return count >= amount +end ----@deprecated use GetPlate from imports/utils.lua -functions.GetPlate = GetPlate +---@deprecated use qbx.getVehiclePlate from modules/lib.lua +---@param vehicle integer +---@return string? +functions.GetPlate = function(vehicle) + if not vehicle or vehicle == 0 then return end + return qbx.getVehiclePlate(vehicle) +end -- Single add item ---@deprecated incompatible with ox_inventory. Update ox_inventory item config instead. diff --git a/bridge/qb/server/main.lua b/bridge/qb/server/main.lua index 90ca117d6..98cd5645c 100644 --- a/bridge/qb/server/main.lua +++ b/bridge/qb/server/main.lua @@ -87,13 +87,23 @@ end ---@deprecated call server function SpawnVehicle instead from imports/utils.lua. qbCoreCompat.Functions.CreateCallback('QBCore:Server:SpawnVehicle', function(source, cb, model, coords, warp) - local netId = SpawnVehicle(source, model, coords, warp) + local ped = GetPlayerPed(source) + local netId = qbx.spawnVehicle({ + model = model, + spawnSource = coords or ped, + warp = warp and ped or nil, + }) if netId then cb(netId) end end) ---@deprecated call server function SpawnVehicle instead from imports/utils.lua. qbCoreCompat.Functions.CreateCallback('QBCore:Server:CreateVehicle', function(source, cb, model, coords, warp) - local netId = SpawnVehicle(source, model, coords, warp) + local ped = GetPlayerPed(source) + local netId = qbx.spawnVehicle({ + model = model, + spawnSource = coords or ped, + warp = warp and ped or nil, + }) if netId then cb(netId) end end) diff --git a/bridge/qb/shared/main.lua b/bridge/qb/shared/main.lua index 108b3d19e..033576fdc 100644 --- a/bridge/qb/shared/main.lua +++ b/bridge/qb/shared/main.lua @@ -1,36 +1,82 @@ local qbShared = require 'shared.main' ----@deprecated use CommaValue from imports/utils.lua -qbShared.CommaValue = CommaValue +---@deprecated use lib.math.groupdigits from ox_lib +---@param num number +---@return string +qbShared.CommaValue = function(num) + return lib.math.groupdigits(num) +end ----@deprecated use RandomLetter from imports/utils.lua -qbShared.RandomStr = RandomLetter +---@deprecated use lib.string.random from ox_lib +---@param length integer +---@return string +qbShared.RandomStr = function(length) + if length <= 0 then return '' end + local pattern = math.random(2) == 1 and 'a' or 'A' + return qbShared.RandomStr(length - 1) .. lib.string.random(pattern) +end ----@deprecated use RandomNumber from imports/utils.lua -qbShared.RandomInt = RandomNumber +---@deprecated use lib.string.random from ox_lib +---@param length integer +---@return string +qbShared.RandomInt = function(length) + if length <= 0 then return '' end + return qbShared.RandomInt(length - 1) .. lib.string.random('1') +end ----@deprecated use string.split from imports/utils.lua -qbShared.SplitStr = string.split +---@deprecated use string.strsplit with CfxLua 5.4 +---@param str string +---@param delimiter string character +---@return string[] +qbShared.SplitStr = function(str, delimiter) + return table.pack(string.strsplit(delimiter, str)) +end ----@deprecated use string.trim from imports/utils.lua -qbShared.Trim = string.trim +---@deprecated use qbx.string.trim from modules/lib.lua +---@param str string +---@return string? +---@return number? count +qbShared.Trim = function(str) + if not str then return end + return string.gsub(str, '^%s*(.-)%s*$', '%1') +end ----@deprecated use string.firstToUpper from imports/utils.lua -qbShared.FirstToUpper = string.firstToUpper +---@deprecated use qbx.string.capitalize from modules/lib.lua +---@param str string +---@return string? +---@return number? count +qbShared.FirstToUpper = function(str) + if not str or str == '' then return end + return str:gsub("^%l", string.upper) +end ----@deprecated use math.round from imports/utils.lua -qbShared.Round = math.round +---@deprecated use qbx.math.round from modules/lib.lua +---@param value number +---@param numDecimalPlaces? integer +---@return number +qbShared.Round = function(value, numDecimalPlaces) + return qbx.math.round(value, numDecimalPlaces) +end ----@deprecated use ChangeVehicleExtra from imports/utils.lua -qbShared.ChangeVehicleExtra = ChangeVehicleExtra +---@deprecated use qbx.setVehicleExtra from modules/lib.lua +---@param vehicle integer +---@param extra integer +---@param enable boolean +qbShared.ChangeVehicleExtra = function(vehicle, extra, enable) + qbx.setVehicleExtra(vehicle, extra, enable) +end ----@deprecated use SetVehicleExtras from imports/utils.lua -qbShared.SetDefaultVehicleExtras = SetVehicleExtras +---@deprecated use qbx.setVehicleExtras from modules/lib.lua +---@param vehicle integer +---@param extras table +qbShared.SetDefaultVehicleExtras = function(vehicle, extras) + qbx.setVehicleExtras(vehicle, extras) +end ----@deprecated use MaleNoGloves from imports/utils.lua -qbShared.MaleNoGloves = MaleNoGloves +---@deprecated use qbx.armsWithoutGloves.male from modules/lib.lua +qbShared.MaleNoGloves = table.clone(qbx.armsWithoutGloves.male) ----@deprecated use FemaleNoGloves from imports/utils.lua -qbShared.FemaleNoGloves = FemaleNoGloves +---@deprecated use qbx.armsWithoutGloves.female from modules/lib.lua +qbShared.FemaleNoGloves = table.clone(qbx.armsWithoutGloves.female) return qbShared \ No newline at end of file diff --git a/client/character.lua b/client/character.lua index 9c620b4cb..ffb3618c5 100644 --- a/client/character.lua +++ b/client/character.lua @@ -287,8 +287,8 @@ local function chooseCharacter() Birthdate = character.charinfo.birthdate, Nationality = character.charinfo.nationality, ['Account Number'] = character.charinfo.account, - Bank = CommaValue(character.money.bank), - Cash = CommaValue(character.money.cash), + Bank = lib.math.groupdigits(character.money.bank), + Cash = lib.math.groupdigits(character.money.cash), Job = character.job.label, ['Job Grade'] = character.job.grade.name, Gang = character.gang.label, diff --git a/client/events.lua b/client/events.lua index 363d208fe..c6c3f3e74 100644 --- a/client/events.lua +++ b/client/events.lua @@ -182,7 +182,10 @@ AddStateBagChangeHandler('me', nil, function(bagName, _, value) local displayTime = 5000 + GetGameTimer() while displayTime > GetGameTimer() do playerPed = isLocalPlayer and cache.ped or GetPlayerPed(playerId) - DrawText3D(value, GetEntityCoords(playerPed)) + qbx.drawText3d({ + text = value, + coords = GetEntityCoords(playerPed), + }) Wait(0) end end) diff --git a/client/main.lua b/client/main.lua index e1ed94253..36450b846 100644 --- a/client/main.lua +++ b/client/main.lua @@ -33,7 +33,7 @@ exports('GetVehiclesByHash', GetVehiclesByHash) ---@return table function GetVehiclesByCategory() - return MapTableBySubfield('category', QBX.Shared.Vehicles) + return qbx.table.mapBySubfield(QBX.Shared.Vehicles, 'category') end exports('GetVehiclesByCategory', GetVehiclesByCategory) diff --git a/config/server.lua b/config/server.lua index a33e93e5d..10cf37547 100644 --- a/config/server.lua +++ b/config/server.lua @@ -25,7 +25,7 @@ return { identifierTypes = { citizenid = { valueFunction = function() - return tostring(RandomLetter(3) .. RandomNumber(5)):upper() + return lib.string.random('AAA11111') end, }, AccountNumber = { @@ -40,7 +40,7 @@ return { }, FingerId = { valueFunction = function() - return tostring(RandomLetter(2) .. RandomNumber(3) .. RandomLetter(1) .. RandomNumber(2) .. RandomLetter(3) .. RandomNumber(4)) + return lib.string.random('aa111A11aaa1111') end, }, WalletId = { diff --git a/fxmanifest.lua b/fxmanifest.lua index f6e15feae..5b550ef08 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -7,14 +7,13 @@ version '1.4.0' shared_scripts { '@ox_lib/init.lua', - 'modules/utils.lua', + 'modules/lib.lua', 'shared/locale.lua', 'locale/en.lua', 'locale/*.lua', } client_scripts { - 'modules/utils.lua', 'client/main.lua', 'client/functions.lua', 'client/loops.lua', diff --git a/server/commands.lua b/server/commands.lua index 39f32e955..a312a2040 100644 --- a/server/commands.lua +++ b/server/commands.lua @@ -102,7 +102,7 @@ lib.addCommand('openserver', { config.server.closed = false Notify(source, Lang:t('success.server_opened'), 'success') else - KickWithReason(source, Lang:t("error.no_permission"), nil, nil) + DropPlayer(source, Lang:t("error.no_permission")) end end) @@ -123,12 +123,12 @@ lib.addCommand('closeserver', { config.server.closedReason = reason for k in pairs(QBX.Players) do if not HasPermission(k, config.server.whitelistPermission) then - KickWithReason(k, reason, nil, nil) + DropPlayer(k --[[@as string]], reason) end end Notify(source, Lang:t('success.server_closed'), 'success') else - KickWithReason(source, Lang:t("error.no_permission"), nil, nil) + DropPlayer(source, Lang:t("error.no_permission")) end end) @@ -142,8 +142,12 @@ lib.addCommand('car', { restricted = "group.admin" }, function(source, args) if not args then return end - local netId = SpawnVehicle(source, args[Lang:t("command.car.params.model.name")], nil, true) - local plate = GetPlate(NetworkGetEntityFromNetworkId(netId)) + local netId = qbx.spawnVehicle({ + model = joaat(args[Lang:t('command.car.params.model.name')]), + spawnSource = GetPlayerPed(source), + warp = true, + }) + local plate = qbx.getVehiclePlate(NetworkGetEntityFromNetworkId(netId)) config.giveVehicleKeys(source, plate) end) diff --git a/server/events.lua b/server/events.lua index bee9c8ed3..35be8a05c 100644 --- a/server/events.lua +++ b/server/events.lua @@ -77,7 +77,7 @@ local function onPlayerConnecting(name, _, deferrals) if not license then deferrals.done(Lang:t('error.no_valid_license')) - elseif serverConfig.checkDuplicateLicense and IsLicenseInUse(license) then + elseif serverConfig.checkDuplicateLicense and usedLicenses[license] then deferrals.done(Lang:t('error.duplicate_license')) end @@ -166,11 +166,11 @@ RegisterNetEvent('QBCore:Server:CloseServer', function(reason) serverConfig.closedReason = reason for k in pairs(QBX.Players) do if not HasPermission(k, serverConfig.whitelistPermission) then - KickWithReason(k, reason, nil, nil) + DropPlayer(k --[[@as string]], reason) end end else - KickWithReason(src, Lang:t("error.no_permission"), nil, nil) + DropPlayer(src --[[@as string]], Lang:t("error.no_permission")) end end) @@ -179,7 +179,7 @@ RegisterNetEvent('QBCore:Server:OpenServer', function() if HasPermission(src, 'admin') then serverConfig.closed = false else - KickWithReason(src, Lang:t("error.no_permission"), nil, nil) + DropPlayer(src --[[@as string]], Lang:t("error.no_permission")) end end) diff --git a/server/main.lua b/server/main.lua index d010459cb..7817c91a7 100644 --- a/server/main.lua +++ b/server/main.lua @@ -118,7 +118,7 @@ exports('GetVehiclesByHash', GetVehiclesByHash) ---@return table function GetVehiclesByCategory() - return MapTableBySubfield('category', QBX.Shared.Vehicles) + return qbx.table.mapBySubfield(QBX.Shared.Vehicles, 'category') end exports('GetVehiclesByCategory', GetVehiclesByCategory)