Skip to content

Commit

Permalink
Update 1.1.1 qb-core support
Browse files Browse the repository at this point in the history
* Added support for qb-core
* Improved the animation for laying down a spikestrip
* You will no longer receive a spike strip if your inventory is full
  • Loading branch information
loaf-scripts authored Nov 9, 2021
1 parent e28ed0e commit 1e7a91e
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 58 deletions.
57 changes: 53 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,59 @@ YouTube Showcase: https://www.youtube.com/watch?v=D3UtNxFXnpU
* You can remove spikestrips
* Only the tyre that is touching the spikestrip will burst, unlike some other resources which bursts all tyres.
* Performance friendly, uses ~0.01ms
* Standalone, but has support for ESX and QBCore

## Standalone & ESX.
The script works with and without ESX. (follow [this guide](https://github.com/loaf-scripts/loaf_spikestrips/wiki/esx_policejob-compatibility) if you use esx_policejob).
## Standalone, ESX & QBCore
The script works with ESX, QBCore or standalone without any framework.

If you only want certain jobs to be able to use spikestrips but you don't have ESX, set [`Config.JobBased`](https://github.com/loaf-scripts/loaf_spikestrips/blob/main/config.lua#L13) to true. You can edit who is able to use the script at the server.lua, simply add an identifier (any identifier works) to the `police` table in the [server/main.lua](https://github.com/loaf-scripts/loaf_spikestrips/blob/main/server/main.lua#L1) file.
### Use with ESX
#### Installation
* Run the esx.sql file
* Rename from `loaf_spikestrips-main` to `loaf_spikestrips`
* Drag the resource to your server resources folder
* Add `ensure loaf_spikestrips` to your server.cfg
#### Configuration
* Set `Config.Framework` to `"esx"`
* Modify `FrameworkFeatures` to your liking.
#### esx_policejob compatibility
Visit [this page](https://github.com/loaf-scripts/loaf_spikestrips/wiki/esx_policejob-compatibility) for a guide on how to use the resource together with esx_policejob

If you only want certain jobs to be able to use spikestrips and you use ESX, set [`Config.JobBased`](https://github.com/loaf-scripts/loaf_spikestrips/blob/main/config.lua#L13) to true. You also need to set [`Config.ESX`](https://github.com/loaf-scripts/loaf_spikestrips/blob/main/config.lua#L4) to true. You can configure what jobs are able to use spikestrips by editing the [`PoliceJobs`](https://github.com/loaf-scripts/loaf_spikestrips/blob/main/config.lua#L21) table in the config.
### Use with QBCore
#### Installation
* Add the following code to your QBShared.Items file, located in [qb-core/shared.lua:51](https://github.com/qbcore-framework/qb-core/blob/main/shared.lua#L51) by default:
```lua
["spikestrip"] = {
["name"] = "spikestrip",
["label"] = "Spikestrip",
["weight"] = 25,
["type"] = "item",
["image"] = "spikestrip.png",
["unique"] = false,
["useable"] = true,
["shouldClose"] = true,
["combinable"] = nil,
["description"] = "A spikestrip"
},
```
* For the not WarMenu menu, you need [qb-menu](https://github.com/qbcore-framework/qb-menu)
* Rename from `loaf_spikestrips-main` to `loaf_spikestrips`
* Drag the resource to your server resources folder
* Add `ensure loaf_spikestrips` to your server.cfg
* Move the spikestrip.png file to your qb-inventory/html/images folder
#### Configuration
* Set `Config.Framework` to `"qb"`
* Modify `FrameworkFeatures` to your liking.
#### qb-policejob compatibility
Visit [this page](https://github.com/loaf-scripts/loaf_spikestrips/wiki/qb-policejob-compatibility) for a guide on how to use the resource together with qb-policejob

### Use standalone
#### Installation
* Rename from `loaf_spikestrips-main` to `loaf_spikestrips`
* Drag the resource to your server resources folder
* Add `ensure loaf_spikestrips` to your server.cfg
#### Configuration
* Set `Config.Framework` to `"none"`
* Modify the `police` table in the server.lua file to change who is able to place spike strips.

## Credits
* `@Jay ;)#6969` - helped with adding support for qb-core
7 changes: 6 additions & 1 deletion client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ function DeployStinger()
NetworkStopSynchronisedScene(scene)

-- play deploy animation for stinger
SetEntityVisible(stinger, true)
PlayEntityAnim(stinger, "P_Stinger_S_Deploy", LoadDict("p_ld_stinger_s"), 1000.0, false, true, 0, 0.0, 0)
while not IsEntityPlayingAnim(stinger, "p_ld_stinger_s", "P_Stinger_S_Deploy", 3) do
Wait(0)
end
SetEntityVisible(stinger, true)
while IsEntityPlayingAnim(stinger, "p_ld_stinger_s", "P_Stinger_S_Deploy", 3) and GetEntityAnimCurrentTime(stinger, "p_ld_stinger_s", "P_Stinger_S_Deploy") <= 0.99 do
Wait(0)
end
Expand All @@ -54,6 +54,11 @@ function RemoveStinger()
end
end

RegisterNetEvent("loaf_spikestrips:removeSpikestrip")
AddEventHandler("loaf_spikestrips:removeSpikestrip", function()
RemoveStinger()
end)

function TouchingStinger(coords, stinger)
local min, max = GetModelDimensions(GetEntityModel(stinger))
local size = max - min
Expand Down
29 changes: 20 additions & 9 deletions client/main.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ESX = nil
QBCore = nil

isPolice = false
closestStinger = 0
Expand All @@ -13,7 +14,7 @@ CreateThread(function()
Wait(500)
end

if Config.ESX then
if Config.Framework == "esx" then
while not ESX do
Wait(500)
TriggerEvent("esx:getSharedObject", function(esx)
Expand All @@ -24,24 +25,34 @@ CreateThread(function()
while not ESX.GetPlayerData() or not ESX.GetPlayerData().job or not ESX.GetPlayerData().job.name do
Wait(500)
end
elseif Config.Framework == "qb" then
while not QBCore do
Wait(500)
QBCore = exports["qb-core"]:GetCoreObject()
end

while not QBCore.Functions.GetPlayerData() or not QBCore.Functions.GetPlayerData().job or not QBCore.Functions.GetPlayerData().job.name do
Wait(500)
end
end

if Config.JobBased then
if Config.ESX then
RegisterNetEvent("esx:setJob")
AddEventHandler("esx:setJob", function(jobData)
if Config.RequireJobPlace then
if Config.Framework == "esx" or Config.Framework == "qb" then
local eventName = Config.Framework == "esx" and "esx:setJob" or "QBCore:Client:OnJobUpdate"
RegisterNetEvent(eventName)
AddEventHandler(eventName, function(jobData)
local jobName = jobData.name
isPolice = false
for _, job in pairs(Config.ESXFeatures.PoliceJobs) do
for _, job in pairs(Config.FrameworkFeatures.PoliceJobs) do
if job == jobName then
isPolice = true
break
end
end
end)
local jobName = ESX.GetPlayerData().job.name
for _, job in pairs(Config.ESXFeatures.PoliceJobs) do

local jobName = Config.Framework == "esx" and ESX.GetPlayerData().job.name or QBCore.Functions.GetPlayerData().job.name
for _, job in pairs(Config.FrameworkFeatures.PoliceJobs) do
if job == jobName then
isPolice = true
break
Expand Down
51 changes: 43 additions & 8 deletions client/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if Config.Menu.Enabled then
end
end

if Config.ESX and not Config.ESXFeatures.UseWarmenu then
if Config.Framework == "esx" and not Config.FrameworkFeatures.UseWarmenu then
while not ESX do
Wait(500)
end
Expand All @@ -38,12 +38,10 @@ if Config.Menu.Enabled then
value = "place_spikestrip"
})
end
if isPolice or not Config.RequireJobRemove then
table.insert(elements, {
label = Strings["remove_spikestrip"],
value = "remove_spikestrip"
})
end
table.insert(elements, {
label = Strings["remove_spikestrip"],
value = "remove_spikestrip"
})

ESX.UI.Menu.Open("default", GetCurrentResourceName(), "spikestrip_menu", {
title = Strings["menu_label"],
Expand All @@ -62,6 +60,43 @@ if Config.Menu.Enabled then
ESX.ShowNotification(Strings["not_police"])
end
end)
elseif Config.Framework == "qb" and not Config.FrameworkFeatures.UseWarmenu then
while not QBCore do
Wait(500)
end

RegisterNetEvent("loaf_spikestrips:spikestripMenu")
AddEventHandler("loaf_spikestrips:spikestripMenu", function()
if isPolice or not Config.RequireJobRemove then
exports["qb-menu"]:closeMenu()

local elements = {
{
header = Strings["place_spikestrip"],
params = {
event = "loaf_spikestrips:placeSpikestrip",
}
}
}
if isPolice or not Config.RequireJobRemove then
table.insert(elements, {
header = Strings["remove_spikestrip"],
params = {
event = "loaf_spikestrips:removeSpikestrip",
}
})
end
table.insert(elements, {
header = Strings["close_menu"],
params = {
event = "qb-menu:client:closeMenu",
}
})
exports["qb-menu"]:openMenu(elements)
else
QBCore.Functions.Notify(Strings["not_police"])
end
end)
else
WarMenu.CreateMenu("spikestrip", Strings["menu_label"], Strings["menu_sublabel"])

Expand All @@ -80,7 +115,7 @@ if Config.Menu.Enabled then
Wait(500)
while WarMenu.Begin("spikestrip") do
Wait(0)
if isPolice and IsPedOnFoot(PlayerPedId()) and WarMenu.Button(Strings["select_spikestrip"]) then
if isPolice and IsPedOnFoot(PlayerPedId()) and WarMenu.Button(Strings["place_spikestrip"]) then
DeployStinger()
end

Expand Down
28 changes: 17 additions & 11 deletions config.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
Config = {
Debugging = false,

Framework = "none", --[[ What framework to use
Valid options:
* "qb" (qb-core)
* "esx" (ESX)
* "none" (Standalone)
]]

ESX = false, -- use esx?
RequireJobRemove = false, -- should everyone be able to remove a spikestrip, or just people with job allowed to use spikestrips?
RequireJobPlace = true, -- require job to place a spike strip?
RequireJobRemove = false, -- should everyone be able to remove a spikestrip, or just people with job allowed to place spikestrips?

Menu = {
Enabled = true, -- TriggerEvent("loaf_spikestrips:spikestripMenu") to open the menu
Command = "spikestrip", -- set to false to disable
Keybind = "F5", -- set to false to disable
Keybind = "F5", -- set to false to disable (NOTE: COMMAND CAN'T BE FALSE IF YOU WANT A KEYBIND)
},

JobBased = false, -- require job to place a spike strip?

ESXFeatures = {
FrameworkFeatures = { -- these features are only if you use Config.Framework "esx" or "qb"
Item = "spikestrip", -- item to deploy a spikestrip (set to false if you don't want to have this enabled)
ReceiveRemove = true, -- receive spikestrip item if you remove a spikestrip?
ReceiveJob = false, -- false = police won't receive a spikestrip when they remove it | true = police will receive a spikestrip item when they remove a spikestrip
ReceiveJob = true, -- false = police won't receive a spikestrip when they remove it | true = police will receive a spikestrip item when they remove a spikestrip

UseWarmenu = false, -- false = default esx menu, true = use warmenu (looks like gta:o)
PoliceJobs = { -- ESX police jobs
PoliceJobs = { -- police jobs
"police",
"sheriff",
},
},
}
Expand All @@ -30,9 +36,9 @@ Strings = {

["menu_label"] = "Spikestrip menu",
["menu_sublabel"] = "By Loaf Scripts#7785",
["place_spikestrip"] = "Place spikestrip",
["place_spikestrip"] = "Place a spikestrip",
["remove_spikestrip"] = "Remove closest spikestrip",
["select_spikestrip"] = "Spikestrip",
["close_menu"] = "Close menu",
["back"] = "Back",

["cant_carry"] = "Your inventory is full, you did not receive a spikestrip.",
}
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fx_version "cerulean"
game "gta5"

version "1.1.0"
version "1.1.1"

shared_script "config.lua"
client_script "client/*.lua"
Expand Down
81 changes: 57 additions & 24 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
local QBCore, ESX
if Config.Framework == "esx" then
TriggerEvent("esx:getSharedObject", function(object)
ESX = object
end)
elseif Config.Framework == "qb" then
QBCore = exports["qb-core"]:GetCoreObject()
end

local police = {
"steam:11000010ded0daa"
}
Expand All @@ -8,6 +17,7 @@ local function IsPolice(identifier)
return true
end
end
return false
end

RegisterNetEvent("loaf_spikestrips:checkPolice")
Expand All @@ -22,37 +32,60 @@ AddEventHandler("loaf_spikestrips:checkPolice", function()
end
end)

if Config.ESX then
TriggerEvent("esx:getSharedObject", function(ESX)
if Config.ESXFeatures.Item then
ESX.RegisterUsableItem(Config.ESXFeatures.Item, function(src)
if Config.Framework == "esx" or Config.Framework == "qb" then
if Config.FrameworkFeatures.Item then
if Config.Framework == "esx" then
ESX.RegisterUsableItem(Config.FrameworkFeatures.Item, function(src)
local xPlayer = ESX.GetPlayerFromId(src)
xPlayer.removeInventoryItem(Config.ESXFeatures.Item, 1)
xPlayer.removeInventoryItem(Config.FrameworkFeatures.Item, 1)
TriggerClientEvent("loaf_spikestrips:placeSpikestrip", src)
end)
elseif Config.Framework == "qb" then
QBCore.Functions.CreateUseableItem(Config.FrameworkFeatures.Item, function(src)
local xPlayer = QBCore.Functions.GetPlayer(src)
xPlayer.Functions.RemoveItem(Config.FrameworkFeatures.Item, 1)
TriggerClientEvent("loaf_spikestrips:placeSpikestrip", src)
end)
end

if Config.ESXFeatures.ReceiveRemove then
-- WARNING: THIS EVENT CAN BE ABUSED TO GET FREE SPIKESTRIPS
RegisterNetEvent("loaf_spikestrips:removedSpike")
AddEventHandler("loaf_spikestrips:removedSpike", function()
local xPlayer = ESX.GetPlayerFromId(source)
if xPlayer then
local isPolice = false
for _, job in pairs(Config.ESXFeatures.PoliceJobs) do
if xPlayer.job.name == job then
isPolice = true
break
end
if Config.FrameworkFeatures.ReceiveRemove then
-- WARNING: THIS EVENT CAN BE ABUSED TO GET FREE SPIKESTRIPS
RegisterNetEvent("loaf_spikestrips:removedSpike")
AddEventHandler("loaf_spikestrips:removedSpike", function()
local src, xPlayer = source
if Config.Framework == "esx" then
xPlayer = ESX.GetPlayerFromId(src)
elseif Config.Framework == "qb" then
xPlayer = QBCore.Functions.GetPlayer(src)
end

if xPlayer then
local isPolice = false
for _, job in pairs(Config.FrameworkFeatures.PoliceJobs) do
if (Config.Framework == "esx" and xPlayer.job.name == job) or (Config.Framework == "qb" and xPlayer.PlayerData.job.name == job) then
isPolice = true
break
end

if isPolice or not Config.RequireJobRemove then
if not isPolice or (isPolice and Config.ESXFeatures.ReceiveJob) then
xPlayer.addInventoryItem(Config.ESXFeatures.Item, 1)
end

if isPolice or not Config.RequireJobRemove then
if not isPolice or (isPolice and Config.FrameworkFeatures.ReceiveJob) then
if Config.Framework == "esx" then
local itemdata = xPlayer.getInventoryItem(Config.FrameworkFeatures.Item)
if xPlayer.canCarryItem and xPlayer.canCarryItem(Config.FrameworkFeatures.Item, 1) then
xPlayer.addInventoryItem(Config.FrameworkFeatures.Item, 1)
elseif not xPlayer.canCarryItem and (itemdata.limit == -1 or (itemdata.count + 1) <= itemdata.limit) then
xPlayer.addInventoryItem(Config.FrameworkFeatures.Item, 1)
else
TriggerClientEvent("esx:showNotification", src, Strings["cant_carry"])
end
elseif Config.Framework == "qb" then
xPlayer.Functions.AddItem(Config.FrameworkFeatures.Item, 1)
end
end
end
end)
end
end
end)
end
end)
end
end

0 comments on commit 1e7a91e

Please sign in to comment.