diff --git a/config.lua b/config.lua index c37c2ae..0f22a44 100644 --- a/config.lua +++ b/config.lua @@ -8,6 +8,8 @@ Config.DeathDropsWeapon = true -- Drops your current weapon upon death. Config.ThrowKeybind = "e" +Config.interact = true + Config.Weapons = { -- Any weapon in this list is throwable. "WEAPON_DAGGER", "WEAPON_BAT", @@ -89,4 +91,4 @@ Config.Weapons = { -- Any weapon in this list is throwable. "WEAPON_HOMINGLAUNCHER", "WEAPON_COMPACTLAUNCHER", "WEAPON_RAYMINIGUN", -} \ No newline at end of file +} diff --git a/modules/weapon/client.lua b/modules/weapon/client.lua index 2234840..6b81fa2 100644 --- a/modules/weapon/client.lua +++ b/modules/weapon/client.lua @@ -24,31 +24,78 @@ end function ThrowCurrentWeapon() if throwingWeapon then return end + local ped = PlayerPedId() local equipped, weaponHash = GetCurrentPedWeapon(ped, 1) local weapon = GetWeaponString(weaponHash) if not equipped or not weapon then return end + + local ammoCount = GetAmmoInPedWeapon(ped, weaponHash) throwingWeapon = true + CreateThread(function() PlayAnim(ped, "melee@thrown@streamed_core", "plyr_takedown_front", -8.0, 8.0, -1, 49) Wait(600) ClearPedTasks(ped) end) + Wait(550) + local coords = GetOffsetFromEntityInWorldCoords(ped, 0.0, 0.0, 1.0) local prop = GetWeaponObjectFromPed(ped, true) local model = GetEntityModel(prop) + RemoveWeaponFromPed(ped, weaponHash) SetCurrentPedWeapon(ped, `WEAPON_UNARMED`, true) DeleteEntity(prop) + prop = CreateProp(model, coords.x, coords.y, coords.z, true, false, true) SetEntityCoords(prop, coords.x, coords.y, coords.z) SetEntityHeading(prop, GetEntityHeading(ped) + 90.0) PerformPhysics(prop) - TriggerServerEvent("pickle_weaponthrowing:throwWeapon", {weapon = weapon, net_id = ObjToNet(prop)}) + + if Config.interact then + -- Add interaction to the thrown weapon + exports.interact:AddEntityInteraction({ + netId = ObjToNet(prop), + id = 'throwWeaponInteraction_' .. ObjToNet(prop), -- Unique ID for removing interactions + distance = 4.0, -- Optional interaction distance + interactDst = 3.0, -- Optional interaction trigger distance + ignoreLos = true, -- Optional ignore line of sight + options = { + { + label = 'Plukk opp', + action = function(entity, coords, args) + local ped = PlayerPedId() + if not IsPlayerDead(ped) and not IsPedInAnyVehicle(ped, true) then + for k, v in pairs(ThrownWeapons) do + if NetworkDoesNetworkIdExist(v.net_id) then + local entity = NetToObj(v.net_id) + ClearPedTasksImmediately(ped) + FreezeEntityPosition(ped, true) + PlayAnim(ped, "pickup_object", "pickup_low", -8.0, 8.0, -1, 49, 1.0) + Wait(800) + TriggerServerEvent("pickle_weaponthrowing:pickupWeapon", k) + Wait(800) + ClearPedTasks(ped) + FreezeEntityPosition(ped, false) + end + end + end + end, + }, + }, + }) + end + + TriggerServerEvent("pickle_weaponthrowing:throwWeapon", {weapon = weapon, net_id = ObjToNet(prop), ammo = ammoCount}) -- Include ammo count throwingWeapon = nil end +RegisterNetEvent("pickle_weaponthrowing:removeInteraction", function(netId) + exports.interact:RemoveEntityInteraction('throwWeaponInteraction_' .. netId) +end) + function OnPlayerDeath() if not Config.DeathDropsWeapon then return end local ped = PlayerPedId() @@ -88,32 +135,34 @@ AddEventHandler('gameEventTriggered', function(event, args) OnPlayerDeath() end) -CreateThread(function() - while true do - local wait = 1000 - local ped = PlayerPedId() - if not IsPlayerDead(ped) and not IsPedInAnyVehicle(ped, true) then - for k,v in pairs(ThrownWeapons) do - if NetworkDoesNetworkIdExist(v.net_id) then - local entity = NetToObj(v.net_id) - local coords = GetEntityCoords(entity) - local dist = #(GetEntityCoords(ped) - coords) - if dist < 5.0 then - wait = 0 - if dist < 1.25 and not ShowInteractText(_L("pickup_weapon")) and IsControlJustPressed(1, 51) then - ClearPedTasksImmediately(ped) - FreezeEntityPosition(ped, true) - PlayAnim(ped, "pickup_object", "pickup_low", -8.0, 8.0, -1, 49, 1.0) - Wait(800) - TriggerServerEvent("pickle_weaponthrowing:pickupWeapon", k) - Wait(800) - ClearPedTasks(ped) - FreezeEntityPosition(ped, false) +if not Config.interact then + CreateThread(function() + while true do + local wait = 1000 + local ped = PlayerPedId() + if not IsPlayerDead(ped) and not IsPedInAnyVehicle(ped, true) then + for k,v in pairs(ThrownWeapons) do + if NetworkDoesNetworkIdExist(v.net_id) then + local entity = NetToObj(v.net_id) + local coords = GetEntityCoords(entity) + local dist = #(GetEntityCoords(ped) - coords) + if dist < 5.0 then + wait = 0 + if dist < 1.25 and not ShowInteractText(_L("pickup_weapon")) and IsControlJustPressed(1, 51) then + ClearPedTasksImmediately(ped) + FreezeEntityPosition(ped, true) + PlayAnim(ped, "pickup_object", "pickup_low", -8.0, 8.0, -1, 49, 1.0) + Wait(800) + TriggerServerEvent("pickle_weaponthrowing:pickupWeapon", k) + Wait(800) + ClearPedTasks(ped) + FreezeEntityPosition(ped, false) + end end end - end - end + end + end + Wait(wait) end - Wait(wait) - end -end) + end) +end diff --git a/modules/weapon/server.lua b/modules/weapon/server.lua index 401d04f..19ea5c3 100644 --- a/modules/weapon/server.lua +++ b/modules/weapon/server.lua @@ -17,7 +17,14 @@ RegisterNetEvent("pickle_weaponthrowing:pickupWeapon", function(weaponID) local source = source if not ThrownWeapons[weaponID] then return end local entity = NetworkGetEntityFromNetworkId(ThrownWeapons[weaponID].net_id) + + if Config.interact then + -- Trigger client event to remove the interaction + TriggerClientEvent("pickle_weaponthrowing:removeInteraction", -1, ThrownWeapons[weaponID].net_id) + end + DeleteEntity(entity) + -- Ensure AddWeapon now accounts for the ammo count AddWeapon(source, ThrownWeapons[weaponID]) ThrownWeapons[weaponID] = nil TriggerClientEvent("pickle_weaponthrowing:setWeaponData", -1, weaponID, nil) @@ -28,4 +35,4 @@ AddEventHandler("onResourceStop", function(name) for k,v in pairs(ThrownWeapons) do DeleteEntity(NetworkGetEntityFromNetworkId(v.net_id)) end -end) \ No newline at end of file +end)