local QBCore = exports['qb-core']:GetCoreObject() local vehicles = {} -- Debug Funktion local function Debug(msg) if Config.Debug then print("[AntiDespawn] " .. msg) end end -- Erstelle Tabelle bei Serverstart CreateThread(function() -- 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) Debug("Datenbank initialisiert") -- Warte kurz, bis die Tabelle aktualisiert wurde Wait(1000) -- 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, mods = vehicle.mods and json.decode(vehicle.mods) or nil, last_updated = vehicle.last_updated } end end end) end) -- Registriere ein Fahrzeug RegisterNetEvent('antidespawn:server:registerVehicle', function(plate, model, coords, heading, mods) local src = source if not plate or not model or not coords then Debug("Ungültige Daten beim Registrieren eines Fahrzeugs") return end -- Stelle sicher, dass das Modell als Zahl gespeichert wird if type(model) == "string" then model = tonumber(model) or model end -- 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) -- 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 return end vehicles[plate] = { model = model, coords = coords, heading = heading, fuel = 100, mods = mods, last_updated = os.time() } MySQL.query("INSERT INTO vehicle_antidespawn (plate, model, coords, heading, fuel