refactor(client/progress): sync props with statebags (#515)

Co-authored-by: Linden <65407488+thelindat@users.noreply.github.com>
This commit is contained in:
Andy
2024-03-19 08:01:23 +01:00
committed by GitHub
parent f4d43e533d
commit 0e00ff57ff

View File

@@ -2,6 +2,7 @@ local progress
local DisableControlAction = DisableControlAction
local DisablePlayerFiring = DisablePlayerFiring
local playerState = LocalPlayer.state
local createdProps = {}
---@class ProgressPropProps
---@field model string
@@ -23,12 +24,12 @@ local playerState = LocalPlayer.state
---@field prop? ProgressPropProps | ProgressPropProps[]
---@field disable? { move?: boolean, sprint?: boolean, car?: boolean, combat?: boolean, mouse?: boolean }
local function createProp(prop)
local function createProp(ped, prop)
lib.requestModel(prop.model)
local coords = GetEntityCoords(cache.ped)
local object = CreateObject(prop.model, coords.x, coords.y, coords.z, true, true, true)
local coords = GetEntityCoords(ped)
local object = CreateObject(prop.model, coords.x, coords.y, coords.z, false, false, false)
AttachEntityToEntity(object, cache.ped, GetPedBoneIndex(cache.ped, prop.bone or 60309), prop.pos.x, prop.pos.y, prop.pos.z, prop.rot.x, prop.rot.y, prop.rot.z, true, true, false, true, 0, true)
AttachEntityToEntity(object, ped, GetPedBoneIndex(ped, prop.bone or 60309), prop.pos.x, prop.pos.y, prop.pos.z, prop.rot.x, prop.rot.y, prop.rot.z, true, true, false, true, 0, true)
SetModelAsNoLongerNeeded(prop.model)
return object
@@ -77,17 +78,7 @@ local function startProgress(data)
end
if data.prop then
if data.prop.model then
data.prop1 = createProp(data.prop)
else
for i = 1, #data.prop do
local prop = data.prop[i]
if prop then
data['prop'..i] = createProp(prop)
end
end
end
playerState:set('lib:progressProps', data.prop, true)
end
local disable = data.disable
@@ -133,14 +124,7 @@ local function startProgress(data)
end
if data.prop then
local n = #data.prop
for i = 1, n > 0 and n or 1 do
local prop = data['prop'..i]
if prop then
DeleteEntity(prop)
end
end
playerState:set('lib:progressProps', nil, true)
end
if anim then
@@ -222,3 +206,48 @@ RegisterCommand('cancelprogress', function()
end)
RegisterKeyMapping('cancelprogress', locale('cancel_progress'), 'keyboard', 'x')
local function deleteProgressProps(serverId)
local playerProps = createdProps[serverId]
if not playerProps then return end
for i = 1, #playerProps do
local prop = playerProps[i]
if DoesEntityExist(prop) then
DeleteEntity(prop)
end
end
createdProps[serverId] = nil
end
RegisterNetEvent('onPlayerDropped', function(serverId)
deleteProgressProps(serverId)
end)
AddStateBagChangeHandler('lib:progressProps', nil, function(bagName, key, value, reserved, replicated)
if replicated then return end
local ply = GetPlayerFromStateBagName(bagName)
if ply == 0 then return end
local ped = GetPlayerPed(ply)
local serverId = GetPlayerServerId(ply)
if not value then
return deleteProgressProps(serverId)
end
createdProps[serverId] = {}
local playerProps = createdProps[serverId]
if value.model then
playerProps[#playerProps+1] = createProp(ped, value)
else
for i = 1, #value do
local prop = value[i]
if prop then
playerProps[#playerProps+1] = createProp(ped, prop)
end
end
end
end)