Skip to content

Commit

Permalink
feat: switch from utils to lib (#318)
Browse files Browse the repository at this point in the history
* feat: switch from utils to lib

* fix: armsWithoutGloves issue?

* fix: move armsWithoutGloves to shared

* fix: spawnVehicle usage

* fix(config/server): lib.string.random

* Update server/commands.lua

Co-authored-by: David Malchin <malchin459@gmail.com>

* Update config/server.lua

Co-authored-by: David Malchin <malchin459@gmail.com>

* Update config/server.lua

Co-authored-by: David Malchin <malchin459@gmail.com>

* Update bridge/qb/client/functions.lua

Co-authored-by: David Malchin <malchin459@gmail.com>

* Update bridge/qb/server/main.lua

Co-authored-by: David Malchin <malchin459@gmail.com>

* Update bridge/qb/server/main.lua

Co-authored-by: David Malchin <malchin459@gmail.com>

* fix: manifest and GetPeds

* fix: GetPlate and GetVehicleDisplayName

* fix: Trim, FirstToUpper, and Cardinal Direction

* fix: add string.split from utils to bridge

* fix: GetPlayers

* fix: SpawnClear

* fix: GetClosestPlayer, GetClosestVehicle, and GetClosestObject

* fix: GetPlayerFromCoords

* feat(lint): new lib

* fix(bridge): qb closestped compat

* tweak(bridge): simplify SpawnClear

* tweak(bridge): don't redefine heading

* tweak: wrap cardinal direction instead

* fix(server/events): use table instead of old func

* perf(server/commands): reuse variable instead

* fix: lint

---------

Co-authored-by: David Malchin <malchin459@gmail.com>
Co-authored-by: BerkieBb <82737367+BerkieBb@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 22, 2024
1 parent 386e7a3 commit c5d6575
Show file tree
Hide file tree
Showing 16 changed files with 523 additions and 214 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"Lua.runtime.nonstandardSymbol": ["/**/", "`", "+=", "-=", "*=", "/="],
"Lua.runtime.version": "Lua 5.4",
"Lua.diagnostics.globals": [
"lib"
"lib",
"MySQL",
"cache"
]
}
2 changes: 1 addition & 1 deletion bridge/qb/client/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
337 changes: 277 additions & 60 deletions bridge/qb/client/functions.lua

Large diffs are not rendered by default.

88 changes: 76 additions & 12 deletions bridge/qb/server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ local functions = {}

local createQbExport = require 'bridge.qb.shared.export-function'

---@deprecated
functions.GetCoords = GetCoordsFromEntity
---@deprecated use the GetEntityCoords and GetEntityHeading natives directly
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
Expand All @@ -21,7 +24,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)
Expand All @@ -40,9 +43,17 @@ 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

Expand All @@ -54,19 +65,72 @@ 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
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
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
functions.HasItem = function(source, items, amount) -- luacheck: ignore
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
functions.GetPlate = qbx.getVehiclePlate

-- Single add item
---@deprecated incompatible with ox_inventory. Update ox_inventory item config instead.
Expand Down
10 changes: 6 additions & 4 deletions bridge/qb/server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,17 @@ function qbCoreCompat.Functions.TriggerCallback(name, source, cb, ...)
qbCoreCompat.ServerCallbacks[name](source, cb, ...)
end

---@deprecated call server function SpawnVehicle instead from imports/utils.lua.
---@deprecated call server function qbx.spawnVehicle from modules/lib.lua
qbCoreCompat.Functions.CreateCallback('QBCore:Server:SpawnVehicle', function(source, cb, model, coords, warp)
local netId = SpawnVehicle(source, model, coords, warp)
local netId = qbCoreCompat.Functions.SpawnVehicle(source, model, coords, warp)

if netId then cb(netId) end
end)

---@deprecated call server function SpawnVehicle instead from imports/utils.lua.
---@deprecated call server function qbx.spawnVehicle from modules/lib.lua
qbCoreCompat.Functions.CreateCallback('QBCore:Server:CreateVehicle', function(source, cb, model, coords, warp)
local netId = SpawnVehicle(source, model, coords, warp)
local netId = qbCoreCompat.Functions.CreateVehicle(source, model, nil, coords, warp)

if netId then cb(netId) end
end)

