Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interact added as a Config and Save ammo (Added from one of the other pull requests) #22

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -89,4 +91,4 @@ Config.Weapons = { -- Any weapon in this list is throwable.
"WEAPON_HOMINGLAUNCHER",
"WEAPON_COMPACTLAUNCHER",
"WEAPON_RAYMINIGUN",
}
}
103 changes: 76 additions & 27 deletions modules/weapon/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
9 changes: 8 additions & 1 deletion modules/weapon/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -28,4 +35,4 @@ AddEventHandler("onResourceStop", function(name)
for k,v in pairs(ThrownWeapons) do
DeleteEntity(NetworkGetEntityFromNetworkId(v.net_id))
end
end)
end)