forked from overextended/ox_inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.lua
1773 lines (1420 loc) · 50.3 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
if not lib then return end
require 'modules.bridge.client'
require 'modules.interface.client'
local Utils = require 'modules.utils.client'
local Weapon = require 'modules.weapon.client'
local currentWeapon
RegisterNetEvent('ox_inventory:noSteal', function()
lib.notify({
title = 'Not Allowed',
description = 'You can not steal from a locked slot.',
type = 'error'
})
end)
exports('getCurrentWeapon', function()
return currentWeapon
end)
RegisterNetEvent('ox_inventory:disarm', function()
currentWeapon = Weapon.Disarm(currentWeapon)
end)
RegisterNetEvent('ox_inventory:clearWeapons', function()
Weapon.ClearAll(currentWeapon)
end)
local StashTarget
exports('setStashTarget', function(id, owner)
StashTarget = id and {id=id, owner=owner}
end)
---@type boolean | number
local invBusy = true
---@type boolean?
local invOpen = false
local plyState = LocalPlayer.state
local IsPedCuffed = IsPedCuffed
local playerPed = cache.ped
lib.onCache('ped', function(ped)
playerPed = ped
Utils.WeaponWheel()
end)
plyState:set('invBusy', true, false)
plyState:set('invHotkeys', false, false)
--[[local function canOpenInventory()
return PlayerData.loaded
and not invBusy
and not PlayerData.dead
and invOpen ~= nil
and (not currentWeapon or currentWeapon.timer == 0)
and not IsPedCuffed(playerPed)
and not IsPauseMenuActive()
and not IsPedFatallyInjured(playerPed)
end--]]
local function canOpenInventory()
return PlayerData.loaded
and not invBusy
and not PlayerData.dead
and invOpen ~= nil
and (not currentWeapon or currentWeapon.timer == 0)
and not IsPedCuffed(playerPed)
and not IsPauseMenuActive()
and not IsPedFatallyInjured(playerPed)
and not exports.wasabi_ambulance: isPlayerDead()
end
---@param ped number
---@return boolean
local function canOpenTarget(ped)
return IsPedFatallyInjured(ped)
or IsEntityPlayingAnim(ped, 'dead', 'dead_a', 3)
or IsPedCuffed(ped)
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 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(playerPed, true)
---@todo animation for vans?
Utils.PlayAnimAdvanced(0, 'anim@heists@fleeca_bank@scope_out@return_case', 'trevor_action', coords.x, coords.y, coords.z, 0.0, 0.0, GetEntityHeading(playerPed), 2.0, 2.0, 1000, 49, 0.25)
CreateThread(function()
local entity = currentInventory.entity
local door = currentInventory.door
Wait(900)
if type(door) == 'table' then
for i = 1, #door do
SetVehicleDoorShut(entity, door[i], false)
end
else
SetVehicleDoorShut(entity, door, false)
end
end)
end
end
local CraftingBenches = require 'modules.crafting.client'
---@param inv string?
---@param data any?
---@return boolean?
function client.openInventory(inv, data)
if invOpen then
if not inv and currentInventory.type == 'newdrop' then
return client.closeInventory()
end
if IsNuiFocused() then
if inv == 'container' and currentInventory.id == PlayerData.inventory[data].metadata.container then
return client.closeInventory()
end
if currentInventory.type == 'drop' and (not data or currentInventory.id == (type(data) == 'table' and data.id or data)) then
return client.closeInventory()
end
if inv ~= 'drop' and inv ~= 'container' then
if (data?.id or data) == currentInventory?.id then
-- Triggering exports.ox_inventory:openInventory('stash', 'mystash') twice in rapid succession is weird behaviour
return warn(("script tried to open inventory, but it is already open\n%s"):format(Citizen.InvokeNative(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString())))
else
return client.closeInventory()
end
end
end
elseif IsNuiFocused() then
-- If triggering event from another nui such as qtarget, may need to wait for focus to end
Wait(100)
if IsNuiFocused() then
return warn('Inventory did not open as another resource has NUI Focus.')
end
end
if inv == 'dumpster' and cache.vehicle then
return lib.notify({ id = 'inventory_right_access', type = 'error', description = locale('inventory_right_access') })
end
if canOpenInventory() then
local left, right
if inv == 'player' then
local targetId, targetPed
if not data then
targetId, targetPed = Utils.GetClosestPlayer()
data = targetId and GetPlayerServerId(targetId)
else
local serverId = type(data) == 'table' and data.id or data
if serverId == cache.serverId then return end
targetId = serverId and GetPlayerFromServerId(serverId)
targetPed = targetId and GetPlayerPed(targetId)
end
local targetCoords = targetPed and GetEntityCoords(targetPed)
if not targetCoords or #(targetCoords - GetEntityCoords(playerPed)) > 1.8 or not (client.hasGroup(shared.police) or canOpenTarget(targetPed)) then
return lib.notify({ id = 'inventory_right_access', type = 'error', description = locale('inventory_right_access') })
end
end
if inv == 'shop' and invOpen == false then
if cache.vehicle then
return lib.notify({ id = 'cannot_perform', type = 'error', description = locale('cannot_perform') })
end
left, right = lib.callback.await('ox_inventory:openShop', 200, data)
elseif inv == 'crafting' then
if cache.vehicle then
return lib.notify({ id = 'cannot_perform', type = 'error', description = locale('cannot_perform') })
end
left = lib.callback.await('ox_inventory:openCraftingBench', 200, data.id, data.index)
if left then
right = CraftingBenches[data.id]
local coords = shared.target == 'ox_target' and right.zones[data.index].coords or right.points[data.index]
right = {
type = 'crafting',
id = data.id,
label = right.label or locale('crafting_bench'),
index = data.index,
slots = right.slots,
items = right.items,
coords = coords
}
end
elseif invOpen ~= nil then
if inv == 'policeevidence' then
local input = lib.inputDialog(locale('police_evidence'), {locale('locker_number')}) --[[@as any]]
if input then
input = tonumber(input[1])
else
return lib.notify({ description = locale('locker_no_value'), type = 'error' })
end
if type(input) ~= 'number' then
return lib.notify({ description = locale('locker_must_number'), type = 'error' })
else
data = input
end
end
left, right = lib.callback.await('ox_inventory:openInventory', false, inv, data)
end
if left then
if not cache.vehicle then
if inv == 'player' then
Utils.PlayAnim(0, 'mp_common', 'givetake1_a', 8.0, 1.0, 2000, 50, 0.0, 0, 0, 0)
elseif inv ~= 'trunk' then
Utils.PlayAnim(0, 'pickup_object', 'putdown_low', 5.0, 1.5, 1000, 48, 0.0, 0, 0, 0)
end
end
plyState.invOpen = true
SetInterval(client.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(playerPed)
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 lib.notify({ id = 'inventory_right_access', type = 'error', description = locale('inventory_right_access') }) end
if invOpen then client.closeInventory() end
end
elseif invBusy then lib.notify({ id = 'inventory_player_access', type = 'error', description = locale('inventory_player_access') }) end
end
RegisterNetEvent('ox_inventory:openInventory', client.openInventory)
exports('openInventory', client.openInventory)
local Animations = data 'animations'
local Items = require 'modules.items.client'
lib.callback.register('ox_inventory:usingItem', function(data)
local item = Items[data.name]
if item and invBusy then
if not item.client then return true end
---@cast item +OxClientProps
item = item.client
plyState.invBusy = true
if type(item.anim) == 'string' then
item.anim = Animations.anim[item.anim]
end
if item.propTwo then
item.prop = { item.prop, item.propTwo }
end
if item.prop then
if item.prop[1] then
for i = 1, #item.prop do
if type(item.prop) == 'string' then
item.prop = Animations.prop[item.prop[i]]
end
end
elseif type(item.prop) == 'string' then
item.prop = Animations.prop[item.prop]
end
end
if not item.disable then
item.disable = { combat = true }
elseif item.disable.combat == nil then
-- Backwards compatibility; you probably don't want people shooting while eating and bandaging anyway
item.disable.combat = true
end
local success = (not item.usetime or lib.progressBar({
duration = item.usetime,
label = item.label or locale('using', data.metadata.label or data.label),
useWhileDead = item.useWhileDead,
canCancel = item.cancel,
disable = item.disable,
anim = item.anim or item.scenario,
prop = item.prop --[[@as ProgressProps]]
})) and not PlayerData.dead
if success then
if item.notification then
lib.notify({ description = item.notification })
end
if item.status then
if client.setPlayerStatus then
client.setPlayerStatus(item.status)
end
end
return true
end
end
end)
local function canUseItem(isAmmo)
local ped = cache.ped
return (not isAmmo or currentWeapon)
and PlayerData.loaded
and not PlayerData.dead
and not invBusy
and not lib.progressActive()
and not IsPedRagdoll(ped)
and not IsPedFalling(ped)
end
---@param data table
---@param cb function?
local function useItem(data, cb)
if invOpen and data.close then client.closeInventory() end
local slotData, result = PlayerData.inventory[data.slot]
if canUseItem(data.ammo and true) then
if currentWeapon and currentWeapon?.timer > 100 then return end
invBusy = true
result = lib.callback.await('ox_inventory:useItem', 200, data.name, data.slot, slotData.metadata)
if not result then
Wait(500)
plyState.invBusy = false
return
end
end
if cb then
local success, response = pcall(cb, result and slotData)
if not success and response then
print(('^1An error occurred while calling item "%s" callback!\n^1SCRIPT ERROR: %s^0'):format(slotData.name, response))
end
end
Wait(200)
plyState.invBusy = false
end
AddEventHandler('ox_inventory:item', useItem)
exports('useItem', useItem)
---@param slot number
---@return boolean?
local function useSlot(slot)
local item = PlayerData.inventory[slot]
if not item then return end
local data = Items[item.name]
if not data then return end
if canUseItem(data.ammo and true) then
if data.component and not currentWeapon then
return lib.notify({ id = 'weapon_hand_required', type = 'error', description = locale('weapon_hand_required') })
end
local durability = item.metadata.durability --[[@as number?]]
local consume = data.consume --[[@as number?]]
local label = item.metadata.label or item.label --[[@as string]]
-- Naive durability check to get an early exit
-- People often don't call the 'useItem' export and then complain about "broken" items being usable
-- This won't work with degradation since we need access to os.time on the server
if durability and durability <= 100 and consume then
if durability <= 0 then
return lib.notify({ type = 'error', description = locale('no_durability', label) })
elseif consume ~= 0 and consume < 1 and durability < consume * 100 then
return lib.notify({ type = 'error', description = locale('not_enough_durability', label) })
end
end
data.slot = slot
if item.metadata.container then
return client.openInventory('container', item.slot)
elseif data.client then
if invOpen and data.close then client.closeInventory() end
if data.export then
return data.export(data, {name = item.name, slot = item.slot, metadata = item.metadata})
elseif data.client.event then -- re-add it, so I don't need to deal with morons taking screenshots of errors when using trigger event
return TriggerEvent(data.client.event, data, {name = item.name, slot = item.slot, metadata = item.metadata})
end
end
if data.effect then
data:effect({name = item.name, slot = item.slot, metadata = item.metadata})
elseif data.weapon then
if EnableWeaponWheel then return end
useItem(data, function(result)
if result then
if currentWeapon then
local weaponSlot = currentWeapon.slot
currentWeapon = Weapon.Disarm(currentWeapon)
if weaponSlot == result.slot then return end
end
currentWeapon = item
currentWeapon = Weapon.Equip(item, data)
if IsCinematicCamRendering() then SetCinematicModeActive(false) end
end
end)
elseif currentWeapon then
if data.ammo then
if EnableWeaponWheel or currentWeapon.metadata.durability <= 0 then return end
local clipSize = GetMaxAmmoInClip(playerPed, currentWeapon.hash, true)
local currentAmmo = GetAmmoInPedWeapon(playerPed, currentWeapon.hash)
local _, maxAmmo = GetMaxAmmo(playerPed, currentWeapon.hash)
if maxAmmo < clipSize then clipSize = maxAmmo end
if currentAmmo == clipSize then return end
useItem(data, function(resp)
if not resp or resp.name ~= currentWeapon?.ammo then return end
if currentWeapon.metadata.specialAmmo ~= resp.metadata.type then
local clipComponentKey = ('%s_CLIP'):format(Items[currentWeapon.name].model:gsub('WEAPON_', 'COMPONENT_'))
local specialClip = ('%s_%s'):format(clipComponentKey, (resp.metadata.type or currentWeapon.metadata.specialAmmo):upper())
if type(resp.metadata.type) == 'string' then
if not HasPedGotWeaponComponent(playerPed, currentWeapon.hash, specialClip) then
if not DoesWeaponTakeWeaponComponent(currentWeapon.hash, specialClip) then
warn('cannot use clip with this weapon')
return
end
local defaultClip = ('%s_01'):format(clipComponentKey)
if not HasPedGotWeaponComponent(playerPed, currentWeapon.hash, defaultClip) then
warn('cannot use clip with currently equipped clip')
return
end
if currentAmmo > 0 then
warn('cannot mix special ammo with base ammo')
return
end
currentWeapon.metadata.specialAmmo = resp.metadata.type
GiveWeaponComponentToPed(playerPed, currentWeapon.hash, specialClip)
end
elseif HasPedGotWeaponComponent(playerPed, currentWeapon.hash, specialClip) then
if currentAmmo > 0 then
warn('cannot mix special ammo with base ammo')
return
end
currentWeapon.metadata.specialAmmo = nil
RemoveWeaponComponentFromPed(playerPed, currentWeapon.hash, specialClip)
end
end
if maxAmmo > clipSize then
clipSize = GetMaxAmmoInClip(playerPed, currentWeapon.hash, true)
end
currentAmmo = GetAmmoInPedWeapon(playerPed, currentWeapon.hash)
local missingAmmo = clipSize - currentAmmo
local addAmmo = resp.count > missingAmmo and missingAmmo or resp.count
local newAmmo = currentAmmo + addAmmo
if newAmmo == currentAmmo then return end
if cache.vehicle then
SetAmmoInClip(playerPed, currentWeapon.hash, newAmmo)
else
AddAmmoToPed(playerPed, currentWeapon.hash, addAmmo)
Wait(100)
MakePedReload(playerPed)
SetTimeout(100, function()
while IsPedReloading(playerPed) do
DisableControlAction(0, 22, true)
Wait(0)
end
end)
end
currentWeapon.metadata.ammo = newAmmo
TriggerServerEvent('ox_inventory:updateWeapon', 'load', currentWeapon.metadata.ammo, false, currentWeapon.metadata.specialAmmo)
end)
elseif data.component 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
return lib.notify({ id = 'component_slot_occupied', type = 'error', description = locale('component_slot_occupied', componentType) })
end
end
for i = 1, #components do
local component = components[i]
if DoesWeaponTakeWeaponComponent(currentWeapon.hash, component) then
if HasPedGotWeaponComponent(playerPed, currentWeapon.hash, component) then
lib.notify({ id = 'component_has', type = 'error', description = locale('component_has', label) })
else
useItem(data, function(data)
if data then
GiveWeaponComponentToPed(playerPed, currentWeapon.hash, component)
table.insert(PlayerData.inventory[currentWeapon.slot].metadata.components, data.name)
TriggerServerEvent('ox_inventory:updateWeapon', 'component', tostring(data.slot), currentWeapon.slot)
end
end)
end
return
end
end
lib.notify({ id = 'component_invalid', type = 'error', description = locale('component_invalid', label) })
elseif data.allowArmed then
useItem(data)
end
elseif not data.ammo and not data.component then
useItem(data)
end
end
end
exports('useSlot', useSlot)
---@param id number
---@param slot number
local function useButton(id, slot)
if PlayerData.loaded and not invBusy and not lib.progressActive() then
local item = PlayerData.inventory[slot]
if not item then return end
local data = Items[item.name]
local buttons = data?.buttons
if buttons and buttons[id]?.action then
buttons[id].action(slot)
end
end
end
local function openNearbyInventory() client.openInventory('player') end
exports('openNearbyInventory', openNearbyInventory)
local currentInstance
local playerCoords
local table = lib.table
local Inventory = require 'modules.inventory.client'
local Shops = require 'modules.shops.client'
---@todo remove or replace when the bridge module gets restructured
function OnPlayerData(key, val)
if key ~= 'groups' and key ~= 'ped' and key ~= 'dead' then return end
if key == 'groups' then
Inventory.Stashes()
Inventory.Evidence()
Shops.refreshShops()
elseif key == 'dead' and val then
currentWeapon = Weapon.Disarm(currentWeapon)
client.closeInventory()
end
Utils.WeaponWheel()
end
-- People consistently ignore errors when one of the "modules" failed to load
if not Utils or not Weapon or not Items or not Inventory then return end
local invHotkeys = false
local function registerCommands()
RegisterCommand('steal', openNearbyInventory, false)
lib.addKeybind({
name = 'inv',
description = locale('open_player_inventory'),
defaultKey = client.keys[1],
onPressed = function()
if invOpen then
return client.closeInventory()
end
local closest = lib.points.closest()
if closest and closest.currentDistance < 1.2 and (not closest.instance or closest.instance == currentInstance) then
if closest.inv == 'crafting' then
return client.openInventory('crafting', { id = closest.id, index = closest.index })
elseif closest.inv ~= 'license' and closest.inv ~= 'policeevidence' then
return client.openInventory(closest.inv or 'drop', { id = closest.invId, type = closest.type })
end
end
return client.openInventory()
end
})
local Vehicles = data 'vehicles'
lib.addKeybind({
name = 'inv2',
description = locale('open_secondary_inventory'),
defaultKey = client.keys[2],
onPressed = function()
if invOpen then
return client.closeInventory()
end
if invBusy then
return lib.notify({ id = 'inventory_player_access', type = 'error', description = locale('inventory_player_access') })
end
if not canOpenInventory() then
return lib.notify({ id = 'inventory_player_access', type = 'error', description = locale('inventory_player_access') })
end
if StashTarget then
return client.openInventory('stash', StashTarget)
end
local vehicle = cache.vehicle
if vehicle then
if not IsPedInAnyVehicle(playerPed, false) or not NetworkGetEntityIsNetworked(vehicle) then return end
local vehicleHash = GetEntityModel(vehicle)
local vehicleClass = GetVehicleClass(vehicle)
local checkVehicle = Vehicles.Storage[vehicleHash]
-- No storage or no glovebox
if (checkVehicle == 0 or checkVehicle == 2) or (not Vehicles.glovebox[vehicleClass] and not Vehicles.glovebox.models[vehicleHash]) then return end
local isOpen = client.openInventory('glovebox', { id = 'glove'..GetVehicleNumberPlateText(vehicle), netid = NetworkGetNetworkIdFromEntity(vehicle) })
if isOpen then
currentInventory.entity = vehicle
end
return
end
local entity, entityType = Utils.Raycast(2|16)
if not entity then return end
if not shared.target and entityType == 3 then
local model = GetEntityModel(entity)
if Inventory.Dumpsters[model] then
return Inventory.OpenDumpster(entity)
end
end
if entityType ~= 2 then return end
local position = GetEntityCoords(entity)
if #(playerCoords - position) > 6 or GetVehiclePedIsEntering(playerPed) ~= 0 or not NetworkGetEntityIsNetworked(entity) then return end
local vehicleHash = GetEntityModel(entity)
local vehicleClass = GetVehicleClass(entity)
local checkVehicle = Vehicles.Storage[vehicleHash]
-- No storage or no glovebox
if (checkVehicle == 0 or checkVehicle == 1) or (not Vehicles.trunk[vehicleClass] and not Vehicles.trunk.models[vehicleHash]) then return end
if GetVehicleDoorLockStatus(entity) > 1 then
return lib.notify({ id = 'vehicle_locked', type = 'error', description = locale('vehicle_locked') })
end
local door, vehBone
if checkVehicle == nil then -- No data, normal trunk
door, vehBone = 5, GetEntityBoneIndexByName(entity, 'boot')
elseif checkVehicle == 3 then -- Trunk in hood
door, vehBone = 4, GetEntityBoneIndexByName(entity, 'bonnet')
else -- No storage or no trunk
return
end
if vehBone == -1 then
if vehicleClass == 12 then
door = { 2, 3 }
end
vehBone = GetEntityBoneIndexByName(entity, Vehicles.trunk.boneIndex[vehicleHash] or 'platelight')
end
position = GetWorldPositionOfEntityBone(entity, vehBone)
if #(playerCoords - position) < 2 and door then
local plate = GetVehicleNumberPlateText(entity)
local invId = 'trunk'..plate
TaskTurnPedToFaceCoord(playerPed, position.x, position.y, position.z, 0)
if not client.openInventory('trunk', { id = invId, netid = NetworkGetNetworkIdFromEntity(entity) }) then return end
if type(door) == 'table' then
for i = 1, #door do
SetVehicleDoorOpen(entity, door[i], false, false)
end
else
SetVehicleDoorOpen(entity, door, false, false)
end
Wait(200)
---@todo animation for vans?
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 = entity
currentInventory.door = door
repeat
Wait(50)
position = GetWorldPositionOfEntityBone(entity, vehBone)
if #(GetEntityCoords(playerPed) - position) >= 2 or not DoesEntityExist(entity) then
break
end
TaskTurnPedToFaceCoord(playerPed, position.x, position.y, position.z, 0)
until currentInventory?.entity ~= entity or not invOpen
if invOpen then client.closeInventory() end
end
end
})
lib.addKeybind({
name = 'reloadweapon',
description = locale('reload_weapon'),
defaultKey = 'r',
onPressed = function(self)
if not currentWeapon or not canUseItem(true) then return end
if currentWeapon.ammo then
if currentWeapon.metadata.durability > 0 then
local ammo = Inventory.Search(1, currentWeapon.ammo, { type = currentWeapon.metadata.specialAmmo })?[1]
if ammo then
useSlot(ammo.slot)
end
else
lib.notify({ id = 'no_durability', type = 'error', description = locale('no_durability', currentWeapon.label) })
end
end
end
})
lib.addKeybind({
name = 'hotbar',
description = locale('disable_hotbar'),
defaultKey = client.keys[3],
onPressed = function()
if EnableWeaponWheel or IsNuiFocused() or lib.progressActive() then return end
SendNUIMessage({ action = 'toggleHotbar' })
end
})
for i = 1, 5 do
lib.addKeybind({
name = ('hotkey%s'):format(i),
description = locale('use_hotbar', i),
defaultKey = tostring(i),
onPressed = function()
if invOpen or IsNuiFocused() or not invHotkeys then return end
useSlot(i)
end
})
end
end
function client.closeInventory(server)
-- because somehow people are triggering this when the inventory isn't loaded
-- and they're incapable of debugging, and I can't repro on a fresh install
if not client.interval then return end
if invOpen then
invOpen = nil
SetNuiFocus(false, false)
SetNuiFocusKeepInput(false)
TriggerScreenblurFadeOut(0)
closeTrunk()
SendNUIMessage({ action = 'closeInventory' })
SetInterval(client.interval, 200)
Wait(200)
if invOpen ~= nil then return end
if not server and currentInventory then
TriggerServerEvent('ox_inventory:closeInventory')
end
currentInventory = nil
plyState.invOpen = false
defaultInventory.coords = nil
end
end
RegisterNetEvent('ox_inventory:closeInventory', client.closeInventory)
local function updateInventory(items, weight)
-- todo: combine iterators
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
SendNUIMessage({ action = 'refreshSlots', data = {itemCount = itemCount} })
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
SendNUIMessage({ action = 'refreshSlots', data = { items = items, itemCount = itemCount} })
client.setPlayerData('weight', weight.left)
end
for item, count in pairs(itemCount) do
local data = Items[item]
if count < 0 then
if currentWeapon and not currentWeapon.throwable and currentWeapon.slot == data.slot then
currentWeapon = Weapon.Disarm(currentWeapon)
end
data.count += count
if shared.framework == 'esx' then
TriggerEvent('esx:removeInventoryItem', data.name, data.count)
else
TriggerEvent('ox_inventory:itemCount', data.name, data.count)
end
if data.client?.remove then
data.client.remove(data.count)
end
elseif count > 0 then
data.count += count
if shared.framework == 'esx' then
TriggerEvent('esx:addInventoryItem', data.name, data.count)
else
TriggerEvent('ox_inventory:itemCount', data.name, data.count)
end
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 source == '' or not next(items) then return end
local item = items[1]?.item
if currentWeapon?.slot == item?.slot and item.metadata then
currentWeapon.metadata = item.metadata
TriggerEvent('ox_inventory:currentWeapon', currentWeapon)
end
if count then
if not item.name then
item = PlayerData.inventory[item.slot]
end
Utils.ItemNotify({item.metadata?.label or item.label, item.metadata?.image or item.metadata?.imageurl or item.name, removed and 'ui_removed' or 'ui_added', count})
end
updateInventory(items, weights)
end)
RegisterNetEvent('ox_inventory:inventoryReturned', function(data)
if source == '' then return end
if currentWeapon then currentWeapon = Weapon.Disarm(currentWeapon) end
lib.notify({ description = locale('items_returned') })
client.closeInventory()
local num, items = 0, {}
for _, slotData in pairs(data[1]) do
num += 1
items[num] = { item = slotData, inventory = 'player' }
end
updateInventory(items, { left = data[3] })
end)
RegisterNetEvent('ox_inventory:inventoryConfiscated', function(message)
if source == '' then return end
if message then lib.notify({ description = locale('items_confiscated') }) end
if currentWeapon then currentWeapon = Weapon.Disarm(currentWeapon) end
client.closeInventory()
local num, items = 0, {}
for slot in pairs(PlayerData.inventory) do
num += 1
items[num] = { item = { slot = slot }, inventory = 'player' }
end
updateInventory(items, { left = 0 })
end)
---@param point CPoint
local function nearbyDrop(point)
if not point.instance or point.instance == currentInstance then
---@diagnostic disable-next-line: param-type-mismatch
DrawMarker(2, point.coords.x, point.coords.y, point.coords.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.3, 0.2, 0.15, 150, 30, 30, 222, false, false, 0, true, false, false, false)
end
end
---@param point CPoint
local function onEnterDrop(point)
if not point.instance or point.instance == currentInstance and not point.entity then