forked from overextended/ox_inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
1132 lines (975 loc) · 36.6 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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local DisableControlActions = import 'controls'
local Items = client.items
local Utils = client.utils
local currentWeapon
RegisterNetEvent('ox_inventory:disarm', function(newSlot)
currentWeapon = Utils.Disarm(currentWeapon, newSlot)
end)
RegisterNetEvent('ox_inventory:clearWeapons', function()
Utils.ClearWeapons(currentWeapon)
end)
local StashTarget
exports('setStashTarget', function(id, owner)
StashTarget = id and {id=id, owner=owner}
end)
local invBusy = true
local invOpen = false
local function CanOpenInventory()
return PlayerData.loaded
and not invBusy
and not PlayerData.dead
and not GetPedConfigFlag(PlayerData.ped, 120, true)
and (currentWeapon == nil or currentWeapon.timer == 0)
and not IsPauseMenuActive()
and not IsPedFatallyInjured(PlayerData.ped)
and invOpen ~= nil
end
local defaultInventory = {
type = 'newdrop',
slots = shared.playerslots,
weight = 0,
maxWeight = shared.playerweight,
items = {}
}
local currentInventory = defaultInventory
local function CloseTrunk()
if currentInventory?.type == 'trunk' then
local coords = GetEntityCoords(PlayerData.ped, true)
Utils.PlayAnimAdvanced(900, 'anim@heists@fleeca_bank@scope_out@return_case', 'trevor_action', coords.x, coords.y, coords.z, 0.0, 0.0, GetEntityHeading(PlayerData.ped), 2.0, 2.0, 1000, 49, 0.25)
CreateThread(function()
local entity = currentInventory.entity
local door = currentInventory.door
Wait(900)
SetVehicleDoorShut(entity, door, false)
end)
end
end
local ServerCallback = import 'callbacks'
local Interface = client.interface
local plyState = LocalPlayer.state
local tick
local interval
local function OpenInventory(inv, data)
if invOpen then
if not inv and currentInventory.type == 'newdrop' then
return TriggerEvent('ox_inventory:closeInventory')
end
if inv == 'container' and currentInventory.id == PlayerData.inventory[data].metadata.container then
return TriggerEvent('ox_inventory:closeInventory')
end
if currentInventory.type == 'drop' and (not data or currentInventory.id == (type(data) == 'table' and data.id or data)) then
return TriggerEvent('ox_inventory:closeInventory')
end
end
if CanOpenInventory() then
local left, right
if inv == 'shop' and invOpen == false then
left, right = ServerCallback.Await(shared.resource, 'openShop', 200, data)
elseif invOpen ~= nil then
if inv == 'policeevidence' then
local input = Interface.Keyboard(shared.locale('police_evidence'), {shared.locale('locker_number')})
if input then
input = tonumber(input[1])
else
return Utils.Notify({text = shared.locale('locker_no_value'), type = 'error'})
end
if type(input) ~= 'number' then
return Utils.Notify({text = shared.locale('locker_must_number'), type = 'error'})
else
data = input
end
end
left, right = ServerCallback.Await(shared.resource, 'openInventory', false, inv, data)
end
if left then
if inv ~= 'trunk' and not IsPedInAnyVehicle(PlayerData.ped, false) then
Utils.PlayAnim(1000, 'pickup_object', 'putdown_low', 5.0, 1.5, -1, 48, 0.0, 0, 0, 0)
end
plyState.invOpen = true
SetInterval(interval, 100)
SetNuiFocus(true, true)
SetNuiFocusKeepInput(true)
if client.screenblur then TriggerScreenblurFadeIn(0) end
CloseTrunk()
currentInventory = right or defaultInventory
left.items = PlayerData.inventory
SendNUIMessage({
action = 'setupInventory',
data = {
leftInventory = left,
rightInventory = currentInventory
}
})
if not currentInventory.coords and not inv == 'container' then
currentInventory.coords = GetEntityCoords(PlayerData.ped)
end
-- Stash exists (useful for custom stashes)
return true
else
-- Stash does not exist
if left == false then return false end
if invOpen == false then Utils.Notify({type = 'error', text = shared.locale('inventory_right_access'), duration = 2500}) end
if invOpen then TriggerEvent('ox_inventory:closeInventory') end
end
elseif invBusy then Utils.Notify({type = 'error', text = shared.locale('inventory_player_access'), duration = 2500}) end
end
RegisterNetEvent('ox_inventory:openInventory', OpenInventory)
exports('openInventory', OpenInventory)
local function useItem(data, cb)
if invOpen and data.close then TriggerEvent('ox_inventory:closeInventory') end
if not invBusy and not Interface.ProgressActive and not IsPedRagdoll(PlayerData.ped) and not IsPedFalling(PlayerData.ped) then
if currentWeapon and currentWeapon?.timer > 100 then return end
invBusy = true
local result = ServerCallback.Await(shared.resource, 'useItem', 200, data.name, data.slot, PlayerData.inventory[data.slot].metadata)
if not result then
Wait(500)
invBusy = false
return
end
local p
if result and invBusy then
plyState.invBusy = true
if data.client then data = data.client end
if data.usetime then
p = promise.new()
Interface.Progress({
duration = data.usetime,
label = data.label or shared.locale('using', result.label),
useWhileDead = data.useWhileDead or false,
canCancel = data.cancel or false,
disable = data.disable,
anim = data.anim or data.scenario,
prop = data.prop,
propTwo = data.propTwo
}, function(cancel)
p:resolve(cancel)
end)
end
if p then Citizen.Await(p) end
if not p or not p.value then
if result.consume and result.consume ~= 0 then
TriggerServerEvent('ox_inventory:removeItem', result.name, result.consume, result.metadata, result.slot, true)
end
if data.status then
for k, v in pairs(data.status) do
if v > 0 then TriggerEvent('esx_status:add', k, v) else TriggerEvent('esx_status:remove', k, -v) end
end
end
if data.notification then
Utils.Notify({text = data.notification})
end
if cb then cb(result) end
Wait(500)
plyState.invBusy = false
return
end
end
end
if cb then cb(false) end
Wait(200)
plyState.invBusy = false
end
AddEventHandler('ox_inventory:item', useItem)
exports('useItem', useItem)
local function useSlot(slot)
if PlayerData.loaded and not invBusy and not Interface.ProgressActive then
local item = PlayerData.inventory[slot]
if not item then return end
local data = item and Items[item.name]
if not data or not data.usable then return end
if data.name:find('at_') and not currentWeapon then Utils.Notify({type = 'error', text = shared.locale('weapon_hand_required')}) return end
data.slot = slot
if item.metadata.container then
return OpenInventory('container', item.slot)
elseif data.client then
if invOpen and data.close then TriggerEvent('ox_inventory:closeInventory') end
if data.client.export then
if type(data.client.export) ~= 'function' then
local resource, fn = string.strsplit('.', data.client.export)
data.client.export = exports[resource][fn]
end
return data.client.export(0, data, {name = item.name, slot = item.slot, metadata = item.metadata})
elseif data.client.event then -- deprecated, to be removed
return print(('unable to trigger event for %s, data.client.event has been removed. utilise exports instead.'):format(item.name))
end
end
if data.effect then
data:effect({name = item.name, slot = item.slot, metadata = item.metadata})
elseif item.name:find('WEAPON_') then
useItem(data, function(result)
if result then
if currentWeapon?.slot == result.slot then
currentWeapon = Utils.Disarm(currentWeapon)
return
end
local playerPed = PlayerData.ped
ClearPedSecondaryTask(playerPed)
if data.throwable then item.throwable = true end
if currentWeapon then currentWeapon = Utils.Disarm(currentWeapon) end
local sleep = (client.hasGroup(shared.police) and (GetWeapontypeGroup(data.hash) == 416676503 or GetWeapontypeGroup(data.hash) == 690389602)) and 400 or 1200
local coords = GetEntityCoords(playerPed, true)
if item.name == 'WEAPON_SWITCHBLADE' then
Utils.PlayAnimAdvanced(sleep*2, 'anim@melee@switchblade@holster', 'unholster', coords.x, coords.y, coords.z, 0, 0, GetEntityHeading(playerPed), 8.0, 3.0, -1, 48, 0.1)
Wait(100)
else
Utils.PlayAnimAdvanced(sleep*2, sleep == 400 and 'reaction@intimidation@cop@unarmed' or 'reaction@intimidation@1h', 'intro', coords.x, coords.y, coords.z, 0, 0, GetEntityHeading(playerPed), 8.0, 3.0, -1, 50, 0.1)
Wait(sleep)
end
SetPedAmmo(playerPed, data.hash, 0)
GiveWeaponToPed(playerPed, data.hash, 0, false, true)
if item.metadata.tint then SetPedWeaponTintIndex(playerPed, data.hash, item.metadata.tint) end
if item.metadata.components then
for i = 1, #item.metadata.components do
local components = Items[item.metadata.components[i]].client.component
for v=1, #components do
local component = components[v]
if DoesWeaponTakeWeaponComponent(data.hash, component) then
if not HasPedGotWeaponComponent(playerPed, data.hash, component) then
GiveWeaponComponentToPed(playerPed, data.hash, component)
end
end
end
end
end
item.hash = data.hash
item.ammo = data.ammoname
item.melee = (not item.throwable and not data.ammoname) and 0
item.timer = 0
SetCurrentPedWeapon(playerPed, data.hash, true)
SetPedCurrentWeaponVisible(playerPed, true, false, false, false)
AddAmmoToPed(playerPed, data.hash, item.metadata.ammo or 100)
Wait(0)
RefillAmmoInstantly(playerPed)
if data.name == 'WEAPON_PETROLCAN' or data.name == 'WEAPON_HAZARDCAN' or data.name == 'WEAPON_FIREEXTINGUISHER' then
item.metadata.ammo = item.metadata.durability
SetPedInfiniteAmmo(playerPed, true, data.hash)
end
currentWeapon = item
TriggerEvent('ox_inventory:currentWeapon', item)
Utils.ItemNotify({item.label, item.name, shared.locale('equipped')})
Wait(sleep)
ClearPedSecondaryTask(playerPed)
end
end)
elseif currentWeapon then
local playerPed = PlayerData.ped
if item.name:find('ammo-') then
local maxAmmo = GetMaxAmmoInClip(playerPed, currentWeapon.hash, true)
local currentAmmo = GetAmmoInPedWeapon(playerPed, currentWeapon.hash)
if currentAmmo ~= maxAmmo and currentAmmo < maxAmmo then
useItem(data, function(data)
if data then
if data.name == currentWeapon.ammo then
local missingAmmo = 0
local newAmmo = 0
missingAmmo = maxAmmo - currentAmmo
if missingAmmo > data.count then newAmmo = currentAmmo + data.count else newAmmo = maxAmmo end
if newAmmo < 0 then newAmmo = 0 end
SetPedAmmo(playerPed, currentWeapon.hash, newAmmo)
MakePedReload(playerPed)
currentWeapon.metadata.ammo = newAmmo
TriggerServerEvent('ox_inventory:updateWeapon', 'load', currentWeapon.metadata.ammo)
end
end
end)
end
elseif item.name:find('at_') then
local components = data.client.component
local componentType = data.type
local weaponComponents = PlayerData.inventory[currentWeapon.slot].metadata.components
-- Checks if the weapon already has the same component type attached
for componentIndex = 1, #weaponComponents do
if componentType == Items[weaponComponents[componentIndex]].type then
-- todo: Update locale?
return Utils.Notify({type = 'error', text = shared.locale('component_has', data.label)})
end
end
for i = 1, #components do
local component = components[i]
if DoesWeaponTakeWeaponComponent(currentWeapon.hash, component) then
if HasPedGotWeaponComponent(playerPed, currentWeapon.hash, component) then
Utils.Notify({type = 'error', text = shared.locale('component_has', data.label)})
else
useItem(data, function(data)
if data then
GiveWeaponComponentToPed(playerPed, currentWeapon.hash, component)
table.insert(PlayerData.inventory[currentWeapon.slot].metadata.components, data.name)
end
end)
end
return
end
end
Utils.Notify({type = 'error', text = shared.locale('component_invalid', data.label) })
elseif data.allowArmed then
useItem(data)
end
else
useItem(data)
end
end
end
exports('useSlot', useSlot)
local function CanOpenTarget(ped)
return IsPedFatallyInjured(ped)
or IsEntityPlayingAnim(ped, 'dead', 'dead_a', 3)
or GetPedConfigFlag(ped, 120, true)
or IsEntityPlayingAnim(ped, 'mp_arresting', 'idle', 3)
or IsEntityPlayingAnim(ped, 'missminuteman_1ig_2', 'handsup_base', 3)
or IsEntityPlayingAnim(ped, 'missminuteman_1ig_2', 'handsup_enter', 3)
or IsEntityPlayingAnim(ped, 'random@mugging3', 'handsup_standing_base', 3)
end
local function OpenNearbyInventory()
if CanOpenInventory() then
local closestPlayer = Utils.GetClosestPlayer()
if closestPlayer.x < 2 and (client.hasGroup(shared.police) or CanOpenTarget(closestPlayer.z)) then
Utils.PlayAnim(2000, 'mp_common', 'givetake1_a', 1.0, 1.0, -1, 50, 0.0, 0, 0, 0)
OpenInventory('player', GetPlayerServerId(closestPlayer.y))
end
end
end
exports('openNearbyInventory', OpenNearbyInventory)
local currentInstance
local nearbyMarkers, closestMarker = {}, {}
local drops, playerCoords
local function Markers(tb, type, rgb, name)
if tb then
for k, v in pairs(tb) do
if not v.instance or v.instance == currentInstance then
if not v.groups or client.hasGroup(v.groups) then
local coords = v.coords or v
local distance = #(playerCoords - coords)
local id = name and type..name..k or type..k
local marker = nearbyMarkers[id]
if distance < 1.2 then
if not marker then nearbyMarkers[id] = mat(vec3(coords), vec3(rgb)) end
if closestMarker[1] == nil or (closestMarker and distance < closestMarker[1]) then
closestMarker[1] = distance
closestMarker[2] = k
closestMarker[3] = type
closestMarker[4] = name or v.name
end
elseif not marker and distance < 8 then
nearbyMarkers[id] = mat(vec3(coords), vec3(rgb))
elseif marker and distance > 8 then
nearbyMarkers[id] = nil
end
end
end
end
end
end
local table = import 'table'
local Shops = client.shops
local Inventory = client.inventory
function OnPlayerData(key, val)
if key == 'groups' then
Shops()
if shared.qtarget then
Inventory.Stashes()
Inventory.Evidence()
end
table.wipe(nearbyMarkers)
elseif key == 'dead' and val then
currentWeapon = Utils.Disarm(currentWeapon)
TriggerEvent('ox_inventory:closeInventory')
Wait(50)
end
SetWeaponsNoAutoswap(1)
SetWeaponsNoAutoreload(1)
end
local function RegisterCommands()
RegisterCommand('inv', function()
if closestMarker[1] and closestMarker[3] ~= 'license' and closestMarker[3] ~= 'policeevidence' then
OpenInventory(closestMarker[3], {id=closestMarker[2], type=closestMarker[4]})
else OpenInventory() end
end)
RegisterKeyMapping('inv', shared.locale('open_player_inventory'), 'keyboard', client.keys[1])
TriggerEvent('chat:removeSuggestion', '/inv')
local Vehicles = data 'vehicles'
RegisterCommand('inv2', function()
if not invOpen then
if invBusy then return Utils.Notify({type = 'error', text = shared.locale('inventory_player_access'), duration = 2500})
else
if not CanOpenInventory() then return Utils.Notify({type = 'error', text = shared.locale('inventory_player_access'), duration = 2500}) end
if StashTarget then
OpenInventory('stash', StashTarget)
elseif IsPedInAnyVehicle(PlayerData.ped, false) then
local vehicle = GetVehiclePedIsIn(PlayerData.ped, false)
if NetworkGetEntityIsNetworked(vehicle) then
local checkVehicle = Vehicles.Storage[GetEntityModel(vehicle)]
if checkVehicle == 0 or checkVehicle == 2 then -- No storage or no glovebox
return
end
local plate = client.trimplate and string.strtrim(GetVehicleNumberPlateText(vehicle)) or GetVehicleNumberPlateText(vehicle)
OpenInventory('glovebox', {id='glove'..plate, class=GetVehicleClass(vehicle), model=GetEntityModel(vehicle)})
while true do
Wait(100)
if not invOpen then break
elseif not IsPedInAnyVehicle(PlayerData.ped, false) then
TriggerEvent('ox_inventory:closeInventory')
break
end
end
end
else
local entity, type = Utils.Raycast()
if not entity then return end
local vehicle, position
if not shared.qtarget then
if type == 2 then vehicle, position = entity, GetEntityCoords(entity)
elseif type == 3 and table.contains(Inventory.Dumpsters, GetEntityModel(entity)) then
local netId = NetworkGetEntityIsNetworked(entity) and NetworkGetNetworkIdFromEntity(entity)
if not netId then
NetworkRegisterEntityAsNetworked(entity)
SetEntityAsMissionEntity(entity)
netId = NetworkGetNetworkIdFromEntity(entity)
NetworkUseHighPrecisionBlending(netId, false)
SetNetworkIdExistsOnAllMachines(netId, true)
SetNetworkIdCanMigrate(netId, true)
end
return OpenInventory('dumpster', 'dumpster'..netId)
end
elseif type == 2 then
vehicle, position = entity, GetEntityCoords(entity)
else return end
local lastVehicle = nil
local class = GetVehicleClass(vehicle)
local vehHash = GetEntityModel(vehicle)
if vehicle and Vehicles.trunk['models'][vehHash] or Vehicles.trunk[class] and #(playerCoords - position) < 6 and NetworkGetEntityIsNetworked(vehicle) then
local locked = GetVehicleDoorLockStatus(vehicle)
if locked == 0 or locked == 1 then
local checkVehicle = Vehicles.Storage[vehHash]
local open, vehBone
if checkVehicle == nil then -- No data, normal trunk
open, vehBone = 5, GetEntityBoneIndexByName(vehicle, 'boot')
elseif checkVehicle == 3 then -- Trunk in hood
open, vehBone = 4, GetEntityBoneIndexByName(vehicle, 'bonnet')
else -- No storage or no trunk
return
end
if vehBone == -1 then vehBone = GetEntityBoneIndexByName(vehicle, Vehicles.trunk.boneIndex[vehHash] or 'platelight') end
position = GetWorldPositionOfEntityBone(vehicle, vehBone)
local distance = #(playerCoords - position)
local closeToVehicle = distance < 2 and (open == 5 and (checkVehicle == nil and true or 2) or open == 4)
if closeToVehicle then
local plate = client.trimplate and string.strtrim(GetVehicleNumberPlateText(vehicle)) or GetVehicleNumberPlateText(vehicle)
TaskTurnPedToFaceCoord(PlayerData.ped, position.x, position.y, position.z)
lastVehicle = vehicle
OpenInventory('trunk', {id='trunk'..plate, class=class, model=vehHash})
local timeout = 20
repeat Wait(50)
timeout -= 1
until (currentInventory and currentInventory.type == 'trunk') or timeout == 0
if timeout == 0 then
closeToVehicle, lastVehicle = false, nil
return
end
SetVehicleDoorOpen(vehicle, open, false, false)
Wait(200)
Utils.PlayAnim(0, 'anim@heists@prison_heiststation@cop_reactions', 'cop_b_idle', 3.0, 3.0, -1, 49, 0.0, 0, 0, 0)
currentInventory.entity = lastVehicle
currentInventory.door = open
while true do
Wait(50)
if closeToVehicle and invOpen then
position = GetWorldPositionOfEntityBone(vehicle, vehBone)
if #(GetEntityCoords(PlayerData.ped) - position) >= 2 or not DoesEntityExist(vehicle) then
break
else TaskTurnPedToFaceCoord(PlayerData.ped, position.x, position.y, position.z) end
else break end
end
if lastVehicle then TriggerEvent('ox_inventory:closeInventory') end
end
else Utils.Notify({type = 'error', text = shared.locale('vehicle_locked'), duration = 2500}) end
end
end
end
else return TriggerEvent('ox_inventory:closeInventory')
end
end)
RegisterKeyMapping('inv2', shared.locale('open_secondary_inventory'), 'keyboard', client.keys[2])
TriggerEvent('chat:removeSuggestion', '/inv2')
RegisterCommand('reload', function()
if currentWeapon?.ammo then
local ammo = Inventory.Search(1, currentWeapon.ammo)
if ammo[1] then useSlot(ammo[1].slot) end
end
end)
RegisterKeyMapping('reload', shared.locale('reload_weapon'), 'keyboard', 'r')
TriggerEvent('chat:removeSuggestion', '/reload')
RegisterCommand('hotbar', function()
if not IsPauseMenuActive() then
SendNUIMessage({ action = 'toggleHotbar' })
end
end)
RegisterKeyMapping('hotbar', shared.locale('disable_hotbar'), 'keyboard', client.keys[3])
TriggerEvent('chat:removeSuggestion', '/hotbar')
RegisterCommand('steal', function()
OpenNearbyInventory()
end)
for i = 1, 5 do
local hotkey = ('hotkey%s'):format(i)
RegisterCommand(hotkey, function() if not invOpen then useSlot(i) end end)
RegisterKeyMapping(hotkey, shared.locale('use_hotbar', i), 'keyboard', i)
TriggerEvent('chat:removeSuggestion', '/'..hotkey)
end
end
RegisterNetEvent('ox_inventory:closeInventory', function(server)
if invOpen then
invOpen = nil
SetNuiFocus(false, false)
SetNuiFocusKeepInput(false)
TriggerScreenblurFadeOut(0)
CloseTrunk()
SendNUIMessage({ action = 'closeInventory' })
SetInterval(interval, 200)
Wait(200)
if not server and currentInventory then
TriggerServerEvent('ox_inventory:closeInventory')
end
currentInventory = false
plyState.invOpen = false
defaultInventory.coords = nil
end
end)
local function updateInventory(items, weight)
local changes = {}
local itemCount = {}
-- swapslots
if type(weight) == 'number' then
for slot, v in pairs(items) do
local item = PlayerData.inventory[slot]
if item then
itemCount[item.name] = (itemCount[item.name] or 0) - item.count
end
if v then
itemCount[v.name] = (itemCount[v.name] or 0) + v.count
end
PlayerData.inventory[slot] = v and v or nil
changes[slot] = v
end
client.setPlayerData('weight', weight)
else
for i = 1, #items do
local v = items[i].item
local item = PlayerData.inventory[v.slot]
if item?.name then
itemCount[item.name] = (itemCount[item.name] or 0) - item.count
end
if v.count then
itemCount[v.name] = (itemCount[v.name] or 0) + v.count
end
changes[v.slot] = v.count and v or false
if not v.count then v.name = nil end
PlayerData.inventory[v.slot] = v.name and v or nil
end
client.setPlayerData('weight', weight.left)
SendNUIMessage({ action = 'refreshSlots', data = items })
end
for item, count in pairs(itemCount) do
local data = Items[item]
if count < 0 then
data.count += count
if data.client?.remove then
data.client.remove(data.count)
end
elseif count > 0 then
data.count += count
if data.client?.add then
data.client.add(data.count)
end
end
end
client.setPlayerData('inventory', PlayerData.inventory)
TriggerEvent('ox_inventory:updateInventory', changes)
end
RegisterNetEvent('ox_inventory:updateSlots', function(items, weights, count, removed)
if count then
local item = items[1].item
Utils.ItemNotify({item.label, item.name, shared.locale(removed and 'removed' or 'added', count)})
end
updateInventory(items, weights)
end)
RegisterNetEvent('ox_inventory:inventoryReturned', function(data)
Utils.Notify({text = shared.locale('items_returned'), duration = 2500})
if currentWeapon then currentWeapon = Utils.Disarm(currentWeapon) end
TriggerEvent('ox_inventory:closeInventory')
PlayerData.inventory = data[1]
client.setPlayerData('inventory', data[1])
client.setPlayerData('weight', data[3])
end)
RegisterNetEvent('ox_inventory:inventoryConfiscated', function(message)
if message then Utils.Notify({text = shared.locale('items_confiscated'), duration = 2500}) end
if currentWeapon then currentWeapon = Utils.Disarm(currentWeapon) end
TriggerEvent('ox_inventory:closeInventory')
table.wipe(PlayerData.inventory)
client.setPlayerData('weight', 0)
end)
RegisterNetEvent('ox_inventory:createDrop', function(drop, data, owner, slot)
if drops then
drops[drop] = data
end
if owner == PlayerData.source and invOpen and #(GetEntityCoords(PlayerData.ped) - data.coords) <= 1 then
if currentWeapon?.slot == slot then currentWeapon = Utils.Disarm(currentWeapon) end
if not IsPedInAnyVehicle(PlayerData.ped, false) then
OpenInventory('drop', drop)
else
SendNUIMessage({
action = 'setupInventory',
data = { rightInventory = currentInventory }
})
end
end
end)
RegisterNetEvent('ox_inventory:removeDrop', function(id)
if closestMarker?[3] == id then table.wipe(closestMarker) end
if drops then drops[id] = nil end
nearbyMarkers['drop'..id] = nil
end)
local uiLoaded = false
local function setStateBagHandler(id)
AddStateBagChangeHandler(nil, 'player:'..id, function(bagName, key, value, _, _)
if key == 'invOpen' then
invOpen = value
elseif key == 'invBusy' then
invBusy = value
if value then
DisableControlActions:Add(23, 25, 36, 263)
else
DisableControlActions:Remove(23, 25, 36, 263)
end
elseif key == 'instance' then
currentInstance = value
elseif shared.police[key] then
PlayerData.groups[key] = value
OnPlayerData('groups')
end
end)
setStateBagHandler = nil
end
RegisterNetEvent('ox_inventory:setPlayerInventory', function(currentDrops, inventory, weight, esxItem, player, source)
PlayerData = player
PlayerData.id = PlayerId()
PlayerData.ped = PlayerPedId()
PlayerData.source = source
PlayerData.dead = false
PlayerData.cuffed = false
PlayerData.loaded = false
if setStateBagHandler then setStateBagHandler(source) end
for _, data in pairs(inventory) do
Items[data.name].count += data.count
end
if Items['phone']?.count < 1 and GetResourceState('npwd') == 'started' then
exports.npwd:setPhoneDisabled(true)
end
client.setPlayerData('inventory', inventory)
client.setPlayerData('weight', weight)
currentWeapon = nil
drops = currentDrops
Utils.ClearWeapons()
local ItemData = table.create(0, #Items)
for _, v in pairs(Items) do
v.usable = (v.client and next(v.client) or v.consume == 0 or esxItem[v.name] or v.name:find('WEAPON_') or v.name:find('ammo-') or v.name:find('at_')) and true or false
ItemData[v.name] = {
label = v.label,
usable = v.usable,
stack = v.stack,
close = v.close,
description = v.description
}
end
local locales = {}
for k, v in pairs(shared.locale()) do
if k:find('ui_') then
locales[k] = v
end
end
while not uiLoaded do Wait(50) end
SendNUIMessage({
action = 'init',
data = {
locale = locales,
items = ItemData,
leftInventory = {
id = PlayerData.id,
slots = shared.playerslots,
items = PlayerData.inventory,
maxWeight = shared.playerweight,
}
}
})
PlayerData.loaded = true
Shops()
Inventory.Stashes()
Inventory.Evidence()
RegisterCommands()
plyState:set('invBusy', false, false)
plyState:set('invOpen', false, false)
TriggerEvent('ox_inventory:updateInventory', PlayerData.inventory)
Utils.Notify({text = shared.locale('inventory_setup'), duration = 2500})
local Licenses = data 'licenses'
interval = SetInterval(function()
PlayerData.ped = PlayerPedId()
if invOpen == false then
playerCoords = GetEntityCoords(PlayerData.ped)
table.wipe(closestMarker)
Markers(drops, 'drop', vec3(150, 30, 30))
if not shared.qtarget then
if client.hasGroup(shared.police) then Markers(Inventory.Evidence, 'policeevidence', vec(30, 30, 150)) end
Markers(Inventory.Stashes, 'stash', vec3(30, 30, 150))
for k, v in pairs(Shops) do
if not v.groups or client.hasGroup(v.groups) then
Markers(v.locations, 'shop', vec3(30, 150, 30), k)
end
end
end
Markers(Licenses, 'license', vec(30, 150, 30))
SetPedCanSwitchWeapon(PlayerData.ped, false)
SetPedEnableWeaponBlocking(PlayerData.ped, true)
if IsPedInAnyVehicle(PlayerData.ped, false) then table.wipe(closestMarker) end
elseif invOpen == true then
if not CanOpenInventory() then
TriggerEvent('ox_inventory:closeInventory')
else
playerCoords = GetEntityCoords(PlayerData.ped)
if currentInventory then
if currentInventory.type == 'otherplayer' then
local id = GetPlayerFromServerId(currentInventory.id)
local ped = GetPlayerPed(id)
local pedCoords = GetEntityCoords(ped)
if not id or #(playerCoords - pedCoords) > 1.8 or not (client.hasGroup(shared.police) or CanOpenTarget(ped)) then
TriggerEvent('ox_inventory:closeInventory')
Utils.Notify({type = 'error', text = shared.locale('inventory_lost_access'), duration = 2500})
else
TaskTurnPedToFaceCoord(PlayerData.ped, pedCoords.x, pedCoords.y, pedCoords.z, 50)
end
elseif currentInventory.coords and (#(playerCoords - currentInventory.coords) > 2 or CanOpenTarget(PlayerData.ped)) then
TriggerEvent('ox_inventory:closeInventory')
Utils.Notify({type = 'error', text = shared.locale('inventory_lost_access'), duration = 2500})
end
end
end
end
if currentWeapon and GetSelectedPedWeapon(PlayerData.ped) ~= currentWeapon.hash then currentWeapon = Utils.Disarm(currentWeapon) end
if client.parachute and GetPedParachuteState(PlayerData.ped) ~= -1 then
Utils.DeleteObject(client.parachute)
client.parachute = false
end
end, 200)
local EnableKeys = client.enablekeys
tick = SetInterval(function()
DisablePlayerVehicleRewards(PlayerData.id)
if invOpen then
DisableAllControlActions(0)
HideHudAndRadarThisFrame()
for i = 1, #EnableKeys do
EnableControlAction(0, EnableKeys[i], true)
end
if currentInventory.type == 'newdrop' then
EnableControlAction(0, 30, true)
EnableControlAction(0, 31, true)
end
else
DisableControlActions()
if invBusy then DisablePlayerFiring(PlayerData.id, true) end
for _, v in pairs(nearbyMarkers) do
local coords, rgb = v[1], v[2]
DrawMarker(2, coords.x, coords.y, coords.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3, 0.2, 0.15, rgb.x, rgb.y, rgb.z, 222, false, false, false, true, false, false, false)
end
if closestMarker and IsControlJustReleased(0, 38) then
if closestMarker[3] == 'license' then
ServerCallback.Async(shared.resource, 'buyLicense', 1000, function(success, message)
if success == false then
Utils.Notify({type = 'error', text = shared.locale(message), duration = 2500})
else
Utils.Notify({text = shared.locale(message), duration = 2500})
end
end, closestMarker[2])
elseif closestMarker[3] == 'shop' then OpenInventory(closestMarker[3], {id=closestMarker[2], type=closestMarker[4]})
elseif closestMarker[3] == 'policeevidence' then OpenInventory(closestMarker[3]) end
end
if currentWeapon then
DisableControlAction(0, 140, true)
if not invBusy and currentWeapon.timer ~= 0 and currentWeapon.timer < GetGameTimer() then
currentWeapon.timer = 0
if currentWeapon.metadata.ammo then
TriggerServerEvent('ox_inventory:updateWeapon', 'ammo', currentWeapon.metadata.ammo)
elseif currentWeapon.metadata.durability then
TriggerServerEvent('ox_inventory:updateWeapon', 'melee', currentWeapon.melee)
currentWeapon.melee = 0
end
elseif currentWeapon.metadata.ammo then
local playerPed = PlayerData.ped
if IsPedShooting(playerPed) then
local currentAmmo
if currentWeapon.name == 'WEAPON_PETROLCAN' or currentWeapon.name == 'WEAPON_HAZARDCAN' or currentWeapon.name == 'WEAPON_FIREEXTINGUISHER' then
currentAmmo = currentWeapon.metadata.ammo - 0.05
if currentAmmo <= 0 then
SetPedInfiniteAmmo(playerPed, false, currentWeapon.hash)
end
else currentAmmo = GetAmmoInPedWeapon(playerPed, currentWeapon.hash) end
currentWeapon.metadata.ammo = (currentWeapon.metadata.ammo < currentAmmo) and 0 or currentAmmo
if currentAmmo <= 0 then
ClearPedTasks(playerPed)
SetCurrentPedWeapon(playerPed, currentWeapon.hash, true)
SetPedCurrentWeaponVisible(playerPed, true, false, false, false)
if currentWeapon?.ammo and shared.autoreload and not Interface.ProgressActive and not IsPedRagdoll(playerPed) and not IsPedFalling(playerPed) then
currentWeapon.timer = 0
local ammo = Inventory.Search(1, currentWeapon.ammo)
if ammo[1] then
TriggerServerEvent('ox_inventory:updateWeapon', 'ammo', currentWeapon.metadata.ammo)
useSlot(ammo[1].slot)
end
else currentWeapon.timer = GetGameTimer() + 400 end
else currentWeapon.timer = GetGameTimer() + 400 end
end
elseif IsControlJustReleased(0, 24) then
if currentWeapon.throwable then
local playerPed = PlayerData.ped
plyState.invBusy = true
SetTimeout(700, function()
ClearPedSecondaryTask(playerPed)
RemoveWeaponFromPed(playerPed, currentWeapon.hash)
TriggerServerEvent('ox_inventory:updateWeapon', 'throw')
currentWeapon = nil
TriggerEvent('ox_inventory:currentWeapon')
plyState.invBusy = false
end)
elseif IsPedPerformingMeleeAction(PlayerData.ped) then
currentWeapon.melee += 1
currentWeapon.timer = GetGameTimer() + 400
end
end
end
end
end)
collectgarbage('collect')
end)
AddEventHandler('onResourceStop', function(resourceName)
if shared.resource == resourceName then
if client.parachute then
Utils.DeleteObject(client.parachute)
end
if invOpen then
SetNuiFocus(false, false)
SetNuiFocusKeepInput(false)
TriggerScreenblurFadeOut(0)