forked from project-infinitys/qb-radio-ui-updated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.lua
446 lines (408 loc) · 15.7 KB
/
client.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
local QBCore = exports['qb-core']:GetCoreObject()
local PlayerData = QBCore.Functions.GetPlayerData() -- Just for resource restart (same as event handler)
local radioMenu = false
local onRadio = false
local RadioChannel = 0
local RadioVolume = 50
local hasRadio = false
local radioProp = nil
--Function
local function LoadAnimDic(dict)
if not HasAnimDictLoaded(dict) then
RequestAnimDict(dict)
while not HasAnimDictLoaded(dict) do
Wait(0)
end
end
end
local function SplitStr(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
t[#t+1] = str
end
return t
end
local function connecttoradio(channel)
RadioChannel = channel
local player = PlayerId()
if onRadio then
exports["pma-voice"]:setRadioChannel(0)
else
onRadio = true
exports["pma-voice"]:setVoiceProperty("radioEnabled", true)
end
exports["pma-voice"]:setRadioChannel(channel)
TriggerServerEvent("qb-radio:server:addradiolist", channel)
if SplitStr(tostring(channel), ".")[2] ~= nil and SplitStr(tostring(channel), ".")[2] ~= "" then
QBCore.Functions.Notify(Config.messages['joined_to_radio'] ..channel.. ' MHz', 'success')
else
QBCore.Functions.Notify(Config.messages['joined_to_radio'] ..channel.. '.00 MHz', 'success')
end
TriggerServerEvent("qb-log:server:CreateLog", "radio", channel .." Radio Joined", "green", ""..GetPlayerName(GetPlayerFromServerId(GetPlayerServerId(PlayerId()))).. " Has Joined The Radio " .. channel)
end
local function closeEvent()
TriggerEvent("InteractSound_CL:PlayOnOne","click",0.6)
end
local function leaveradio()
closeEvent()
local player = PlayerId()
local rchannel = RadioChannel
RadioChannel = 0
onRadio = false
exports["pma-voice"]:setRadioChannel(0)
exports["pma-voice"]:setVoiceProperty("radioEnabled", false)
QBCore.Functions.Notify(Config.messages['you_leave'] , 'error')
TriggerServerEvent("qb-radio:remove:removeradiolist")
TriggerServerEvent("qb-log:server:CreateLog", "radio", rchannel .." Radio Left", "red", ""..GetPlayerName(GetPlayerFromServerId(GetPlayerServerId(PlayerId()))).. " has Left The Radio " .. rchannel)
end
local function toggleRadioAnimation(pState)
LoadAnimDic("cellphone@")
if pState then
TriggerEvent("attachItemRadio","radio01")
TaskPlayAnim(PlayerPedId(), "cellphone@", "cellphone_text_read_base", 2.0, 3.0, -1, 49, 0, 0, 0, 0)
radioProp = CreateObject(`prop_cs_hand_radio`, 1.0, 1.0, 1.0, 1, 1, 0)
AttachEntityToEntity(radioProp, PlayerPedId(), GetPedBoneIndex(PlayerPedId(), 57005), 0.14, 0.01, -0.02, 110.0, 120.0, -15.0, 1, 0, 0, 0, 2, 1)
else
StopAnimTask(PlayerPedId(), "cellphone@", "cellphone_text_read_base", 1.0)
ClearPedTasks(PlayerPedId())
if radioProp ~= 0 then
DeleteObject(radioProp)
radioProp = 0
end
end
end
local function toggleRadio(toggle)
radioMenu = toggle
SetNuiFocus(radioMenu, radioMenu)
if radioMenu then
toggleRadioAnimation(true)
SendNUIMessage({type = "open"})
else
toggleRadioAnimation(false)
SendNUIMessage({type = "close"})
end
end
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
end
--Exports
exports("IsRadioOn", IsRadioOn)
--Events
-- list connected players
RegisterNUICallback('getPlayers', function (_, cb)
QBCore.Functions.TriggerCallback('qb-radio:remove:getlist', function(playerlist)
if playerlist then
cb(playerlist)
else
cb(nil)
end
end, RadioChannel)
end)
-- Handles state right when the player selects their character and location.
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
PlayerData = QBCore.Functions.GetPlayerData()
DoRadioCheck(PlayerData.items)
end)
-- Resets state on logout, in case of character change.
RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
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)
end)
-- Handles state if resource is restarted live.
AddEventHandler('onResourceStart', function(resource)
if GetCurrentResourceName() == resource then
PlayerData = QBCore.Functions.GetPlayerData()
DoRadioCheck(PlayerData.items)
end
end)
RegisterNetEvent('qb-radio:use', function()
toggleRadio(not radioMenu)
end)
RegisterNetEvent('qb-radio:onRadioDrop', function()
if RadioChannel ~= 0 then
leaveradio()
end
end)
-- NUI
RegisterNUICallback('joinRadio', function(data, cb)
local rchannel = tonumber(data.channel)
if rchannel ~= nil then
if rchannel <= Config.MaxFrequency and rchannel ~= 0 then
if rchannel ~= RadioChannel then
if Config.RestrictedChannels[rchannel] ~= nil then
if Config.RestrictedChannels[rchannel][PlayerData.job.name] and PlayerData.job.onduty then
connecttoradio(rchannel)
else
QBCore.Functions.Notify(Config.messages['restricted_channel_error'], 'error')
end
else
connecttoradio(rchannel)
end
else
QBCore.Functions.Notify(Config.messages['you_on_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
cb("ok")
end)
RegisterNUICallback('leaveRadio', function(_, cb)
local player = PlayerId()
local rchannel = RadioChannel
if RadioChannel == 0 then
QBCore.Functions.Notify(Config.messages['not_on_radio'], 'error')
else
leaveradio()
end
cb("ok")
end)
RegisterNUICallback("volumeUp", function(_, cb)
if RadioVolume <= 95 then
RadioVolume = RadioVolume + 5
QBCore.Functions.Notify(Config.messages["volume_radio"] .. RadioVolume, "success")
exports["pma-voice"]:setRadioVolume(RadioVolume)
else
QBCore.Functions.Notify(Config.messages["decrease_radio_volume"], "error")
end
cb('ok')
end)
RegisterNUICallback("volumeDown", function(_, cb)
if RadioVolume >= 10 then
RadioVolume = RadioVolume - 5
QBCore.Functions.Notify(Config.messages["volume_radio"] .. RadioVolume, "success")
exports["pma-voice"]:setRadioVolume(RadioVolume)
else
QBCore.Functions.Notify(Config.messages["increase_radio_volume"], "error")
end
cb('ok')
end)
RegisterNUICallback("increaseradiochannel", function(_, cb)
--local newChannel = RadioChannel + 1
local newChannel = tonumber(data.channel) + 1
TriggerServerEvent("qb-log:server:CreateLog", "radio", "Radio Changed + ", "yellow", ""..GetPlayerName(GetPlayerFromServerId(GetPlayerServerId(PlayerId()))).. " Has Changed The Radio from "..RadioChannel.." to " .. newChannel)
connecttoradio(newChannel)
cb("ok")
end)
RegisterNUICallback("decreaseradiochannel", function(_, cb)
if not onRadio then return end
-- local newChannel = RadioChannel - 1
local newChannel = tonumber(data.channel) - 1
if newChannel >= 1 then
TriggerServerEvent("qb-log:server:CreateLog", "radio", "Radio Changed - ", "yellow", ""..GetPlayerName(GetPlayerFromServerId(GetPlayerServerId(PlayerId()))).. " Has Changed The Radio from "..RadioChannel.." to " .. newChannel)
connecttoradio(newChannel)
cb("ok")
end
end)
RegisterNUICallback('poweredOff', function(_, cb)
leaveradio()
cb("ok")
end)
RegisterNUICallback('escape', function(_, cb)
toggleRadio(false)
cb("ok")
end)
--Main Thread
CreateThread(function()
while true do
Wait(1000)
if LocalPlayer.state.isLoggedIn and onRadio then
if not hasRadio or PlayerData.metadata.isdead then
if RadioChannel ~= 0 then
leaveradio()
end
end
end
end
end)
-- Leave Radio
RegisterNetEvent('qb-radio:client:leaveRadio', function()
leaveradio()
end)
RegisterNetEvent('qb-radio:client:volumeUpradio', function(val)
local newvalue = tonumber(val)
if newvalue <= 100 and newvalue >= 0 then
RadioVolume = newvalue
QBCore.Functions.Notify(Config.messages["volume_radio"] .. RadioVolume, "success")
exports["pma-voice"]:setRadioVolume(RadioVolume)
else
QBCore.Functions.Notify("Invalid Volume Amount", "error")
end
end)
RegisterNetEvent('qb-radio:client:JoinRadioChannel1', function()
QBCore.Functions.TriggerCallback('QBCore:HasItem', function()
local rchannel = 1
if rchannel ~= nil then
if rchannel <= Config.MaxFrequency and rchannel ~= 0 then
if rchannel ~= RadioChannel then
if Config.RestrictedChannels[rchannel] ~= nil then
if Config.RestrictedChannels[rchannel][PlayerData.job.name] and PlayerData.job.onduty then
connecttoradio(rchannel)
else
QBCore.Functions.Notify(Config.messages['restricted_channel_error'], 'error')
end
else
connecttoradio(rchannel)
end
else
QBCore.Functions.Notify(Config.messages['you_on_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
end, 'radio')
end)
RegisterNetEvent('qb-radio:client:JoinRadioChannel2', function()
QBCore.Functions.TriggerCallback('QBCore:HasItem', function()
local rchannel = 2
if rchannel ~= nil then
if rchannel <= Config.MaxFrequency and rchannel ~= 0 then
if rchannel ~= RadioChannel then
if Config.RestrictedChannels[rchannel] ~= nil then
if Config.RestrictedChannels[rchannel][PlayerData.job.name] and PlayerData.job.onduty then
connecttoradio(rchannel)
else
QBCore.Functions.Notify(Config.messages['restricted_channel_error'], 'error')
end
else
connecttoradio(rchannel)
end
else
QBCore.Functions.Notify(Config.messages['you_on_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
end, 'radio')
end)
RegisterNetEvent('qb-radio:client:JoinRadioChannel3', function()
QBCore.Functions.TriggerCallback('QBCore:HasItem', function()
local rchannel = 3
if rchannel ~= nil then
if rchannel <= Config.MaxFrequency and rchannel ~= 0 then
if rchannel ~= RadioChannel then
if Config.RestrictedChannels[rchannel] ~= nil then
if Config.RestrictedChannels[rchannel][PlayerData.job.name] and PlayerData.job.onduty then
connecttoradio(rchannel)
else
QBCore.Functions.Notify(Config.messages['restricted_channel_error'], 'error')
end
else
connecttoradio(rchannel)
end
else
QBCore.Functions.Notify(Config.messages['you_on_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
end, 'radio')
end)
RegisterNetEvent('qb-radio:client:JoinRadioChannel4', function()
QBCore.Functions.TriggerCallback('QBCore:HasItem', function()
local rchannel = 4
if rchannel ~= nil then
if rchannel <= Config.MaxFrequency and rchannel ~= 0 then
if rchannel ~= RadioChannel then
if Config.RestrictedChannels[rchannel] ~= nil then
if Config.RestrictedChannels[rchannel][PlayerData.job.name] and PlayerData.job.onduty then
connecttoradio(rchannel)
else
QBCore.Functions.Notify(Config.messages['restricted_channel_error'], 'error')
end
else
connecttoradio(rchannel)
end
else
QBCore.Functions.Notify(Config.messages['you_on_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
end, 'radio')
end)
RegisterNetEvent('qb-radio:client:JoinRadioChannel5', function()
QBCore.Functions.TriggerCallback('QBCore:HasItem', function()
local rchannel = 5
if rchannel ~= nil then
if rchannel <= Config.MaxFrequency and rchannel ~= 0 then
if rchannel ~= RadioChannel then
if Config.RestrictedChannels[rchannel] ~= nil then
if Config.RestrictedChannels[rchannel][PlayerData.job.name] and PlayerData.job.onduty then
connecttoradio(rchannel)
else
QBCore.Functions.Notify(Config.messages['restricted_channel_error'], 'error')
end
else
connecttoradio(rchannel)
end
else
QBCore.Functions.Notify(Config.messages['you_on_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
end, 'radio')
end)
RegisterNetEvent('qb-radio:client:JoinRadioChannel6', function()
QBCore.Functions.TriggerCallback('QBCore:HasItem', function()
local rchannel = 6
if rchannel ~= nil then
if rchannel <= Config.MaxFrequency and rchannel ~= 0 then
if rchannel ~= RadioChannel then
if Config.RestrictedChannels[rchannel] ~= nil then
if Config.RestrictedChannels[rchannel][PlayerData.job.name] and PlayerData.job.onduty then
connecttoradio(rchannel)
else
QBCore.Functions.Notify(Config.messages['restricted_channel_error'], 'error')
end
else
connecttoradio(rchannel)
end
else
QBCore.Functions.Notify(Config.messages['you_on_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
else
QBCore.Functions.Notify(Config.messages['invalid_radio'] , 'error')
end
end, 'radio')
end)