forked from qbcore-framework/qb-smallresources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumables.lua
293 lines (242 loc) · 10.7 KB
/
consumables.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
local QBCore = exports['qb-core']:GetCoreObject()
----------- / alcohol
for k, _ in pairs(Config.Consumables.alcohol) do
QBCore.Functions.CreateUseableItem(k, function(source, item)
TriggerClientEvent('consumables:client:DrinkAlcohol', source, item.name)
end)
end
----------- / Eat
for k, _ in pairs(Config.Consumables.eat) do
QBCore.Functions.CreateUseableItem(k, function(source, item)
if not exports['qb-inventory']:RemoveItem(source, item.name, 1, item.slot, 'qb-smallresources:consumables:eat') then return end
TriggerClientEvent('consumables:client:Eat', source, item.name)
end)
end
----------- / Drink
for k, _ in pairs(Config.Consumables.drink) do
QBCore.Functions.CreateUseableItem(k, function(source, item)
if not exports['qb-inventory']:RemoveItem(source, item.name, 1, item.slot, 'qb-smallresources:consumables:drink') then return end
TriggerClientEvent('consumables:client:Drink', source, item.name)
end)
end
----------- / Custom
for k, _ in pairs(Config.Consumables.custom) do
QBCore.Functions.CreateUseableItem(k, function(source, item)
if not exports['qb-inventory']:RemoveItem(source, item.name, 1, item.slot, 'qb-smallresources:consumables:custom') then return end
TriggerClientEvent('consumables:client:Custom', source, item.name)
end)
end
local function createItem(name, type)
QBCore.Functions.CreateUseableItem(name, function(source, item)
if not exports['qb-inventory']:RemoveItem(source, item.name, 1, item.slot, 'qb-smallresources:consumables:createItem') then return end
TriggerClientEvent('consumables:client:' .. type, source, item.name)
end)
end
----------- / Drug
QBCore.Functions.CreateUseableItem('joint', function(source, item)
if not exports['qb-inventory']:RemoveItem(source, item.name, 1, item.slot, 'qb-smallresources:joint') then return end
TriggerClientEvent('consumables:client:UseJoint', source)
end)
QBCore.Functions.CreateUseableItem('cokebaggy', function(source)
TriggerClientEvent('consumables:client:Cokebaggy', source)
end)
QBCore.Functions.CreateUseableItem('crack_baggy', function(source)
TriggerClientEvent('consumables:client:Crackbaggy', source)
end)
QBCore.Functions.CreateUseableItem('xtcbaggy', function(source)
TriggerClientEvent('consumables:client:EcstasyBaggy', source)
end)
QBCore.Functions.CreateUseableItem('oxy', function(source)
TriggerClientEvent('consumables:client:oxy', source)
end)
QBCore.Functions.CreateUseableItem('meth', function(source)
TriggerClientEvent('consumables:client:meth', source)
end)
----------- / Tools
QBCore.Functions.CreateUseableItem('armor', function(source)
TriggerClientEvent('consumables:client:UseArmor', source)
end)
QBCore.Functions.CreateUseableItem('heavyarmor', function(source)
TriggerClientEvent('consumables:client:UseHeavyArmor', source)
end)
QBCore.Commands.Add('resetarmor', 'Resets Vest (Police Only)', {}, false, function(source)
local Player = QBCore.Functions.GetPlayer(source)
if Player.PlayerData.job.name == 'police' then
TriggerClientEvent('consumables:client:ResetArmor', source)
else
TriggerClientEvent('QBCore:Notify', source, 'For Police Officer Only', 'error')
end
end)
QBCore.Functions.CreateUseableItem('binoculars', function(source)
TriggerClientEvent('binoculars:Toggle', source)
end)
QBCore.Functions.CreateUseableItem('parachute', function(source, item)
if not exports['qb-inventory']:RemoveItem(source, item.name, 1, item.slot, 'qb-smallresources:parachute') then return end
TriggerClientEvent('consumables:client:UseParachute', source)
end)
QBCore.Commands.Add('resetparachute', 'Resets Parachute', {}, false, function(source)
TriggerClientEvent('consumables:client:ResetParachute', source)
end)
----------- / Firework
for _, v in pairs(Config.Fireworks.items) do
QBCore.Functions.CreateUseableItem(v, function(source, item)
local src = source
TriggerClientEvent('fireworks:client:UseFirework', src, item.name, 'proj_indep_firework')
end)
end
----------- / Lockpicking
QBCore.Functions.CreateUseableItem('lockpick', function(source)
TriggerClientEvent('lockpicks:UseLockpick', source, false)
end)
QBCore.Functions.CreateUseableItem('advancedlockpick', function(source)
TriggerClientEvent('lockpicks:UseLockpick', source, true)
end)
-- Events for adding and removing specific items to fix some exploits
RegisterNetEvent('consumables:server:AddParachute', function()
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
exports['qb-inventory']:AddItem(source, 'parachute', 1, false, false, 'consumables:server:AddParachute')
end)
RegisterNetEvent('consumables:server:resetArmor', function()
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
exports['qb-inventory']:AddItem(source, 'heavyarmor', 1, false, false, 'consumables:server:resetArmor')
end)
RegisterNetEvent('consumables:server:useHeavyArmor', function()
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
if not exports['qb-inventory']:RemoveItem(source, 'heavyarmor', 1, false, 'consumables:server:useHeavyArmor') then return end
TriggerClientEvent('qb-inventory:client:ItemBox', source, QBCore.Shared.Items['heavyarmor'], 'remove')
TriggerClientEvent('hospital:server:SetArmor', source, 100)
SetPedArmour(GetPlayerPed(source), 100)
end)
RegisterNetEvent('consumables:server:useArmor', function()
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
if not exports['qb-inventory']:RemoveItem(source, 'armor', 1, false, 'consumables:server:useArmor') then return end
TriggerClientEvent('qb-inventory:client:ItemBox', source, QBCore.Shared.Items['armor'], 'remove')
TriggerClientEvent('hospital:server:SetArmor', source, 75)
SetPedArmour(GetPlayerPed(source), 75)
end)
RegisterNetEvent('consumables:server:useMeth', function()
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
exports['qb-inventory']:RemoveItem(source, 'meth', 1, false, 'consumables:server:useMeth')
end)
RegisterNetEvent('consumables:server:useOxy', function()
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
exports['qb-inventory']:RemoveItem(source, 'oxy', 1, false, 'consumables:server:useOxy')
end)
RegisterNetEvent('consumables:server:useXTCBaggy', function()
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
exports['qb-inventory']:RemoveItem(source, 'xtcbaggy', 1, false, 'consumables:server:useXTCBaggy')
end)
RegisterNetEvent('consumables:server:useCrackBaggy', function()
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
exports['qb-inventory']:RemoveItem(source, 'crack_baggy', 1, false, 'consumables:server:useCrackBaggy')
end)
RegisterNetEvent('consumables:server:useCokeBaggy', function()
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
exports['qb-inventory']:RemoveItem(source, 'cokebaggy', 1, false, 'consumables:server:useCokeBaggy')
end)
RegisterNetEvent('consumables:server:drinkAlcohol', function(item)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
local foundItem = nil
for k in pairs(Config.Consumables.alcohol) do
if k == item then
foundItem = k
break
end
end
if not foundItem then return end
exports['qb-inventory']:RemoveItem(source, foundItem, 1, false, 'consumables:server:drinkAlcohol')
end)
RegisterNetEvent('consumables:server:UseFirework', function(item)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
local foundItem = nil
for i = 1, #Config.Fireworks.items do
if Config.Fireworks.items[i] == item then
foundItem = Config.Fireworks.items[i]
break
end
end
if not foundItem then return end
exports['qb-inventory']:RemoveItem(source, foundItem, 1, false, 'consumables:server:UseFirework')
end)
RegisterNetEvent('consumables:server:addThirst', function(amount)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
Player.Functions.SetMetaData('thirst', amount)
TriggerClientEvent('hud:client:UpdateNeeds', source, Player.PlayerData.metadata.hunger, amount)
end)
RegisterNetEvent('consumables:server:addHunger', function(amount)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return end
Player.Functions.SetMetaData('hunger', amount)
TriggerClientEvent('hud:client:UpdateNeeds', source, amount, Player.PlayerData.metadata.thirst)
end)
QBCore.Functions.CreateCallback('consumables:itemdata', function(_, cb, itemName)
cb(Config.Consumables.custom[itemName])
end)
---Checks if item already exists in the table. If not, it creates it.
---@param drinkName string name of item
---@param replenish number amount it replenishes
---@return boolean, string
local function addDrink(drinkName, replenish)
if Config.Consumables.drink[drinkName] ~= nil then
return false, 'already added'
else
Config.Consumables.drink[drinkName] = replenish
createItem(drinkName, 'Drink')
return true, 'success'
end
end
exports('AddDrink', addDrink)
---Checks if item already exists in the table. If not, it creates it.
---@param foodName string name of item
---@param replenish number amount it replenishes
---@return boolean, string
local function addFood(foodName, replenish)
if Config.Consumables.eat[foodName] ~= nil then
return false, 'already added'
else
Config.Consumables.eat[foodName] = replenish
createItem(foodName, 'Eat')
return true, 'success'
end
end
exports('AddFood', addFood)
---Checks if item already exists in the table. If not, it creates it.
---@param alcoholName string name of item
---@param replenish number amount it replenishes
---@return boolean, string
local function addAlcohol(alcoholName, replenish)
if Config.Consumables.alcohol[alcoholName] ~= nil then
return false, 'already added'
else
Config.Consumables.alcohol[alcoholName] = replenish
createItem(alcoholName, 'DrinkAlcohol')
return true, 'success'
end
end
exports('AddAlcohol', addAlcohol)
---Checks if item already exists in the table. If not, it creates it.
---@param itemName string name of item
---@param data number amount it replenishes
---@return boolean, string
local function addCustom(itemName, data)
if 'consumables:itemdata' ~= nil then
return false, 'already added'
else
Config.Consumables.custom[itemName] = data
createItem(itemName, 'Custom')
return true, 'success'
end
end
exports('AddCustom', addCustom)