1
0
Fork 0
forked from Simnation/Main
Main/resources/[carscripts]/nordi_antidespawn/server/main.lua

140 lines
4.3 KiB
Lua
Raw Normal View History

2025-08-06 17:33:26 +02:00
local QBCore = exports['qb-core']:GetCoreObject()
2025-08-06 18:18:04 +02:00
local vehicles = {}
-- Debug Funktion
local function Debug(msg)
if Config.Debug then
print("[AntiDespawn] " .. msg)
end
end
2025-08-06 17:33:26 +02:00
-- Erstelle Tabelle bei Serverstart
CreateThread(function()
MySQL.query([[
2025-08-06 18:18:04 +02:00
CREATE TABLE IF NOT EXISTS `vehicle_antidespawn` (
2025-08-06 17:33:26 +02:00
`id` int(11) NOT NULL AUTO_INCREMENT,
`plate` varchar(50) NOT NULL,
`model` varchar(50) NOT NULL,
2025-08-06 18:18:04 +02:00
`coords` longtext NOT NULL,
`heading` float NOT NULL,
2025-08-06 17:33:26 +02:00
`fuel` int(11) DEFAULT 100,
`last_updated` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `plate` (`plate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
]])
2025-08-06 18:18:04 +02:00
Debug("Datenbank initialisiert")
-- Lade alle Fahrzeuge aus der Datenbank
MySQL.query("SELECT * FROM vehicle_antidespawn", {}, function(results)
if results and #results > 0 then
Debug("Lade " .. #results .. " Fahrzeuge aus der Datenbank")
for _, vehicle in pairs(results) do
vehicles[vehicle.plate] = {
model = vehicle.model,
coords = json.decode(vehicle.coords),
heading = vehicle.heading,
fuel = vehicle.fuel,
last_updated = vehicle.last_updated
}
end
end
end)
2025-08-06 17:33:26 +02:00
end)
2025-08-06 18:18:04 +02:00
-- Registriere ein Fahrzeug
RegisterNetEvent('antidespawn:server:registerVehicle', function(plate, model, coords, heading)
2025-08-06 17:33:26 +02:00
local src = source
2025-08-06 18:18:04 +02:00
if not plate or not model or not coords then
Debug("Ungültige Daten beim Registrieren eines Fahrzeugs")
return
end
2025-08-06 17:33:26 +02:00
2025-08-06 18:18:04 +02:00
vehicles[plate] = {
model = model,
coords = coords,
heading = heading,
fuel = 100,
last_updated = os.time()
}
2025-08-06 17:33:26 +02:00
2025-08-06 18:18:04 +02:00
MySQL.query("INSERT INTO vehicle_antidespawn (plate, model, coords, heading, fuel) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE coords = VALUES(coords), heading = VALUES(heading), last_updated = CURRENT_TIMESTAMP", {
plate,
model,
json.encode(coords),
heading,
100
2025-08-06 17:33:26 +02:00
})
2025-08-06 18:18:04 +02:00
Debug("Fahrzeug registriert: " .. plate)
end)
-- Aktualisiere ein Fahrzeug
RegisterNetEvent('antidespawn:server:updateVehicle', function(plate, coords, heading)
if not vehicles[plate] then return end
vehicles[plate].coords = coords
vehicles[plate].heading = heading
vehicles[plate].last_updated = os.time()
MySQL.query("UPDATE vehicle_antidespawn SET coords = ?, heading = ?, last_updated = CURRENT_TIMESTAMP WHERE plate = ?", {
json.encode(coords),
heading,
plate
})
Debug("Fahrzeug aktualisiert: " .. plate)
2025-08-06 17:33:26 +02:00
end)
2025-08-06 18:18:04 +02:00
-- Entferne ein Fahrzeug
RegisterNetEvent('antidespawn:server:removeVehicle', function(plate)
if not vehicles[plate] then return end
vehicles[plate] = nil
MySQL.query("DELETE FROM vehicle_antidespawn WHERE plate = ?", {
plate
})
Debug("Fahrzeug entfernt: " .. plate)
end)
-- Lade Fahrzeuge für einen Spieler
RegisterNetEvent('antidespawn:server:loadVehicles', function()
2025-08-06 17:33:26 +02:00
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end
2025-08-06 18:18:04 +02:00
local playerCoords = GetEntityCoords(GetPlayerPed(src))
2025-08-06 17:33:26 +02:00
2025-08-06 18:18:04 +02:00
-- Lade nur Fahrzeuge in der Nähe des Spielers
for plate, vehicle in pairs(vehicles) do
local distance = #(playerCoords - vector3(vehicle.coords.x, vehicle.coords.y, vehicle.coords.z))
if distance < 100.0 then
TriggerClientEvent('antidespawn:client:spawnVehicle', src, {
plate = plate,
model = vehicle.model,
coords = vehicle.coords,
heading = vehicle.heading,
fuel = vehicle.fuel
})
Debug("Fahrzeug für Spieler geladen: " .. plate)
end
2025-08-06 17:33:26 +02:00
end
end)
-- Cleanup alte Einträge (älter als 24 Stunden)
CreateThread(function()
while true do
Wait(3600000) -- 1 Stunde
2025-08-06 18:18:04 +02:00
MySQL.query("DELETE FROM vehicle_antidespawn WHERE last_updated < DATE_SUB(NOW(), INTERVAL 24 HOUR)")
Debug("Alte Fahrzeugeinträge bereinigt")
2025-08-06 17:33:26 +02:00
end
end)