-
Notifications
You must be signed in to change notification settings - Fork 365
/
main.lua
314 lines (273 loc) · 10.1 KB
/
main.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
QBCore = exports['qb-core']:GetCoreObject()
local hotbarShown = false
-- Handlers
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
LocalPlayer.state:set('inv_busy', false, true)
PlayerData = QBCore.Functions.GetPlayerData()
GetDrops()
end)
RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
LocalPlayer.state:set('inv_busy', true, true)
PlayerData = {}
end)
RegisterNetEvent('QBCore:Client:UpdateObject', function()
QBCore = exports['qb-core']:GetCoreObject()
end)
RegisterNetEvent('QBCore:Player:SetPlayerData', function(val)
PlayerData = val
end)
AddEventHandler('onResourceStart', function(resourceName)
if resourceName == GetCurrentResourceName() then
PlayerData = QBCore.Functions.GetPlayerData()
end
end)
-- Functions
function LoadAnimDict(dict)
if HasAnimDictLoaded(dict) then return end
RequestAnimDict(dict)
while not HasAnimDictLoaded(dict) do
Wait(10)
end
end
local function FormatWeaponAttachments(itemdata)
if not itemdata.info or not itemdata.info.attachments or #itemdata.info.attachments == 0 then
return {}
end
local attachments = {}
local weaponName = itemdata.name
local WeaponAttachments = exports['qb-weapons']:getConfigWeaponAttachments()
if not WeaponAttachments then return {} end
for attachmentType, weapons in pairs(WeaponAttachments) do
local componentHash = weapons[weaponName]
if componentHash then
for _, attachmentData in pairs(itemdata.info.attachments) do
if attachmentData.component == componentHash then
local label = QBCore.Shared.Items[attachmentType] and QBCore.Shared.Items[attachmentType].label or 'Unknown'
attachments[#attachments + 1] = {
attachment = attachmentType,
label = label
}
end
end
end
end
return attachments
end
--- Checks if the player has a certain item or items in their inventory with a specified amount.
--- @param items string|table - The item(s) to check for. Can be a table of items or a single item as a string.
--- @param amount number [optional] - The minimum amount required for each item. If not provided, any amount greater than 0 will be considered.
--- @return boolean - Returns true if the player has the item(s) with the specified amount, false otherwise.
function HasItem(items, amount)
local isTable = type(items) == 'table'
local isArray = isTable and table.type(items) == 'array' or false
local totalItems = isArray and #items or 0
local count = 0
if isTable and not isArray then
for _ in pairs(items) do totalItems = totalItems + 1 end
end
for _, itemData in pairs(PlayerData.items) do
if isTable then
for k, v in pairs(items) do
if itemData and itemData.name == (isArray and v or k) and ((amount and itemData.amount >= amount) or (not isArray and itemData.amount >= v) or (not amount and isArray)) then
count = count + 1
if count == totalItems then
return true
end
end
end
else -- Single item as string
if itemData and itemData.name == items and (not amount or (itemData and amount and itemData.amount >= amount)) then
return true
end
end
end
return false
end
exports('HasItem', HasItem)
-- Events
RegisterNetEvent('qb-inventory:client:requiredItems', function(items, bool)
local itemTable = {}
if bool then
for k in pairs(items) do
itemTable[#itemTable + 1] = {
item = items[k].name,
label = QBCore.Shared.Items[items[k].name]['label'],
image = items[k].image,
}
end
end
SendNUIMessage({
action = 'requiredItem',
items = itemTable,
toggle = bool
})
end)
RegisterNetEvent('qb-inventory:client:hotbar', function(items)
hotbarShown = not hotbarShown
SendNUIMessage({
action = 'toggleHotbar',
open = hotbarShown,
items = items
})
end)
RegisterNetEvent('qb-inventory:client:closeInv', function()
SendNUIMessage({
action = 'close',
})
end)
RegisterNetEvent('qb-inventory:client:updateInventory', function()
SendNUIMessage({
action = 'update',
inventory = PlayerData.items
})
end)
RegisterNetEvent('qb-inventory:client:ItemBox', function(itemData, type, amount)
SendNUIMessage({
action = 'itemBox',
item = itemData,
type = type,
amount = amount
})
end)
RegisterNetEvent('qb-inventory:server:RobPlayer', function(TargetId)
SendNUIMessage({
action = 'RobMoney',
TargetId = TargetId,
})
end)
RegisterNetEvent('qb-inventory:client:openInventory', function(items, other)
SetNuiFocus(true, true)
SendNUIMessage({
action = 'open',
inventory = items,
slots = Config.MaxSlots,
maxweight = Config.MaxWeight,
other = other
})
end)
RegisterNetEvent('qb-inventory:client:giveAnim', function()
if IsPedInAnyVehicle(PlayerPedId(), false) then return end
LoadAnimDict('mp_common')
TaskPlayAnim(PlayerPedId(), 'mp_common', 'givetake1_b', 8.0, 1.0, -1, 16, 0, false, false, false)
end)
-- NUI Callbacks
RegisterNUICallback('PlayDropFail', function(_, cb)
PlaySound(-1, 'Place_Prop_Fail', 'DLC_Dmod_Prop_Editor_Sounds', 0, 0, 1)
cb('ok')
end)
RegisterNUICallback('AttemptPurchase', function(data, cb)
QBCore.Functions.TriggerCallback('qb-inventory:server:attemptPurchase', function(canPurchase)
cb(canPurchase)
end, data)
end)
RegisterNUICallback('CloseInventory', function(data, cb)
SetNuiFocus(false, false)
if data.name then
if data.name:find('trunk-') then
CloseTrunk()
end
TriggerServerEvent('qb-inventory:server:closeInventory', data.name)
elseif CurrentDrop then
TriggerServerEvent('qb-inventory:server:closeInventory', CurrentDrop)
CurrentDrop = nil
end
cb('ok')
end)
RegisterNUICallback('UseItem', function(data, cb)
TriggerServerEvent('qb-inventory:server:useItem', data.item)
cb('ok')
end)
RegisterNUICallback('SetInventoryData', function(data, cb)
TriggerServerEvent('qb-inventory:server:SetInventoryData', data.fromInventory, data.toInventory, data.fromSlot, data.toSlot, data.fromAmount, data.toAmount)
cb('ok')
end)
RegisterNUICallback('GiveItem', function(data, cb)
local player, distance = QBCore.Functions.GetClosestPlayer(GetEntityCoords(PlayerPedId()))
if player ~= -1 and distance < 3 then
local playerId = GetPlayerServerId(player)
QBCore.Functions.TriggerCallback('qb-inventory:server:giveItem', function(success)
cb(success)
end, playerId, data.item.name, data.amount)
else
QBCore.Functions.Notify(Lang:t('notify.nonb'), 'error')
cb(false)
end
end)
RegisterNUICallback('GetWeaponData', function(cData, cb)
local data = {
WeaponData = QBCore.Shared.Items[cData.weapon],
AttachmentData = FormatWeaponAttachments(cData.ItemData)
}
cb(data)
end)
RegisterNUICallback('RemoveAttachment', function(data, cb)
local ped = PlayerPedId()
local WeaponData = data.WeaponData
local allAttachments = exports['qb-weapons']:getConfigWeaponAttachments()
local Attachment = allAttachments[data.AttachmentData.attachment][WeaponData.name]
local itemInfo = QBCore.Shared.Items[data.AttachmentData.attachment]
QBCore.Functions.TriggerCallback('qb-weapons:server:RemoveAttachment', function(NewAttachments)
if NewAttachments ~= false then
local Attachies = {}
RemoveWeaponComponentFromPed(ped, joaat(WeaponData.name), joaat(Attachment))
for _, v in pairs(NewAttachments) do
for attachmentType, weapons in pairs(allAttachments) do
local componentHash = weapons[WeaponData.name]
if componentHash and v.component == componentHash then
local label = itemInfo and itemInfo.label or 'Unknown'
Attachies[#Attachies + 1] = {
attachment = attachmentType,
label = label,
}
end
end
end
local DJATA = {
Attachments = Attachies,
WeaponData = WeaponData,
itemInfo = itemInfo,
}
cb(DJATA)
else
RemoveWeaponComponentFromPed(ped, joaat(WeaponData.name), joaat(Attachment))
cb({})
end
end, data.AttachmentData, WeaponData)
end)
-- Vending
CreateThread(function()
exports['qb-target']:AddTargetModel(Config.VendingObjects, {
options = {
{
type = 'server',
event = 'qb-inventory:server:openVending',
icon = 'fa-solid fa-cash-register',
label = Lang:t('menu.vending'),
},
},
distance = 2.5
})
end)
-- Commands
RegisterCommand('openInv', function()
if IsNuiFocused() or IsPauseMenuActive() then return end
ExecuteCommand('inventory')
end, false)
RegisterCommand('toggleHotbar', function()
ExecuteCommand('hotbar')
end, false)
for i = 1, 5 do
RegisterCommand('slot_' .. i, function()
local itemData = PlayerData.items[i]
if not itemData then return end
if itemData.type == "weapon" then
if holdingDrop then
return QBCore.Functions.Notify("Your already holding a bag, Go Drop it!", "error", 5500)
end
end
TriggerServerEvent('qb-inventory:server:useItem', itemData)
end, false)
RegisterKeyMapping('slot_' .. i, Lang:t('inf_mapping.use_item') .. i, 'keyboard', i)
end
RegisterKeyMapping('openInv', Lang:t('inf_mapping.opn_inv'), 'keyboard', Config.Keybinds.Open)
RegisterKeyMapping('toggleHotbar', Lang:t('inf_mapping.tog_slots'), 'keyboard', Config.Keybinds.Hotbar)