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

121 lines
3.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 then
if Sling.object then
safeDelete(Sling.object)
Sling.object = nil
end
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
end
Sling.currentAttachedAmount = 0
end
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
end
otherPlayersWeapons[playerId] = nil
end
collectgarbage("collect")
end
CreateThread(function()
while not Sling do
Wait(100)
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)
end)
RegisterNetEvent('force-sling:client:syncWeapons')
AddEventHandler('force-sling:client:syncWeapons', function(playerId, weaponData, action)
if not Sling then return end
if playerId == cache.serverId then return end -- Ignoriere eigene Events
if action == 'attach' then
local targetPed = GetPlayerPed(GetPlayerFromServerId(playerId))
if not targetPed or not DoesEntityExist(targetPed) then return end
-- Erstelle einen eindeutigen Key für die Waffe des anderen Spielers
local uniqueWeaponKey = playerId .. '_' .. weaponData.weaponName
otherPlayersWeapons[playerId] = otherPlayersWeapons[playerId] or {}
-- Verwende den eindeutigen Key für die Waffe
Utils:CreateAndAttachWeapon(
uniqueWeaponKey, -- Eindeutiger Key
weaponData.weaponVal,
weaponData.coords,
targetPed,
true -- Flag für andere Spieler
)
otherPlayersWeapons[playerId][uniqueWeaponKey] = true
elseif action == 'detach' then
local uniqueWeaponKey = playerId .. '_' .. weaponData.weaponName
if otherPlayersWeapons[playerId] and otherPlayersWeapons[playerId][uniqueWeaponKey] then
Utils:DeleteWeapon(uniqueWeaponKey)
otherPlayersWeapons[playerId][uniqueWeaponKey] = nil
end
end
end)
RegisterNetEvent('force-sling:client:cleanupPlayerWeapons')
AddEventHandler('force-sling:client:cleanupPlayerWeapons', function(playerId)
if not Sling then return end
if otherPlayersWeapons[playerId] then
for weaponKey, _ in pairs(otherPlayersWeapons[playerId]) do
Utils:DeleteWeapon(weaponKey)
end
otherPlayersWeapons[playerId] = nil
end
end)