Refactor: ESX Shops - Optimisations and Security Patches

This commit is contained in:
Mycroft
2022-03-02 19:53:50 +00:00
parent 72aa093086
commit b37ac788f4
3 changed files with 53 additions and 54 deletions
+14 -28
View File
@@ -85,7 +85,18 @@ end)
-- Enter / Exit marker events
CreateThread(function()
while true do
Wait(0)
local Sleep = 1500
if currentAction then
sleep = 0
ESX.ShowHelpNotification(currentActionMsg)
if IsControlJustReleased(0, 38) and currentAction == 'shop_menu' then
currentAction = nil
OpenShopMenu(currentActionData.zone)
end
end
local playerCoords = GetEntityCoords(PlayerPedId())
local isInMarker, letSleep, currentZone = false, false
@@ -94,11 +105,10 @@ CreateThread(function()
local distance = #(playerCoords - v.Pos[i])
if distance < Config.DrawDistance then
sleep = 0
if v.ShowMarker then
DrawMarker(Config.MarkerType, v.Pos[i], 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Config.MarkerSize.x, Config.MarkerSize.y, Config.MarkerSize.z, Config.MarkerColor.r, Config.MarkerColor.g, Config.MarkerColor.b, 100, false, true, 2, false, nil, nil, false)
end
letSleep = false
if distance < 2.0 then
isInMarker = true
currentZone = k
@@ -117,30 +127,6 @@ CreateThread(function()
hasAlreadyEnteredMarker = false
TriggerEvent('esx_shops:hasExitedMarker', lastZone)
end
if letSleep then
Wait(500)
end
end
end)
-- Key Controls
CreateThread(function()
while true do
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
Wait(500)
end
Wait(Sleep)
end
end)
+2 -2
View File
@@ -1,7 +1,7 @@
Config = {}
Config.DrawDistance = 100
Config.DrawDistance = 10
Config.MarkerSize = {x = 1.1, y = 1.1, z = 0.9}
Config.MarkerType = 21
Config.MarkerType = 29
Config.MarkerColor = {r = 102, g = 102, b = 204, a = 255}
Config.Locale = 'en'
+37 -24
View File
@@ -1,10 +1,30 @@
local ShopItems = {}
function GetItemFromShop(Item, Zone)
local item = {}
local found = false
for i=1, #Config.Zones[Zone].Items, 1 do
if Config.Zones[Zone].Items[i].name == Item then
item = Config.Zones[Zone].Items[i]
found = true
break
end
end
if found then
return true, item.price, item.label
else
return false
end
end
RegisterServerEvent('esx_shops:buyItem')
AddEventHandler('esx_shops:buyItem', function(itemName, amount, zone)
local source = source
local xPlayer = ESX.GetPlayerFromId(source)
local Exists, price,label = GetItemFromShop(itemName,zone)
amount = ESX.Math.Round(amount)
if amount < 0 then
@@ -12,33 +32,26 @@ AddEventHandler('esx_shops:buyItem', function(itemName, amount, zone)
return
end
-- get price
local price = 0
local itemLabel = ''
for i=1, #Config.Zones[zone].Items, 1 do
local item = Config.Zones[zone].Items[i]
if item.name == itemName then
price = item.price
itemLabel = item.label
break
end
if not Exists then
print('esx_shops: ' .. xPlayer.identifier .. ' attempted to exploit the shop!')
return
end
price = price * amount
if Exists then
price = price * amount
-- can the player afford this item?
if xPlayer.getMoney() >= price then
-- can the player carry the said amount of x item?
if xPlayer.canCarryItem(itemName, amount) then
xPlayer.removeMoney(price)
xPlayer.addInventoryItem(itemName, amount)
xPlayer.showNotification(_U('bought', amount, itemLabel, ESX.Math.GroupDigits(price)))
if xPlayer.getMoney() >= price then
-- can the player carry the said amount of x item?
if xPlayer.canCarryItem(itemName, amount) then
xPlayer.removeMoney(price)
xPlayer.addInventoryItem(itemName, amount)
xPlayer.showNotification(_U('bought', amount, itemLabel, ESX.Math.GroupDigits(price)))
else
xPlayer.showNotification(_U('player_cannot_hold'))
end
else
xPlayer.showNotification(_U('player_cannot_hold'))
local missingMoney = price - xPlayer.getMoney()
xPlayer.showNotification(_U('not_enough', ESX.Math.GroupDigits(missingMoney)))
end
else
local missingMoney = price - xPlayer.getMoney()
xPlayer.showNotification(_U('not_enough', ESX.Math.GroupDigits(missingMoney)))
end
end)