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

106 lines
3.9 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()
2025-08-06 19:45:59 +02:00
-- Prüfe ob die Tabelle existiert
MySQL.query("SHOW TABLES LIKE 'vehicle_antidespawn'", {}, function(result)
if result and #result > 0 then
-- Tabelle existiert, prüfe ob das mods-Feld existiert
MySQL.query("SHOW COLUMNS FROM vehicle_antidespawn LIKE 'mods'", {}, function(columns)
if columns and #columns == 0 then
-- mods-Feld existiert nicht, füge es hinzu
Debug("Füge mods-Feld zur Tabelle hinzu...")
MySQL.query("ALTER TABLE vehicle_antidespawn ADD COLUMN mods LONGTEXT DEFAULT NULL", {})
end
end)
else
-- Tabelle existiert nicht, erstelle sie
Debug("Erstelle Datenbank-Tabelle...")
MySQL.query([[
CREATE TABLE IF NOT EXISTS `vehicle_antidespawn` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`plate` varchar(50) NOT NULL,
`model` varchar(50) NOT NULL,
`coords` longtext NOT NULL,
`heading` float NOT NULL,
`fuel` int(11) DEFAULT 100,
`mods` longtext DEFAULT NULL,
`last_updated` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `plate` (`plate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
]])
end
end)
2025-08-06 18:18:04 +02:00
Debug("Datenbank initialisiert")
2025-08-06 19:45:59 +02:00
-- Warte kurz, bis die Tabelle aktualisiert wurde
Wait(1000)
2025-08-06 18:18:04 +02:00
-- 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,
2025-08-06 19:45:59 +02:00
mods = vehicle.mods and json.decode(vehicle.mods) or nil,
2025-08-06 18:18:04 +02:00
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
2025-08-06 18:50:02 +02:00
RegisterNetEvent('antidespawn:server:registerVehicle', function(plate, model, coords, heading, mods)
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 19:16:18 +02:00
-- Stelle sicher, dass das Modell als Zahl gespeichert wird
if type(model) == "string" then
model = tonumber(model) or model
end
2025-08-06 18:32:55 +02:00
-- Prüfe ob Fahrzeug in der Garage ist
MySQL.query('SELECT * FROM player_vehicles WHERE plate = ? AND state = ?', {plate, 1}, function(result)
if result and #result > 0 then
Debug("Fahrzeug ist in der Garage, nicht registrieren: " .. plate)
2025-08-06 19:45:59 +02:00
-- Entferne aus Anti-Despawn Datenbank falls vorhanden
if vehicles[plate] then
vehicles[plate] = nil
MySQL.query("DELETE FROM vehicle_antidespawn WHERE plate = ?", {plate})
Debug("Fahrzeug aus Anti-Despawn entfernt: " .. plate)
end
2025-08-06 18:32:55 +02:00
return
end
vehicles[plate] = {
model = model,
coords = coords,
heading = heading,
fuel = 100,
2025-08-06 18:50:02 +02:00
mods = mods,
2025-08-06 18:32:55 +02:00
last_updated = os.time()
}
2025-08-06 19:45:59 +02:00
MySQL.query("INSERT INTO vehicle_antidespawn (plate, model, coords, heading, fuel