231 lines
		
	
	
	
		
			7.4 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			231 lines
		
	
	
	
		
			7.4 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
local spawnedObjects = {}
 | 
						|
local imageDisplayed = false
 | 
						|
 | 
						|
RegisterNetEvent('pl_printer:notification')
 | 
						|
AddEventHandler('pl_printer:notification', function(message, type)
 | 
						|
    if Config.Notify == 'ox' then
 | 
						|
        TriggerEvent('ox_lib:notify', {description = message, type = type or "success"})
 | 
						|
    elseif Config.Notify == 'esx' then
 | 
						|
        Notification(message)
 | 
						|
    elseif Config.Notify == 'okok' then
 | 
						|
        TriggerEvent('okokNotify:Alert', message, 6000, type)
 | 
						|
    elseif Config.Notify == 'qb' then
 | 
						|
        Notification(message, type)
 | 
						|
    elseif Config.Notify == 'wasabi' then
 | 
						|
        exports.wasabi_notify:notify('Printer', message, 6000, type, false, 'fas fa-ghost')
 | 
						|
    elseif Config.Notify == 'custom' then
 | 
						|
        -- Add your custom notifications here
 | 
						|
    end
 | 
						|
end)
 | 
						|
 | 
						|
function disableControls()
 | 
						|
    SetEntityInvincible(PlayerPedId(), true) 
 | 
						|
    FreezeEntityPosition(PlayerPedId(), true)
 | 
						|
end
 | 
						|
 | 
						|
function enableControls()
 | 
						|
    SetEntityInvincible(PlayerPedId(), false) 
 | 
						|
    FreezeEntityPosition(PlayerPedId(), false)
 | 
						|
end
 | 
						|
 | 
						|
-- Event für QB-Core Inventar
 | 
						|
RegisterNetEvent("pl_printer:showImageQB")
 | 
						|
AddEventHandler("pl_printer:showImageQB", function(imageName)
 | 
						|
    print("[DEBUG] Received showImageQB event with imageName: " .. tostring(imageName))
 | 
						|
    TriggerServerEvent('pl_printer:fetchImageLink', imageName)
 | 
						|
end)
 | 
						|
 | 
						|
-- Hauptevent zum Anzeigen des Bildes
 | 
						|
RegisterNetEvent("pl_printer:showImage")
 | 
						|
AddEventHandler("pl_printer:showImage", function(imageUrl)
 | 
						|
    print("[DEBUG] Received showImage event with URL: " .. tostring(imageUrl))
 | 
						|
    
 | 
						|
    if not imageDisplayed then
 | 
						|
        imageDisplayed = true
 | 
						|
        SetNuiFocus(true, true) 
 | 
						|
        
 | 
						|
        -- Extrahiere den Dateinamen aus der URL für die Dokument-ID
 | 
						|
        local documentId = imageUrl:match(".*/(.*)$") or "unknown"
 | 
						|
        
 | 
						|
        SendNUIMessage({
 | 
						|
            action = "show",
 | 
						|
            imageUrl = imageUrl,
 | 
						|
            documentId = documentId -- Füge die Dokument-ID hinzu
 | 
						|
        })
 | 
						|
        
 | 
						|
        disableControls()
 | 
						|
    else
 | 
						|
        print("[DEBUG] Image already displayed, ignoring request")
 | 
						|
    end
 | 
						|
end) 
 | 
						|
 | 
						|
-- NUI-Callback zum Schließen des Bildes
 | 
						|
RegisterNUICallback('hideFrame', function(data, cb)
 | 
						|
    print("[DEBUG] Hiding image frame")
 | 
						|
    imageDisplayed = false
 | 
						|
    SetNuiFocus(false, false)
 | 
						|
    enableControls()
 | 
						|
    
 | 
						|
    -- Füge einen Callback hinzu, wenn er benötigt wird
 | 
						|
    if cb then cb('ok') end
 | 
						|
end)
 | 
						|
 | 
						|
