forked from qbcore-framework/qb-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.lua
316 lines (286 loc) · 10.6 KB
/
events.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
315
316
-- Player joined
RegisterServerEvent("QBCore:PlayerJoined")
AddEventHandler('QBCore:PlayerJoined', function()
local src = source
SetPlayerRoutingBucket(src, 0)
end)
AddEventHandler('playerDropped', function(reason)
local src = source
if QBCore.Players[src] then
local Player = QBCore.Players[src]
TriggerEvent("qb-log:server:CreateLog", "joinleave", "Dropped", "red", "**".. GetPlayerName(src) .. "** ("..Player.PlayerData.license..") left..")
Player.Functions.Save()
QBCore.Players[src] = nil
end
end)
local function OnPlayerConnecting(name, setKickReason, deferrals)
local player = source
local license
local identifiers = GetPlayerIdentifiers(player)
deferrals.defer()
-- mandatory wait!
Wait(0)
deferrals.update(string.format("Hello %s. Validating Your Rockstar License", name))
for _, v in pairs(identifiers) do
if string.find(v, 'license') then
license = v
break
end
end
-- mandatory wait!
Wait(2500)
deferrals.update(string.format("Hello %s. We are checking if you are banned.", name))
local isBanned, Reason = QBCore.Functions.IsPlayerBanned(player)
local isLicenseAlreadyInUse = QBCore.Functions.IsLicenseInUse(license)
Wait(2500)
deferrals.update(string.format("Welcome %s to {Server Name}.", name))
if not license then
deferrals.done('No Valid Rockstar License Found')
elseif isBanned then
deferrals.done(Reason)
elseif isLicenseAlreadyInUse then
deferrals.done('Duplicate Rockstar License Found')
else
deferrals.done()
Wait(1000)
TriggerEvent("connectqueue:playerConnect", name, setKickReason, deferrals)
end
--Add any additional defferals you may need!
end
AddEventHandler("playerConnecting", OnPlayerConnecting)
RegisterServerEvent("QBCore:server:CloseServer")
AddEventHandler('QBCore:server:CloseServer', function(reason)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if QBCore.Functions.HasPermission(source, "admin") or QBCore.Functions.HasPermission(source, "god") then
local reason = reason ~= nil and reason or "No reason specified..."
QBCore.Config.Server.closed = true
QBCore.Config.Server.closedReason = reason
TriggerClientEvent("qbadmin:client:SetServerStatus", -1, true)
else
QBCore.Functions.Kick(src, "You don't have permissions for this..", nil, nil)
end
end)
RegisterServerEvent("QBCore:server:OpenServer")
AddEventHandler('QBCore:server:OpenServer', function()
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if QBCore.Functions.HasPermission(source, "admin") or QBCore.Functions.HasPermission(source, "god") then
QBCore.Config.Server.closed = false
TriggerClientEvent("qbadmin:client:SetServerStatus", -1, false)
else
QBCore.Functions.Kick(src, "You don't have permissions for this..", nil, nil)
end
end)
RegisterServerEvent("QBCore:UpdatePlayer")
AddEventHandler('QBCore:UpdatePlayer', function(data)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player ~= nil then
Player.PlayerData.position = data.position
local newHunger = Player.PlayerData.metadata["hunger"] - QBCore.Config.Player.HungerRate
local newThirst = Player.PlayerData.metadata["thirst"] - QBCore.Config.Player.ThirstRate
if newHunger <= 0 then newHunger = 0 end
if newThirst <= 0 then newThirst = 0 end
Player.Functions.SetMetaData("thirst", newThirst)
Player.Functions.SetMetaData("hunger", newHunger)
TriggerClientEvent("hud:client:UpdateNeeds", src, newHunger, newThirst)
Player.Functions.Save()
end
end)
RegisterServerEvent("QBCore:UpdatePlayerPosition")
AddEventHandler("QBCore:UpdatePlayerPosition", function(position)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player ~= nil then
Player.PlayerData.position = position
end
end)
RegisterServerEvent("QBCore:Server:TriggerCallback")
AddEventHandler('QBCore:Server:TriggerCallback', function(name, ...)
local src = source
QBCore.Functions.TriggerCallback(name, src, function(...)
TriggerClientEvent("QBCore:Client:TriggerCallback", src, name, ...)
end, ...)
end)
RegisterServerEvent("QBCore:Server:UseItem")
AddEventHandler('QBCore:Server:UseItem', function(item)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if item ~= nil and item.amount > 0 then
if QBCore.Functions.CanUseItem(item.name) then
QBCore.Functions.UseItem(src, item)
end
end
end)
RegisterServerEvent("QBCore:Server:RemoveItem")
AddEventHandler('QBCore:Server:RemoveItem', function(itemName, amount, slot)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
Player.Functions.RemoveItem(itemName, amount, slot)
end)
RegisterServerEvent("QBCore:Server:AddItem")
AddEventHandler('QBCore:Server:AddItem', function(itemName, amount, slot, info)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
Player.Functions.AddItem(itemName, amount, slot, info)
end)
RegisterServerEvent('QBCore:Server:SetMetaData')
AddEventHandler('QBCore:Server:SetMetaData', function(meta, data)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if meta == "hunger" or meta == "thirst" then
if data > 100 then
data = 100
end
end
if Player ~= nil then
Player.Functions.SetMetaData(meta, data)
end
TriggerClientEvent("hud:client:UpdateNeeds", src, Player.PlayerData.metadata["hunger"], Player.PlayerData.metadata["thirst"])
end)
AddEventHandler('chatMessage', function(source, n, message)
if string.sub(message, 1, 1) == "/" then
local args = QBCore.Shared.SplitStr(message, " ")
local command = string.gsub(args[1]:lower(), "/", "")
CancelEvent()
if QBCore.Commands.List[command] ~= nil then
local Player = QBCore.Functions.GetPlayer(tonumber(source))
if Player ~= nil then
table.remove(args, 1)
if (QBCore.Functions.HasPermission(source, "god") or QBCore.Functions.HasPermission(source, QBCore.Commands.List[command].permission)) then
if (QBCore.Commands.List[command].argsrequired and #QBCore.Commands.List[command].arguments ~= 0 and args[#QBCore.Commands.List[command].arguments] == nil) then
TriggerClientEvent('QBCore:Notify', source, "All arguments must be filled out!", "error")
local agus = ""
for name, help in pairs(QBCore.Commands.List[command].arguments) do
agus = agus .. " ["..help.name.."]"
end
TriggerClientEvent('chatMessage', source, "/"..command, false, agus)
else
QBCore.Commands.List[command].callback(source, args)
end
else
TriggerClientEvent('QBCore:Notify', source, "No Access To This Command", "error")
end
end
end
end
end)
RegisterServerEvent('QBCore:CallCommand')
AddEventHandler('QBCore:CallCommand', function(command, args)
if QBCore.Commands.List[command] ~= nil then
local Player = QBCore.Functions.GetPlayer(tonumber(source))
if Player ~= nil then
if (QBCore.Functions.HasPermission(source, "god")) or (QBCore.Functions.HasPermission(source, QBCore.Commands.List[command].permission)) or (QBCore.Commands.List[command].permission == Player.PlayerData.job.name) then
if (QBCore.Commands.List[command].argsrequired and #QBCore.Commands.List[command].arguments ~= 0 and args[#QBCore.Commands.List[command].arguments] == nil) then
TriggerClientEvent('QBCore:Notify', source, "All arguments must be filled out!", "error")
local agus = ""
for name, help in pairs(QBCore.Commands.List[command].arguments) do
agus = agus .. " ["..help.name.."]"
end
TriggerClientEvent('chatMessage', source, "/"..command, false, agus)
else
QBCore.Commands.List[command].callback(source, args)
end
else
TriggerClientEvent('QBCore:Notify', source, "No Access To This Command", "error")
end
end
end
end)
RegisterServerEvent("QBCore:AddCommand")
AddEventHandler('QBCore:AddCommand', function(name, help, arguments, argsrequired, callback, persmission)
QBCore.Commands.Add(name, help, arguments, argsrequired, callback, persmission)
end)
RegisterServerEvent("QBCore:ToggleDuty")
AddEventHandler('QBCore:ToggleDuty', function()
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if Player.PlayerData.job.onduty then
Player.Functions.SetJobDuty(false)
TriggerClientEvent('QBCore:Notify', src, "You are now off duty!")
else
Player.Functions.SetJobDuty(true)
TriggerClientEvent('QBCore:Notify', src, "You are now on duty!")
end
TriggerClientEvent("QBCore:Client:SetDuty", src, Player.PlayerData.job.onduty)
end)
Citizen.CreateThread(function()
local result = exports.oxmysql:fetchSync('SELECT * FROM permissions', {})
if result[1] ~= nil then
for k, v in pairs(result) do
QBCore.Config.Server.PermissionList[v.license] = {
license = v.license,
permission = v.permission,
optin = true,
}
end
end
end)
QBCore.Functions.CreateCallback('QBCore:HasItem', function(source, cb, items, amount)
local retval = false
local Player = QBCore.Functions.GetPlayer(source)
if Player ~= nil then
if type(items) == 'table' then
local count = 0
local finalcount = 0
for k, v in pairs(items) do
if type(k) == 'string' then
finalcount = 0
for i, _ in pairs(items) do
if i then finalcount = finalcount + 1 end
end
local item = Player.Functions.GetItemByName(k)
if item ~= nil then
if item.amount >= v then
count = count + 1
if count == finalcount then
retval = true
end
end
end
else
finalcount = #items
local item = Player.Functions.GetItemByName(v)
if item ~= nil then
if amount ~= nil then
if item.amount >= amount then
count = count + 1
if count == finalcount then
retval = true
end
end
else
count = count + 1
if count == finalcount then
retval = true
end
end
end
end
end
else
local item = Player.Functions.GetItemByName(items)
if item ~= nil then
if amount ~= nil then
if item.amount >= amount then
retval = true
end
else
retval = true
end
end
end
end
cb(retval)
end)
RegisterServerEvent('QBCore:Command:CheckOwnedVehicle')
AddEventHandler('QBCore:Command:CheckOwnedVehicle', function(VehiclePlate)
if VehiclePlate ~= nil then
local result = exports.oxmysql:fetchSync('SELECT * FROM player_vehicles WHERE plate = ?', { VehiclePlate })
if result[1] ~= nil then
exports.oxmysql:execute('UPDATE player_vehicles SET state = ? WHERE citizenid = ?', { 1, result[1].citizenid })
TriggerEvent('qb-garages:server:RemoveVehicle', result[1].citizenid, VehiclePlate)
end
end
end)