Untitled

mail@pastecode.io avatar
unknown
lua
11 days ago
5.5 kB
3
Indexable
Never
-- Client File 
QBCore = exports['qb-core']:GetCoreObject()

local customPlateUI = nil

RegisterNetEvent("qb-customplate:openUI")
AddEventHandler("qb-customplate:openUI", function()
    print("[DEBUG] openUI event invoked")
    customPlateUI = exports['qb-input']:ShowInput({
        header = "Custom Plate",
        inputs = {
            {
                type = 'text',
                isRequired = false,
                name = 'newplate',
                text = 'Enter new plate text (leave empty to revert)'
            }
        }
    })

    if customPlateUI then
        local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
        local currentPlate = GetVehicleNumberPlateText(vehicle)
        print("[DEBUG] currentPlate: " .. currentPlate)

        if customPlateUI.newplate == "" then
            TriggerServerEvent("qb-customplate:resetPlate", currentPlate)
        else
            TriggerServerEvent("qb-customplate:updatePlate", customPlateUI.newplate, currentPlate)
        end
    end
end)


RegisterNetEvent("qb-customplate:resetPlateClient")
AddEventHandler("qb-customplate:resetPlateClient", function(originalPlate)
    print("[DEBUG] resetPlateClient event invoked")
    print("[DEBUG] originalPlate: " .. originalPlate)
    local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
    SetVehicleNumberPlateText(vehicle, originalPlate)
end)


RegisterNetEvent("qb-customplate:updatePlateClient")
AddEventHandler("qb-customplate:updatePlateClient", function(newPlate)
    print("[DEBUG] updatePlateClient event invoked")
    print("[DEBUG] newPlate: " .. newPlate)
    local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
    SetVehicleNumberPlateText(vehicle, newPlate)
    TriggerEvent('vehiclekeys:client:SetOwner', QBCore.Functions.GetPlate(vehicle))
end)


-- Server File
QBCore = exports['qb-core']:GetCoreObject()

QBCore.Commands.Add("customplate", "Open the custom plate UI", {}, false, function(source, args)
    local player = source
    local Player = QBCore.Functions.GetPlayer(player)
    local citizenid = Player.PlayerData.citizenid

    MySQL.Async.fetchScalar('SELECT fakeplate FROM player_vehicles WHERE citizenid = @citizenid AND fakeplate IS NOT NULL', {
        ['@citizenid'] = citizenid
    }, function(fakeplate)
        if fakeplate then
            TriggerClientEvent('QBCore:Notify', player, 'You must remove your current fake plate to put a new one', 'error', 3000)
        else
            TriggerClientEvent("qb-customplate:openUI", source)
        end
    end)
end)

QBCore.Commands.Add("resetplate", "Reset the vehicle plate to the original", {}, false, function(source, args)
    local player = source
    local Player = QBCore.Functions.GetPlayer(player)
    local citizenid = Player.PlayerData.citizenid

    MySQL.Async.fetchScalar('SELECT plate FROM player_vehicles WHERE citizenid = @citizenid AND fakeplate IS NOT NULL', {
        ['@citizenid'] = citizenid
    }, function(originalPlate)
        print("[DEBUG] originalPlate query result: " .. tostring(originalPlate))
        if originalPlate then
            local vehicle = GetVehiclePedIsIn(GetPlayerPed(player), false)
            SetVehicleNumberPlateText(vehicle, originalPlate)
            MySQL.Async.execute('UPDATE player_vehicles SET fakeplate = NULL WHERE plate = @originalPlate AND citizenid = @citizenid', {
                ['@originalPlate'] = originalPlate,
                ['@citizenid'] = citizenid
            })
            TriggerClientEvent('QBCore:Notify', player, 'You took your fake plate off.', 'success', 3000)
        else
            -- Check if the player already has the original plate
            MySQL.Async.fetchScalar('SELECT plate FROM player_vehicles WHERE citizenid = @citizenid AND fakeplate IS NULL', {
                ['@citizenid'] = citizenid
            }, function(currentPlate)
                if currentPlate then
                    TriggerClientEvent('QBCore:Notify', player, 'You already have your real plate on, what a shitty criminal.', 'error', 3000)
                else
                    TriggerClientEvent('QBCore:Notify', player, 'Original plate not found.', 'error', 3000)
                end
            end)
        end
    end)
end)

RegisterNetEvent("qb-customplate:updatePlate")
AddEventHandler("qb-customplate:updatePlate", function(newPlate, currentPlate)
    local player = source
    local Player = QBCore.Functions.GetPlayer(player)
    local citizenid = Player.PlayerData.citizenid

    print("[DEBUG] updatePlate event invoked")
    print("[DEBUG] newPlate: " .. newPlate)
    print("[DEBUG] currentPlate: " .. currentPlate)
    print("[DEBUG] citizenid: " .. citizenid)

    MySQL.Async.execute('UPDATE player_vehicles SET fakeplate = @newPlate WHERE plate = @currentPlate AND citizenid = @citizenid', {
        ['@newPlate'] = newPlate,
        ['@currentPlate'] = currentPlate,
        ['@citizenid'] = citizenid
    }, function(rowsChanged)
        print("[DEBUG] rowsChanged: " .. rowsChanged)
        if rowsChanged > 0 then
            TriggerClientEvent('QBCore:Notify', player, 'You changed your plate to: ' .. newPlate, 'success', 5000)
            TriggerClientEvent("qb-customplate:updatePlateClient", player, newPlate)
        else
            TriggerClientEvent('QBCore:Notify', player, 'Failed to change plate.', 'error', 3000)
        end
    end)
end)


Leave a Comment