1
0
Fork 0
forked from Simnation/Main
This commit is contained in:
Nordi98 2025-08-06 18:07:18 +02:00
parent 7fc981e7fc
commit 5b0effa7af

View file

@ -179,10 +179,14 @@ end)
-- Spawne gespeicherte Fahrzeuge -- Spawne gespeicherte Fahrzeuge
RegisterNetEvent('vehicle-persistence:client:spawnSavedVehicles', function(vehicles) RegisterNetEvent('vehicle-persistence:client:spawnSavedVehicles', function(vehicles)
if Config.Debug then if Config.Debug then
print(string.format("Attempting to spawn %d saved vehicles", #vehicles)) print(string.format("Received %d vehicles to spawn", #vehicles))
end end
for _, vehicleData in pairs(vehicles) do for _, vehicleData in pairs(vehicles) do
if Config.Debug then
print(string.format("Processing vehicle: %s", vehicleData.plate))
end
local position = json.decode(vehicleData.position) local position = json.decode(vehicleData.position)
local rotation = json.decode(vehicleData.rotation) local rotation = json.decode(vehicleData.rotation)
@ -195,18 +199,26 @@ RegisterNetEvent('vehicle-persistence:client:spawnSavedVehicles', function(vehic
modelHash = GetHashKey(modelHash) modelHash = GetHashKey(modelHash)
end end
if Config.Debug then
print(string.format("Requesting model: %s (Hash: %s)", vehicleData.model, modelHash))
end
RequestModel(modelHash) RequestModel(modelHash)
local timeout = 0 local timeout = 0
while not HasModelLoaded(modelHash) and timeout < 50 do while not HasModelLoaded(modelHash) and timeout < 100 do
Wait(100) Wait(100)
timeout = timeout + 1 timeout = timeout + 1
end end
if HasModelLoaded(modelHash) then if HasModelLoaded(modelHash) then
if Config.Debug then
print(string.format("Model loaded, creating vehicle at: %.2f, %.2f, %.2f", position.x, position.y, position.z))
end
local vehicle = CreateVehicle(modelHash, position.x, position.y, position.z, rotation.z, true, false) local vehicle = CreateVehicle(modelHash, position.x, position.y, position.z, rotation.z, true, false)
if DoesEntityExist(vehicle) then if DoesEntityExist(vehicle) then
Wait(500) Wait(1000) -- Längere Wartezeit
-- Setze Fahrzeugdaten -- Setze Fahrzeugdaten
SetVehicleNumberPlateText(vehicle, vehicleData.plate) SetVehicleNumberPlateText(vehicle, vehicleData.plate)
@ -222,22 +234,33 @@ RegisterNetEvent('vehicle-persistence:client:spawnSavedVehicles', function(vehic
-- Setze Mods -- Setze Mods
if vehicleData.mods then if vehicleData.mods then
local mods = json.decode(vehicleData.mods) local success, mods = pcall(json.decode, vehicleData.mods)
if success then
SetVehicleMods(vehicle, mods) SetVehicleMods(vehicle, mods)
end end
end
-- Verhindere Despawn -- Verhindere Despawn
SetEntityAsMissionEntity(vehicle, true, true) SetEntityAsMissionEntity(vehicle, true, true)
SetVehicleHasBeenOwnedByPlayer(vehicle, true) SetVehicleHasBeenOwnedByPlayer(vehicle, true)
SetVehicleOnGroundProperly(vehicle)
playerDrivenVehicles[vehicleData.plate] = vehicle playerDrivenVehicles[vehicleData.plate] = vehicle
if Config.Debug then if Config.Debug then
print(string.format("Successfully spawned saved vehicle: %s", vehicleData.plate)) print(string.format("Successfully spawned saved vehicle: %s", vehicleData.plate))
end end
else
if Config.Debug then
print(string.format("Failed to create vehicle: %s", vehicleData.plate))
end
end end
SetModelAsNoLongerNeeded(modelHash) SetModelAsNoLongerNeeded(modelHash)
else
if Config.Debug then
print(string.format("Failed to load model for vehicle: %s", vehicleData.plate))
end
end end
end) end)
else else
@ -245,6 +268,10 @@ RegisterNetEvent('vehicle-persistence:client:spawnSavedVehicles', function(vehic
playerDrivenVehicles[vehicleData.plate] = existingVehicle playerDrivenVehicles[vehicleData.plate] = existingVehicle
SetEntityAsMissionEntity(existingVehicle, true, true) SetEntityAsMissionEntity(existingVehicle, true, true)
SetVehicleHasBeenOwnedByPlayer(existingVehicle, true) SetVehicleHasBeenOwnedByPlayer(existingVehicle, true)
if Config.Debug then
print(string.format("Vehicle already exists: %s", vehicleData.plate))
end
end end
end end
end) end)
@ -262,10 +289,32 @@ end
-- Lade Fahrzeuge beim Spawn -- Lade Fahrzeuge beim Spawn
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function() RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
Wait(10000) if Config.Debug then
print("Player loaded, waiting before loading vehicles...")
end
Wait(15000) -- Längere Wartezeit
if Config.Debug then
print("Loading vehicles...")
end
TriggerServerEvent('vehicle-persistence:server:loadVehicles') TriggerServerEvent('vehicle-persistence:server:loadVehicles')
end) end)
-- Lade Fahrzeuge auch beim Resource Start (falls Spieler bereits online)
CreateThread(function()
Wait(20000) -- Noch längere Wartezeit beim Resource Start
local playerData = QBCore.Functions.GetPlayerData()
if playerData and playerData.citizenid then
if Config.Debug then
print("Resource started, loading vehicles for existing player...")
end
TriggerServerEvent('vehicle-persistence:server:loadVehicles')
end
end)
-- jg-advanced-garage Events -- jg-advanced-garage Events
RegisterNetEvent('jg-advancedgarages:client:vehicle-stored', function(data) RegisterNetEvent('jg-advancedgarages:client:vehicle-stored', function(data)
if data and data.plate and playerDrivenVehicles[data.plate] then if data and data.plate and playerDrivenVehicles[data.plate] then
@ -285,6 +334,14 @@ RegisterNetEvent('jg-advancedgarages:client:vehicle-spawned', function(data)
end end
end) end)
-- Debug Command zum manuellen Laden
RegisterCommand('loadvehicles', function()
if Config.Debug then
print("Manual vehicle load triggered...")
TriggerServerEvent('vehicle-persistence:server:loadVehicles')
end
end, false)
-- Cleanup beim Disconnect -- Cleanup beim Disconnect
AddEventHandler('onResourceStop', function(resourceName) AddEventHandler('onResourceStop', function(resourceName)
if resourceName == GetCurrentResourceName() then if resourceName == GetCurrentResourceName() then