-- Sicherheits-Callback für den Fall, dass die Seite neu geladen wird
 | 
						|
RegisterNUICallback('pageLoaded', function(data, cb)
 | 
						|
    print("[DEBUG] NUI page loaded")
 | 
						|
    -- Stelle sicher, dass der Focus zurückgesetzt wird, falls die Seite neu geladen wurde
 | 
						|
    if not imageDisplayed then
 | 
						|
        SetNuiFocus(false, false)
 | 
						|
    end
 | 
						|
    if cb then cb('ok') end
 | 
						|
end)
 | 
						|
 | 
						|
-- Event zum Öffnen des Druckermenüs
 | 
						|
RegisterNetEvent("pl_printer:openprinter")
 | 
						|
AddEventHandler("pl_printer:openprinter", function()
 | 
						|
    local input = lib.inputDialog('Print Menu', {
 | 
						|
        {type = 'input', label = Locale("image_link"), description = Locale("image_url"), required = true},
 | 
						|
        {type = 'number', label = Locale("copies"), description = Locale("image_url"), required = true, placeholder='1', icon = 'hashtag'},
 | 
						|
    })
 | 
						|
    
 | 
						|
    if input then
 | 
						|
        if input[1] and input[2] then
 | 
						|
            print("[DEBUG] Sending image data to server: " .. input[1] .. ", copies: " .. input[2])
 | 
						|
            TriggerServerEvent('pl_printer:insertImageData', input[1], input[2])
 | 
						|
        else
 | 
						|
            _debug('[DEBUG] '..'Invalid Input'..'')
 | 
						|
        end
 | 
						|
    end
 | 
						|
end)
 | 
						|
 | 
						|
-- Target-System-Konfiguration
 | 
						|
for _, model in ipairs(Config.PrinterModel) do
 | 
						|
    if GetResourceState('qb-target') == 'started' then
 | 
						|
        exports['qb-target']:AddTargetModel(model, {
 | 
						|
            options = {
 | 
						|
                {
 | 
						|
                    icon = 'fa-solid fa-print',
 | 
						|
                    label = Locale("prints"),
 | 
						|
                    action = function(data)
 | 
						|
                        TriggerEvent('pl_printer:openprinter')
 | 
						|
                    end,
 | 
						|
                },
 | 
						|
            },
 | 
						|
            distance = 2
 | 
						|
        })
 | 
						|
    elseif GetResourceState('qtarget') == 'started' or GetResourceState('ox_target') == 'started' then
 | 
						|
        exports.ox_target:addModel(model, {
 | 
						|
            {
 | 
						|
                name = 'printer_interaction',
 | 
						|
                label = Locale("prints"),
 | 
						|
                icon = 'fa-solid fa-print',
 | 
						|
                onSelect = function(data)
 | 
						|
                    TriggerEvent('pl_printer:openprinter')
 | 
						|
                end,
 | 
						|
                distance = 2,
 | 
						|
            }
 | 
						|
        })
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
-- Funktion zum Spawnen von Objekten
 | 
						|
local function spawnObject(object, coords, heading)
 | 
						|
    lib.requestModel(object)
 | 
						|
 | 
						|
    if not HasModelLoaded(object) then
 | 
						|
        _debug('[DEBUG] '..object..' failed to load.'..'')
 | 
						|
        return
 | 
						|
    end
 | 
						|
    
 | 
						|
    local entity = CreateObject(object, coords.x, coords.y, coords.z, true, true, true)
 | 
						|
 | 
						|
    if DoesEntityExist(entity) then
 | 
						|
        SetEntityHeading(entity, heading)
 | 
						|
        FreezeEntityPosition(entity, true)
 | 
						|
        table.insert(spawnedObjects, entity)
 | 
						|
    else
 | 
						|
        _debug('[DEBUG] '..' Failed to spawn object: '..object..'')
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
-- Funktion zum Löschen von gespawnten Objekten
 | 
						|
