-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.lua
296 lines (244 loc) · 10.7 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
local currentDustbin = {}
local isDead = false
AddEventHandler('onResourceStop', function(resource)
if GetCurrentResourceName() == resource then
lib.hideContext('msk_dustbin_main')
end
end)
AddEventHandler('esx:onPlayerDeath', function() isDead = true end)
AddEventHandler('esx:onPlayerSpawn', function() isDead = false end)
getIsDead = function()
local isPlayerDead = isDead
if GetResourceState("visn_are") ~= "missing" then
local healthBuffer = exports.visn_are:GetHealthBuffer()
isPlayerDead = healthBuffer.unconscious
end
return isPlayerDead
end
CreateThread(function()
while true do
local sleep = 500
currentDustbin = {}
if NearDustbin() and isCurrent() and not getIsDead() then
sleep = 0
ESX.ShowHelpNotification(Translation[Config.Locale]['open'])
if IsControlJustPressed(0, Config.Hotkey) then
if Config.Inventory == 'chezza' then
TriggerEvent('inventory:openInventory', {
type = "msk_dustbin",
id = currentDustbin.model .. ' - ' .. coordsToJson(currentDustbin.coords),
title = Translation[Config.Locale]['dustbin_label'],
weight = false,
delay = 250,
save = true
})
elseif Config.Inventory == 'ox_lib' then
registerMenus()
elseif Config.Inventory == 'ox_inventory' then
local stashId = currentDustbin.model .. ' - ' .. coordsToJson(currentDustbin.coords)
if exports.ox_inventory:openInventory('stash', stashId) == false then
ESX.TriggerServerCallback('msk_dustbin:registerStash', function(registered)
if registered then
exports.ox_inventory:openInventory('stash', stashId)
end
end, {
id = stashId,
label = Translation[Config.Locale]['dustbin_label'],
slots = 50,
weight = 100000
})
end
end
end
end
Wait(sleep)
end
end)
registerMenus = function()
local binOptions = {{title = Translation[Config.Locale]['menu_throw_label'], menu = 'msk_dustbin_other', icon = 'trash', arrow = true}}
local binCallback = false
ESX.TriggerServerCallback('msk_dustbin:getDustbin', function(data)
for k, v in pairs(data) do
local icon, title = 'dollar-sign', ''
if v.type == 'money' or v.type == 'item' then
if v.type == 'money' then
icon = 'dollar-sign'
title = ('%s - $%s'):format(v.label, comma(v.money))
elseif v.type == 'item' then
icon = 'box'
title = ('%s - %sx'):format(v.label, v.count)
end
binOptions[#binOptions + 1] = {
title = title,
description = Translation[Config.Locale]['menu_get_desc'],
icon = icon,
args = v,
onSelect = function(data)
local binId = currentDustbin.model .. ' - ' .. coordsToJson(currentDustbin.coords)
local input = lib.inputDialog(Translation[Config.Locale]['amount'], {
{type = 'number', label = Translation[Config.Locale]['amount'], description = Translation[Config.Locale]['amount_desc'], required = true},
})
if input and input[1] then
local amount = data.money
if data.type == 'item' then amount = data.count end
if input[1] <= amount then
TriggerServerEvent('msk_dustbin:getItem', binId, data, input[1])
reopenMenu()
else
Config.Notification(nil, Translation[Config.Locale]['bin_not_enough'], 'error')
end
end
end
}
end
end
for _, i in pairs(data) do
if not i.type then
icon = 'gun'
for k, v in pairs(i) do
title = ('%s - %s Ammo'):format(v.label, v.ammo)
local metadata = false
for a, comp in pairs(v.components) do
if type(metadata) ~= 'table' then metadata = {} end
table.insert(metadata, {label = ESX.GetWeaponComponent(v.name, comp).label})
end
binOptions[#binOptions + 1] = {
title = title,
description = Translation[Config.Locale]['menu_get_desc'],
icon = icon,
args = v,
metadata = metadata,
onSelect = function(data)
local binId = currentDustbin.model .. ' - ' .. coordsToJson(currentDustbin.coords)
TriggerServerEvent('msk_dustbin:getItem', binId, data, nil, k)
reopenMenu()
end
}
end
end
end
binCallback = true
end, currentDustbin.model .. ' - ' .. coordsToJson(currentDustbin.coords), true)
while not binCallback do Wait(0) end
lib.registerContext({
id = 'msk_dustbin_main',
title = Translation[Config.Locale]['dustbin_label'],
options = binOptions
})
local inventoryOptions, callback = {}, false
for k, v in pairs(ESX.PlayerData.accounts) do
if v.money > 0 and v.name ~= 'bank' then
v.type = 'money'
inventoryOptions[#inventoryOptions + 1] = {
title = ('%s - $%s'):format(v.label, comma(v.money)),
icon = 'dollar-sign',
args = v,
onSelect = function(data)
local input = lib.inputDialog(Translation[Config.Locale]['amount'], {
{type = 'number', label = Translation[Config.Locale]['amount'], description = Translation[Config.Locale]['menu_throw_desc'], required = true},
})
if input and input[1] then
if input[1] <= data.money then
local binId = currentDustbin.model .. ' - ' .. coordsToJson(currentDustbin.coords)
TriggerServerEvent('msk_dustbin:throwAway', binId, data, input[1])
reopenMenu()
else
Config.Notification(nil, Translation[Config.Locale]['player_not_enough'], 'error')
end
end
end
}
end
end
ESX.TriggerServerCallback('msk_dustbin:getInventory', function(inventory, loadout)
for k, v in pairs(inventory) do
if v.count > 0 then
v.type = 'item'
inventoryOptions[#inventoryOptions + 1] = {
title = ('%s - %sx'):format(v.label, v.count),
icon = 'box',
args = v,
onSelect = function(data)
local input = lib.inputDialog(Translation[Config.Locale]['amount'], {
{type = 'number', label = Translation[Config.Locale]['amount'], description = Translation[Config.Locale]['menu_throw_desc'], required = true},
})
if input and input[1] then
if input[1] <= data.count then
local binId = currentDustbin.model .. ' - ' .. coordsToJson(currentDustbin.coords)
TriggerServerEvent('msk_dustbin:throwAway', binId, data, input[1])
reopenMenu()
else
Config.Notification(nil, Translation[Config.Locale]['player_not_enough'], 'error')
end
end
end
}
end
end
for k, v in pairs(loadout) do
v.type = 'weapon'
inventoryOptions[#inventoryOptions + 1] = {
title = ('%s - %s Ammo'):format(v.label, v.ammo),
icon = 'gun',
args = v,
onSelect = function(data)
local binId = currentDustbin.model .. ' - ' .. coordsToJson(currentDustbin.coords)
TriggerServerEvent('msk_dustbin:throwAway', binId, data)
reopenMenu()
end
}
end
callback = true
end)
while not callback do Wait(0) end
lib.registerContext({
id = 'msk_dustbin_other',
title = Translation[Config.Locale]['menu_throw_label'],
menu = 'msk_dustbin_main',
options = inventoryOptions
})
lib.showContext('msk_dustbin_main')
end
NearDustbin = function()
local playerCoords = GetEntityCoords(PlayerPedId())
for k, prop in pairs(Config.Dustbins) do
local entity = GetClosestObjectOfType(playerCoords.x, playerCoords.y, playerCoords.z, 1.5, prop, false, false, false)
if DoesEntityExist(entity) then
local entityCoords = GetEntityCoords(entity)
local x, y, z = table.unpack(entityCoords)
local tCoords = {[1] = round(x, 2), [2] = round(y, 2), [3] = round(z, 2)}
currentDustbin = {model = prop, coords = tCoords, entity = entity}
return true
end
end
return false
end
reopenMenu = function()
if getIsDead() then return end
Wait(500)
registerMenus()
end
isCurrent = function()
return currentDustbin and currentDustbin.model and currentDustbin.coords
end
coordsToJson = function(coords)
local sorted = {}
for k, v in ESX.Table.Sort(coords, function(t, a, b) return t[b] < t[a] end) do
sorted[k] = v
end
return json.encode(sorted)
end
comma = function(int, tag)
if not tag then tag = '.' end
local newInt = int
while true do
newInt, k = string.gsub(newInt, "^(-?%d+)(%d%d%d)", '%1'..tag..'%2')
if (k == 0) then
break
end
end
return newInt
end
round = function(num, decimal)
return tonumber(string.format("%." .. (decimal or 0) .. "f", num))
end