1
0
Fork 0
forked from Simnation/Main
This commit is contained in:
Nordi98 2025-08-06 19:16:18 +02:00
parent 0be79cdd60
commit cf533de903
2 changed files with 84 additions and 4 deletions

View file

@ -377,25 +377,47 @@ RegisterNetEvent('antidespawn:client:spawnVehicle', function(data)
return return
end end
-- Spawne Fahrzeug -- Konvertiere Modell zu Hash wenn nötig
local modelHash = data.model 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) RequestModel(modelHash)
local timeout = 0 local timeout = 0
while not HasModelLoaded(modelHash) and timeout < 100 do while not HasModelLoaded(modelHash) and timeout < 100 do
Wait(100) Wait(100)
timeout = timeout + 1 timeout = timeout + 1
Debug("Warte auf Modell: " .. tostring(timeout) .. "/100")
end end
if HasModelLoaded(modelHash) then if HasModelLoaded(modelHash) then
Debug("Modell geladen, erstelle Fahrzeug...")
-- Verwende CREATE_AUTOMOBILE für bessere Persistenz -- Verwende CREATE_AUTOMOBILE für bessere Persistenz
local vehicle local vehicle
if Citizen and Citizen.InvokeNative then if Citizen and Citizen.InvokeNative then
-- OneSync Methode -- OneSync Methode
Debug("Verwende OneSync Methode")
vehicle = Citizen.InvokeNative(0xAF35D0D2583051B0, modelHash, data.coords.x, data.coords.y, data.coords.z, data.heading, true, true) vehicle = Citizen.InvokeNative(0xAF35D0D2583051B0, modelHash, data.coords.x, data.coords.y, data.coords.z, data.heading, true, true)
else else
-- Fallback -- Fallback
Debug("Verwende Fallback Methode")
vehicle = CreateVehicle(modelHash, data.coords.x, data.coords.y, data.coords.z, data.heading, true, false) vehicle = CreateVehicle(modelHash, data.coords.x, data.coords.y, data.coords.z, data.heading, true, false)
end end
@ -408,6 +430,7 @@ RegisterNetEvent('antidespawn:client:spawnVehicle', function(data)
-- Setze Mods -- Setze Mods
if data.mods then if data.mods then
Debug("Setze Fahrzeugmods...")
SetVehicleMods(vehicle, data.mods) SetVehicleMods(vehicle, data.mods)
end end
@ -433,10 +456,39 @@ RegisterNetEvent('antidespawn:client:spawnVehicle', function(data)
SetModelAsNoLongerNeeded(modelHash) SetModelAsNoLongerNeeded(modelHash)
else 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
end) end)
-- Hilfsfunktion um Fahrzeug anhand Kennzeichen zu finden -- Hilfsfunktion um Fahrzeug anhand Kennzeichen zu finden
function GetVehicleByPlate(plate) function GetVehicleByPlate(plate)
local vehicles = GetGamePool('CVehicle') local vehicles = GetGamePool('CVehicle')

View file

@ -55,6 +55,11 @@ RegisterNetEvent('antidespawn:server:registerVehicle', function(plate, model, co
return return
end 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 -- Prüfe ob Fahrzeug in der Garage ist
MySQL.query('SELECT * FROM player_vehicles WHERE plate = ? AND state = ?', {plate, 1}, function(result) MySQL.query('SELECT * FROM player_vehicles WHERE plate = ? AND state = ?', {plate, 1}, function(result)
if result and #result > 0 then 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", { 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, plate,
model, tostring(model), -- Speichere als String in der Datenbank
json.encode(coords), json.encode(coords),
heading, heading,
100, 100,
json.encode(mods) json.encode(mods)
}) })
Debug("Fahrzeug registriert: " .. plate) Debug("Fahrzeug registriert: " .. plate .. " (Modell: " .. tostring(model) .. ")")
end) end)
end) end)
-- Aktualisiere ein Fahrzeug -- Aktualisiere ein Fahrzeug
RegisterNetEvent('antidespawn:server:updateVehicle', function(plate, coords, heading, mods) RegisterNetEvent('antidespawn:server:updateVehicle', function(plate, coords, heading, mods)
if not vehicles[plate] then return end if not vehicles[plate] then return end
@ -240,3 +246,25 @@ RegisterNetEvent('jg-advancedgarages:server:vehicle-spawned', function(data)
end end
end 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)