Skip to content

Commit

Permalink
feat: qbx_core support (#1763)
Browse files Browse the repository at this point in the history
  • Loading branch information
solareon authored Aug 14, 2024
1 parent 34a3e9b commit 937f775
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
30 changes: 30 additions & 0 deletions modules/bridge/qbx/client.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
AddStateBagChangeHandler('isLoggedIn', ('player:%s'):format(cache.serverId), function(_, _, value)
if not value then client.onLogout() end
end)

RegisterNetEvent('qbx_core:client:onGroupUpdate', function(groupName, groupGrade)
local groups = PlayerData.groups
if not groupGrade then
groups[groupName] = nil
else
groups[groupName] = groupGrade
end
client.setPlayerData('groups', groups)
end)

RegisterNetEvent('qbx_core:client:setGroups', function(groups)
client.setPlayerData('groups', groups)
end)

---@diagnostic disable-next-line: duplicate-set-field
function client.setPlayerStatus(values)
local playerState = LocalPlayer.state
for name, value in pairs(values) do
-- compatibility for ESX style values
if value > 100 or value < -100 then
value = value * 0.0001
end

playerState:set(name, playerState[name] + value, true)
end
end
100 changes: 100 additions & 0 deletions modules/bridge/qbx/server.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
assert(lib.checkDependency('qbx_core', '1.18.0'), 'qbx_core v1.18.0 or higher is required')
assert(lib.checkDependency('qbx_vehicles', '1.2.0'), 'qbx_vehicles v1.2.0 or higher is required')
local Inventory = require 'modules.inventory.server'
local QBX = exports.qbx_core

AddEventHandler('qbx_core:server:playerLoggedOut', server.playerDropped)

AddEventHandler('qbx_core:server:onGroupUpdate', function(source, groupName, groupGrade)
local inventory = Inventory(source)
if not inventory then return end
inventory.player.groups[groupName] = not groupGrade and nil or groupGrade
end)

local function setupPlayer(playerData)
playerData.identifier = playerData.citizenid
playerData.name = ('%s %s'):format(playerData.charinfo.firstname, playerData.charinfo.lastname)
server.setPlayerInventory(playerData)

Inventory.SetItem(playerData.source, 'money', playerData.money.cash)
end

AddStateBagChangeHandler('loadInventory', nil, function(bagName, _, value)
if not value then return end
local plySrc = GetPlayerFromStateBagName(bagName)
if not plySrc then return end
setupPlayer(QBX:GetPlayer(plySrc).PlayerData)
end)

SetTimeout(500, function()
local playersData = QBX:GetPlayersData()
for i = 1, #playersData do setupPlayer(playersData[i]) end
end)

function server.UseItem(source, itemName, data)
local cb = QBX:CanUseItem(itemName)
return cb and cb(source, data)
end

---@diagnostic disable-next-line: duplicate-set-field
function server.setPlayerData(player)
local groups = QBX:GetGroups(player.source)
return {
source = player.source,
name = ('%s %s'):format(player.charinfo.firstname, player.charinfo.lastname),
groups = groups,
sex = player.charinfo.gender,
dateofbirth = player.charinfo.birthdate,
}
end

---@diagnostic disable-next-line: duplicate-set-field
function server.syncInventory(inv)
local accounts = Inventory.GetAccountItemCounts(inv)

if not accounts then return end
local player = QBX:GetPlayer(inv.id)

for account, amount in pairs(accounts) do
account = account == 'money' and 'cash' or account
if player.Functions.GetMoney(account) ~= amount then
player.Functions.SetMoney(account, amount, ('Sync %s with inventory'):format(account))
end
end
end

---@diagnostic disable-next-line: duplicate-set-field
function server.hasLicense(inv, license)
local player = QBX:GetPlayer(inv.id)
return player and player.PlayerData.metadata.licences[license]
end

---@diagnostic disable-next-line: duplicate-set-field
function server.buyLicense(inv, license)
local player = QBX:GetPlayer(inv.id)
if not player then return end

if player.PlayerData.metadata.licences[license.name] then
return false, 'already_have'
elseif Inventory.GetItem(inv, 'money', false, true) < license.price then
return false, 'can_not_afford'
end

Inventory.RemoveItem(inv, 'money', license.price)
player.PlayerData.metadata.licences[license.name] = true
player.Functions.SetMetaData('licences', player.PlayerData.metadata.licences)

return true, 'have_purchased'
end

---@diagnostic disable-next-line: duplicate-set-field
function server.isPlayerBoss(playerId, group, grade)
return QBX:IsGradeBoss(group, grade)
end

---@param entityId number
---@return number | string
---@diagnostic disable-next-line: duplicate-set-field
function server.getOwnedVehicleId(entityId)
return Entity(entityId).vehicleid or exports.qbx_vehicles:GetVehicleIdByPlate(GetVehicleNumberPlateText(entityId)) or GetVehicleNumberPlateText(entityId)
end
5 changes: 5 additions & 0 deletions modules/mysql/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Citizen.CreateThreadNow(function()
playerColumn = 'charid'
vehicleTable = 'nd_vehicles'
vehicleColumn = 'id'
elseif shared.framework == 'qbx' then
playerTable = 'players'
playerColumn = 'citizenid'
vehicleTable = 'player_vehicles'
vehicleColumn = 'id'
end

for k, v in pairs(Query) do
Expand Down

0 comments on commit 937f775

Please sign in to comment.