Skip to content

Commit

Permalink
bugfix, blips, throw from vehicle
Browse files Browse the repository at this point in the history
  • Loading branch information
loaf-scripts committed Feb 29, 2024
1 parent abeb3db commit 2b98a0e
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 9 deletions.
79 changes: 71 additions & 8 deletions client/client.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---@alias stinger { id: string, netId?: number, entity?: number, coords: vector3, rotation: vector3, minOffset: vector3, maxOffset: vector3, point?: table }
---@alias stinger { id: string, netId?: number, entity?: number, coords: vector3, rotation: vector3, minOffset: vector3, maxOffset: vector3, point?: table, blip?: number }

---@type {string: stinger }
local stingers = lib.callback.await("loaf_spikestrips:getSpikestrips")
Expand All @@ -17,10 +17,22 @@ local function placeStinger()
local playerPed = cache.ped
local offset = GetOffsetFromEntityInWorldCoords(playerPed, -0.2, 2.0, 0.0)
local heading = GetEntityHeading(playerPed)
local onFoot = IsPedOnFoot(playerPed)
local skipAnimation = false
local stinger
local netId

if not IsPedOnFoot(playerPed) then
if not onFoot and cache.vehicle then
local model = GetEntityModel(cache.vehicle)
local min = model and GetModelDimensions(model) or { y = -2.5 }

offset = GetOffsetFromEntityInWorldCoords(cache.vehicle, 0.0, min.y, 0.0)
heading -= 90
skipAnimation = true
end

if not Config.AllowFromVehicle and not onFoot then
Notify(L("cant_vehicle"), "error")
return
end

Expand Down Expand Up @@ -54,13 +66,14 @@ local function placeStinger()
if Config.SpawnMethod == "local" then
stinger = CreateObject(model, offset.x, offset.y, offset.z, false, false, false)
placing = stinger
else
elseif Config.SpawnMethod == "networked" then
stinger = CreateObject(model, offset.x, offset.y, offset.z, true, true, false)
netId = NetworkGetNetworkIdFromEntity(stinger)
end

FreezeEntityPosition(stinger, true)
SetEntityVisible(stinger, false, false)
SetEntityCoordsNoOffset(stinger, offset.x, offset.y, offset.z, true, true, true)
SetEntityHeading(stinger, heading)
PlaceObjectOnGroundProperly(stinger)

Expand All @@ -70,14 +83,16 @@ local function placeStinger()

TriggerServerEvent("loaf_spikestrips:placedSpikestrip", coords, GetEntityRotation(stinger, 2), minOffset, maxOffset, netId)

-- Player deploying animation
TaskPlayAnim(playerPed, playerDict, "enter", 1000.0, -1.0, 200, 16, 0, false, false, false)
if not skipAnimation then
-- Player deploying animation
TaskPlayAnim(playerPed, playerDict, "enter", 1000.0, -1.0, 200, 16, 0, false, false, false)

WaitForAnimation(playerPed, playerDict, "enter")
WaitForAnimation(playerPed, playerDict, "enter")

SetAnimRate(playerPed, 3.0, 1.0, false)
SetAnimRate(playerPed, 3.0, 1.0, false)

Wait(550)
Wait(550)
end

