forked from Simnation/Main
sling
This commit is contained in:
parent
90cf348723
commit
619c6d40f6
4 changed files with 86 additions and 8 deletions
|
@ -29,7 +29,6 @@ local function cleanupEntities()
|
||||||
collectgarbage("collect")
|
collectgarbage("collect")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add these event handlers
|
|
||||||
AddEventHandler('onResourceStart', function(resourceName)
|
AddEventHandler('onResourceStart', function(resourceName)
|
||||||
if resourceName == GetCurrentResourceName() then
|
if resourceName == GetCurrentResourceName() then
|
||||||
cleanupEntities()
|
cleanupEntities()
|
||||||
|
@ -51,3 +50,43 @@ AddEventHandler('playerDropped', function()
|
||||||
cleanupEntities()
|
cleanupEntities()
|
||||||
end)
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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()
|
function Sling:InitMain()
|
||||||
Debug("info", "Initializing main thread")
|
Debug("info", "Initializing main thread")
|
||||||
|
|
||||||
|
@ -122,23 +133,22 @@ function Sling:WeaponThread()
|
||||||
local weapon = GetSelectedPedWeapon(cache.ped)
|
local weapon = GetSelectedPedWeapon(cache.ped)
|
||||||
local isInVehicle = IsPedInAnyVehicle(cache.ped, false)
|
local isInVehicle = IsPedInAnyVehicle(cache.ped, false)
|
||||||
|
|
||||||
-- Check for vehicle state change
|
|
||||||
if lastVehicleState ~= isInVehicle then
|
if lastVehicleState ~= isInVehicle then
|
||||||
lastVehicleState = isInVehicle
|
lastVehicleState = isInVehicle
|
||||||
if isInVehicle then
|
if isInVehicle then
|
||||||
-- Remove all weapons when entering vehicle
|
|
||||||
for weaponName, _ in pairs(Sling.cachedAttachments) do
|
for weaponName, _ in pairs(Sling.cachedAttachments) do
|
||||||
if DoesEntityExist(Sling.cachedAttachments[weaponName].obj) then
|
if DoesEntityExist(Sling.cachedAttachments[weaponName].obj) then
|
||||||
Utils:DeleteWeapon(weaponName)
|
Utils:DeleteWeapon(weaponName)
|
||||||
|
Sling:SyncWeaponAttachment(weaponName, nil, nil, 'detach')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
-- Reattach weapons when exiting vehicle
|
|
||||||
for weaponName, weaponVal in pairs(Sling.cachedWeapons) do
|
for weaponName, weaponVal in pairs(Sling.cachedWeapons) do
|
||||||
if not DoesEntityExist(Sling.cachedAttachments[weaponName]?.obj) then
|
if not DoesEntityExist(Sling.cachedAttachments[weaponName]?.obj) then
|
||||||
local coords = Sling.cachedPositions[weaponName] or Sling.cachedPresets[weaponName] or
|
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 }
|
{ 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)
|
Utils:CreateAndAttachWeapon(weaponName, weaponVal, coords, cache.ped)
|
||||||
|
Sling:SyncWeaponAttachment(weaponName, weaponVal, coords, 'attach')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -263,13 +273,11 @@ function Sling:StartPositioning(selectData)
|
||||||
SetEntityCollision(Sling.object, false, false)
|
SetEntityCollision(Sling.object, false, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ENTER Handle control inputs for positioning
|
|
||||||
if IsDisabledControlJustPressed(0, 18) then
|
if IsDisabledControlJustPressed(0, 18) then
|
||||||
Sling:OnPositioningDone(coords, selectData)
|
Sling:OnPositioningDone(coords, selectData)
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Backspace cancel
|
|
||||||
if IsDisabledControlJustPressed(0, 177) then
|
if IsDisabledControlJustPressed(0, 177) then
|
||||||
DeleteObject(Sling.object)
|
DeleteObject(Sling.object)
|
||||||
Sling.inPositioning = false
|
Sling.inPositioning = false
|
||||||
|
|
|
@ -43,6 +43,9 @@ function Utils:CreateAndAttachWeapon(weaponName, weaponVal, coords, playerPed)
|
||||||
Sling.currentAttachedAmount = Sling.currentAttachedAmount + 1
|
Sling.currentAttachedAmount = Sling.currentAttachedAmount + 1
|
||||||
SetModelAsNoLongerNeeded(weaponVal.model)
|
SetModelAsNoLongerNeeded(weaponVal.model)
|
||||||
|
|
||||||
|
if NetworkGetEntityOwner(playerPed) == PlayerId() then
|
||||||
|
Sling:SyncWeaponAttachment(weaponName, weaponVal, coords, 'attach')
|
||||||
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -57,4 +60,8 @@ function Utils:DeleteWeapon(weaponName)
|
||||||
end
|
end
|
||||||
DeleteObject(attachment.placeholder)
|
DeleteObject(attachment.placeholder)
|
||||||
Sling.currentAttachedAmount = Sling.currentAttachedAmount - 1
|
Sling.currentAttachedAmount = Sling.currentAttachedAmount - 1
|
||||||
|
|
||||||
|
if NetworkGetEntityOwner(cache.ped) == PlayerId() then
|
||||||
|
Sling:SyncWeaponAttachment(weaponName, nil, nil, 'detach')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,27 @@
|
||||||
CreateThread(function()
|
local attachedWeapons = {}
|
||||||
Sling:InitMain()
|
|
||||||
|
-- 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)
|
end)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue