From cf533de9037eacba19e6c305ddcd5b828045031a Mon Sep 17 00:00:00 2001 From: Nordi98 Date: Wed, 6 Aug 2025 19:16:18 +0200 Subject: [PATCH] ed --- .../nordi_antidespawn/client/main.lua | 56 ++++++++++++++++++- .../nordi_antidespawn/server/main.lua | 32 ++++++++++- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/resources/[carscripts]/nordi_antidespawn/client/main.lua b/resources/[carscripts]/nordi_antidespawn/client/main.lua index e51898065..06229eb5b 100644 --- a/resources/[carscripts]/nordi_antidespawn/client/main.lua +++ b/resources/[carscripts]/nordi_antidespawn/client/main.lua @@ -377,25 +377,47 @@ RegisterNetEvent('antidespawn:client:spawnVehicle', function(data) return end - -- Spawne Fahrzeug + -- Konvertiere Modell zu Hash wenn nötig local modelHash = data.model + if type(modelHash) == "string" then + -- Versuche den String als Hash zu interpretieren + if tonumber(modelHash) then + modelHash = tonumber(modelHash) + else + -- Versuche den String als Modellnamen zu interpretieren + modelHash = GetHashKey(modelHash) + end + end + + Debug("Versuche Modell zu laden: " .. tostring(modelHash)) + + -- Prüfe ob Modell existiert + if not IsModelInCdimage(modelHash) then + Debug("Modell existiert nicht in CD Image: " .. tostring(modelHash)) + return + end RequestModel(modelHash) local timeout = 0 while not HasModelLoaded(modelHash) and timeout < 100 do Wait(100) timeout = timeout + 1 + Debug("Warte auf Modell: " .. tostring(timeout) .. "/100") end if HasModelLoaded(modelHash) then + Debug("Modell geladen, erstelle Fahrzeug...") + -- Verwende CREATE_AUTOMOBILE für bessere Persistenz local vehicle if Citizen and Citizen.InvokeNative then -- OneSync Methode + Debug("Verwende OneSync Methode") vehicle = Citizen.InvokeNative(0xAF35D0D2583051B0, modelHash, data.coords.x, data.coords.y, data.coords.z, data.heading, true, true) else -- Fallback + Debug("Verwende Fallback Methode") vehicle = CreateVehicle(modelHash, data.coords.x, data.coords.y, data.coords.z, data.heading, true, false) end @@ -408,6 +430,7 @@ RegisterNetEvent('antidespawn:client:spawnVehicle', function(data) -- Setze Mods if data.mods then + Debug("Setze Fahrzeugmods...") SetVehicleMods(vehicle, data.mods) end @@ -433,10 +456,39 @@ RegisterNetEvent('antidespawn:client:spawnVehicle', function(data) SetModelAsNoLongerNeeded(modelHash) else - Debug("Modell konnte nicht geladen werden: " .. data.plate) + Debug("Modell konnte nicht geladen werden: " .. data.plate .. " (Hash: " .. tostring(modelHash) .. ")") + + -- Versuche es mit einem Standard-Fahrzeug als Fallback + local fallbackModel = GetHashKey("adder") + RequestModel(fallbackModel) + + timeout = 0 + while not HasModelLoaded(fallbackModel) and timeout < 50 do + Wait(100) + timeout = timeout + 1 + end + + if HasModelLoaded(fallbackModel) then + Debug("Verwende Fallback-Modell") + local vehicle = CreateVehicle(fallbackModel, data.coords.x, data.coords.y, data.coords.z, data.heading, true, false) + + if DoesEntityExist(vehicle) then + SetVehicleNumberPlateText(vehicle, data.plate) + PreventDespawn(vehicle) + trackedVehicles[data.plate] = vehicle + lastKnownCoords[data.plate] = GetEntityCoords(vehicle) + + Debug("Fahrzeug mit Fallback-Modell gespawnt: " .. data.plate) + end + + SetModelAsNoLongerNeeded(fallbackModel) + else + Debug("Auch Fallback-Modell konnte nicht geladen werden!") + end end end) + -- Hilfsfunktion um Fahrzeug anhand Kennzeichen zu finden function GetVehicleByPlate(plate) local vehicles = GetGamePool('CVehicle') diff --git a/resources/[carscripts]/nordi_antidespawn/server/main.lua b/resources/[carscripts]/nordi_antidespawn/server/main.lua index 846f96d38..4263cdc19 100644 --- a/resources/[carscripts]/nordi_antidespawn/server/main.lua +++ b/resources/[carscripts]/nordi_antidespawn/server/main.lua @@ -55,6 +55,11 @@ RegisterNetEvent('antidespawn:server:registerVehicle', function(plate, model, co 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 @@ -73,17 +78,18 @@ RegisterNetEvent('antidespawn:server:registerVehicle', function(plate, model, co MySQL.query("INSERT INTO vehicle_antidespawn (plate, model, coords, heading, fuel, mods) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE coords = VALUES(coords), heading = VALUES(heading), mods = VALUES(mods), last_updated = CURRENT_TIMESTAMP", { plate, - model, + tostring(model), -- Speichere als String in der Datenbank json.encode(coords), heading, 100, json.encode(mods) }) - Debug("Fahrzeug registriert: " .. plate) + Debug("Fahrzeug registriert: " .. plate .. " (Modell: " .. tostring(model) .. ")") end) end) + -- Aktualisiere ein Fahrzeug RegisterNetEvent('antidespawn:server:updateVehicle', function(plate, coords, heading, mods) if not vehicles[plate] then return end @@ -240,3 +246,25 @@ RegisterNetEvent('jg-advancedgarages:server:vehicle-spawned', function(data) end end end) + +-- Befehl zum Bereinigen der Datenbank +RegisterCommand('clearvehicles', function(source, args, rawCommand) + if source == 0 then -- Nur über Konsole ausführbar + local count = 0 + + for plate, vehicle in pairs(vehicles) do + local model = vehicle.model + + -- Prüfe ob das Modell gültig ist + if type(model) == "string" and not tonumber(model) then + -- Ungültiges Modell, entferne aus Datenbank + MySQL.query("DELETE FROM vehicle_antidespawn WHERE plate = ?", {plate}) + vehicles[plate] = nil + count = count + 1 + Debug("Ungültiges Modell entfernt: " .. plate .. " (Modell: " .. tostring(model) .. ")") + end + end + + Debug("Bereinigung abgeschlossen. " .. count .. " Fahrzeuge entfernt.") + end +end, true)