Main/resources/[jobs]/[weapons]/force-sling/client/events.lua
2025-06-12 00:25:14 +02:00

92 lines
2.6 KiB
Lua

local function cleanupEntities()
local function safeDelete(entity)
if DoesEntityExist(entity) then
if IsEntityAttachedToAnyPed(entity) then
DetachEntity(entity, true, true)
end
NetworkUnregisterNetworkedEntity(entity)
DeleteObject(entity)
SetEntityAsNoLongerNeeded(entity)
return true
end
return false
end
if Sling.object then
safeDelete(Sling.object)
Sling.object = nil
end
for weaponName, attachment in pairs(Sling.cachedAttachments) do
if attachment then
safeDelete(attachment.obj)
safeDelete(attachment.placeholder)
Sling.cachedAttachments[weaponName] = nil
end
end
Sling.currentAttachedAmount = 0
collectgarbage("collect")
end
AddEventHandler('onResourceStart', function(resourceName)
if resourceName == GetCurrentResourceName() then
cleanupEntities()
end
end)
RegisterNetEvent('QBCore:Client:OnPlayerLoaded')
AddEventHandler('QBCore:Client:OnPlayerLoaded', function()
cleanupEntities()
end)
AddEventHandler('onResourceStop', function(resourceName)
if resourceName == GetCurrentResourceName() then
cleanupEntities()
end
end)
AddEventHandler('playerDropped', function()
cleanupEntities()
end)
RegisterNetEvent('force-sling:client:syncWeapons')
AddEventHandler('force-sling:client:syncWeapons', function(playerId, weaponData, action)
if playerId == cache.serverId then return end
if action == 'attach' then
local targetPed = GetPlayerPed(GetPlayerFromServerId(playerId))
if not targetPed or not DoesEntityExist(targetPed) then return end
if not otherPlayersWeapons[playerId] then
otherPlayersWeapons[playerId] = {}
end
Utils:CreateAndAttachWeapon(
weaponData.weaponName,
weaponData.weaponVal,
weaponData.coords,
targetPed
)
otherPlayersWeapons[playerId][weaponData.weaponName] = true
elseif action == 'detach' then
if otherPlayersWeapons[playerId] and otherPlayersWeapons[playerId][weaponData.weaponName] then
Utils:DeleteWeapon(weaponData.weaponName)
otherPlayersWeapons[playerId][weaponData.weaponName] = nil
end
end
end)
RegisterNetEvent('force-sling:client:cleanupPlayerWeapons')
AddEventHandler('force-sling:client:cleanupPlayerWeapons', function(playerId)
if otherPlayersWeapons[playerId] then
for weaponName, _ in pairs(otherPlayersWeapons[playerId]) do
Utils:DeleteWeapon(weaponName)
end
otherPlayersWeapons[playerId] = nil
end
end)