搜索
开启左侧

帮我转成qb框架

[复制链接]
ACBD 发表于 2025-2-22 00:13:16 | 显示全部楼层 |阅读模式

Keys = {
    ["ESC"] = 322, ["F1"] = 288, ["F2"] = 289, ["F3"] = 170, ["F5"] = 166, ["F6"] = 167, ["F7"] = 168, ["F8"] = 169, ["F9"] = 56, ["F10"] = 57,
    ["~"] = 243, ["1"] = 157, ["2"] = 158, ["3"] = 160, ["4"] = 164, ["5"] = 165, ["6"] = 159, ["7"] = 161, ["8"] = 162, ["9"] = 163, ["-"] = 84, ["="] = 83, ["BACKSPACE"] = 177,
    ["TAB"] = 37, ["Q"] = 44, ["W"] = 32, ["E"] = 38, ["R"] = 45, ["T"] = 245, ["Y"] = 246, ["U"] = 303, ["P"] = 199, ["["] = 39, ["]"] = 40, ["ENTER"] = 18,
    ["CAPS"] = 137, ["A"] = 34, ["S"] = 8, ["D"] = 9, ["F"] = 23, ["G"] = 47, ["H"] = 74, ["K"] = 311, ["L"] = 182,
    ["LEFTSHIFT"] = 21, ["Z"] = 20, ["X"] = 73, ["C"] = 26, ["V"] = 0, ["B"] = 29, ["N"] = 249, ["M"] = 244, [","] = 82, ["."] = 81,
    ["LEFTCTRL"] = 36, ["LEFTALT"] = 19, ["SPACE"] = 22, ["RIGHTCTRL"] = 70,
    ["HOME"] = 213, ["PAGEUP"] = 10, ["PAGEDOWN"] = 11, ["DELETE"] = 178,
    ["LEFT"] = 174, ["RIGHT"] = 175, ["TOP"] = 27, ["DOWN"] = 173,
    ["NENTER"] = 201, ["N4"] = 108, ["N5"] = 60, ["N6"] = 107, ["N+"] = 96, ["N-"] = 97, ["N7"] = 117, ["N8"] = 61, ["N9"] = 118
}

--ESX = nil
ESX = exports["es_extended"]:getSharedObject()
local menuOpen = false
local wasOpen = false
local lastEntity = nil
local currentAction = nil
local currentData = nil
local owns = false

Citizen.CreateThread(function()
    --while ESX == nil do
        --TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
        --Citizen.Wait(0)
    --end

    while ESX.GetPlayerData().job == nil do
        Citizen.Wait(100)
    end

    ESX.PlayerData = ESX.GetPlayerData()
end)

RegisterNetEvent('esx_hifi:place_hifi')
AddEventHandler('esx_hifi:place_hifi', function()
    local playerPed = PlayerPedId()
    local lCoords = GetEntityCoords(playerPed)
    startAnimation("anim@heists@money_grab@briefcase","put_down_case")
    Citizen.Wait(1000)
    ClearPedTasks(PlayerPedId())
    -- TriggerEvent('esx:spawnObject', 'prop_boombox_01')
    ESX.Game.SpawnObject('prop_boombox_01', lCoords)
    owns = true
end)

RegisterNetEvent('esx_hifi:play_music')
AddEventHandler('esx_hifi:play_music', function(id, object)
    if distance(object) < Config.distance then
        SendNUIMessage({
            transactionType = 'playSound',
            transactionData = id
        })

        Citizen.CreateThread(function()
            while true do
                Citizen.Wait(100)
                if distance(object) > Config.distance and distance(object) < 50 then
                SendNUIMessage({
                transactionType = 'volume',
                transactionData = 0.0
                })
                elseif distance(object) < Config.distance then
                SendNUIMessage({
                transactionType = 'volume',
                transactionData = 0.3
                })
                else
                    SendNUIMessage({
                        transactionType = 'stopSound'
                    })
                    break
                end
            end
        end)
    end
end)

RegisterNetEvent('esx_hifi:stop_music')
AddEventHandler('esx_hifi:stop_music', function(object)
    if distance(object) < Config.distance then
        SendNUIMessage({
            transactionType = 'stopSound'
        })
    end
end)

