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

Refactor radio check function to simplify logic and use configuration… #60

Merged
merged 4 commits into from
Feb 13, 2025
Merged
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
21 changes: 6 additions & 15 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,8 @@ local function IsRadioOn()
return onRadio
end

local function DoRadioCheck(PlayerItems)
local _hasRadio = false

for _, item in pairs(PlayerItems) do
if item.name == "radio" then
_hasRadio = true
break;
end
end

hasRadio = _hasRadio
local function DoRadioCheck()
hasRadio = QBCore.Functions.HasItem(Config.RadioItem, 1)
end

--Exports
Expand All @@ -119,27 +110,27 @@ exports("IsRadioOn", IsRadioOn)
-- Handles state right when the player selects their character and location.
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
PlayerData = QBCore.Functions.GetPlayerData()
DoRadioCheck(PlayerData.items)
DoRadioCheck()
end)

-- Resets state on logout, in case of character change.
RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
DoRadioCheck({})
DoRadioCheck()
PlayerData = {}
leaveradio()
end)

-- Handles state when PlayerData is changed. We're just looking for inventory updates.
RegisterNetEvent('QBCore:Player:SetPlayerData', function(val)
PlayerData = val
DoRadioCheck(PlayerData.items)
DoRadioCheck()
end)

-- Handles state if resource is restarted live.
AddEventHandler('onResourceStart', function(resource)
if GetCurrentResourceName() == resource then
PlayerData = QBCore.Functions.GetPlayerData()
DoRadioCheck(PlayerData.items)
DoRadioCheck()
end
end)

Expand Down
2 changes: 2 additions & 0 deletions config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Config = {}

Config.RadioItem = 'radio'

Config.RestrictedChannels = {
[1] = {
police = true,
Expand Down