-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
284 lines (244 loc) · 9.44 KB
/
server.lua
File metadata and controls
284 lines (244 loc) · 9.44 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
local QBCore, ESX, QBox = nil, nil, nil
local framework = "none"
local Discord = nil
-- Load Discord module
local function LoadDiscord()
local status, discord = pcall(require, 'discord')
if status then
return discord
else
print("^1[VehicleDelete]^7 Failed to load Discord module: " .. tostring(discord))
return {
SendWebhook = function() end,
SendCleanupStart = function() end,
SendCleanupComplete = function() end,
LogVehicleDeletion = function() end
}
end
end
-- Framework detection
CreateThread(function()
while not framework do
if GetResourceState('qb-core') == 'started' then
QBCore = exports['qb-core']:GetCoreObject()
framework = "qb"
elseif GetResourceState('es_extended') == 'started' then
ESX = exports['es_extended']:getSharedObject()
framework = "esx"
elseif GetResourceState('qbx_core') == 'started' then
QBox = exports['qbx_core']:GetCoreObject()
framework = "qbox"
end
if Config.ForceFramework then
framework = Config.ForceFramework:lower()
end
if framework ~= "none" then
if Config.Debug then
print("^3[VehicleDelete]^7 Detected framework: " .. framework:upper())
end
break
end
Wait(1000)
end
end)
-- Check if a vehicle is in a safe zone
local function IsInSafezone(coords)
for _, zone in ipairs(Config.SafeZones) do
local distance = #(coords - zone.coords)
if distance <= zone.radius then
return true
end
end
return false
end
-- Check if a vehicle is protected
local function IsVehicleProtected(vehicleModel)
for _, protectedModel in ipairs(Config.ProtectedVehicles) do
if string.find(string.lower(vehicleModel), string.lower(protectedModel)) then
return true
end
end
return false
end
-- Check if a vehicle is occupied by any player
local function IsVehicleOccupied(vehicle)
-- Check driver seat (-1)
if GetPedInVehicleSeat(vehicle, -1) ~= 0 then
return true
end
-- Check all passenger seats
local maxSeats = GetVehicleMaxNumberOfPassengers(vehicle)
for i = 0, maxSeats - 1 do
if GetPedInVehicleSeat(vehicle, i) ~= 0 then
return true
end
end
return false
end
-- Main function to check and delete vehicles
local function CheckAndDeleteVehicles()
local vehicles = GetGamePool('CVehicle')
local deletedCount = 0
local totalChecked = #vehicles
-- Send start notification to Discord
if Discord then
Discord:SendCleanupStart(totalChecked)
end
for _, vehicle in ipairs(vehicles) do
local shouldDelete = false
local vehicleCoords = GetEntityCoords(vehicle)
local vehicleModel = GetEntityModel(vehicle)
local displayName = GetDisplayNameFromVehicleModel(vehicleModel)
-- Check if vehicle is in a safe zone
if IsInSafezone(vehicleCoords) then
if Config.Debug then
print("^3[VehicleDelete]^7 Skipping vehicle in safe zone: " .. displayName)
end
goto continue
end
-- Check if vehicle is occupied by any player
if IsVehicleOccupied(vehicle) then
if Config.Debug then
print("^3[VehicleDelete]^7 Skipping occupied vehicle: " .. displayName)
end
goto continue
end
-- Skip protected vehicles
if IsVehicleProtected(displayName) then
if Config.Debug then
print("^3[VehicleDelete]^7 Skipping protected vehicle: " .. displayName)
end
goto continue
end
-- Delete the vehicle (we've already checked it's not in a safe zone, not protected, and not occupied)
local netId = NetworkGetNetworkIdFromEntity(vehicle)
if netId then
if Config.Debug then
print("^3[VehicleDelete]^7 Deleting vehicle: " .. displayName)
end
-- Notify nearby players that a vehicle is being deleted
local players = GetPlayers()
for _, playerId in ipairs(players) do
local playerPed = GetPlayerPed(playerId)
local playerCoords = GetEntityCoords(playerPed)
local distance = #(playerCoords - vehicleCoords)
-- Notify players within 50 units of the deleted vehicle
if distance < 50.0 then
TriggerClientEvent('vehicleDelete:showDeletionNotification', playerId, displayName)
end
end
-- Delete the vehicle
TriggerClientEvent('vehicleDelete:deleteVehicle', -1, netId)
deletedCount = deletedCount + 1
end
::continue::
end
-- Send completion notification to Discord
if Discord then
Discord:SendCleanupComplete(deletedCount, totalChecked)
end
if Config.Debug then
if deletedCount > 0 then
print("^3[VehicleDelete]^7 Deleted " .. deletedCount .. " out of " .. totalChecked .. " vehicles")
else
print("^3[VehicleDelete]^7 No vehicles were deleted")
end
end
end
-- Function to send notification to all players
local function SendNotification(message, type)
if framework == "esx" then
TriggerClientEvent('esx:showNotification', -1, message)
elseif framework == "qb" or framework == "qbox" then
TriggerClientEvent('QBCore:Notify', -1, message, type or 'primary')
end
-- Fallback for any other framework or direct chat message
TriggerClientEvent('chat:addMessage', -1, {
color = {255, 0, 0},
multiline = true,
args = {"Vehicle Cleanup", message}
})
end
-- Initialize Discord module
Discord = LoadDiscord()
-- Function to check if it's time for scheduled cleanup
local function IsScheduledCleanupTime()
if not Config.Advanced or not Config.Advanced.schedule or not Config.Advanced.schedule.enabled then
return true -- Run on interval if scheduling is disabled
end
local currentTime = os.date("%H:%M")
for _, time in ipairs(Config.Advanced.schedule.times) do
if currentTime == time then
return true
end
end
return false
end
-- Start the deletion loop with notifications
CreateThread(function()
local nextCheck = GetGameTimer()
local tenMinWarningSent = false
local oneMinWarningSent = false
while true do
local currentTime = GetGameTimer()
local timeUntilNextCheck = (nextCheck + Config.CheckInterval - currentTime) / 1000 -- Convert to seconds
-- Check if we're 10 minutes away from next cleanup
if not tenMinWarningSent and timeUntilNextCheck <= 600 and timeUntilNextCheck > 300 then
SendNotification("Vehicle cleanup will occur in 10 minutes. Please move your vehicles to safe zones.", 'inform')
tenMinWarningSent = true
oneMinWarningSent = false
-- Check if we're 1 minute away from next cleanup
elseif not oneMinWarningSent and timeUntilNextCheck <= 60 and timeUntilNextCheck > 30 then
SendNotification("Vehicle cleanup in 60 seconds! Move your vehicles to safe zones now!", 'error')
oneMinWarningSent = true
end
-- Reset flags after cleanup
if timeUntilNextCheck > 600 then -- After cleanup, before next 10 min warning
tenMinWarningSent = false
oneMinWarningSent = false
end
-- Perform the actual cleanup
if currentTime >= nextCheck then
if framework ~= "none" and IsScheduledCleanupTime() then
CheckAndDeleteVehicles()
-- Send cleanup complete message
SendNotification("Vehicle cleanup complete!", 'success')
end
nextCheck = currentTime + Config.CheckInterval
end
Wait(10000) -- Check every 10 seconds for better timing accuracy
end
end)
-- Client event to delete vehicles
RegisterNetEvent('vehicleDelete:deleteVehicle')
AddEventHandler('vehicleDelete:deleteVehicle', function(netId, vehicleModel, coords)
local vehicle = NetworkGetEntityFromNetworkId(netId)
if DoesEntityExist(vehicle) then
-- Log the deletion to Discord
if Discord and Config.DiscordWebhook.enabled then
Discord:LogVehicleDeletion(vehicleModel, coords, "Auto cleanup")
end
-- Delete the vehicle
DeleteEntity(vehicle)
end
end)
-- Command to manually trigger vehicle cleanup (for admins)
RegisterCommand('cleanupvehicles', function(source, args, rawCommand)
if source ~= 0 then -- Player triggered
local xPlayer = nil
if framework == "esx" then
xPlayer = ESX.GetPlayerFromId(source)
elseif framework == "qb" then
xPlayer = QBCore.Functions.GetPlayer(source)
elseif framework == "qbox" then
xPlayer = QBox.Functions.GetPlayer(source)
end
if not xPlayer or not (xPlayer.getGroup and xPlayer.getGroup() == 'admin') then
return
end
end
if Config.Debug then
print("^3[VehicleDelete]^7 Manual vehicle cleanup triggered")
end
CheckAndDeleteVehicles()
end, false)