RegisterNetEvent('esx_hifi:setVolume')
AddEventHandler('esx_hifi:setVolume', function(volume, object)
    if distance(object) < Config.distance then
   
    if volume == 0 then
    volumes = 0
    elseif volume == 1 then
    volumes = 0.1
    elseif volume >= 2 and volume <= 5 then
    volumes = 0.3
    elseif volume >= 6 and volume <= 9 then
    volumes = 0.7
    elseif volume == 10 then
    volumes = 1.0
    else
    end
        SendNUIMessage({
            transactionType = 'volume',
            transactionData = volumes
        })
    end
end)

function distance(object)
    local playerPed = PlayerPedId()
    local lCoords = GetEntityCoords(playerPed)
    local distance  = GetDistanceBetweenCoords(lCoords, object, true)
    return distance
end

function OpenhifiMenu()
    menuOpen = true
    ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'hifi', {
        title   = 'La hifi a momo',
        align   = 'top-left',
        elements = {
            {label = _U('get_hifi'), value = 'get_hifi'},
            {label = _U('play_music'), value = 'play'},
            {label = _U('volume_music'), value = 'volume'},
            {label = _U('stop_music'), value = 'stop'}
        }
    }, function(data, menu)
        local playerPed = PlayerPedId()
        local lCoords = GetEntityCoords(playerPed)
        if data.current.value == 'get_hifi' then
            if owns then
            if not alreadyOne then
                NetworkRequestControlOfEntity(currentData)
                menu.close()
                menuOpen = false
                startAnimation("anim@heists@narcotics@trash","pickup")
                Citizen.Wait(700)
                SetEntityAsMissionEntity(currentData,false,true)
                DeleteEntity(currentData)
                ESX.Game.DeleteObject(currentData)
                if not DoesEntityExist(currentData) then
                    TriggerServerEvent('esx_hifi:remove_hifi', lCoords)
                    currentData = nil
                end
                Citizen.Wait(500)
                ClearPedTasks(PlayerPedId())
            else
                menu.close()
                menuOpen = false
                TriggerEvent('esx:showNotification', _U('hifi_alreadyOne'))
            end
            else
               
                TriggerEvent('esx:showNotification', '收音机不属于您')
            end
        elseif data.current.value == 'play' then
            play(lCoords)
        elseif data.current.value == 'stop' then
            TriggerServerEvent('esx_hifi:stop_music', lCoords)
            menuOpen = false
            menu.close()
        elseif data.current.value == 'volume' then
            setVolume(lCoords)
        end
    end, function(data, menu)
        menuOpen = false
        menu.close()
    end)
end

function setVolume(coords)
    ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'setvolume',
        {
            title = _U('set_volume'),
        }, function(data, menu)
            local value = tonumber(data.value)
            if value < 0 or value > 10 then
                ESX.ShowNotification(_U('sound_limit'))
            else
                TriggerServerEvent('esx_hifi:setVolume', value, coords)
                menu.close()
            end
        end, function(data, menu)
            menu.close()
        end)
end


function play(coords)
    ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'play',
        {
            title = _U('play'),
        }, function(data, menu)
            TriggerServerEvent('esx_hifi:play_music', data.value, coords)
            menu.close()
        end, function(data, menu)
            menu.close()
        end)
end

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(500)

        local playerPed = PlayerPedId()
        local coords    = GetEntityCoords(playerPed)

        local closestDistance = -1
        local closestEntity   = nil

        local object = GetClosestObjectOfType(coords, 3.0, GetHashKey('prop_boombox_01'), false, false, false)

        if DoesEntityExist(object) then
            local objCoords = GetEntityCoords(object)
            local distance  = GetDistanceBetweenCoords(coords, objCoords, true)

            if closestDistance == -1 or closestDistance > distance then
                closestDistance = distance
                closestEntity   = object
            end
        end

        if closestDistance ~= -1 and closestDistance <= 3.0 then
            if lastEntity ~= closestEntity and not menuOpen then
                ESX.ShowHelpNotification(_U('hifi_help'))
                lastEntity = closestEntity
                currentAction = "music"
                currentData = closestEntity
            end
        else
            if lastEntity then
                lastEntity = nil
                currentAction = nil
                currentData = nil
            end
        end
    end
end)


Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)

        if currentAction then
            if IsControlJustReleased(0, 38) and currentAction == 'music' then
                OpenhifiMenu()
            end
        end
    end