-- Stinger animation
PlayEntityAnim(stinger, "p_stinger_s_deploy", stingerDict, 1000.0, false, true, false, 0.0, 0)
Expand Down Expand Up @@ -184,6 +199,10 @@ RegisterNetEvent("loaf_spikestrips:spikestripAdded", function(placer, id, coords
point = point,
}

if Config.Blips and IsPolice() then
stingers[id].blip = CreateStingerBlip(coords)
end

if Config.RemoveDistance and placer == cache.serverId then
while #(GetEntityCoords(cache.ped) - coords) < Config.RemoveDistance and stingers[id] do
Wait(1000)
Expand Down Expand Up @@ -214,9 +233,14 @@ RegisterNetEvent("loaf_spikestrips:spikestripRemoved", function(id)
end

if stinger.point then
stinger.point:onExit()
stinger.point:remove()
end

if stinger.blip then
RemoveBlip(stinger.blip)
end

if Config.SpawnMethod == "local" and stinger.entity then
if Config.InteractStyle == "target" and targettableEntities[stinger.entity] then
targettableEntities[stinger.entity] = nil
Expand Down Expand Up @@ -426,12 +450,51 @@ if Config.Command then
TriggerEvent("chat:addSuggestion", "/" .. Config.Command, L("place_description"), {})
end

local function refreshBlips(isPolice)
for _, stinger in pairs(stingers) do
if Config.Blips and isPolice and not stinger.blip then
stinger.blip = CreateStingerBlip(stinger.coords)
elseif stinger.blip then
RemoveBlip(stinger.blip)
stinger.blip = nil
end
end
end

if Config.Blips and Config.BlipsCommand then
RegisterCommand(Config.BlipsCommand, function()
local isPolice = IsPolice()

if not isPolice then
return debugprint("not police")
end

Config.Blips = not Config.Blips

refreshBlips(isPolice)
end, false)
end

AddEventHandler("loaf_spikestrips:toggleIsPolice", function(isPolice)
if not Config.Blips then
return
end

refreshBlips(isPolice)
end)

AddTextEntry("SPIKESTRIP_BLIP", L("blip_name"))

AddEventHandler("onResourceStop", function(resource)
if resource ~= GetCurrentResourceName() then
return
end

for _, stinger in pairs(stingers) do
if stinger.blip then
RemoveBlip(stinger.blip)
end

if stinger.entity then
DeleteEntity(stinger.entity)
end
Expand Down
8 changes: 8 additions & 0 deletions client/framework/esx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ RegisterNetEvent("esx:setJob", function(job)
return
end

local wasPolice = IsPolice()

ESX.PlayerData.job = job

local isPolice = IsPolice()

if wasPolice ~= isPolice then
TriggerEvent("loaf_spikestrips:toggleIsPolice", isPolice)
end
end)

function IsPolice()
Expand Down
8 changes: 8 additions & 0 deletions client/framework/qb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ end
local PlayerJob = QB.Functions.GetPlayerData().job

RegisterNetEvent("QBCore:Client:OnJobUpdate", function(jobInfo)
local wasPolice = IsPolice()

PlayerJob = jobInfo

local isPolice = IsPolice()

if wasPolice ~= isPolice then
TriggerEvent("loaf_spikestrips:toggleIsPolice", isPolice)
end
end)

function IsPolice()
Expand Down
17 changes: 17 additions & 0 deletions client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ function WaitForControlAndNetId(netId)
return entity
end

---@param coords vector3 | vector4
---@return number
function CreateStingerBlip(coords)
local blip = AddBlipForCoord(coords.x, coords.y, coords.z)

SetBlipSprite(blip, 237) -- 237, 274, 677
SetBlipColour(blip, 39)
SetBlipScale(blip, 0.6)
SetBlipAsShortRange(blip, true)
SetBlipDisplay(blip, 2)

BeginTextCommandSetBlipName("SPIKESTRIP_BLIP")
EndTextCommandSetBlipName(blip)

return blip
end

function ShowHelpText(textEntry)
ClearHelp(true)
BeginTextCommandDisplayHelp(textEntry)
Expand Down
3 changes: 3 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ Config.NotificationSystem = "ox_lib" -- framework or ox_lib, modify in client/fu
Config.SpawnMethod = "server" -- local (non-networked), networked or server
Config.BurstNPC = false -- burst tires of NPCs? note that this can be resource intensive
Config.LogSystem = false -- "discord" or "ox_lib". Set your discord webhook in server/logs.lua. Set to false to disable
Config.Blips = false -- show blips of all spike strips on the map for allowed jobs?
Config.AllowFromVehicle = false -- allow throwing spike strips from vehicles?

Config.BlipsCommand = "spikestripblips" -- command to toggle blips (set to false to disable)
Config.Command = "spikestrip" -- command to place spike strip, set to false to disable
Config.ClearCommand = "clearspikestrips" -- admin command to clear all spike strips, set to false to disable

Expand Down
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ lua54 "yes"
author "Loaf Scripts"
description "Spike strip script that only bursts the touching tires."

version "2.0.1"
version "2.1.0"

shared_scripts {
"@ox_lib/init.lua",
Expand Down
4 changes: 4 additions & 0 deletions locales/en.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ Locales = {
logs_removed_distance = "Removed the spike strip {id} because the player went too far away",
logs_picked_up = "Picked up the spike strip {id}",
logs_placed_spikestrip = "Placed a spike strip with the id {id}",

cant_vehicle = "You can't place spike strips from a vehicle",

blip_name = "Spike Strip",
}

0 comments on commit 2b98a0e

Please sign in to comment.