119 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			119 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
local QBCore = exports['qb-core']:GetCoreObject()
 | 
						|
 | 
						|
local activeTrackings = {}
 | 
						|
 | 
						|
-- Callback für Fahrzeug Tracking
 | 
						|
QBCore.Functions.CreateCallback('tracking:server:trackVehicle', function(source, cb, plate)
 | 
						|
    local src = source
 | 
						|
    local Player = QBCore.Functions.GetPlayer(src)
 | 
						|
    
 | 
						|
    if not Player then
 | 
						|
        cb(false, 'Spieler nicht gefunden!')
 | 
						|
        return
 | 
						|
    end
 | 
						|
    
 | 
						|
    -- Überprüfe Job Berechtigung
 | 
						|
    local allowedJobs = {'police', 'ambulance', 'mechanic'}
 | 
						|
    local hasPermission = false
 | 
						|
    
 | 
						|
    for _, job in pairs(allowedJobs) do
 | 
						|
        if Player.PlayerData.job.name == job then
 | 
						|
            hasPermission = true
 | 
						|
            break
 | 
						|
        end
 | 
						|
    end
 | 
						|
    
 | 
						|
    if not hasPermission then
 | 
						|
        cb(false, 'Keine Berechtigung!')
 | 
						|
        return
 | 
						|
    end
 | 
						|
    
 | 
						|
    -- Suche Fahrzeug in der Datenbank mit Collation-Fix
 | 
						|
    MySQL.Async.fetchAll('SELECT pv.*, p.charinfo FROM player_vehicles pv LEFT JOIN players p ON pv.citizenid COLLATE utf8mb4_general_ci = p.citizenid COLLATE utf8mb4_general_ci WHERE pv.plate = ?', {plate}, function(result)
 | 
						|
        if result[1] then
 | 
						|
            -- Extrahiere Besitzerinformationen
 | 
						|
            local ownerInfo = "Unbekannt"
 | 
						|
            if result[1].charinfo then
 | 
						|
                local charInfo = json.decode(result[1].charinfo)
 | 
						|
                if charInfo then
 | 
						|
                    ownerInfo = charInfo.firstname .. " " .. charInfo.lastname
 | 
						|
                end
 | 
						|
            end
 | 
						|
            
 | 
						|
            -- Suche das Fahrzeug in der Welt
 | 
						|
            local vehicles = GetAllVehicles()
 | 
						|
            local foundVehicle = nil
 | 
						|
            local vehicleCoords = nil
 | 
						|
            
 | 
						|
            for _, vehicle in pairs(vehicles) do
 | 
						|
                if DoesEntityExist(vehicle) then
 | 
						|
                    local vehiclePlate = GetVehicleNumberPlateText(vehicle)
 | 
						|
                    if vehiclePlate and string.gsub(vehiclePlate, '^%s*(.-)%s*$', '%1') == plate then
 | 
						|
                        foundVehicle = vehicle
 | 
						|
                        vehicleCoords = GetEntityCoords(vehicle)
 | 
						|
                        break
 | 
						|
                    end
 | 
						|
                end
 | 
						|
            end
 | 
						|
            
 | 
						|
            if foundVehicle and vehicleCoords then
 | 
						|
                activeTrackings[plate] = {
 | 
						|
                    vehicle = foundVehicle,
 | 
						|
                    coords = {x = vehicleCoords.x, y = vehicleCoords.y, z = vehicleCoords.z},
 | 
						|
                    tracker = src,
 | 
						|
                    owner = ownerInfo
 | 
						|
                }
 | 
						|
                
 | 
						|
                cb(true, 'Fahrzeug gefunden!', {
 | 
						|
                    vehicle = foundVehicle,
 | 
						|
                    coords = {x = vehicleCoords.x, y = vehicleCoords.y, z = vehicleCoords.z},
 | 
						|
                    owner = ownerInfo
 | 
						|
                })
 | 
						|
            else
 | 
						|
                cb(false, 'Fahrzeug nicht in der Welt gefunden!')
 | 
						|
            end
 | 
						|
        else
 | 
						|
            cb(false, 'Fahrzeug mit diesem Kennzeichen existiert nicht!')
 | 
						|
        end
 | 
						|
    end)
 | 
						|
end)
 | 
						|
 | 
						|
-- Update Fahrzeug Position
 | 
						|
RegisterNetEvent('tracking:server:updateVehicleLocation', function(plate)
 | 
						|
    local src = source
 | 
						|
    
 | 
						|
    if activeTrackings[plate] and activeTrackings[plate].tracker == src then
 | 
						|
        local vehicle = activeTrackings[plate].vehicle
 | 
						|
        
 | 
						|
        if DoesEntityExist(vehicle) then
 | 
						|
            local coords = GetEntityCoords(vehicle)
 | 
						|
            activeTrackings[plate].coords = {x = coords.x, y = coords.y, z = coords.z}
 | 
						|
            
 | 
						|
            TriggerClientEvent('tracking:client:updatePosition', src, plate, {x = coords.x, y = coords.y, z = coords.z})
 | 
						|
        else
 | 
						|
            -- Fahrzeug existiert nicht mehr
 | 
						|
            activeTrackings[plate] = nil
 | 
						|
            TriggerClientEvent('QBCore:Notify', src, 'Tracking verloren - Fahrzeug nicht mehr verfügbar!', 'error')
 | 
						|
        end
 | 
						|
    end
 | 
						|
end)
 | 
						|
 | 
						|
-- Stoppe Tracking
 | 
						|
RegisterNetEvent('tracking:server:stopTracking', function(plate)
 | 
						|
    local src = source
 | 
						|
    
 | 
						|
    if activeTrackings[plate] and activeTrackings[plate].tracker == src then
 | 
						|
        activeTrackings[plate] = nil
 | 
						|
    end
 | 
						|
end)
 | 
						|
 | 
						|
-- Cleanup bei Disconnect
 | 
						|
AddEventHandler('playerDropped', function()
 | 
						|
    local src = source
 | 
						|
    
 | 
						|
    for plate, data in pairs(activeTrackings) do
 | 
						|
        if data.tracker == src then
 | 
						|
            activeTrackings[plate] = nil
 | 
						|
        end
 | 
						|
    end
 | 
						|
end)
 |