mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-31 12:01:35 +00:00
0a6dc54564
fix empty shop bug
165 lines
4.1 KiB
Lua
165 lines
4.1 KiB
Lua
ESX = nil
|
|
local hasAlreadyEnteredMarker, lastZone
|
|
local currentAction, currentActionMsg, currentActionData = nil, nil, {}
|
|
|
|
Citizen.CreateThread(function()
|
|
while ESX == nil do
|
|
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
|
|
Citizen.Wait(0)
|
|
end
|
|
|
|
Citizen.Wait(5000)
|
|
|
|
ESX.TriggerServerCallback('esx_shops:requestDBItems', function(ShopItems)
|
|
for k,v in pairs(ShopItems) do
|
|
if (Config.Zones[k] == nil) then
|
|
Config.Zones[k] = {
|
|
Items = {},
|
|
Pos = {}
|
|
}
|
|
end
|
|
|
|
Config.Zones[k].Items = v
|
|
end
|
|
end)
|
|
end)
|
|
|
|
function OpenShopMenu(zone)
|
|
local elements = {}
|
|
for i=1, #Config.Zones[zone].Items, 1 do
|
|
local item = Config.Zones[zone].Items[i]
|
|
|
|
table.insert(elements, {
|
|
label = ('%s - <span style="color:green;">%s</span>'):format(item.label, _U('shop_item', ESX.Math.GroupDigits(item.price))),
|
|
itemLabel = item.label,
|
|
item = item.item,
|
|
price = item.price,
|
|
|
|
-- menu properties
|
|
value = 1,
|
|
type = 'slider',
|
|
min = 1,
|
|
max = 100
|
|
})
|
|
end
|
|
|
|
ESX.UI.Menu.CloseAll()
|
|
|
|
ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'shop', {
|
|
title = _U('shop'),
|
|
align = 'bottom-right',
|
|
elements = elements
|
|
}, function(data, menu)
|
|
ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'shop_confirm', {
|
|
title = _U('shop_confirm', data.current.value, data.current.itemLabel, ESX.Math.GroupDigits(data.current.price * data.current.value)),
|
|
align = 'bottom-right',
|
|
elements = {
|
|
{label = _U('no'), value = 'no'},
|
|
{label = _U('yes'), value = 'yes'}
|
|
}}, function(data2, menu2)
|
|
if data2.current.value == 'yes' then
|
|
TriggerServerEvent('esx_shops:buyItem', data.current.item, data.current.value, zone)
|
|
end
|
|
|
|
menu2.close()
|
|
end, function(data2, menu2)
|
|
menu2.close()
|
|
end)
|
|
end, function(data, menu)
|
|
menu.close()
|
|
|
|
currentAction = 'shop_menu'
|
|
currentActionMsg = _U('press_menu')
|
|
currentActionData = {zone = zone}
|
|
end)
|
|
end
|
|
|
|
AddEventHandler('esx_shops:hasEnteredMarker', function(zone)
|
|
currentAction = 'shop_menu'
|
|
currentActionMsg = _U('press_menu')
|
|
currentActionData = {zone = zone}
|
|
end)
|
|
|
|
AddEventHandler('esx_shops:hasExitedMarker', function(zone)
|
|
currentAction = nil
|
|
ESX.UI.Menu.CloseAll()
|
|
end)
|
|
|
|
-- Create Blips
|
|
Citizen.CreateThread(function()
|
|
for k,v in pairs(Config.Zones) do
|
|
for i = 1, #v.Pos, 1 do
|
|
local blip = AddBlipForCoord(v.Pos[i].x, v.Pos[i].y, v.Pos[i].z)
|
|
|
|
SetBlipSprite (blip, 52)
|
|
SetBlipScale (blip, 1.0)
|
|
SetBlipColour (blip, 2)
|
|
SetBlipAsShortRange(blip, true)
|
|
|
|
BeginTextCommandSetBlipName('STRING')
|
|
AddTextComponentSubstringPlayerName(_U('shops'))
|
|
EndTextCommandSetBlipName(blip)
|
|
end
|
|
end
|
|
end)
|
|
|
|
-- Enter / Exit marker events
|
|
Citizen.CreateThread(function()
|
|
while true do
|
|
Citizen.Wait(0)
|
|
local playerCoords = GetEntityCoords(PlayerPedId())
|
|
local isInMarker, letSleep, currentZone = false, false
|
|
|
|
for k,v in pairs(Config.Zones) do
|
|
for i = 1, #v.Pos, 1 do
|
|
local distance = GetDistanceBetweenCoords(playerCoords, v.Pos[i].x, v.Pos[i].y, v.Pos[i].z, true)
|
|
|
|
if distance < Config.DrawDistance then
|
|
DrawMarker(Config.Type, v.Pos[i].x, v.Pos[i].y, v.Pos[i].z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Config.Size.x, Config.Size.y, Config.Size.z, Config.Color.r, Config.Color.g, Config.Color.b, 100, false, true, 2, false, nil, nil, false)
|
|
letSleep = false
|
|
|
|
if distance < Config.Size.x then
|
|
isInMarker = true
|
|
currentZone = k
|
|
lastZone = k
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
if isInMarker and not hasAlreadyEnteredMarker then
|
|
hasAlreadyEnteredMarker = true
|
|
TriggerEvent('esx_shops:hasEnteredMarker', currentZone)
|
|
end
|
|
|
|
if not isInMarker and hasAlreadyEnteredMarker then
|
|
hasAlreadyEnteredMarker = false
|
|
TriggerEvent('esx_shops:hasExitedMarker', lastZone)
|
|
end
|
|
|
|
if letSleep then
|
|
Citizen.Wait(500)
|
|
end
|
|
end
|
|
end)
|
|
|
|
-- Key Controls
|
|
Citizen.CreateThread(function()
|
|
while true do
|
|
Citizen.Wait(0)
|
|
|
|
if currentAction then
|
|
ESX.ShowHelpNotification(currentActionMsg)
|
|
|
|
if IsControlJustReleased(0, 38) then
|
|
if currentAction == 'shop_menu' then
|
|
OpenShopMenu(currentActionData.zone)
|
|
end
|
|
|
|
currentAction = nil
|
|
end
|
|
else
|
|
Citizen.Wait(500)
|
|
end
|
|
end
|
|
end) |