以下是代码的注释和中文翻译:
-- 注册一个网络事件 'shx-idcard:client:buyPDBadge'
RegisterNetEvent('shx-idcard:client:buyPDBadge')
-- 添加事件处理器,当事件被触发时会执行以下函数
AddEventHandler('shx-idcard:client:buyPDBadge', function(whynot)
-- 检查框架是否为 "qbcore"
if Config.Framework == "qbcore" then
-- 检查玩家的工作是否与传入的参数相符
if QBCore.Functions.GetPlayerData().job.name == whynot.job then
-- 如果配置要求 oxlib,并且菜单类型为 "ox_lib"
if Config.oxlibRequire and Config.MenuType == "ox_lib" then
-- 弹出警告对话框,询问用户是否确认购买
local alert = lib.alertDialog({
content = Lang("buytext") .. " $" .. whynot.price, -- 显示的内容
centered = true, -- 对话框居中显示
cancel = true -- 允许取消操作
})
-- 如果用户选择确认
if alert == "confirm" then
-- 触发服务器事件,请求购买徽章
TriggerServerEvent("shx-idcard:server:buyRequest", whynot.price, "pdbadge")
end
-- 如果菜单类型为 "qb_menu"
elseif Config.MenuType == "qb_menu" then
-- 显示输入对话框,询问用户是否接受购买
local dialog = exports['qb-input']:ShowInput({
header = Lang("idcardMenu"), -- 输入对话框的标题
submitText = "Accept", -- 提交文本
inputs = {
{
text = Lang("buytext") .. " $" .. whynot.price, -- 显示的输入头部文本
name = "buytype", -- 输入的名称,应该是唯一的
type = "radio", -- 输入的类型 - 单选
options = { -- 选项
{ value = "no", text = "No" }, -- 选项必须包含值和文本
{ value = "yes", text = "Yes" }, -- 选项必须包含值和文本
},
},
},
})
-- 如果用户选择了 "yes"
if dialog.buytype == "yes" then
-- 触发服务器事件,请求购买徽章
TriggerServerEvent("shx-idcard:server:buyRequest", whynot.price, "pdbadge")
end
end
end
end
end)
这段代码应该放在 `Client.lua` 中,因为它涉及到客户端的事件处理和用户界面交互。 |