-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathserver.lua
More file actions
339 lines (299 loc) · 11.3 KB
/
Copy pathserver.lua
File metadata and controls
339 lines (299 loc) · 11.3 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
local QBCore = exports['qb-core']:GetCoreObject({ 'Functions', 'Commands' })
-- Functions
local function showWarning(msg)
print(('^3%s: %s^0'):format(Lang:t('general.warning'), msg))
end
local function removeItem(Player, item)
if Config.Consumables[item.name] then
Player.RemoveItem(item.name, item.amount >= Config.Consumables[item.name] and Config.Consumables[item.name] or 1)
end
end
local function checkAndRemoveItem(Player, item, shouldRemove)
if not item then return false end
if shouldRemove then
removeItem(Player, item)
end
return true
end
local function checkItems(Player, items, needsAll, shouldRemove)
if needsAll == nil then needsAll = true end
local isTable = type(items) == 'table'
local isArray = isTable and table.type(items) == 'array' or false
local totalItems = 0
local count = 0
if isTable then for _ in pairs(items) do totalItems += 1 end else totalItems = #items end
local kvIndex
if isArray then kvIndex = 2 else kvIndex = 1 end
if isTable then
for k, v in pairs(items) do
local itemKV = { k, v }
local item = Player.GetItemByName(itemKV[kvIndex])
if needsAll then
if checkAndRemoveItem(Player, item, false) then
count += 1
end
else
if checkAndRemoveItem(Player, item, shouldRemove) then
return true
end
end
end
if count == totalItems then
for k, v in pairs(items) do
local itemKV = { k, v }
local item = Player.GetItemByName(itemKV[kvIndex])
checkAndRemoveItem(Player, item, shouldRemove)
end
return true
end
else -- Single item as string
local item = Player.GetItemByName(items)
return checkAndRemoveItem(Player, item, shouldRemove)
end
return false
end
local function isAuthorized(Player, door, usedLockpick)
if door.allAuthorized then return true end
if Config.AdminAccess and QBCore.Functions.HasPermission(Player.PlayerData.source, Config.AdminPermission) then
if Config.Warnings then
showWarning(Lang:t('general.warn_admin_privilege_used', { player = Player.PlayerData.name, license = Player.PlayerData.license }))
end
return true
end
if (door.pickable or door.lockpick) and usedLockpick then return true end
if door.authorizedJobs then
if door.authorizedJobs[Player.PlayerData.job.name] and Player.PlayerData.job.grade.level >= door.authorizedJobs[Player.PlayerData.job.name] then
return true
elseif type(door.authorizedJobs[1]) == 'string' then
for _, job in pairs(door.authorizedJobs) do -- Support for old format
if job == Player.PlayerData.job.name then return true end
end
end
end
if door.authorizedGangs then
if door.authorizedGangs[Player.PlayerData.gang.name] and Player.PlayerData.gang.grade.level >= door.authorizedGangs[Player.PlayerData.gang.name] then
return true
elseif type(door.authorizedGangs[1]) == 'string' then
for _, gang in pairs(door.authorizedGangs) do -- Support for old format
if gang == Player.PlayerData.gang.name then return true end
end
end
end
if door.authorizedCitizenIDs then
if door.authorizedCitizenIDs[Player.PlayerData.citizenid] then
return true
elseif type(door.authorizedCitizenIDs[1]) == 'string' then
for _, id in pairs(door.authorizedCitizenIDs) do -- Support for old format
if id == Player.PlayerData.citizenid then return true end
end
end
end
if door.items then return checkItems(Player, door.items, door.needsAllItems, true) end
return false
end
local function SaveDoorStates()
SaveResourceFile(GetCurrentResourceName(), './saves/doorstates.json', json.encode(Config.DoorStates), -1)
end
local function LoadDoorStates()
local DoorStates = LoadResourceFile(GetCurrentResourceName(), './saves/doorstates.json')
if DoorStates then
DoorStates = json.decode(DoorStates)
if not next(DoorStates) then return end
for key, isLocked in pairs(DoorStates) do
if Config.DoorList[key] ~= nil then
Config.DoorList[key].locked = isLocked
end
end
Config.DoorStates = DoorStates
end
end
-- Callbacks
QBCore.Functions.CreateCallback('qb-doorlock:server:setupDoors', function(_, cb)
cb(Config.DoorList)
end)
QBCore.Functions.CreateCallback('qb-doorlock:server:checkItems', function(source, cb, items, needsAll)
local Player = exports['qb-core']:GetPlayer(source)
cb(checkItems(Player, items, needsAll, false))
end)
-- Events
RegisterNetEvent('qb-doorlock:server:updateState', function(doorID, locked, src, usedLockpick, unlockAnyway, enableSounds, enableAnimation, sentSource)
local playerId = sentSource or source
local Player = exports['qb-core']:GetPlayer(playerId)
if not Player then return end
if type(doorID) ~= 'number' and type(doorID) ~= 'string' then
if Config.Warnings then
showWarning((Lang:t('general.warn_wrong_doorid_type', { player = Player.PlayerData.name, license = Player.PlayerData.license, doorID = doorID })))
end
return
end
if type(locked) ~= 'boolean' then
if Config.Warnings then
showWarning((Lang:t('general.warn_wrong_state', { player = Player.PlayerData.name, license = Player.PlayerData.license, state = locked })))
end
return
end
if not Config.DoorList[doorID] then
if Config.Warnings then
showWarning(Lang:t('general.warn_wrong_doorid', { player = Player.PlayerData.name, license = Player.PlayerData.license, doorID = doorID }))
end
return
end
if not unlockAnyway and not isAuthorized(Player, Config.DoorList[doorID], usedLockpick) then
if Config.Warnings then
showWarning(Lang:t('general.warn_no_authorisation', { player = Player.PlayerData.name, license = Player.PlayerData.license, doorID = doorID }))
end
return
end
Config.DoorList[doorID].locked = locked
if Config.DoorStates[doorID] == nil then Config.DoorStates[doorID] = locked elseif Config.DoorStates[doorID] ~= locked then Config.DoorStates[doorID] = nil end
TriggerClientEvent('qb-doorlock:client:setState', -1, playerId, doorID, locked, src or false, enableSounds, enableAnimation)
if not Config.DoorList[doorID].autoLock then return end
SetTimeout(Config.DoorList[doorID].autoLock, function()
if Config.DoorList[doorID].locked then return end
Config.DoorList[doorID].locked = true
if Config.DoorStates[doorID] == nil then Config.DoorStates[doorID] = locked elseif Config.DoorStates[doorID] ~= locked then Config.DoorStates[doorID] = nil end
TriggerClientEvent('qb-doorlock:client:setState', -1, playerId, doorID, true, src or false, enableSounds, enableAnimation)
end)
end)
RegisterNetEvent('qb-doorlock:server:saveNewDoor', function(data, doubleDoor)
local src = source
if not QBCore.Functions.HasPermission(src, Config.CommandPermission) and not IsPlayerAceAllowed(src, 'command') then
if Config.Warnings then
showWarning(Lang:t('general.warn_no_permission_newdoor', { player = GetPlayerName(src), license = QBCore.Functions.GetIdentifier(src, 'license'), source = src }))
end
return
end
local Player = exports['qb-core']:GetPlayer(src)
if not Player then return end
local configData = {}
local jobs, jobGrade, gangs, gangGrade, cids, items, doorType, identifier
if data.job then
jobGrade = tonumber(data.jobGrade) or 0
configData.authorizedJobs = { [data.job] = jobGrade }
jobs = "['" .. data.job .. "'] = " .. jobGrade
end
if data.gang then
gangGrade = tonumber(data.gangGrade) or 0
configData.authorizedGangs = { [data.gang] = gangGrade }
gangs = "['" .. data.gang .. "'] = " .. gangGrade
end
if data.cid then
configData.authorizedCitizenIDs = { [data.cid] = true }
cids = "['" .. data.cid .. "'] = true"
end
if data.item then
configData.items = { [data.item] = 1 }
items = "['" .. data.item .. "'] = 1"
end
configData.locked = data.locked
configData.pickable = data.pickable
configData.cantUnlock = data.cantunlock
configData.hideLabel = data.hidelabel
configData.distance = data.distance
configData.doorType = data.doortype
configData.doorRate = 1.0
configData.doorLabel = data.doorlabel
doorType = "'" .. data.doortype .. "'"
identifier = data.configfile .. '-' .. data.dooridentifier
if doubleDoor then
configData.doors = {
{ objName = data.model[1], objYaw = data.heading[1], objCoords = data.coords[1] },
{ objName = data.model[2], objYaw = data.heading[2], objCoords = data.coords[2] }
}
else
configData.objName = data.model
configData.objYaw = data.heading
configData.objCoords = data.coords
configData.fixText = false
end
local path = GetResourcePath(GetCurrentResourceName())
if data.configfile then
local tempfile, err = io.open(path:gsub('//', '/') .. '/configs/' .. string.gsub(data.configfile, '.lua', '') .. '.lua', 'a+')
if tempfile then
tempfile:close()
path = path:gsub('//', '/') .. '/configs/' .. string.gsub(data.configfile, '.lua', '') .. '.lua'
else
return error(err)
end
else
path = path:gsub('//', '/') .. '/config.lua'
end
local file = io.open(path, 'a+')
local label = '\n\n-- ' .. data.dooridentifier .. ' ' .. Lang:t('general.created_by') .. ' ' .. Player.PlayerData.name .. "\nConfig.DoorList['" .. identifier .. "'] = {"
file:write(label)
for k, v in pairs(configData) do
if k == 'authorizedJobs' or k == 'authorizedGangs' or k == 'authorizedCitizenIDs' or k == 'items' then
local auth = jobs
if k == 'authorizedGangs' then
auth = gangs
elseif k == 'authorizedCitizenIDs' then
auth = cids
elseif k == 'items' then
auth = items
end
local str = ('\n %s = { %s },'):format(k, auth)
file:write(str)
elseif k == 'doors' then
local doors = {}
for i = 1, 2 do
doors[i] = (' {objName = %s, objYaw = %s, objCoords = %s}'):format(configData.doors[i].objName, configData.doors[i].objYaw, configData.doors[i].objCoords)
end
local str = ('\n %s = {\n %s,\n %s\n },'):format(k, doors[1], doors[2])
file:write(str)
elseif k == 'doorType' then
local str = ('\n %s = %s,'):format(k, doorType)
file:write(str)
elseif k == 'doorLabel' then
local str = ("\n %s = '%s',"):format(k, v)
file:write(str)
else
local str = ('\n %s = %s,'):format(k, v)
file:write(str)
end
end
file:write('\n}')
file:close()
Config.DoorList[identifier] = configData
TriggerClientEvent('qb-doorlock:client:newDoorAdded', -1, configData, identifier, src)
end)
AddEventHandler('onResourceStart', function(resource)
if GetCurrentResourceName() == resource and Config.PersistentDoorStates then
CreateThread(function()
LoadDoorStates()
Wait(1000)
while true do
Wait(Config.PersistentSaveInternal)
SaveDoorStates()
end
end)
end
end)
AddEventHandler('onResourceStop', function(resource)
if GetCurrentResourceName() == resource and Config.PersistentDoorStates then
SaveDoorStates()
end
end)
RegisterNetEvent('txAdmin:events:scheduledRestart', function(eventData)
if eventData.secondsRemaining == 60 then
CreateThread(function()
Wait(45000)
SaveDoorStates()
end)
else
SaveDoorStates()
end
end)
RegisterNetEvent('qb-doorlock:server:removeLockpick', function(type)
local Player = exports['qb-core']:GetPlayer(source)
if not Player then return end
if type == 'advancedlockpick' or type == 'lockpick' then
Player.RemoveItem(type, 1)
end
end)
-- Commands
QBCore.Commands.Add('newdoor', Lang:t('general.newdoor_command_description'), {}, false, function(source)
TriggerClientEvent('qb-doorlock:client:addNewDoor', source)
end, Config.CommandPermission)
QBCore.Commands.Add('doordebug', Lang:t('general.doordebug_command_description'), {}, false, function(source)
TriggerClientEvent('qb-doorlock:client:ToggleDoorDebug', source)
end, Config.CommandPermission)