end)

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)
        local playerPed = PlayerPedId()
        local coords    = GetEntityCoords(playerPed)

        local closestDistance = -1
        local closestEntity   = nil

        local object = GetClosestObjectOfType(coords, 20.0, GetHashKey('prop_boombox_01'), false, false, false)
        local objCoords = GetEntityCoords(object)
        local distance  = GetDistanceBetweenCoords(coords, objCoords, true)
        if distance < 50 then
        DrawText3D(objCoords.x,objCoords.y,objCoords.z+0.65, "~r~音~y~乐~b~盒:~g~音~h~乐~c~放~d~送~q~中...", 1.2, 1)
        else
        Citizen.Wait(200)
        end
    end
end)

function DrawText3D(x,y,z, text, scl, font)

    local onScreen,_x,_y=World3dToScreen2d(x,y,z)
    local px,py,pz=table.unpack(GetGameplayCamCoords())
    local dist = GetDistanceBetweenCoords(px,py,pz, x,y,z, 1)

    local scale = (1/dist)*scl
    local fov = (1/GetGameplayCamFov())*100
    local scale = scale*fov
   
    if onScreen then
        SetTextScale(0.0*scale, 1.1*scale)
        SetTextFont(1)
        SetTextProportional(1)
        SetTextColour(255, 255, 255, 255)
        SetTextDropshadow(0, 0, 0, 0, 255)
        SetTextEdge(2, 0, 0, 0, 150)
        SetTextDropShadow()
        SetTextOutline()
        SetTextEntry("STRING")
        SetTextCentre(1)
        AddTextComponentString(text)
        DrawText(_x,_y)
    end
end

function startAnimation(lib,anim)
    ESX.Streaming.RequestAnimDict(lib, function()
        TaskPlayAnim(PlayerPedId(), lib, anim, 8.0, -8.0, -1, 1, 0, false, false, false)
    end)
end


回复 论坛版权

使用道具 举报

ChatGPT 发表于 2025-2-22 00:13:39 | 显示全部楼层
当然可以!下面是将你提供的代码转换为QBCore框架的版本,并添加了注释以帮助理解。请注意,在转换过程中,某些函数和变量可能需要根据具体的QBCore实现进行调整。
-- 定义按键映射
Keys = {
    ["ESC"] = 322, ["F1"] = 288, ["F2"] = 289, ["F3"] = 170, ["F5"] = 166, 
    ["F6"] = 167, ["F7"] = 168, ["F8"] = 169, ["F9"] = 56, ["F10"] = 57,
    ["~"] = 243, ["1"] = 157, ["2"] = 158, ["3"] = 160, ["4"] = 164, 
    ["5"] = 165, ["6"] = 159, ["7"] = 161, ["8"] = 162, ["9"] = 163, 
    ["-"] = 84, ["="] = 83, ["BACKSPACE"] = 177,
    ["TAB"] = 37, ["Q"] = 44, ["W"] = 32, ["E"] = 38, ["R"] = 45, 
    ["T"] = 245, ["Y"] = 246, ["U"] = 303, ["P"] = 199, ["]"] = 39, 
    ["["] = 40, ["ENTER"] = 18,
    ["CAPS"] = 137, ["A"] = 34, ["S"] = 8, ["D"] = 9, ["F"] = 23, 
    ["G"] = 47, ["H"] = 74, ["K"] = 311, ["L"] = 182,
    ["LEFTSHIFT"] = 21, ["Z"] = 20, ["X"] = 73, ["C"] = 26, ["V"] = 0, 
    ["B"] = 29, ["N"] = 249, ["M"] = 244, [","] = 82, ["."] = 81,
    ["LEFTCTRL"] = 36, ["LEFTALT"] = 19, ["SPACE"] = 22, ["RIGHTCTRL"] = 70,
    ["HOME"] = 213, ["PAGEUP"] = 10, ["PAGEDOWN"] = 11, ["DELETE"] = 178,
    ["LEFT"] = 174, ["RIGHT"] = 175, ["TOP"] = 27, ["DOWN"] = 173,
    ["NENTER"] = 201, ["N4"] = 108, ["N5"] = 60, ["N6"] = 107, 
    ["N+"] = 96, ["N-"] = 97, ["N7"] = 117, ["N8"] = 61, ["N9"] = 118
}

