Main/resources/[jobs]/[weapons]/force-sling/client/events.lua

122 lines
3.5 KiB
Lua
Raw Normal View History

2025-06-07 08:51:21 +02:00
local function cleanupEntities()
local function safeDelete(entity)
if DoesEntityExist(entity) then
if IsEntityAttachedToAnyPed(entity) then
DetachEntity(entity, true, true)
end
2025-06-12 00:07:24 +02:00
NetworkUnregisterNetworkedEntity(entity)
2025-06-07 08:51:21 +02:00
DeleteObject(entity)
SetEntityAsNoLongerNeeded(entity)
return true
end
return false
end
2025-06-12 16:37:41 +02:00
-- Sicherheitsprüfung für Sling
if Sling then
if Sling.object then
safeDelete(Sling.object)
Sling.object = nil
end
2025-06-07 08:51:21 +02:00
2025-06-12 16:37:41 +02:00
if Sling.cachedAttachments then
for weaponName, attachment in pairs(Sling.cachedAttachments) do
if attachment then
safeDelete(attachment.obj)
safeDelete(attachment.placeholder)
Sling.cachedAttachments[weaponName] = nil
end
end
2025-06-07 08:51:21 +02:00
end
2025-06-12 16:37:41 +02:00
Sling.currentAttachedAmount = 0
2025-06-07 08:51:21 +02:00
end
2025-06-12 16:16:47 +02:00
-- Cleanup other players weapons
2025-06-12 16:37:41 +02:00
if otherPlayersWeapons then
for playerId, weapons in pairs(otherPlayersWeapons) do
for weaponName, _ in pairs(weapons) do
if Sling and Sling.cachedAttachments and Sling.cachedAttachments[weaponName] then
safeDelete(Sling.cachedAttachments[weaponName].obj)
safeDelete(Sling.cachedAttachments[weaponName].placeholder)
end
2025-06-12 16:16:47 +02:00
end
2025-06-12 16:37:41 +02:00
otherPlayersWeapons[playerId] = nil
2025-06-12 16:16:47 +02:00
end
end
2025-06-07 08:51:21 +02:00
collectgarbage("collect")
end
2025-06-12 16:37:41 +02:00
-- Verzögere die Registrierung der Events bis Sling initialisiert ist
CreateThread(function()
while not Sling do
Wait(100)
end
2025-06-07 08:51:21 +02:00
2025-06-12 16:37:41 +02:00
AddEventHandler('onResourceStart', function(resourceName)
if resourceName == GetCurrentResourceName() then
cleanupEntities()
end
end)
2025-06-12 00:07:24 +02:00
2025-06-12 16:37:41 +02:00
RegisterNetEvent('QBCore:Client:OnPlayerLoaded')
AddEventHandler('QBCore:Client:OnPlayerLoaded', function()
cleanupEntities()
end)
AddEventHandler('onResourceStop', function(resourceName)
if resourceName == GetCurrentResourceName() then
cleanupEntities()
end
end)
2025-06-07 08:51:21 +02:00
2025-06-12 16:37:41 +02:00
AddEventHandler('playerDropped', function()
cleanupEntities()
end)
2025-06-07 08:51:21 +02:00
end)
2025-06-12 00:07:24 +02:00
2025-06-12 00:25:14 +02:00
RegisterNetEvent('force-sling:client:syncWeapons')
AddEventHandler('force-sling:client:syncWeapons', function(playerId, weaponData, action)
2025-06-12 16:37:41 +02:00
if not Sling then return end
2025-06-12 00:25:14 +02:00
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)
2025-06-12 16:37:41 +02:00
if not Sling then return end
2025-06-12 00:25:14 +02:00
if otherPlayersWeapons[playerId] then
for weaponName, _ in pairs(otherPlayersWeapons[playerId]) do
Utils:DeleteWeapon(weaponName)
end
otherPlayersWeapons[playerId] = nil
end
end)
2025-06-12 16:37:41 +02:00