-- 服务器端代码,存放在服务器的脚本文件server.lua中
-- 加载ESX框架
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
-- 注册oxmysql插件
oxmysql = exports.oxmysql
-- 创建空表存放玩家的武器授权信息
local authorizedWeapons = {}
-- 获取授权列表
RegisterServerEvent('getAuthorizedWeapons')
AddEventHandler('getAuthorizedWeapons', function()
local xPlayer = ESX.GetPlayerFromId(source)
local playerIdentifier = xPlayer.getIdentifier()
if authorizedWeapons[playerIdentifier] then
TriggerClientEvent('showAuthorizedWeapons', source, authorizedWeapons[playerIdentifier])
else
TriggerClientEvent('showAuthorizedWeapons', source, {})
end
end)
-- 更新数据库内授权信息
function updateAuthorizedWeapons(playerIdentifier)
oxmysql.execute('UPDATE players SET weapons = ? WHERE identifier = ?', {
json.encode(authorizedWeapons[playerIdentifier]),
playerIdentifier
})
end
-- 检查武器授权
function checkAuthorization(identifier, weapon)
if authorizedWeapons[identifier] and authorizedWeapons[identifier][weapon] then
return true
else
return false
end
end
-- 授权武器
RegisterServerEvent('grantWeaponAuthorization')
AddEventHandler('grantWeaponAuthorization', function(target, weapon)
local targetPlayer = ESX.GetPlayerFromId(target)
local targetIdentifier = targetPlayer.getIdentifier()
if not authorizedWeapons[targetIdentifier] then
authorizedWeapons[targetIdentifier] = {}
end
authorizedWeapons[targetIdentifier][weapon] = true
updateAuthorizedWeapons(targetIdentifier)
end)
-- 取消武器授权
RegisterServerEvent('revokeWeaponAuthorization')
AddEventHandler('revokeWeaponAuthorization', function(target, weapon)
local targetPlayer = ESX.GetPlayerFromId(target)
local targetIdentifier = targetPlayer.getIdentifier()
if authorizedWeapons[targetIdentifier] then
authorizedWeapons[targetIdentifier][weapon] = nil
updateAuthorizedWeapons(targetIdentifier)
end
end)
-- 检查武器授权
RegisterServerEvent('checkWeaponAuthorization')
AddEventHandler('checkWeaponAuthorization', function(weapon)
local xPlayer = ESX.GetPlayerFromId(source)
local playerIdentifier = xPlayer.getIdentifier()
local authorized = checkAuthorization(playerIdentifier, weapon)
TriggerClientEvent('displayAuthorizationStatus', source, authorized)
end)
-- 客户端代码
-- 这部分代码需放在客户端的脚本文件中
-- 显示武器授权菜单
RegisterCommand('openWeaponMenu', function()
TriggerServerEvent('getAuthorizedWeapons')
end)
-- 展示玩家的授权武器列表
RegisterNetEvent('showAuthorizedWeapons')
AddEventHandler('showAuthorizedWeapons', function(authorizedWeapons)
for weapon, _ in pairs(authorizedWeapons) do
print('授权武器: ' .. weapon)
end
end)
-- 显示武器授权状态
RegisterNetEvent('displayAuthorizationStatus')
AddEventHandler('displayAuthorizationStatus', function(authorized)
if authorized then
print('已授权使用此武器')
else
print('未授权使用此武器')
end
end)
这份代码实现了基于ESX Legacy框架,支持最新版本的枪支授权插件。利用ox_lib菜单,通过命令打开武器授权菜单,可以选择授权、取消授权、检查授权和列出玩家授权列表等功能。数据将以JSON格式存储在MySQL数据库中,保证数据的持久性。
请根据你的具体情况修改和完善这段代码,以确保它符合你的服务器需求。 |