local QBCore = exports['qb-core']:GetCoreObject()  -- 获取 QBCore 对象
local menuOpen = false
local owns = false

Citizen.CreateThread(function()
    while QBCore.Functions.GetPlayerData().job == nil do
        Citizen.Wait(100)
    end
    
    PlayerData = QBCore.Functions.GetPlayerData()  -- 获取玩家数据
end)

RegisterNetEvent('qb_hifi:place_hifi', function()
    local playerPed = PlayerPedId()
    local lCoords = GetEntityCoords(playerPed)
    startAnimation("anim@heists@money_grab@briefcase", "put_down_case")
    Citizen.Wait(1000)
    ClearPedTasks(playerPed)
    QBCore.Functions.SpawnObject('prop_boombox_01', lCoords, function(object)
        SetEntityAsMissionEntity(object, true, true)  -- 设置物体为任务实体
    end)
    owns = true
end)

RegisterNetEvent('qb_hifi:play_music', function(id, object)
    if distance(object) < Config.distance then
        SendNUIMessage({
            transactionType = 'playSound',
            transactionData = id
        })
        
        Citizen.CreateThread(function()
            while true do
                Citizen.Wait(100)
                if distance(object) > Config.distance and distance(object) < 50 then
                    SendNUIMessage({
                        transactionType = 'volume',
                        transactionData = 0.0
                    })
                elseif distance(object) < Config.distance then
                    SendNUIMessage({
                        transactionType = 'volume',
                        transactionData = 0.3
                    })
                else
                    SendNUIMessage({
                        transactionType = 'stopSound'
                    })
                    break
                end
            end
        end)
    end
end)

RegisterNetEvent('qb_hifi:stop_music', function(object)
    if distance(object) < Config.distance then
        SendNUIMessage({
            transactionType = 'stopSound'
        })
    end
end)

RegisterNetEvent('qb_hifi:setVolume', function(volume, object)
    if distance(object) < Config.distance then
        local volumes = 0
        if volume >= 0 and volume <= 10 then
            volumes = volume / 10  -- 将音量值转化为范围 [0, 1]
            TriggerServerEvent('qb_hifi:setVolume', volumes, coords)
        else
            QBCore.Functions.Notify('音量超出范围!', 'error')
        end
    end
end)

function play(coords)
    QBCore.UI.Menu.Open('dialog', GetCurrentResourceName(), 'play', {
        title = '播放音乐',
    }, function(data, menu)
        TriggerServerEvent('qb_hifi:play_music', data.value, coords)
        menu.close()
    end, function(data, menu)
        menu.close()
    end)
end

Citizen.CreateThread(function()
    while true do
        Citizen.Wait(500)
        local playerPed = PlayerPedId()
        local coords = GetEntityCoords(playerPed)

        local closestDistance = -1
        local closestEntity = nil

        local object = GetClosestObjectOfType(coords, 3.0, GetHashKey('prop_boombox_01'), false, false, false)

        if DoesEntityExist(object) then
            local objCoords = GetEntityCoords(object)
            local distance = GetDistanceBetweenCoords(coords, objCoords, true)

            if closestDistance == -1 or closestDistance > distance then
                closestDistance = distance
                closestEntity = object
            end
        end

        if closestDistance ~= -1 and closestDistance < 3.0 then
            -- 这里可以插入与目标对象的交互逻辑
        end
    end
end)

### 注释说明:
- **Keys**: 定义了按键的映射,使得可以通过按键进行操作。
- **QBCore**: 使用 `exports['qb-core']:GetCoreObject()` 获取 QBCore 对象,便于后续调用。
- **注册事件**: 使用 `RegisterNetEvent` 来处理各种事件,比如放置音响、播放音乐等。
- **音量设置**: 在设置音量时,确保音量值在合理范围内,避免用户输入错误的值。
- **使用 NUI**: 通过 `SendNUIMessage` 与前端交互,实现声音的播放和停止。

根据你具体的需求和框架环境,可能还需要进一步调整。希望这能帮助到你!如果有其他问题,请随时问我。
回复

使用道具 举报

文明发言,和谐互动
文明发言,和谐互动
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|首页 ( 冀ICP备2021005225号-1 ) |网站地图

GMT+8, 2025-3-13 02:54 , Processed in 0.088442 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2013 ELITE论坛【技术无价-分享快乐】

快速回复 返回顶部 返回列表