Expand Down
81 changes: 49 additions & 32 deletions bridge/qb/shared/main.lua
Original file line number Diff line number Diff line change
@@ -1,36 +1,53 @@
local qbShared = require 'shared.main'

---@deprecated use CommaValue from imports/utils.lua
qbShared.CommaValue = CommaValue

---@deprecated use RandomLetter from imports/utils.lua
qbShared.RandomStr = RandomLetter

---@deprecated use RandomNumber from imports/utils.lua
qbShared.RandomInt = RandomNumber

---@deprecated use string.split from imports/utils.lua
qbShared.SplitStr = string.split

---@deprecated use string.trim from imports/utils.lua
qbShared.Trim = string.trim

---@deprecated use string.firstToUpper from imports/utils.lua
qbShared.FirstToUpper = string.firstToUpper

---@deprecated use math.round from imports/utils.lua
qbShared.Round = math.round

---@deprecated use ChangeVehicleExtra from imports/utils.lua
qbShared.ChangeVehicleExtra = ChangeVehicleExtra

---@deprecated use SetVehicleExtras from imports/utils.lua
qbShared.SetDefaultVehicleExtras = SetVehicleExtras

---@deprecated use MaleNoGloves from imports/utils.lua
qbShared.MaleNoGloves = MaleNoGloves

---@deprecated use FemaleNoGloves from imports/utils.lua
qbShared.FemaleNoGloves = FemaleNoGloves
---@deprecated use lib.math.groupdigits from ox_lib
qbShared.CommaValue = lib.math.groupdigits

---@deprecated use lib.string.random from ox_lib
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 lib.string.random from ox_lib
qbShared.RandomInt = function(length)
if length <= 0 then return '' end
return qbShared.RandomInt(length - 1) .. lib.string.random('1')
end

---@deprecated use string.strsplit with CfxLua 5.4
qbShared.SplitStr = function(str, delimiter)
local result = table.pack(string.strsplit(delimiter, str))
result.n = nil
return result
end

---@deprecated use qbx.string.trim from modules/lib.lua
qbShared.Trim = function(str)
if not str then return nil end
return qbx.string.trim(str)
end

---@deprecated use qbx.string.capitalize from modules/lib.lua
qbShared.FirstToUpper = function(str)
if not str then return nil end
return qbx.string.capitalize(str)
end

---@deprecated use qbx.math.round from modules/lib.lua
qbShared.Round = qbx.math.round

---@deprecated use qbx.setVehicleExtra from modules/lib.lua
qbShared.ChangeVehicleExtra = qbx.setVehicleExtras

---@deprecated use qbx.setVehicleExtra from modules/lib.lua
qbShared.SetDefaultVehicleExtras = qbx.setVehicleExtras

---@deprecated use qbx.armsWithoutGloves.male from modules/lib.lua
qbShared.MaleNoGloves = qbx.armsWithoutGloves.male

---@deprecated use qbx.armsWithoutGloves.female from modules/lib.lua
qbShared.FemaleNoGloves = qbx.armsWithoutGloves.female

return qbShared
4 changes: 2 additions & 2 deletions client/character.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion client/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ 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)
Expand Down
2 changes: 1 addition & 1 deletion client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exports('GetVehiclesByHash', GetVehiclesByHash)

---@return table<string, Vehicle[]>
function GetVehiclesByCategory()
return MapTableBySubfield('category', QBX.Shared.Vehicles)
return qbx.table.mapBySubfield(QBX.Shared.Vehicles, 'category')
end

exports('GetVehiclesByCategory', GetVehiclesByCategory)
Expand Down
4 changes: 2 additions & 2 deletions config/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ return {
identifierTypes = {
citizenid = {
valueFunction = function()
return tostring(RandomLetter(3) .. RandomNumber(5)):upper()
return lib.string.random('........')
end,
},
AccountNumber = {
Expand All @@ -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('...............')
end,
},
WalletId = {
Expand Down
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version '1.4.0'

shared_scripts {
'@ox_lib/init.lua',
'modules/utils.lua',
'modules/lib.lua',
'shared/locale.lua',
}

Expand Down
Loading

0 comments on commit c5d6575

Please sign in to comment.