-
Notifications
You must be signed in to change notification settings - Fork 14
/
menu.lua
631 lines (579 loc) · 23.2 KB
/
menu.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
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
-- _
-- | |
-- _____ _| | _____ ___ _ __
-- / __\ \ /\ / / |/ / _ \/ _ \ '_ \
-- \__ \\ V V /| < __/ __/ |_) |
-- |___/ \_/\_/ |_|\_\___|\___| .__/
-- | |
-- |_|
-- https://github.com/swkeep
local QBCore = exports['qb-core']:GetCoreObject()
local keep_menu = {}
local Cachedata = nil
local currentVeh = {}
function Open_menu()
Config.menu = 'keep-menu'
if Config.menu == 'keep-menu' then
keep_menu:garage_menu()
return
end
end
AddEventHandler('keep-jobgarages:menu:open:garage_menu', function(option)
Open_menu()
end)
---------------------------------------------------- functions ------------------------------------------
local function isWhitelisted(currentgarage, model)
if type(model) == "number" then model = tostring(model) end
local list = Config.JobGarages[currentgarage].WhiteList
if not list then return end
for _, value in ipairs(list) do
if value.spawncode == model then
return true, value
end
end
return false, nil
end
local function get_vehicle(currentgarage, model)
local state, info = isWhitelisted(currentgarage, model)
if not state then
print('error: check the vehiclewhite list')
print('helper: vehicle that is saved in this is not in whitelist anymore')
print('helper: remove that vehicle in database or add it back to whitelist')
TriggerServerEvent('keep-jobgarages:server:Notification', 'Vehiclewhite error', 'error')
return '', ''
end
return info.label, info.icon
end
local function split(s, delimiter)
local result = {};
for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
table.insert(result, match);
end
return result;
end
local function is_restricted_by_grades(vehicle)
for key, value in pairs(vehicle.permissions.grades) do
if value then
return value
end
end
return false
end
local function check_grades(vehicle)
for key, value in pairs(vehicle.permissions.grades) do
if GetJobInfo().grade.level == tonumber(key) and value == true then
return true
end
end
return false
end
local function is_restricted_by_cid(vehicle)
if string.len(vehicle.permissions.cids) == 0 then
return false
end
return true
end
local function check_cids(vehicle)
local cids = split(vehicle.permissions.cids, ",")
for key, value in pairs(cids) do
if vehicle.current_player_id == value then
return true
end
end
return false
end
---------------------------------------------------- keep-menu ------------------------------------------
function keep_menu:garage_menu()
local Menu = {}
Menu = {
{
header = 'Shared Garage',
subheader = 'Current: ' .. Config.JobGarages[GetCurrentgarage()].label,
icon = 'fa-solid fa-car-on',
disabled = true
},
{
header = 'Shared Vehicles',
subheader = 'List of shared vehicles',
icon = 'fa-solid fa-car',
args = { 2 },
action = function(args)
local currentgarage = GetCurrentgarage()
TriggerCallback('keep-jobgarages:server:fetch_categories', function(result)
Cachedata = result
keep_menu:categories(result)
end, {
garage = currentgarage
})
end,
submenu = true,
},
{
header = 'Leave',
event = 'keep-menu:closeMenu',
leave = true
},
}
exports['keep-menu']:createMenu(Menu)
end
function keep_menu:categories(data)
local Menu = {}
Menu = {
{
header = "Go Back",
args = { 0 },
action = function()
keep_menu:garage_menu()
end,
back = true
},
{
header = 'Leave',
event = "keep-menu:closeMenu",
leave = true
},
}
local currentgarage = GetCurrentgarage()
for _, DISTINCT in pairs(data.DISTINCT) do
local lebel, icon = get_vehicle(currentgarage, DISTINCT.model)
Menu[#Menu + 1] = {
header = lebel,
subheader = #data[DISTINCT.model] .. " Vehicles",
icon = icon,
args = { DISTINCT.model, lebel, icon },
action = function(args)
keep_menu:vehicles_inside_category(args[1], args[2], args[3])
end
}
end
exports['keep-menu']:createMenu(Menu)
end
function keep_menu:vehicles_inside_category(model, lebel, icon)
local Menu = {
{
header = "Go Back",
args = { 0 },
action = function()
keep_menu:categories(Cachedata)
end,
back = true
},
}
for k, vehicle in pairs(Cachedata[model]) do
local state = 'out'
if vehicle.state == true then state = 'In' end
Menu[#Menu + 1] = {
header = 'Name: ' .. vehicle.name,
subheader = 'Model: ' .. lebel,
footer = string.format('Plate: %s | State: %s', vehicle.plate, state),
icon = icon,
args = {
{
type = 'vehicle_actions_menu',
model = model, -- local data
lebel = lebel,
key = k,
icon = icon
}
},
action = function(args)
keep_menu:vehicle_actions_menu(args[1])
end
}
end
exports['keep-menu']:createMenu(Menu)
end
function keep_menu:take_out_menu(data, vehicle, veh, per)
local engine = math.floor(vehicle.engine / 10)
local body = math.floor(vehicle.body / 10)
local fuel = vehicle.fuel
local status = "Engine: " .. engine .. " | Body: " .. body .. " | fuel: " .. fuel
local Menu = {
{
header = "Go Back",
icon = 'fa-solid fa-angle-left',
args = {
{
model = data.model,
veh = veh,
plate = vehicle.plate
}
},
action = function(args)
TriggerServerEvent('keep-jobgarages:server:update_out_vehicles', {
type = 'remove',
plate = args[1].plate
})
QBCore.Functions.DeleteVehicle(args[1].veh)
while DoesEntityExist(args[1].veh) do Wait(50) end
local lebel, icon = get_vehicle(GetCurrentgarage(), args[1].model)
keep_menu:vehicles_inside_category(args[1].model, lebel, icon)
end
},
{
header = 'Take Out Vehicle',
icon = 'fa-solid fa-arrow-right-from-bracket',
args = {
{
vehicle = veh,
fuel = fuel,
engine = engine,
body = body,
plate = vehicle.plate,
}
},
action = function()
ResetEntityAlpha(veh)
FreezeEntityPosition(veh, false)
SetEntityCollision(veh, true, true)
SetEntityAsMissionEntity(dveh, true, true)
TriggerEvent("vehiclekeys:client:SetOwner", vehicle.plate)
exports[Config.fuel_script]:SetFuel(veh, fuel)
TriggerCallback('keep-jobgarages:server:give_keys_to_all_same_job', function(receivers)
for _, id in pairs(receivers) do
TriggerServerEvent('qb-vehiclekeys:server:GiveVehicleKeys', id, vehicle.plate)
end
end, PlayerJob)
TriggerServerEvent('keep-jobgarages:server:update_state', vehicle.plate, {
vehicle = veh,
fuel = fuel,
engine = engine,
body = body,
plate = vehicle.plate,
})
end
},
{
header = 'Vehicle Status',
icon = 'fa-solid fa-car-battery',
subheader = status,
disabled = true
},
{
header = 'Vehicle Parking Log',
icon = 'fa-solid fa-clipboard-question',
args = { { plate = vehicle.plate, data = data, vehicle = vehicle, veh = veh } },
action = function(args)
keep_menu:vehicle_parking_log(args[1], per)
end
},
}
if per then
Menu[#Menu + 1] = {
header = 'Duplicate The Vehicle',
type = 'server',
event = 'keep-jobgarages:server:dupe',
icon = 'fa-solid fa-clone',
args = { vehicle.plate }
}
Menu[#Menu + 1] = {
header = "Modify Vehicle's Name",
icon = 'fa-solid fa-pen-to-square',
action = function()
local Input = {
inputs = {
{
type = 'text',
isRequired = true,
name = 'vehicle_name',
text = "What vehicle's name will be?",
icon = 'fa-solid fa-money-bill-trend-up',
title = 'Vehicle Name',
},
}
}
local inputData, reason = exports['keep-input']:ShowInput(Input)
if reason == 'submit' then
if not inputData.vehicle_name then return end
TriggerServerEvent('keep-jobgarages:server:update_vehicle_name', inputData.vehicle_name,
vehicle.plate)
end
end
}
Menu[#Menu + 1] = {
header = "Modify Vehicle's Plate",
icon = 'fa-solid fa-pen-to-square',
action = function()
local Input = {
inputs = {
{
type = 'text',
isRequired = true,
name = 'vehicle_plate',
text = "What vehicle's name will be?",
icon = 'fa-solid fa-money-bill-trend-up',
title = 'Vehicle Plate',
force_value = vehicle.plate,
},
}
}
local inputData, reason = exports['keep-input']:ShowInput(Input)
if reason == 'submit' then
if not inputData.vehicle_plate then return end
TriggerServerEvent('keep-jobgarages:server:update_vehicle_plate', inputData.vehicle_plate,
vehicle.plate)
end
end
}
Menu[#Menu + 1] = {
header = "Modify Vehicle's Customizability",
icon = 'fa-solid fa-pen-to-square',
action = function()
local Input = {
inputs = {
{
isRequired = true,
name = 'customizability',
title = "Modify Vehicle's Customizability",
force_value = vehicle.plate,
type = "radio",
options = {
{ value = true, text = 'Yes' },
{ value = false, text = 'No' },
},
},
}
}
local inputData, reason = exports['keep-input']:ShowInput(Input)
if reason == 'submit' then
if not inputData.customizability then return end
TriggerServerEvent('keep-jobgarages:server:set_is_customizable', inputData.customizability,
vehicle.plate)
end
end
}
Menu[#Menu + 1] = {
header = "Delete Vehicle",
icon = 'fa-solid fa-pen-to-square',
action = function()
local Input = {
inputs = {
{
isRequired = true,
name = 'delete',
title = "Delete Vehicle",
force_value = vehicle.plate,
type = "radio",
options = {
{ value = true, text = 'Yes' },
{ value = false, text = 'No' },
},
},
}
}
local inputData, reason = exports['keep-input']:ShowInput(Input)
if reason == 'submit' then
if not inputData.delete then return end
if inputData.delete == 'true' then
TriggerServerEvent('keep-jobgarages:server:delete', vehicle.plate)
end
end
end
}
end
local op = exports['keep-menu']:createMenu(Menu)
if not op or op == vehicle.plate then
if op == vehicle.plate then
TriggerServerEvent('keep-jobgarages:server:dupe', vehicle.plate)
end
TriggerServerEvent('keep-jobgarages:server:update_out_vehicles', {
type = 'remove',
plate = vehicle.plate
})
QBCore.Functions.DeleteVehicle(veh)
end
end
function keep_menu:vehicle_actions_menu(data)
local vehicle = Cachedata[data.model][data.key]
local nearspawnpoint = GetNearspawnpoint()
local currentgarage = GetCurrentgarage()
if is_restricted_by_cid(vehicle) then
if check_cids(vehicle) == false then
TriggerServerEvent('keep-jobgarages:server:Notification', 'This vehicle is not allowed for you', 'error')
keep_menu:vehicles_inside_category(data.model, data.lebel, data.icon)
return
end
end
if is_restricted_by_grades(vehicle) then
if check_grades(vehicle) == false then
TriggerServerEvent('keep-jobgarages:server:Notification',
'This vehicle is not allowed for you current rank', 'error')
keep_menu:vehicles_inside_category(data.model, data.lebel, data.icon)
return
end
end
if vehicle.state == 0 then
TriggerServerEvent('keep-jobgarages:server:Notification', 'Vehicle is already out!', 'error')
keep_menu:vehicles_inside_category(data.model, data.lebel, data.icon)
return
end
TriggerCallback('keep-jobgarages:server:doesVehicleExist', function(result, per)
if result == true then
TriggerServerEvent('keep-jobgarages:server:Notification', 'Vehicle is already out!', 'error')
TriggerEvent('keep-jobgarages:menu:open:vehicles_list', {
type = 'vehicles_inside_category',
model = data.model -- local data
})
return
end
if not currentgarage or not nearspawnpoint then
TriggerServerEvent('keep-jobgarages:server:Notification', 'Try to find a free spot!', 'error')
return
end
QBCore.Functions.SpawnVehicle(data.model, function(veh)
SetEntityAlpha(veh, 100, true)
QBCore.Functions.SetVehicleProperties(veh, vehicle.mods)
currentVeh = {
veh = veh,
plate = vehicle.plate
}
SetVehicleNumberPlateText(veh, vehicle.plate)
if veh and vehicle.plate then
SetNetworkIdAlwaysExistsForPlayer(NetworkGetNetworkIdFromEntity(veh), PlayerPedId(), true)
end
SetEntityHeading(veh, Config.JobGarages[currentgarage].spawnPoint[nearspawnpoint].w)
PlaceObjectOnGroundProperly(veh)
FreezeEntityPosition(veh, true)
-- add it to serverside list
TriggerServerEvent('keep-jobgarages:server:update_out_vehicles', {
type = 'add',
netId = NetworkGetNetworkIdFromEntity(veh),
plate = vehicle.plate
})
RecoverVehicleDamages(veh, vehicle)
keep_menu:take_out_menu(data, vehicle, veh, per)
end, Config.JobGarages[currentgarage].spawnPoint[nearspawnpoint], true)
end, vehicle.plate)
end
function keep_menu:vehicle_parking_log(data, per)
local Menu = {
-- {
-- header = "Go Back",
-- icon = 'fa-solid fa-angle-left',
-- action = function()
-- keep_menu:take_out_menu(data.data, data.vehicle, data.veh, per)
-- end
-- },
}
TriggerCallback('keep-jobgarages:server:get_vehicle_log', function(LOGS)
local icons = {
retrive = 'fa-solid fa-arrow-right-arrow-left',
store = 'fa-solid fa-arrow-right-to-bracket',
out = 'fa-solid fa-arrow-right-from-bracket'
}
if type(LOGS) == "table" then
for _, log in pairs(LOGS) do
local header = log.action
local _data = json.decode(log.data)
local sub_header = "Name: " .. _data.charinfo.firstname .. " " .. _data.charinfo.lastname
Menu[#Menu + 1] = {
header = header,
subheader = sub_header,
footer = log.Action_timestamp,
icon = icons[log.action],
disabled = true
}
end
local op = exports['keep-menu']:createMenu(Menu)
if not op then
TriggerServerEvent('keep-jobgarages:server:update_out_vehicles', {
type = 'remove',
plate = data.plate
})
QBCore.Functions.DeleteVehicle(data.veh)
end
end
end, { plate = data.plate })
end
AddEventHandler('keep-jobgarages:client:take_out', function(data)
data = data[1]
local plate = QBCore.Functions.GetPlate(data.vehicle)
ResetEntityAlpha(data.vehicle)
FreezeEntityPosition(data.vehicle, false)
SetEntityCollision(veh, true, true)
SetEntityAsMissionEntity(data.vehicle, true, true)
TriggerEvent("vehiclekeys:client:SetOwner", plate)
exports[Config.fuel_script]:SetFuel(data.vehicle, data.fuel)
TriggerCallback('keep-jobgarages:server:give_keys_to_all_same_job', function(receivers)
for _, id in pairs(receivers) do
TriggerServerEvent('qb-vehiclekeys:server:GiveVehicleKeys', id, plate)
end
end, PlayerJob)
TriggerServerEvent('keep-jobgarages:server:update_state', data.plate, data)
end)
AddEventHandler('keep-jobgarages:client:delete_if_exist', function(plate, veh)
if not plate and not veh then
plate = currentVeh.plate
veh = currentVeh.veh
end
TriggerServerEvent('keep-jobgarages:server:update_out_vehicles', {
type = 'remove',
plate = plate
})
QBCore.Functions.DeleteVehicle(veh)
end)
AddEventHandler('keep-jobgarages:client:keep_put_back_to_garage', function(e)
local plyped = PlayerPedId()
local IsInVehicle = IsPedInAnyVehicle(plyped, false)
local playercoord = GetEntityCoords(plyped)
local veh = nil
if IsInVehicle then
veh = GetVehiclePedIsIn(plyped, false)
else
local vehcheck, distance = QBCore.Functions.GetClosestVehicle(playercoord)
if distance < 3.0 then
if vehcheck ~= nil and NetworkGetEntityIsNetworked(vehcheck) and DoesEntityExist(vehcheck) and
IsEntityAVehicle(vehcheck) then
veh = vehcheck
end
else
veh = nil
end
end
if veh == nil then
local inGarageStation = GetInGarageStation()
if CanPlayerUseGarage() and not IsPauseMenuActive() and inGarageStation then
-- store vehicle
if IsPedInAnyVehicle(plyped, false) then
TriggerEvent('keep-jobgarages:client:keep_put_back_to_garage')
return
end
Open_menu()
end
return
end
if IsInVehicle then
TaskLeaveVehicle(plyped, veh, 0)
while IsPedInAnyVehicle(plyped, false) do
Wait(100)
end
Wait(500)
end
local VehicleProperties = {}
VehicleProperties.metadata = GetVehicleDamages(veh)
VehicleProperties.VehicleProperties = QBCore.Functions.GetVehicleProperties(veh)
TriggerCallback('keep-jobgarages:server:can_we_store_this_vehicle', function(result)
if result ~= nil then
local currentgarage = GetCurrentgarage()
VehicleProperties.currentgarage = currentgarage
TriggerServerEvent('keep-jobgarages:server:update_state', VehicleProperties.VehicleProperties.plate,
VehicleProperties)
TriggerEvent('keep-jobgarages:client:delete_if_exist', VehicleProperties.VehicleProperties.plate, veh)
return
end
TriggerServerEvent('keep-jobgarages:server:Notification', 'You can not store this vehicle', 'error')
end, VehicleProperties)
end)
RegisterKeyMapping('+garage_menu', 'garage_menu', 'keyboard', 'u')
RegisterCommand('+garage_menu', function()
local plyped = PlayerPedId()
local inGarageStation = GetInGarageStation()
if CanPlayerUseGarage() and not IsPauseMenuActive() and inGarageStation then
-- store vehicle
if IsPedInAnyVehicle(plyped, false) then
TriggerEvent('keep-jobgarages:client:keep_put_back_to_garage')
return
end
Open_menu()
end
end, false)