local function deleteSpawnedObjects()
 | 
						|
    for _, obj in ipairs(spawnedObjects) do
 | 
						|
        if DoesEntityExist(obj) then
 | 
						|
            DeleteObject(obj)
 | 
						|
        end
 | 
						|
    end
 | 
						|
    spawnedObjects = {}
 | 
						|
end
 | 
						|
 | 
						|
-- Resource-Start-Event
 | 
						|
AddEventHandler('onResourceStart', function(resourceName)
 | 
						|
    if GetCurrentResourceName() ~= resourceName then return end
 | 
						|
    for _, location in ipairs(Config.Locations) do
 | 
						|
        spawnObject(location.object, location.coords, location.heading)
 | 
						|
    end
 | 
						|
end)
 | 
						|
 | 
						|
-- Resource-Stop-Event
 | 
						|
AddEventHandler('onResourceStop', function(resourceName)
 | 
						|
    if GetCurrentResourceName() ~= resourceName then return end
 | 
						|
    deleteSpawnedObjects()
 | 
						|
end)
 | 
						|
 | 
						|
-- Funktion für Spieler-Login
 | 
						|
function onPlayerLoaded()
 | 
						|
    Wait(3000)
 | 
						|
    for _, location in ipairs(Config.Locations) do
 | 
						|
        spawnObject(location.object, location.coords, location.heading)
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
-- Debug-Hilfsfunktion
 | 
						|
function _debug(...)
 | 
						|
    if Config.Debug then
 | 
						|
        print(...)
 | 
						|
    end
 | 
						|
end
 | 
						|
 | 
						|
-- Notfall-Thread zum Zurücksetzen des NUI-Focus, falls etwas schief geht
 | 
						|
Citizen.CreateThread(function()
 | 
						|
    while true do
 | 
						|
        Citizen.Wait(1000)
 | 
						|
        if imageDisplayed then
 | 
						|
            -- Prüfe, ob ESC gedrückt wurde (Fallback für den Fall, dass der NUI-Callback nicht funktioniert)
 | 
						|
            if IsControlJustReleased(0, 177) then -- ESC-Taste
 | 
						|
                print("[DEBUG] ESC key detected in thread, resetting focus")
 | 
						|
                imageDisplayed = false
 | 
						|
                SetNuiFocus(false, false)
 | 
						|
                enableControls()
 | 
						|
                SendNUIMessage({
 | 
						|
                    action = "hide"
 | 
						|
                })
 | 
						|
            end
 | 
						|
        else
 | 
						|
            Citizen.Wait(1000) -- Längere Wartezeit, wenn kein Bild angezeigt wird
 | 
						|
        end
 | 
						|
    end
 | 
						|
end)
 | 
						|
 | 
						|
-- Füge diesen Code hinzu, um das HTML zu aktualisieren, wenn die Ressource neu gestartet wird
 | 
						|
AddEventHandler('onResourceStart', function(resourceName)
 | 
						|
    if GetCurrentResourceName() == resourceName then
 | 
						|
        -- Stelle sicher, dass der NUI-Focus zurückgesetzt wird
 | 
						|
        SetNuiFocus(false, false)
 | 
						|
        imageDisplayed = false
 | 
						|
        enableControls()
 | 
						|
    end
 | 
						|
end)
 | 
						|
 | 
						|
-- Füge diesen Code hinzu, um den NUI-Focus zurückzusetzen, wenn der Spieler stirbt
 | 
						|
AddEventHandler('playerSpawned', function()
 | 
						|
    if imageDisplayed then
 | 
						|
        imageDisplayed = false
 | 
						|
        SetNuiFocus(false, false)
 | 
						|
        enableControls()
 | 
						|
        SendNUIMessage({
 | 
						|
            action = "hide"
 | 
						|
        })
 | 
						|
    end
 | 
						|
end)
 |