当然可以,下面是一个完整的摘葡萄插件示例,包含数据库操作、位置提示和图片展示功能。请根据以下结构创建资源文件。
### 文件结构
your_grape_script/
├── fxmanifest.lua
├── client.lua
└── server.lua
### `fxmanifest.lua`
fx_version 'adamant'
game 'gta5'
author 'Your Name'
description 'Grapes Picking and Selling Script'
version '1.0'
client_script 'client.lua'
server_script 'server.lua'
files {
'html/your_image.png', -- 替换为你的图片文件名
}
ui_page 'html/index.html'
-- 确保使用 DrawSprite 函数时,资源图片可用
### `server.lua`
local MySQL = require('mysql-async')
RegisterServerEvent('grapes:pick')
AddEventHandler('grapes:pick', function()
local src = source
local identifier = GetPlayerIdentifiers(src)[1] -- 获取玩家的标识符
-- 从数据库获取玩家的目前葡萄数
MySQL.Async.fetchAll('SELECT grapes FROM user_grapes WHERE identifier = @identifier', {
['@identifier'] = identifier
}, function(result)
local grapes = 0
if result[1] then
grapes = result[1].grapes
end
grapes = grapes + 1 -- 摘取一颗葡萄
-- 更新数据库中葡萄的数量
MySQL.Async.execute('INSERT INTO user_grapes (identifier, grapes) VALUES (@identifier, @grapes) ON DUPLICATE KEY UPDATE grapes = @grapes', {
['@identifier'] = identifier,
['@grapes'] = grapes
})
TriggerClientEvent('grapes:updateGrapes', src, grapes)
end)
end)
RegisterServerEvent('grapes:sell')
AddEventHandler('grapes:sell', function()
local src = source
local identifier = GetPlayerIdentifiers(src)[1]
-- 从数据库获取玩家的目前葡萄数
MySQL.Async.fetchAll('SELECT grapes FROM user_grapes WHERE identifier = @identifier', {
['@identifier'] = identifier
}, function(result)
if result[1] and result[1].grapes > 0 then
local grapes = result[1].grapes
local sellPrice = grapes * 5 -- 每颗葡萄售价5
grapes = 0 -- 出售后清零葡萄数量
-- 更新数据库
MySQL.Async.execute('UPDATE user_grapes SET grapes = @grapes WHERE identifier = @identifier', {
['@identifier'] = identifier,
['@grapes'] = grapes
})
-- 添加金钱到给定的账户
TriggerEvent('esx_addonaccount:getSharedAccount', 'society_fruit', function(account)
account.addMoney(sellPrice)
end)
TriggerClientEvent('grapes:sellComplete', src, sellPrice)
else
TriggerClientEvent('grapes:noGrapes', src)
end
end)
end)
### `client.lua`
local grapesCount = 0
local grapePickingLocation = vector3(1234.56, -1234.56, 34.56) -- 示例坐标(请根据需要调整)
local isNearGrape = false
RegisterNetEvent('grapes:updateGrapes')
AddEventHandler('grapes:updateGrapes', function(count)
grapesCount = count
print('当前葡萄数量: ' .. grapesCount)
end)
RegisterNetEvent('grapes:sellComplete')
AddEventHandler('grapes:sellComplete', function(amount)
print('成功出售葡萄,获得金额: $' .. amount)
end)
RegisterNetEvent('grapes:noGrapes')
AddEventHandler('grapes:noGrapes', function()
print('没有葡萄可以出售!')
end)
function pickGrapes()
TriggerServerEvent('grapes:pick')
end
function sellGrapes()
TriggerServerEvent('grapes:sell')
end
-- 绘制文字和标识
function DrawText3D(x, y, z, text)
local onScreen, _x, _y = World3dToScreen2d(x, y, z)
local camCoords = GetGameplayCamCoords()
local dist = GetDistanceBetweenCoords(camCoords, x, y, z, true)
local scale = (2 / dist) * 2
if onScreen then
SetTextScale(0.0 + scale, 0.55 + scale)
SetTextFont(4)
SetTextProportional(1)
SetTextColour(255, 255, 255, 215)
SetTextEntry("STRING")
AddTextComponentString(text)
DrawText(_x, _y)
end
end
-- 创建循环来检查玩家是否靠近摘葡萄位置
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
local playerCoords = GetEntityCoords(PlayerPedId())
local distance = #(playerCoords - grapePickingLocation)
if distance < 5 then
isNearGrape = true
DrawText3D(grapePickingLocation.x, grapePickingLocation.y, grapePickingLocation.z + 1.0, " 摘葡萄")
if IsControlJustPressed(1, 38) then -- E键
pickGrapes()
end
-- 在附近绘制葡萄的图片(示范需要图片路径)
DrawSprite("your_texture_dictionary", "your_texture_name", 0.5, 0.5, 0.1, 0.1, 0.0, 255, 255, 255, 255) -- 替换为实际的资源路径和名称
else
isNearGrape = false
end
end
end)
### 说明
1. **图片资源**:
- 请确保你有一个实际的图片文件,并将其放在 `html/` 文件夹中。图片文件名应与 `fxmanifest.lua` 中的定义相匹配。
2. **数据库**:
- 确保已经设置好数据库。使用下面的 SQL 语句创建表:
CREATE TABLE IF NOT EXISTS user_grapes (
identifier VARCHAR(64) NOT NULL PRIMARY KEY,
grapes INT DEFAULT 0
);
3. **坐标调整**:
- 请根据你的需求调整 `grapePickingLocation` 的坐标。
4. **图片和图片字典**:
- 使用 `DrawSprite` 方法前,需要加载纹理字典,你可以使用 `RequestStreamedTextureDict()` 函数在你的代码中请求字典。
5. **依赖项**:
- 确保使用了适当的依赖项,比如 `mysql-async`,并根据需要在你的服务器中正确配置。
### 总结
您可以根据上述代码文件创建您的插件,确保所有文件和配置正确。如果有其他需求或问题,欢迎随时咨询! |