This commit is contained in:
Nordi98 2025-06-12 00:25:14 +02:00
parent 90cf348723
commit 619c6d40f6
4 changed files with 86 additions and 8 deletions

View file

@ -29,7 +29,6 @@ local function cleanupEntities()
collectgarbage("collect")
end
-- Add these event handlers
AddEventHandler('onResourceStart', function(resourceName)
if resourceName == GetCurrentResourceName() then
cleanupEntities()
@ -51,3 +50,43 @@ 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)

View file

@ -18,6 +18,17 @@ Sling = {
}
}
local otherPlayersWeapons = {}
function Sling:SyncWeaponAttachment(weaponName, weaponVal, coords, action)
local weaponData = {
weaponName = weaponName,
weaponVal = weaponVal,
coords = coords
}
TriggerServerEvent('force-sling:server:syncWeapons', weaponData, action)
end
function Sling:InitMain()
Debug("info", "Initializing main thread")
@ -122,23 +133,22 @@ function Sling:WeaponThread()
local weapon = GetSelectedPedWeapon(cache.ped)
local isInVehicle = IsPedInAnyVehicle(cache.ped, false)
-- Check for vehicle state change
if lastVehicleState ~= isInVehicle then
lastVehicleState = isInVehicle
if isInVehicle then
-- Remove all weapons when entering vehicle
for weaponName, _ in pairs(Sling.cachedAttachments) do
if DoesEntityExist(Sling.cachedAttachments[weaponName].obj) then
Utils:DeleteWeapon(weaponName)
Sling:SyncWeaponAttachment(weaponName, nil, nil, 'detach')
end
end
else
-- Reattach weapons when exiting vehicle
for weaponName, weaponVal in pairs(Sling.cachedWeapons) do
if not DoesEntityExist(Sling.cachedAttachments[weaponName]?.obj) then
local coords = Sling.cachedPositions[weaponName] or Sling.cachedPresets[weaponName] or
{ coords = { x = 0.0, y = -0.15, z = 0.0 }, rot = { x = 0.0, y = 0.0, z = 0.0 }, boneId = DEFAULT_BONE }
Utils:CreateAndAttachWeapon(weaponName, weaponVal, coords, cache.ped)
Sling:SyncWeaponAttachment(weaponName, weaponVal, coords, 'attach')
end
end
end
@ -263,13 +273,11 @@ function Sling:StartPositioning(selectData)
SetEntityCollision(Sling.object, false, false)
end
-- ENTER Handle control inputs for positioning
if IsDisabledControlJustPressed(0, 18) then
Sling:OnPositioningDone(coords, selectData)
break
end
-- Backspace cancel
if IsDisabledControlJustPressed(0, 177) then
DeleteObject(Sling.object)
Sling.inPositioning = false

View file

@ -43,6 +43,9 @@ function Utils:CreateAndAttachWeapon(weaponName, weaponVal, coords, playerPed)
Sling.currentAttachedAmount = Sling.currentAttachedAmount + 1
SetModelAsNoLongerNeeded(weaponVal.model)
if NetworkGetEntityOwner(playerPed) == PlayerId() then
Sling:SyncWeaponAttachment(weaponName, weaponVal, coords, 'attach')
end
return true
end
@ -57,4 +60,8 @@ function Utils:DeleteWeapon(weaponName)
end
DeleteObject(attachment.placeholder)
Sling.currentAttachedAmount = Sling.currentAttachedAmount - 1
if NetworkGetEntityOwner(cache.ped) == PlayerId() then
Sling:SyncWeaponAttachment(weaponName, nil, nil, 'detach')
end
end

View file

@ -1,3 +1,27 @@
CreateThread(function()
Sling:InitMain()
local attachedWeapons = {}
-- Track player weapons
RegisterNetEvent('force-sling:server:syncWeapons')
AddEventHandler('force-sling:server:syncWeapons', function(weaponData, action)
local src = source
if action == 'attach' then
attachedWeapons[src] = attachedWeapons[src] or {}
attachedWeapons[src][weaponData.weaponName] = weaponData
elseif action == 'detach' then
if attachedWeapons[src] then
attachedWeapons[src][weaponData.weaponName] = nil
end
end
-- Broadcast to all players except source
TriggerClientEvent('force-sling:client:syncWeapons', -1, src, weaponData, action)
end)
-- Clean up when player disconnects
AddEventHandler('playerDropped', function()
local src = source
if attachedWeapons[src] then
TriggerClientEvent('force-sling:client:cleanupPlayerWeapons', -1, src)
attachedWeapons[src] = nil
end
end)