Skip to content

Commit

Permalink
Merge branch 'Qbox-project:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
CodexisPhantom authored Nov 29, 2024
2 parents 50b3523 + 8e06398 commit 52605fd
Show file tree
Hide file tree
Showing 7 changed files with 1,039 additions and 137 deletions.
33 changes: 14 additions & 19 deletions client/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,29 +206,24 @@ RegisterNetEvent('QBCore:Client:OnSharedUpdateMultiple', function(tableName, val
end)

-- Set vehicle props
---@param vehicle number
---@param netId number
---@param props table<any, any>
qbx.entityStateHandler('setVehicleProperties', function(vehicle, _, props)
RegisterNetEvent('qbx_core:client:setVehicleProperties', function(netId, props)
if not props then return end

SetTimeout(0, function()
local state = Entity(vehicle).state

local timeOut = GetGameTimer() + 10000

while state.setVehicleProperties do
if NetworkGetEntityOwner(vehicle) == cache.playerId then
if lib.setVehicleProperties(vehicle, props) then
state:set('setVehicleProperties', nil, true)
end
end
if GetGameTimer() > timeOut then
break
local timeOut = GetGameTimer() + 1000
local vehicle = NetworkGetEntityFromNetworkId(netId)
while true do
if NetworkGetEntityOwner(vehicle) == cache.playerId then
if lib.setVehicleProperties(vehicle, props) then
return
end

Wait(50)
end
end)
if GetGameTimer() > timeOut then
return
end

Wait(50)
end
end)

-- Clear vehicle peds
Expand Down
92 changes: 57 additions & 35 deletions modules/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -271,51 +271,73 @@ if isServer then
local vehicleType = GetVehicleType(tempVehicle)
DeleteEntity(tempVehicle)

local veh = CreateVehicleServerSetter(model, vehicleType, coords.x, coords.y, coords.z, coords.w)
while not DoesEntityExist(veh) do Wait(0) end
while GetVehicleNumberPlateText(veh) == '' do Wait(0) end
local attempts = 0

if bucket and bucket > 0 then
exports.qbx_core:SetEntityBucket(veh, bucket)
end

if ped then
SetPedIntoVehicle(ped, veh, -1)
end
local veh, netId
while attempts < 3 do
veh = CreateVehicleServerSetter(model, vehicleType, coords.x, coords.y, coords.z, coords.w)
while not DoesEntityExist(veh) do Wait(0) end
while GetVehicleNumberPlateText(veh) == '' do Wait(0) end

if not pcall(function()
lib.waitFor(function()
local owner = NetworkGetEntityOwner(veh)
if ped then
--- the owner should be transferred to the driver
if owner == NetworkGetEntityOwner(ped) then return true end
else
if owner ~= -1 then return true end
end
end, 'client never set as owner', 5000)
end) then
DeleteEntity(veh)
error('Deleting vehicle which timed out finding an owner')
end
if bucket and bucket > 0 then
exports.qbx_core:SetEntityBucket(veh, bucket)
end

local state = Entity(veh).state
state:set('initVehicle', true, true)
if ped then
SetPedIntoVehicle(ped, veh, -1)
end

if props and type(props) == 'table' and props.plate then
state:set('setVehicleProperties', props, true)
if not pcall(function()
lib.waitFor(function()
if qbx.string.trim(GetVehicleNumberPlateText(veh)) == qbx.string.trim(props.plate) then
return true
local owner = NetworkGetEntityOwner(veh)
if ped then
--- the owner should be transferred to the driver
if owner == NetworkGetEntityOwner(ped) then return true end
else
if owner ~= -1 then return true end
end
end, 'Failed to set vehicle properties within 5 seconds', 5000)
end, 'client never set as owner', 5000)
end) then
DeleteEntity(veh)
error('Deleting vehicle which timed out setting vehicle properties')
error('Deleting vehicle which timed out finding an owner')
end

local state = Entity(veh).state
local owner = NetworkGetEntityOwner(veh)
state:set('initVehicle', true, true)
netId = NetworkGetNetworkIdFromEntity(veh)
if props and type(props) == 'table' and props.plate then
TriggerClientEvent('qbx_core:client:setVehicleProperties', owner, netId, props)
local success = pcall(function()
local plateMatched = false
lib.waitFor(function()
if qbx.string.trim(GetVehicleNumberPlateText(veh)) == qbx.string.trim(props.plate) then
local currentOwner = NetworkGetEntityOwner(veh)
assert(currentOwner == owner, ('Owner changed during vehicle init. expected=%s, actual=%s'):format(owner, currentOwner))
--- check that the plate matches twice, 100ms apart as a bug has been observed in which server side matches but plate is not observed by clients to match
if plateMatched then
return true
end
plateMatched = true
Wait(100)
end
end, 'Failed to set vehicle properties within 1 second', 1000)
end)
if success then
break
else
DeleteEntity(veh)
attempts += 1
end
else
break
end
end

if attempts == 3 then
error('unable to successfully spawn vehicle after 3 attempts')
end

local netId = NetworkGetNetworkIdFromEntity(veh)
exports.qbx_core:EnablePersistence(veh)
return netId, veh
end
Expand All @@ -325,8 +347,8 @@ else
---@field scale? integer default: `0.35`
---@field font? integer default: `4`
---@field color? vector4 rgba, white by default
---@field enableDropShadow? boolean
---@field enableOutline? boolean
---@field enableDropShadow? boolean
---@field enableOutline? boolean

---@class LibDrawText2DParams : LibDrawTextParams
---@field coords vector2
Expand Down
4 changes: 2 additions & 2 deletions qbx_core.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ CREATE TABLE IF NOT EXISTS `players` (
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `players`
ADD IF NOT EXISTS `last_logged_out` timestamp NULL DEFAULT NULL AFTER `last_updated`,
ADD COLUMN IF NOT EXISTS `last_logged_out` timestamp NULL DEFAULT NULL AFTER `last_updated`,
MODIFY COLUMN `name` varchar(255) NOT NULL COLLATE utf8mb4_unicode_ci;

ALTER TABLE `players`
ADD IF NOT EXISTS `userId` INT UNSIGNED DEFAULT NULL AFTER `id`;
ADD COLUMN IF NOT EXISTS `userId` INT UNSIGNED DEFAULT NULL AFTER `id`;

CREATE TABLE IF NOT EXISTS `bans` (
`id` int(11) NOT NULL AUTO_INCREMENT,
Expand Down
2 changes: 1 addition & 1 deletion server/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ AddEventHandler('onResourceStart', function(resource)

MySQL.query([[
ALTER TABLE `players`
ADD IF NOT EXISTS `userId` INT UNSIGNED DEFAULT NULL AFTER `id`;
ADD COLUMN IF NOT EXISTS `userId` INT UNSIGNED DEFAULT NULL AFTER `id`;
]])
end)

Expand Down
28 changes: 28 additions & 0 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ end

exports('GetSource', GetSource)

---@param identifier Identifier
---@return integer source of the player with the matching identifier or 0 if no player found
function GetUserId(identifier)
for src in pairs(QBX.Players) do
local idens = GetPlayerIdentifiers(src)
for _, id in pairs(idens) do
if identifier == id then
return QBX.Players[src].PlayerData.userId
end
end
end
return 0
end

exports('GetUserId', GetUserId)

---@param source Source|string source or identifier of the player
---@return Player
function GetPlayer(source)
Expand All @@ -51,6 +67,18 @@ end

exports('GetPlayerByCitizenId', GetPlayerByCitizenId)

---@param userId string
---@return Player?
function GetPlayerByUserId(userId)
for src in pairs(QBX.Players) do
if QBX.Players[src].PlayerData.userId == userId then
return QBX.Players[src]
end
end
end

exports('GetPlayerByUserId', GetPlayerByUserId)

---@param number string
---@return Player?
function GetPlayerByPhone(number)
Expand Down
9 changes: 1 addition & 8 deletions server/vehicle-persistence.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,7 @@ RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff)
end

if diff.tyres then
local damage = {}
for i = 0, 7 do
if IsVehicleTyreBurst(vehicle, i, false) then
damage[i] = IsVehicleTyreBurst(vehicle, i, true) and 2 or 1
end
end

props.tyres = damage
props.tyres = diff.tyres ~= 'deleted' and diff.tyres or nil
end

if persistence == 2 then
Expand Down
Loading

0 comments on commit 52605fd

Please sign in to comment.