From b37ac788f4aa89bea1910628a74124260b3e3ce0 Mon Sep 17 00:00:00 2001 From: Mycroft Date: Wed, 2 Mar 2022 19:53:50 +0000 Subject: [PATCH] Refactor: ESX Shops - Optimisations and Security Patches --- [esx_addons]/esx_shops/client/main.lua | 42 ++++++------------ [esx_addons]/esx_shops/config.lua | 4 +- [esx_addons]/esx_shops/server/main.lua | 61 ++++++++++++++++---------- 3 files changed, 53 insertions(+), 54 deletions(-) diff --git a/[esx_addons]/esx_shops/client/main.lua b/[esx_addons]/esx_shops/client/main.lua index 8b508510..e997c3c3 100644 --- a/[esx_addons]/esx_shops/client/main.lua +++ b/[esx_addons]/esx_shops/client/main.lua @@ -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) diff --git a/[esx_addons]/esx_shops/config.lua b/[esx_addons]/esx_shops/config.lua index 58047091..c5c86dce 100644 --- a/[esx_addons]/esx_shops/config.lua +++ b/[esx_addons]/esx_shops/config.lua @@ -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' diff --git a/[esx_addons]/esx_shops/server/main.lua b/[esx_addons]/esx_shops/server/main.lua index 90335a56..b62b7b60 100644 --- a/[esx_addons]/esx_shops/server/main.lua +++ b/[esx_addons]/esx_shops/server/main.lua @@ -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)