下面是一个简单的银行插件示例,使用了`ox_lib`的菜单和通知功能,以及`ox_target`对准NPC打开菜单。代码包括基本功能:取钱、存钱、贷款和还款。
### 文件结构your_resource/
│
├── fxmanifest.lua
├── config.lua
├── server.lua
└── client.lua
### fxmanifest.luafx_version 'cerulean'
game 'gta5'
author '你的名字'
description '银行插件'
version '1.0.0'
shared_scripts {
'config.lua',
}
client_scripts {
'client.lua',
}
server_scripts {
'server.lua',
}
dependencies {
'ox_lib',
'ox_target',
}
### config.luaConfig = {}
-- 银行地图坐标
Config.BankLocations = {
{ x = 150.0, y = -1040.0, z = 29.0 }, -- 银行位置示例
}
-- ATM机坐标
Config.ATMLocations = {
{ x = 150.0, y = -1050.0, z = 29.0 }, -- ATM位置示例
}
### server.lualocal loans = {}
RegisterNetEvent('bank:requestLoan')
AddEventHandler('bank:requestLoan', function(amount)
local src = source
if not loans[src] then
loans[src] = 0
end
if amount <= 50000 and amount >= 10000 and (loans[src] + amount) <= 50000 then
loans[src] = loans[src] + amount
TriggerClientEvent('ox_lib:notify', src, '贷款成功: $' .. amount)
else
TriggerClientEvent('ox_lib:notify', src, '贷款失败: 超出限额或已达上限!')
end
end)
RegisterNetEvent('bank:repayLoan')
AddEventHandler('bank:repayLoan', function(amount)
local src = source
if loans[src] and loans[src] > 0 then
if amount <= loans[src] then
loans[src] = loans[src] - amount
TriggerClientEvent('ox_lib:notify', src, '成功还款: $' .. amount)
else
TriggerClientEvent('ox_lib:notify', src, '还款失败: 超过借款余额!')
end
else
TriggerClientEvent('ox_lib:notify', src, '没有贷款项目!')
end
end)
RegisterNetEvent('bank:checkLoan')
AddEventHandler('bank:checkLoan', function()
local src = source
if loans[src] and loans[src] > 0 then
TriggerClientEvent('ox_lib:notify', src, '当前贷款余额: $' .. loans[src])
else
TriggerClientEvent('ox_lib:notify', src, '没有贷款项目!')
end
end)
### client.lualocal ox_target = exports['ox_target']
local function openBankMenu()
local menu = {
{
title = '取钱',
description = '取出现金',
onSelect = function()
-- 这里可以实现取钱的逻辑
TriggerServerEvent('bank:withdraw', amount) -- amount应通过用户输入获得
end
},
{
title = '存钱',
description = '存入现金',
onSelect = function()
-- 这里可以实现存钱的逻辑
TriggerServerEvent('bank:deposit', amount) -- amount应通过用户输入获得
end
},
{
title = '贷款',
description = '申请贷款',
onSelect = function()
local loanAmount = 10000 -- 这里可以实现用户选择的逻辑
TriggerServerEvent('bank:requestLoan', loanAmount)
end
},
{
title = '还款',
description = '还款',
onSelect = function()
TriggerServerEvent('bank:repayLoan', amount) -- amount应通过用户输入获得
end
},
{
title = '查询贷款',
description = '查看当前贷款',
onSelect = function()
TriggerServerEvent('bank:checkLoan')
end
}
}
exports['ox_lib']:menu({ menuItems = menu })
end
-- 自动检测ATM机和银行位置
for _, loc in pairs(Config.ATMLocations) do
ox_target:addSphereZone({
coords = vector3(loc.x, loc.y, loc.z),
radius = 1.5,
options = {
{
name = 'atm',
label = '使用ATM',
onSelect = function()
openBankMenu()
end
}
}
})
end
for _, loc in pairs(Config.BankLocations) do
ox_target:addSphereZone({
coords = vector3(loc.x, loc.y, loc.z),
radius = 1.5,
options = {
{
name = 'bank',
label = '访问银行',
onSelect = function()
openBankMenu()
end
}
}
})
end
### 说明
此代码为一个简单的银行插件示例。你可以根据需要修改和扩展它。
- **取款与存款逻辑**:在`openBankMenu`函数中可以实现取款和存款的具体逻辑。
- **贷款和还款**:提供基本的申请和检查贷款的功能,需要进一步实现用户输入界面。
- **小地图标记**:通过`ox_target`将银行和ATM机添加到目标选项中。
### 注意事项
- 在开发和测试过程中,请确保你的FiveM服务器上已安装`ox_lib`和`ox_target`。
- 确保在运行代码前,检查和处理可能的错误。
希望这个示例对你有所帮助!如果有其他需求或问题,请随时告知。 |