Skip to content

Commit e725307

Browse files
authored
Merge pull request #60 from Cocodrulo/refactor/hasitem
Refactor radio check function to simplify logic and use configuration…
2 parents 9527254 + 8d90eeb commit e725307

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

client.lua

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,8 @@ local function IsRadioOn()
9898
return onRadio
9999
end
100100

101-
local function DoRadioCheck(PlayerItems)
102-
local _hasRadio = false
103-
104-
for _, item in pairs(PlayerItems) do
105-
if item.name == "radio" then
106-
_hasRadio = true
107-
break;
108-
end
109-
end
110-
111-
hasRadio = _hasRadio
101+
local function DoRadioCheck()
102+
hasRadio = QBCore.Functions.HasItem(Config.RadioItem, 1)
112103
end
113104

114105
--Exports
@@ -119,27 +110,27 @@ exports("IsRadioOn", IsRadioOn)
119110
-- Handles state right when the player selects their character and location.
120111
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
121112
PlayerData = QBCore.Functions.GetPlayerData()
122-
DoRadioCheck(PlayerData.items)
113+
DoRadioCheck()
123114
end)
124115

125116
-- Resets state on logout, in case of character change.
126117
RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
127-
DoRadioCheck({})
118+
DoRadioCheck()
128119
PlayerData = {}
129120
leaveradio()
130121
end)
131122

132123
-- Handles state when PlayerData is changed. We're just looking for inventory updates.
133124
RegisterNetEvent('QBCore:Player:SetPlayerData', function(val)
134125
PlayerData = val
135-
DoRadioCheck(PlayerData.items)
126+
DoRadioCheck()
136127
end)
137128

138129
-- Handles state if resource is restarted live.
139130
AddEventHandler('onResourceStart', function(resource)
140131
if GetCurrentResourceName() == resource then
141132
PlayerData = QBCore.Functions.GetPlayerData()
142-
DoRadioCheck(PlayerData.items)
133+
DoRadioCheck()
143134
end
144135
end)
145136

config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Config = {}
22

3+
Config.RadioItem = 'radio'
4+
35
Config.RestrictedChannels = {
46
[1] = {
57
police = true,

0 commit comments

Comments
 (0)