-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathinit.lua
More file actions
505 lines (423 loc) · 17.5 KB
/
Copy pathinit.lua
File metadata and controls
505 lines (423 loc) · 17.5 KB
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
local function addDeferral(err)
err = err:gsub("%^%d", "")
AddEventHandler('playerConnecting', function(_, _, deferrals)
deferrals.defer()
deferrals.done(err)
end)
end
-- Do not modify this file at all. This isn't a "config" file. You want to change
-- resource settings? Use convars like you were told in the documentation.
-- You did read the docs, right? Probably not, if you're here.
-- https://coxdocs.dev/ox_inventory#config
shared = {
resource = GetCurrentResourceName(),
framework = GetConvar('inventory:framework', 'esx'),
playerslots = GetConvarInt('inventory:slots', 50),
playerweight = GetConvarInt('inventory:weight', 30000),
target = GetConvarInt('inventory:target', 0) == 1,
police = json.decode(GetConvar('inventory:police', '["police", "sheriff"]')),
networkdumpsters = GetConvarInt('inventory:networkdumpsters', 0) == 1
}
shared.dropslots = GetConvarInt('inventory:dropslots', shared.playerslots)
shared.dropweight = GetConvarInt('inventory:dropweight', shared.playerweight)
do
if type(shared.police) == 'string' then
shared.police = { shared.police }
end
local police = table.create(0, shared.police and #shared.police or 0)
for i = 1, #shared.police do
police[shared.police[i]] = 0
end
shared.police = police
end
if IsDuplicityVersion() then
server = {
bulkstashsave = GetConvarInt('inventory:bulkstashsave', 1) == 1,
loglevel = GetConvarInt('inventory:loglevel', 1),
randomprices = GetConvarInt('inventory:randomprices', 0) == 1,
randomloot = GetConvarInt('inventory:randomloot', 1) == 1,
evidencegrade = GetConvarInt('inventory:evidencegrade', 2),
trimplate = GetConvarInt('inventory:trimplate', 1) == 1,
vehicleloot = json.decode(GetConvar('inventory:vehicleloot', [[
[
["sprunk", 1, 1],
["water", 1, 1],
["garbage", 1, 2, 50],
["panties", 1, 1, 5],
["money", 1, 50],
["money", 200, 400, 5],
["bandage", 1, 1]
]
]])),
dumpsterloot = json.decode(GetConvar('inventory:dumpsterloot', [[
[
["mustard", 1, 1],
["garbage", 1, 3],
["money", 1, 10],
["burger", 1, 1]
]
]])),
validhosts = json.decode(GetConvar('inventory:validhosts', [[
{
"r2.fivemanage.com": true,
"i.fmfile.com": true
}
]])),
}
local accounts = json.decode(GetConvar('inventory:accounts', '["money"]'))
server.accounts = table.create(0, #accounts)
for i = 1, #accounts do
server.accounts[accounts[i]] = 0
end
else
PlayerData = {}
client = {
autoreload = GetConvarInt('inventory:autoreload', 0) == 1,
screenblur = GetConvarInt('inventory:screenblur', 1) == 1,
keys = json.decode(GetConvar('inventory:keys', '')) or { 'F2', 'K', 'TAB' },
enablekeys = json.decode(GetConvar('inventory:enablekeys', '[249]')),
aimedfiring = GetConvarInt('inventory:aimedfiring', 0) == 1,
giveplayerlist = GetConvarInt('inventory:giveplayerlist', 0) == 1,
weaponanims = GetConvarInt('inventory:weaponanims', 1) == 1,
itemnotify = GetConvarInt('inventory:itemnotify', 1) == 1,
weaponnotify = GetConvarInt('inventory:weaponnotify', 1) == 1,
imagepath = GetConvar('inventory:imagepath', 'nui://ox_inventory/web/images'),
dropprops = GetConvarInt('inventory:dropprops', 0) == 1,
dropmodel = joaat(GetConvar('inventory:dropmodel', 'prop_med_bag_01b')),
weaponmismatch = GetConvarInt('inventory:weaponmismatch', 1) == 1,
ignoreweapons = json.decode(GetConvar('inventory:ignoreweapons', '[]')),
suppresspickups = GetConvarInt('inventory:suppresspickups', 1) == 1,
disableweapons = GetConvarInt('inventory:disableweapons', 0) == 1,
disablesetupnotification = GetConvarInt('inventory:disablesetupnotification', 0) == 1,
enablestealcommand = GetConvarInt('inventory:enablestealcommand', 1) == 1,
}
local ignoreweapons = table.create(0, (client.ignoreweapons and #client.ignoreweapons or 0) + 3)
for i = 1, #client.ignoreweapons do
local weapon = client.ignoreweapons[i]
ignoreweapons[tonumber(weapon) or joaat(weapon)] = true
end
ignoreweapons[`WEAPON_UNARMED`] = true
ignoreweapons[`WEAPON_HANDCUFFS`] = true
ignoreweapons[`WEAPON_GARBAGEBAG`] = true
ignoreweapons[`OBJECT`] = true
ignoreweapons[`WEAPON_HOSE`] = true
client.ignoreweapons = ignoreweapons
local fallbackmarker = {
type = 0,
colour = {150, 150, 150},
scale = {0.5, 0.5, 0.5}
}
client.shopmarker = json.decode(GetConvar('inventory:shopmarker', [[
{
"type": 29,
"colour": [30, 150, 30],
"scale": [0.5, 0.5, 0.5]
}
]])) or fallbackmarker
client.evidencemarker = json.decode(GetConvar('inventory:evidencemarker', [[
{
"type": 2,
"colour": [30, 30, 150],
"scale": [0.3, 0.2, 0.15]
}
]])) or fallbackmarker
client.craftingmarker = json.decode(GetConvar('inventory:craftingmarker', [[
{
"type": 2,
"colour": [150, 150, 30],
"scale": [0.3, 0.2, 0.15]
}
]])) or fallbackmarker
client.dropmarker = json.decode(GetConvar('inventory:dropmarker', [[
{
"type": 2,
"colour": [150, 30, 30],
"scale": [0.3, 0.2, 0.15]
}
]])) or fallbackmarker
end
function shared.print(...) print(string.strjoin(' ', ...)) end
function shared.info(...) lib.print.info(string.strjoin(' ', ...)) end
---Throws a formatted type error.
---```lua
---error("expected %s to have type '%s' (received %s)")
---```
---@param variable string
---@param expected string
---@param received string
function TypeError(variable, expected, received)
error(("expected %s to have type '%s' (received %s)"):format(variable, expected, received))
end
-- People like ignoring errors for some reason
local function spamError(err)
shared.ready = false
CreateThread(function()
while true do
Wait(10000)
CreateThread(function()
error(err, 0)
end)
end
end)
addDeferral(err)
error(err, 0)
end
---@param name string
---@return table
---@deprecated
function data(name)
if shared.server and shared.ready == nil then return {} end
local file = ('data/%s.lua'):format(name)
local datafile = LoadResourceFile(shared.resource, file)
local path = ('@@%s/%s'):format(shared.resource, file)
if not datafile then
warn(('no datafile found at path %s'):format(path:gsub('@@', '')))
return {}
end
local func, err = load(datafile, path)
if not func or err then
shared.ready = false
---@diagnostic disable-next-line: return-type-mismatch
return spamError(err)
end
return func()
end
if not lib then
return spamError('ox_inventory requires the ox_lib resource, refer to the documentation.')
end
local success, msg = lib.checkDependency('oxmysql', '2.7.3')
if success then
success, msg = lib.checkDependency('ox_lib', '3.27.0')
end
if not success then
return spamError(msg)
end
if not LoadResourceFile(shared.resource, 'web/build/index.html') then
return spamError(
'UI has not been built, refer to the documentation or download a release build.\n ^3https://coxdocs.dev/ox_inventory^0')
end
-- No we're not going to support qtarget any longer.
if shared.target and GetResourceState('ox_target') ~= 'started' then
shared.target = false
warn('ox_target is not loaded - it should start before ox_inventory')
end
do
-- Mirrors data/ui.lua so the resource still starts when that file is missing or broken.
-- Only the 'white' theme is duplicated here; the other six live in data/ui.lua alone.
local default = {
layout = 'slots',
pedPreview = {
mode = 'silhouette',
distance = 2.4,
height = -0.9,
side = 0.0,
turn = 0.0,
light = true,
lightRange = 3.0,
lightIntensity = 2.0,
},
grid = {
columns = 10,
allowRotate = true,
defaultSize = { 1, 1 },
defaults = {
weapon = { 2, 2 },
ammo = { 1, 1 },
component = { 1, 1 },
tint = { 1, 1 },
},
},
hotbar = {
enabled = true,
slots = 5,
},
clothing = {
enabled = true,
slots = {
{ name = 'hat', label = 'Hat', side = 'left' },
{ name = 'glasses', label = 'Glasses', side = 'left' },
{ name = 'mask', label = 'Mask', side = 'left' },
{ name = 'earpiece', label = 'Earpiece', side = 'left' },
{ name = 'torso', label = 'Torso', side = 'left' },
{ name = 'armour', label = 'Armour', side = 'right' },
{ name = 'backpack', label = 'Backpack', side = 'right' },
{ name = 'gloves', label = 'Gloves', side = 'right' },
{ name = 'legs', label = 'Legs', side = 'right' },
{ name = 'shoes', label = 'Shoes', side = 'right' },
},
},
rarity = {
enabled = true,
default = 'common',
tiers = {
common = { label = 'Common', color = '#9CA3AF', order = 1 },
uncommon = { label = 'Uncommon', color = '#4ADE80', order = 2 },
rare = { label = 'Rare', color = '#38BDF8', order = 3 },
epic = { label = 'Epic', color = '#A855F7', order = 4 },
legendary = { label = 'Legendary', color = '#F59E0B', order = 5 },
mythic = { label = 'Mythic', color = '#FB7185', order = 6 },
},
},
theme = 'white',
themes = {
white = {
backgroundColor1 = 'rgba(74, 75, 74, 0)',
backgroundColor2 = 'rgba(77, 77, 77, 0.05)',
backgroundColor3 = 'rgba(138, 138, 138, 0.1)',
rgbColor1 = 'rgba(224, 224, 224, 0.1)',
rgbColor2 = 'rgba(228, 228, 228, 0.05)',
mainColor = '#d6d6d6ff',
secondaryColor = '#757575ff',
textShadow = 'rgba(226, 226, 226, 0.36)',
photoShadowColor = 'rgba(221, 221, 221, 0.3)',
},
},
}
local success, ui = pcall(lib.load, 'data.ui')
if not success or type(ui) ~= 'table' then
warn('unable to load data/ui.lua - using default interface settings')
ui = default
end
for key, value in pairs(default) do
if type(value) == 'table' then
if type(ui[key]) ~= 'table' then ui[key] = value end
elseif ui[key] == nil then
ui[key] = value
end
end
if type(ui.hotbar) ~= 'table' then ui.hotbar = default.hotbar end
if type(ui.grid.defaults) ~= 'table' then ui.grid.defaults = default.grid.defaults end
if type(ui.grid.defaultSize) ~= 'table' then ui.grid.defaultSize = default.grid.defaultSize end
if type(ui.clothing.slots) ~= 'table' then ui.clothing.slots = default.clothing.slots end
if type(ui.rarity.tiers) ~= 'table' then ui.rarity.tiers = default.rarity.tiers end
local layout = GetConvar('inventory:layout', '')
local gridcolumns = GetConvarInt('inventory:gridcolumns', 0)
local clothing = GetConvarInt('inventory:clothing', -1)
local hotbar = GetConvarInt('inventory:hotbar', -1)
local rarity = GetConvarInt('inventory:rarity', -1)
local dim = GetConvarInt('inventory:dim', -1)
local theme = GetConvar('inventory:theme', '')
if layout ~= '' then ui.layout = layout end
if gridcolumns > 0 then ui.grid.columns = gridcolumns end
if clothing >= 0 then ui.clothing.enabled = clothing == 1 end
if hotbar >= 0 then ui.hotbar.enabled = hotbar == 1 end
if rarity >= 0 then ui.rarity.enabled = rarity == 1 end
if type(ui.dim) ~= 'table' then ui.dim = {} end
if dim >= 0 then ui.dim.enabled = dim == 1 end
if theme ~= '' then ui.theme = theme end
if ui.layout ~= 'slots' and ui.layout ~= 'grid' then
warn(("unknown inventory layout '%s' - falling back to 'slots'"):format(tostring(ui.layout)))
ui.layout = 'slots'
end
local columns = math.floor(tonumber(ui.grid.columns) or default.grid.columns)
ui.grid.columns = columns < 5 and 5 or columns > 14 and 14 or columns
ui.grid.allowRotate = ui.grid.allowRotate ~= false
ui.clothing.enabled = ui.clothing.enabled ~= false
ui.hotbar.enabled = ui.hotbar.enabled ~= false
local hotbarSlots = math.floor(tonumber(ui.hotbar.slots) or default.hotbar.slots)
ui.hotbar.slots = hotbarSlots < 0 and 0 or hotbarSlots > 5 and 5 or hotbarSlots
ui.rarity.enabled = ui.rarity.enabled ~= false
ui.dim.enabled = ui.dim.enabled ~= false
if type(ui.rarity.default) ~= 'string' or not ui.rarity.tiers[ui.rarity.default] then
warn(("unknown default item rarity '%s' - falling back to 'common'"):format(tostring(ui.rarity.default)))
ui.rarity.tiers.common = ui.rarity.tiers.common or default.rarity.tiers.common
ui.rarity.default = 'common'
end
if type(ui.themes[ui.theme]) ~= 'table' then
warn(("unknown inventory theme '%s' - falling back to 'white'"):format(tostring(ui.theme)))
ui.themes.white = ui.themes.white or default.themes.white
ui.theme = 'white'
end
local slots = {}
for i = 1, #ui.clothing.slots do
local slot = ui.clothing.slots[i]
if type(slot) == 'table' and type(slot.name) == 'string' then
slots[#slots + 1] = {
name = slot.name,
label = type(slot.label) == 'string' and slot.label or slot.name,
side = slot.side == 'right' and 'right' or 'left',
wearable = slot.wearable ~= false,
}
else
warn(('ignoring malformed clothing slot at index %s in data/ui.lua'):format(i))
end
end
ui.clothing.slots = slots
local MAX_COLOR_LENGTH = 64
---@param value string
---@param max number
---@return boolean
local function inColorRange(value, max)
-- The patterns below only ever capture plain digits, so `tonumber` cannot see `0x`/`1e9`.
local number = tonumber(value)
return number ~= nil and number >= 0 and number <= max
end
---`#rgb`, `#rrggbb` or `#rrggbbaa`. Lua's `$` anchors the whole subject, so a trailing newline
---or `; background-image: ...` cannot slip past.
---@param value string
---@return boolean
local function isHexColor(value)
local hex = value:match('^#(%x+)$')
if not hex then return false end
local length = #hex
return length == 3 or length == 6 or length == 8
end
---`rgb(r, g, b)` or `rgba(r, g, b, a)` with integer channels 0-255 and alpha 0-1.
---@param value string
---@return boolean
local function isRgbColor(value)
local r, g, b = value:match('^rgb%(%s*(%d+)%s*,%s*(%d+)%s*,%s*(%d+)%s*%)$')
if r then
return inColorRange(r, 255) and inColorRange(g, 255) and inColorRange(b, 255)
end
local a
r, g, b, a = value:match('^rgba%(%s*(%d+)%s*,%s*(%d+)%s*,%s*(%d+)%s*,%s*(%d*%.?%d+)%s*%)$')
if not r then return false end
return inColorRange(r, 255) and inColorRange(g, 255) and inColorRange(b, 255) and inColorRange(a, 1)
end
---@param value any
---@return boolean
local function isUiColor(value)
if type(value) ~= 'string' then return false end
local length = #value
if length < 4 or length > MAX_COLOR_LENGTH then return false end
return isHexColor(value) or isRgbColor(value)
end
---Part and type keys travel to the UI as object keys and come back in from other resources via
---@param value any
---@param fallback number
---@return number 0..100
local function clampAnchor(value, fallback)
local number = tonumber(value)
-- NaN fails its own equality test; either infinity would otherwise clamp onto a bound.
if number == nil or number ~= number or number == math.huge or number == -math.huge then
number = fallback
end
return number < 0 and 0 or number > 100 and 100 or number
end
if ui.layout == 'grid' then
local gridColumns = tonumber(ui.grid and ui.grid.columns)
if gridColumns then
gridColumns = math.min(14, math.max(5, math.floor(gridColumns)))
local gridRows = tonumber(ui.grid and ui.grid.rows)
local containerRows = tonumber(ui.grid and ui.grid.containerRows)
if gridRows then
shared.playerslots = math.max(1, math.floor(gridRows)) * gridColumns
end
if containerRows then
shared.dropslots = math.max(1, math.floor(containerRows)) * gridColumns
end
end
end
---Resolved interface configuration, shared by both sides.
shared.ui = ui
---Number of equipment slots appended after `shared.playerslots`; 0 when clothing is disabled.
shared.equipslots = ui.clothing.enabled and #slots or 0
shared.fastslots = (ui.layout == 'grid' and ui.hotbar.enabled) and ui.hotbar.slots or 0
shared.totalplayerslots = shared.playerslots + shared.equipslots
end
if lib.context == 'server' then
shared.ready = false
return require 'server'
end
require 'client'