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

feat: switch from utils to lib #318

Merged
merged 29 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2c455cd
feat: switch from utils to lib
mafewtm Jan 15, 2024
7c95c7a
fix: armsWithoutGloves issue?
mafewtm Jan 16, 2024
3e86d1d
fix: move armsWithoutGloves to shared
mafewtm Jan 16, 2024
22d86da
fix: spawnVehicle usage
mafewtm Jan 16, 2024
e78f6a3
fix(config/server): lib.string.random
mafewtm Jan 17, 2024
254c026
Update server/commands.lua
mafewtm Jan 17, 2024
7bffe7b
Update config/server.lua
mafewtm Jan 17, 2024
bdfd7cc
Update config/server.lua
mafewtm Jan 17, 2024
d5a950d
Update bridge/qb/client/functions.lua
mafewtm Jan 17, 2024
b514110
Update bridge/qb/server/main.lua
mafewtm Jan 17, 2024
13312a0
Update bridge/qb/server/main.lua
mafewtm Jan 17, 2024
ab2a6b7
fix: manifest and GetPeds
mafewtm Jan 17, 2024
0e86896
fix: GetPlate and GetVehicleDisplayName
mafewtm Jan 17, 2024
390b59b
fix: Trim, FirstToUpper, and Cardinal Direction
mafewtm Jan 18, 2024
24682c6
fix: add string.split from utils to bridge
mafewtm Jan 19, 2024
cf6e71d
fix: GetPlayers
mafewtm Jan 19, 2024
2ca97b7
fix: SpawnClear
mafewtm Jan 19, 2024
1f37eba
fix: GetClosestPlayer, GetClosestVehicle, and GetClosestObject
mafewtm Jan 19, 2024
4bbabee
fix: GetPlayerFromCoords
mafewtm Jan 19, 2024
18d8efa
feat(lint): new lib
BerkieBb Jan 20, 2024
2b03040
fix(bridge): qb closestped compat
BerkieBb Jan 20, 2024
fbf6c64
tweak(bridge): simplify SpawnClear
BerkieBb Jan 20, 2024
198e964
tweak(bridge): don't redefine heading
BerkieBb Jan 20, 2024
dab9752
tweak: wrap cardinal direction instead
BerkieBb Jan 20, 2024
089a2af
Merge branch 'main' into main
BerkieBb Jan 21, 2024
5dc062c
fix(server/events): use table instead of old func
BerkieBb Jan 22, 2024
f24df92
Merge branch 'main' into main
BerkieBb Jan 22, 2024
2a5caf8
perf(server/commands): reuse variable instead
BerkieBb Jan 22, 2024
bfb94df
fix: lint
BerkieBb Jan 22, 2024
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
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
Loading