-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
527 lines (449 loc) · 17.1 KB
/
Copy pathclient.lua
File metadata and controls
527 lines (449 loc) · 17.1 KB
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
local QBCore = exports['qb-core']:GetCoreObject()
-- ============================================================================
-- LOCAL STATE
-- ============================================================================
local BusinessState = {} -- [businessName] = { playing, trackIndex, volume, shuffle, shuffleOrder }
local SpawnedProps = {} -- [businessName] = propEntity
local ActiveSounds = {} -- [businessName] = true/false
local OpenJukebox = nil -- Currently open jukebox business name
-- ============================================================================
-- HELPERS
-- ============================================================================
local function DebugPrint(...)
if Config.Debug then
print('[dynamic-jukebox]', ...)
end
end
local function GetBusinessByName(name)
for _, biz in ipairs(Config.Businesses) do
if biz.name == name then
return biz
end
end
return nil
end
local function GetSoundId(businessName)
return 'djb_' .. businessName
end
local function GetSongUrl(filename)
return 'nui://dynamic-jukebox/music/' .. filename
end
local function GetPlaylistForBusiness(biz)
return biz.playlist or {}
end
-- ============================================================================
-- XSOUND PLAYBACK
-- ============================================================================
local function StopSound(businessName)
local soundId = GetSoundId(businessName)
if ActiveSounds[businessName] then
pcall(function()
exports.xsound:Destroy(soundId)
end)
ActiveSounds[businessName] = false
DebugPrint('Stopped sound for', businessName)
end
end
local function PlaySound(businessName)
local biz = GetBusinessByName(businessName)
if not biz then return end
local state = BusinessState[businessName]
if not state or not state.playing then return end
local playlist = GetPlaylistForBusiness(biz)
if #playlist == 0 then return end
local trackIndex = state.trackIndex or 1
if trackIndex > #playlist then trackIndex = 1 end
local soundId = GetSoundId(businessName)
local url = GetSongUrl(playlist[trackIndex])
local volume = state.volume or Config.DefaultVolume
local maxDist = biz.maxDistance or Config.MaxSoundDistance
local coords = biz.soundCoords
-- Destroy existing sound first
StopSound(businessName)
-- Play positional sound (not looped — we handle advancement ourselves)
exports.xsound:PlayUrlPos(soundId, url, volume, coords, false)
-- Wait for xsound to register the sound before setting distance
Wait(200)
exports.xsound:Distance(soundId, maxDist)
ActiveSounds[businessName] = true
DebugPrint('Playing track', trackIndex, '(' .. playlist[trackIndex] .. ') for', businessName)
-- Update NUI if this jukebox is open
if OpenJukebox == businessName then
SendNUIMessage({
action = 'updateState',
playing = state.playing,
trackIndex = trackIndex,
trackName = playlist[trackIndex],
volume = volume,
totalTracks = #playlist,
shuffle = state.shuffle or false,
})
end
end
local function SetVolume(businessName, volume)
local soundId = GetSoundId(businessName)
if ActiveSounds[businessName] then
pcall(function()
exports.xsound:setVolume(soundId, volume)
end)
end
end
-- ============================================================================
-- SONG END DETECTION & AUTO-ADVANCE
-- ============================================================================
-- xsound fires 'xsound:Finish' when a sound finishes playing.
-- If your xsound version uses a different event, change the name below.
-- Common alternatives: 'xsound:onPlayEnd', 'xsound:soundEnd'
-- ============================================================================
RegisterNetEvent('xsound:Finish', function(soundId)
for _, biz in ipairs(Config.Businesses) do
if GetSoundId(biz.name) == soundId then
local state = BusinessState[biz.name]
if state and state.playing then
DebugPrint('Sound finished for', biz.name, '- requesting advance')
ActiveSounds[biz.name] = false
TriggerServerEvent('djb:server:AdvanceTrack', biz.name)
end
break
end
end
end)
-- ============================================================================
-- SERVER SYNC EVENTS
-- ============================================================================
RegisterNetEvent('djb:client:SyncState', function(allStates)
DebugPrint('Received full state sync')
if type(allStates) ~= 'table' then return end
for businessName, state in pairs(allStates) do
BusinessState[businessName] = state
if state.playing then
CreateThread(function()
PlaySound(businessName)
end)
else
StopSound(businessName)
end
end
end)
RegisterNetEvent('djb:client:UpdateBusiness', function(businessName, state)
if type(businessName) ~= 'string' or type(state) ~= 'table' then return end
DebugPrint('Received update for', businessName, '| playing:', state.playing, '| track:', state.trackIndex)
local oldState = BusinessState[businessName]
BusinessState[businessName] = state
if state.playing then
if not oldState or not oldState.playing or oldState.trackIndex ~= state.trackIndex then
CreateThread(function()
PlaySound(businessName)
end)
else
SetVolume(businessName, state.volume)
end
else
StopSound(businessName)
end
-- Update NUI if open
if OpenJukebox == businessName then
local biz = GetBusinessByName(businessName)
if not biz then return end
local playlist = GetPlaylistForBusiness(biz)
local trackIndex = state.trackIndex or 1
SendNUIMessage({
action = 'updateState',
playing = state.playing,
trackIndex = trackIndex,
trackName = playlist[trackIndex] or 'Unknown',
volume = state.volume or Config.DefaultVolume,
totalTracks = #playlist,
shuffle = state.shuffle or false,
})
end
end)
-- ============================================================================
-- JUKEBOX INTERACTION
-- ============================================================================
local function OpenJukeboxUI(businessName)
local biz = GetBusinessByName(businessName)
if not biz then return end
-- Prevent opening if already open
if OpenJukebox then return end
-- Job lock check
if biz.jobLock then
local PlayerData = QBCore.Functions.GetPlayerData()
if not PlayerData or not PlayerData.job or PlayerData.job.name ~= biz.jobLock then
QBCore.Functions.Notify('You don\'t have access to this jukebox.', 'error')
return
end
end
local state = BusinessState[businessName] or {
playing = false,
trackIndex = 1,
volume = biz.defaultVolume or Config.DefaultVolume,
shuffle = Config.ShuffleMode,
}
local playlist = GetPlaylistForBusiness(biz)
local trackIndex = state.trackIndex or 1
OpenJukebox = businessName
SetNuiFocus(true, true)
SendNUIMessage({
action = 'open',
businessLabel = biz.label,
playing = state.playing,
trackIndex = trackIndex,
trackName = playlist[trackIndex] or 'No tracks loaded',
volume = state.volume or Config.DefaultVolume,
totalTracks = #playlist,
playlist = playlist,
shuffle = state.shuffle or false,
})
DebugPrint('Opened jukebox UI for', businessName)
end
local function CloseJukeboxUI()
if not OpenJukebox then return end
SetNuiFocus(false, false)
SendNUIMessage({ action = 'close' })
OpenJukebox = nil
end
-- ============================================================================
-- NUI CALLBACKS
-- ============================================================================
RegisterNUICallback('close', function(_, cb)
CloseJukeboxUI()
cb('ok')
end)
RegisterNUICallback('togglePlay', function(_, cb)
if OpenJukebox then
TriggerServerEvent('djb:server:TogglePlay', OpenJukebox)
end
cb('ok')
end)
RegisterNUICallback('skip', function(_, cb)
if OpenJukebox then
TriggerServerEvent('djb:server:SkipTrack', OpenJukebox)
end
cb('ok')
end)
RegisterNUICallback('previous', function(_, cb)
if OpenJukebox then
TriggerServerEvent('djb:server:PrevTrack', OpenJukebox)
end
cb('ok')
end)
RegisterNUICallback('setVolume', function(data, cb)
if OpenJukebox then
local vol = tonumber(data.volume) or Config.DefaultVolume
vol = math.max(0.0, math.min(1.0, vol))
TriggerServerEvent('djb:server:SetVolume', OpenJukebox, vol)
end
cb('ok')
end)
RegisterNUICallback('selectTrack', function(data, cb)
if OpenJukebox then
local trackIndex = tonumber(data.trackIndex)
if trackIndex then
TriggerServerEvent('djb:server:SelectTrack', OpenJukebox, trackIndex)
end
end
cb('ok')
end)
RegisterNUICallback('toggleShuffle', function(_, cb)
if OpenJukebox then
TriggerServerEvent('djb:server:ToggleShuffle', OpenJukebox)
end
cb('ok')
end)
-- ============================================================================
-- PROP SPAWNING
-- ============================================================================
local function SpawnJukeboxProps()
if not Config.JukeboxProp then return end
local propHash = GetHashKey(Config.JukeboxProp)
for _, biz in ipairs(Config.Businesses) do
local coords = biz.jukeboxCoords
local heading = biz.jukeboxHeading or Config.JukeboxHeading or 0.0
if type(coords) == 'vector4' then
heading = coords.w
coords = vector3(coords.x, coords.y, coords.z)
end
RequestModel(propHash)
local timeout = 0
while not HasModelLoaded(propHash) and timeout < 50 do
Wait(100)
timeout = timeout + 1
end
if HasModelLoaded(propHash) then
local prop = CreateObject(propHash, coords.x, coords.y, coords.z, false, false, false)
SetEntityHeading(prop, heading)
FreezeEntityPosition(prop, true)
SetEntityAsMissionEntity(prop, true, true)
SpawnedProps[biz.name] = prop
DebugPrint('Spawned jukebox prop for', biz.name)
else
DebugPrint('ERROR: Failed to load jukebox model for', biz.name)
end
end
end
local function DeleteJukeboxProps()
for name, prop in pairs(SpawnedProps) do
if DoesEntityExist(prop) then
DeleteEntity(prop)
end
SpawnedProps[name] = nil
end
end
-- ============================================================================
-- TARGET / INTERACTION ZONES
-- ============================================================================
local function SetupTargets()
for _, biz in ipairs(Config.Businesses) do
local coords = biz.jukeboxCoords
if type(coords) == 'vector4' then
coords = vector3(coords.x, coords.y, coords.z)
end
if Config.UseTarget then
if Config.TargetResource == 'ox_target' then
exports.ox_target:addSphereZone({
coords = coords,
radius = Config.InteractionDistance,
debug = Config.Debug,
options = {
{
label = '🎵 ' .. biz.label .. ' Jukebox',
icon = 'fas fa-music',
onSelect = function()
OpenJukeboxUI(biz.name)
end,
}
}
})
else
exports['qb-target']:AddCircleZone(
'djb_' .. biz.name,
coords,
Config.InteractionDistance,
{
name = 'djb_' .. biz.name,
debugPoly = Config.Debug,
},
{
options = {
{
type = 'client',
icon = 'fas fa-music',
label = biz.label .. ' Jukebox',
action = function()
OpenJukeboxUI(biz.name)
end,
}
},
distance = Config.InteractionDistance + 1.0,
}
)
end
end
end
end
-- ============================================================================
-- DRAWTEXT FALLBACK (if not using target)
-- ============================================================================
local function DrawTextInteraction()
if Config.UseTarget then return end
CreateThread(function()
while true do
local sleep = 1000
local playerCoords = GetEntityCoords(PlayerPedId())
for _, biz in ipairs(Config.Businesses) do
local coords = biz.jukeboxCoords
if type(coords) == 'vector4' then
coords = vector3(coords.x, coords.y, coords.z)
end
local dist = #(playerCoords - coords)
if dist < Config.InteractionDistance + 2.0 then
sleep = 0
if dist < Config.InteractionDistance then
DrawText3D(coords.x, coords.y, coords.z + 1.0, '~g~[E]~w~ ' .. biz.label .. ' Jukebox')
if IsControlJustPressed(0, 38) then
OpenJukeboxUI(biz.name)
end
end
end
end
Wait(sleep)
end
end)
end
function DrawText3D(x, y, z, text)
SetTextScale(0.35, 0.35)
SetTextFont(4)
SetTextProportional(true)
SetTextColour(255, 255, 255, 215)
BeginTextCommandDisplayText('STRING')
SetTextCentre(true)
AddTextComponentSubstringPlayerName(text)
SetDrawOrigin(x, y, z, 0)
EndTextCommandDisplayText(0.0, 0.0)
ClearDrawOrigin()
end
-- ============================================================================
-- ESCAPE KEY TO CLOSE NUI
-- ============================================================================
CreateThread(function()
while true do
Wait(0)
if OpenJukebox then
DisableControlAction(0, 1, true)
DisableControlAction(0, 2, true)
DisableControlAction(0, 142, true)
DisableControlAction(0, 18, true)
DisableControlAction(0, 322, true)
DisableControlAction(0, 106, true)
if IsDisabledControlJustPressed(0, 177) then
CloseJukeboxUI()
end
else
Wait(500)
end
end
end)
-- ============================================================================
-- INITIALIZATION
-- ============================================================================
CreateThread(function()
while not LocalPlayer.state.isLoggedIn do
Wait(500)
end
Wait(2000)
SpawnJukeboxProps()
SetupTargets()
DrawTextInteraction()
TriggerServerEvent('djb:server:RequestState')
DebugPrint('Client initialized')
end)
AddEventHandler('onResourceStop', function(resource)
if resource ~= GetCurrentResourceName() then return end
DeleteJukeboxProps()
for _, biz in ipairs(Config.Businesses) do
StopSound(biz.name)
end
if OpenJukebox then
SetNuiFocus(false, false)
OpenJukebox = nil
end
end)
-- ============================================================================
-- COMMANDS
-- ============================================================================
RegisterCommand('jukeboxreload', function()
for _, biz in ipairs(Config.Businesses) do
StopSound(biz.name)
end
TriggerServerEvent('djb:server:RequestState')
QBCore.Functions.Notify('Jukebox state reloaded', 'success')
end, false)
RegisterCommand('jukeboxhelp', function()
TriggerEvent('chat:addMessage', { color = {83, 167, 234}, args = {'Jukebox', '--- Dynamic Jukebox Commands ---'}})
TriggerEvent('chat:addMessage', { color = {200, 200, 200}, args = {'Jukebox', '/jukeboxreload — Re-sync jukebox state from server'}})
TriggerEvent('chat:addMessage', { color = {200, 200, 200}, args = {'Jukebox', '/jukeboxhelp — Show this help message'}})
TriggerEvent('chat:addMessage', { color = {255, 200, 100}, args = {'Jukebox', 'Admin: /jukeboxstop — Stop all jukeboxes'}})
TriggerEvent('chat:addMessage', { color = {255, 200, 100}, args = {'Jukebox', 'Admin: /jukeboxstart — Start all jukeboxes'}})
TriggerEvent('chat:addMessage', { color = {255, 200, 100}, args = {'Jukebox', 'Admin: /jukeboxvol [business] [0-100] — Set volume'}})
end, false)