forked from Simnation/Main
Update main.lua
This commit is contained in:
parent
40b090bfb8
commit
f2b9b3fc29
1 changed files with 163 additions and 79 deletions
|
@ -1,6 +1,6 @@
|
||||||
local QBCore = exports['qb-core']:GetCoreObject()
|
local QBCore = exports['qb-core']:GetCoreObject()
|
||||||
local trackedVehicles = {}
|
local trackedVehicles = {}
|
||||||
local playerVehicles = {}
|
local spawnedVehicles = {}
|
||||||
|
|
||||||
-- Funktion um zu prüfen ob Fahrzeugklasse erlaubt ist
|
-- Funktion um zu prüfen ob Fahrzeugklasse erlaubt ist
|
||||||
local function IsVehicleClassAllowed(vehicle)
|
local function IsVehicleClassAllowed(vehicle)
|
||||||
|
@ -94,27 +94,21 @@ local function SetVehicleMods(vehicle, mods)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Prüfe ob Fahrzeug einem Spieler gehört (vereinfachte Version)
|
-- Verhindere Despawn für alle getrackten Fahrzeuge
|
||||||
local function DoesVehicleBelongToPlayer(plate)
|
local function PreventVehicleDespawn(vehicle)
|
||||||
-- Da der Export nicht existiert, prüfen wir über QB-Core
|
if DoesEntityExist(vehicle) then
|
||||||
local Player = QBCore.Functions.GetPlayerData()
|
SetEntityAsMissionEntity(vehicle, true, true)
|
||||||
if not Player or not Player.citizenid then return false end
|
SetVehicleHasBeenOwnedByPlayer(vehicle, true)
|
||||||
|
SetVehicleNeedsToBeHotwired(vehicle, false)
|
||||||
|
SetVehicleOnGroundProperly(vehicle)
|
||||||
|
FreezeEntityPosition(vehicle, false)
|
||||||
|
|
||||||
-- Hier könntest du eine eigene Logik implementieren oder
|
-- Verhindere dass das Fahrzeug als "abandoned" markiert wird
|
||||||
-- einfach alle Fahrzeuge tracken die nicht NPC Fahrzeuge sind
|
DecorSetBool(vehicle, "IgnoredByQuickSave", false)
|
||||||
local vehicle = GetVehicleByPlate(plate)
|
|
||||||
if vehicle then
|
|
||||||
-- Prüfe ob es ein NPC Fahrzeug ist
|
|
||||||
local driver = GetPedInVehicleSeat(vehicle, -1)
|
|
||||||
if driver and IsPedAPlayer(driver) then
|
|
||||||
return true
|
|
||||||
elseif not driver then
|
|
||||||
-- Kein Fahrer = wahrscheinlich Spielerfahrzeug
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return false
|
-- Setze Vehicle als persistent
|
||||||
|
SetEntityLoadCollisionFlag(vehicle, true)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Hauptloop für Fahrzeugtracking
|
-- Hauptloop für Fahrzeugtracking
|
||||||
|
@ -125,47 +119,77 @@ CreateThread(function()
|
||||||
local playerPed = PlayerPedId()
|
local playerPed = PlayerPedId()
|
||||||
local playerCoords = GetEntityCoords(playerPed)
|
local playerCoords = GetEntityCoords(playerPed)
|
||||||
|
|
||||||
-- Finde alle Fahrzeuge in der Nähe
|
-- Finde alle Fahrzeuge
|
||||||
local vehicles = GetGamePool('CVehicle')
|
local vehicles = GetGamePool('CVehicle')
|
||||||
|
|
||||||
for _, vehicle in pairs(vehicles) do
|
for _, vehicle in pairs(vehicles) do
|
||||||
if DoesEntityExist(vehicle) and IsVehicleClassAllowed(vehicle) then
|
if DoesEntityExist(vehicle) and IsVehicleClassAllowed(vehicle) then
|
||||||
local plate = QBCore.Functions.GetPlate(vehicle)
|
local plate = QBCore.Functions.GetPlate(vehicle)
|
||||||
local vehicleCoords = GetEntityCoords(vehicle)
|
local vehicleCoords = GetEntityCoords(vehicle)
|
||||||
local distance = #(playerCoords - vehicleCoords)
|
|
||||||
|
|
||||||
-- Prüfe ob Fahrzeug einem Spieler gehört
|
-- Prüfe ob es ein Spielerfahrzeug ist (nicht NPC)
|
||||||
if DoesVehicleBelongToPlayer(plate) then
|
local isPlayerVehicle = false
|
||||||
|
local driver = GetPedInVehicleSeat(vehicle, -1)
|
||||||
|
|
||||||
|
if driver == 0 or IsPedAPlayer(driver) then
|
||||||
|
isPlayerVehicle = true
|
||||||
|
elseif trackedVehicles[plate] or spawnedVehicles[plate] then
|
||||||
|
-- Bereits getrackt = Spielerfahrzeug
|
||||||
|
isPlayerVehicle = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if isPlayerVehicle then
|
||||||
|
-- Verhindere Despawn
|
||||||
|
PreventVehicleDespawn(vehicle)
|
||||||
|
|
||||||
-- Speichere Fahrzeugdaten
|
-- Speichere Fahrzeugdaten
|
||||||
local vehicleData = {
|
local vehicleData = {
|
||||||
plate = plate,
|
plate = plate,
|
||||||
model = GetDisplayNameFromVehicleModel(GetEntityModel(vehicle)),
|
model = GetEntityModel(vehicle),
|
||||||
position = {x = vehicleCoords.x, y = vehicleCoords.y, z = vehicleCoords.z},
|
position = {x = vehicleCoords.x, y = vehicleCoords.y, z = vehicleCoords.z},
|
||||||
rotation = {x = 0.0, y = 0.0, z = GetEntityHeading(vehicle)},
|
rotation = {x = 0.0, y = 0.0, z = GetEntityHeading(vehicle)},
|
||||||
engineHealth = GetVehicleEngineHealth(vehicle),
|
engineHealth = GetVehicleEngineHealth(vehicle),
|
||||||
bodyHealth = GetVehicleBodyHealth(vehicle),
|
bodyHealth = GetVehicleBodyHealth(vehicle),
|
||||||
fuel = exports['LegacyFuel']:GetFuel(vehicle) or 100, -- Anpassen je nach Fuel System
|
fuel = 100, -- Standardwert, anpassen je nach Fuel System
|
||||||
mods = GetVehicleMods(vehicle)
|
mods = GetVehicleMods(vehicle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- Versuche Fuel zu bekommen
|
||||||
|
if GetResourceState('LegacyFuel') == 'started' then
|
||||||
|
vehicleData.fuel = exports['LegacyFuel']:GetFuel(vehicle) or 100
|
||||||
|
elseif GetResourceState('ps-fuel') == 'started' then
|
||||||
|
vehicleData.fuel = exports['ps-fuel']:GetFuel(vehicle) or 100
|
||||||
|
end
|
||||||
|
|
||||||
TriggerServerEvent('vehicle-persistence:server:saveVehiclePosition', vehicleData)
|
TriggerServerEvent('vehicle-persistence:server:saveVehiclePosition', vehicleData)
|
||||||
trackedVehicles[plate] = vehicle
|
trackedVehicles[plate] = vehicle
|
||||||
|
|
||||||
-- Verhindere Despawn
|
|
||||||
SetEntityAsMissionEntity(vehicle, true, true)
|
|
||||||
SetVehicleHasBeenOwnedByPlayer(vehicle, true)
|
|
||||||
|
|
||||||
if Config.Debug then
|
if Config.Debug then
|
||||||
print(string.format("Tracking vehicle: %s at distance: %.2f", plate, distance))
|
local distance = #(playerCoords - vehicleCoords)
|
||||||
|
print(string.format("Tracking vehicle: %s (Model: %s) at distance: %.2f", plate, GetDisplayNameFromVehicleModel(GetEntityModel(vehicle)), distance))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Prüfe ob getrackte Fahrzeuge noch existieren
|
||||||
|
for plate, vehicle in pairs(trackedVehicles) do
|
||||||
|
if not DoesEntityExist(vehicle) then
|
||||||
|
trackedVehicles[plate] = nil
|
||||||
|
if Config.Debug then
|
||||||
|
print(string.format("Vehicle no longer exists, removed from tracking: %s", plate))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end)
|
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
|
||||||
|
print(string.format("Attempting to spawn %d saved vehicles", #vehicles))
|
||||||
|
end
|
||||||
|
|
||||||
for _, vehicleData in pairs(vehicles) do
|
for _, vehicleData in pairs(vehicles) do
|
||||||
local position = json.decode(vehicleData.position)
|
local position = json.decode(vehicleData.position)
|
||||||
local rotation = json.decode(vehicleData.rotation)
|
local rotation = json.decode(vehicleData.rotation)
|
||||||
|
@ -173,25 +197,36 @@ RegisterNetEvent('vehicle-persistence:client:spawnSavedVehicles', function(vehic
|
||||||
-- Prüfe ob Fahrzeug bereits existiert
|
-- Prüfe ob Fahrzeug bereits existiert
|
||||||
local existingVehicle = GetVehicleByPlate(vehicleData.plate)
|
local existingVehicle = GetVehicleByPlate(vehicleData.plate)
|
||||||
if not existingVehicle then
|
if not existingVehicle then
|
||||||
-- Spawne Fahrzeug
|
CreateThread(function()
|
||||||
local modelHash = GetHashKey(vehicleData.model)
|
local modelHash = vehicleData.model
|
||||||
|
if type(modelHash) == "string" then
|
||||||
RequestModel(modelHash)
|
modelHash = GetHashKey(modelHash)
|
||||||
while not HasModelLoaded(modelHash) do
|
|
||||||
Wait(100)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
RequestModel(modelHash)
|
||||||
|
local timeout = 0
|
||||||
|
while not HasModelLoaded(modelHash) and timeout < 50 do
|
||||||
|
Wait(100)
|
||||||
|
timeout = timeout + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if HasModelLoaded(modelHash) then
|
||||||
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
|
||||||
|
-- Warte bis Fahrzeug vollständig geladen ist
|
||||||
|
Wait(500)
|
||||||
|
|
||||||
-- Setze Fahrzeugdaten
|
-- Setze Fahrzeugdaten
|
||||||
SetVehicleNumberPlateText(vehicle, vehicleData.plate)
|
SetVehicleNumberPlateText(vehicle, vehicleData.plate)
|
||||||
SetVehicleEngineHealth(vehicle, vehicleData.engine_health)
|
SetVehicleEngineHealth(vehicle, vehicleData.engine_health or 1000.0)
|
||||||
SetVehicleBodyHealth(vehicle, vehicleData.body_health)
|
SetVehicleBodyHealth(vehicle, vehicleData.body_health or 1000.0)
|
||||||
|
|
||||||
-- Setze Fuel (anpassen je nach System)
|
-- Setze Fuel
|
||||||
if exports['LegacyFuel'] then
|
if GetResourceState('LegacyFuel') == 'started' then
|
||||||
exports['LegacyFuel']:SetFuel(vehicle, vehicleData.fuel)
|
exports['LegacyFuel']:SetFuel(vehicle, vehicleData.fuel or 100)
|
||||||
|
elseif GetResourceState('ps-fuel') == 'started' then
|
||||||
|
exports['ps-fuel']:SetFuel(vehicle, vehicleData.fuel or 100)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Setze Mods
|
-- Setze Mods
|
||||||
|
@ -201,17 +236,34 @@ RegisterNetEvent('vehicle-persistence:client:spawnSavedVehicles', function(vehic
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Verhindere Despawn
|
-- Verhindere Despawn
|
||||||
SetEntityAsMissionEntity(vehicle, true, true)
|
PreventVehicleDespawn(vehicle)
|
||||||
SetVehicleHasBeenOwnedByPlayer(vehicle, true)
|
|
||||||
|
|
||||||
|
spawnedVehicles[vehicleData.plate] = vehicle
|
||||||
trackedVehicles[vehicleData.plate] = vehicle
|
trackedVehicles[vehicleData.plate] = vehicle
|
||||||
|
|
||||||
if Config.Debug then
|
if Config.Debug then
|
||||||
print(string.format("Spawned saved vehicle: %s", vehicleData.plate))
|
print(string.format("Successfully spawned saved vehicle: %s", vehicleData.plate))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if Config.Debug then
|
||||||
|
print(string.format("Failed to create vehicle: %s", vehicleData.plate))
|
||||||
end
|
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)
|
||||||
|
else
|
||||||
|
-- Fahrzeug existiert bereits, füge zu Tracking hinzu
|
||||||
|
trackedVehicles[vehicleData.plate] = existingVehicle
|
||||||
|
PreventVehicleDespawn(existingVehicle)
|
||||||
|
if Config.Debug then
|
||||||
|
print(string.format("Vehicle already exists, added to tracking: %s", vehicleData.plate))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
@ -229,35 +281,67 @@ end
|
||||||
|
|
||||||
-- Lade Fahrzeuge beim Spawn
|
-- Lade Fahrzeuge beim Spawn
|
||||||
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
|
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function()
|
||||||
Wait(5000) -- Warte bis alles geladen ist
|
Wait(10000) -- Längere Wartezeit bis alles geladen ist
|
||||||
TriggerServerEvent('vehicle-persistence:server:loadVehicles')
|
TriggerServerEvent('vehicle-persistence:server:loadVehicles')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
-- Lade Fahrzeuge auch beim Resource Start
|
||||||
|
CreateThread(function()
|
||||||
|
Wait(15000) -- Warte bis Server vollständig geladen ist
|
||||||
|
if QBCore.Functions.GetPlayerData().citizenid then
|
||||||
|
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)
|
||||||
-- Entferne aus Tracking wenn Fahrzeug gespeichert wird
|
if data and data.plate then
|
||||||
if data and data.plate and trackedVehicles[data.plate] then
|
if trackedVehicles[data.plate] then
|
||||||
trackedVehicles[data.plate] = nil
|
trackedVehicles[data.plate] = nil
|
||||||
|
end
|
||||||
|
if spawnedVehicles[data.plate] then
|
||||||
|
spawnedVehicles[data.plate] = nil
|
||||||
|
end
|
||||||
if Config.Debug then
|
if Config.Debug then
|
||||||
print(string.format("Vehicle stored, removed from tracking: %s", data.plate))
|
print(string.format("Vehicle stored in garage, removed from tracking: %s", data.plate))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
RegisterNetEvent('jg-advancedgarages:client:vehicle-spawned', function(data)
|
RegisterNetEvent('jg-advancedgarages:client:vehicle-spawned', function(data)
|
||||||
-- Entferne aus Tracking da Fahrzeug jetzt über Garage gespawnt wurde
|
if data and data.plate then
|
||||||
if data and data.plate and trackedVehicles[data.plate] then
|
if trackedVehicles[data.plate] then
|
||||||
trackedVehicles[data.plate] = nil
|
trackedVehicles[data.plate] = nil
|
||||||
|
end
|
||||||
|
if spawnedVehicles[data.plate] then
|
||||||
|
spawnedVehicles[data.plate] = nil
|
||||||
|
end
|
||||||
if Config.Debug then
|
if Config.Debug then
|
||||||
print(string.format("Vehicle spawned from garage, removed from tracking: %s", data.plate))
|
print(string.format("Vehicle spawned from garage, removed from tracking: %s", data.plate))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
-- Kontinuierliche Despawn-Verhinderung
|
||||||
|
CreateThread(function()
|
||||||
|
while true do
|
||||||
|
Wait(30000) -- Alle 30 Sekunden
|
||||||
|
|
||||||
|
for plate, vehicle in pairs(trackedVehicles) do
|
||||||
|
if DoesEntityExist(vehicle) then
|
||||||
|
PreventVehicleDespawn(vehicle)
|
||||||
|
else
|
||||||
|
trackedVehicles[plate] = nil
|
||||||
|
spawnedVehicles[plate] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
-- Cleanup beim Disconnect
|
-- Cleanup beim Disconnect
|
||||||
AddEventHandler('onResourceStop', function(resourceName)
|
AddEventHandler('onResourceStop', function(resourceName)
|
||||||
if resourceName == GetCurrentResourceName() then
|
if resourceName == GetCurrentResourceName() then
|
||||||
-- Cleanup Code hier falls nötig
|
|
||||||
trackedVehicles = {}
|
trackedVehicles = {}
|
||||||
|
spawnedVehicles = {}
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue