From e02629fa4e7d31164cd4efac918a3ce492d5300b Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Thu, 5 Apr 2018 12:40:58 +0200 Subject: [PATCH] Fixed having more than you can carry, closes #9 - Translation improvements with formatting - General improvements --- client/main.lua | 23 ++++++++--------------- config.lua | 2 +- locales/en.lua | 11 +++++------ server/main.lua | 36 ++++++++++++++++++------------------ 4 files changed, 32 insertions(+), 40 deletions(-) diff --git a/client/main.lua b/client/main.lua index 733a499c..69db5031 100644 --- a/client/main.lua +++ b/client/main.lua @@ -23,38 +23,31 @@ AddEventHandler('onClientMapStart', function() 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 = item.label .. ' - $' .. item.price .. ' ', + label = item.label .. ' - ' .. item.price .. '$', realLabel = item.label, value = item.name, price = item.price }) - end - ESX.UI.Menu.CloseAll() - ESX.UI.Menu.Open( 'default', GetCurrentResourceName(), 'shop', { - title = _U('shop'), + title = _U('shop'), + align = 'bottom-right', elements = elements }, function(data, menu) TriggerServerEvent('esx_shop:buyItem', data.current.value, data.current.price) end, function(data, menu) - menu.close() - CurrentAction = 'shop_menu' CurrentActionMsg = _U('press_menu') CurrentActionData = {zone = zone} @@ -97,7 +90,7 @@ end) -- Display markers Citizen.CreateThread(function() while true do - Wait(0) + Citizen.Wait(10) local coords = GetEntityCoords(GetPlayerPed(-1)) for k,v in pairs(Config.Zones) do for i = 1, #v.Pos, 1 do @@ -112,7 +105,7 @@ end) -- Enter / Exit marker events Citizen.CreateThread(function() while true do - Wait(0) + Citizen.Wait(10) local coords = GetEntityCoords(GetPlayerPed(-1)) local isInMarker = false local currentZone = nil @@ -141,7 +134,7 @@ end) -- Key Controls Citizen.CreateThread(function() while true do - Citizen.Wait(0) + Citizen.Wait(10) if CurrentAction ~= nil then SetTextComponentFormat('STRING') @@ -160,4 +153,4 @@ Citizen.CreateThread(function() end end -end) +end) \ No newline at end of file diff --git a/config.lua b/config.lua index 43d7bbed..10a1f089 100644 --- a/config.lua +++ b/config.lua @@ -4,7 +4,7 @@ Config.DrawDistance = 100 Config.Size = {x = 1.5, y = 1.5, z = 1.5} Config.Color = {r = 0, g = 128, b = 255} Config.Type = 1 -Config.Locale = 'fr' +Config.Locale = 'en' Config.Zones = { diff --git a/locales/en.lua b/locales/en.lua index 35ff8cd0..f88753a4 100644 --- a/locales/en.lua +++ b/locales/en.lua @@ -1,9 +1,8 @@ Locales['en'] = { - ['shop'] = 'shop', ['shops'] = 'shops', - ['press_menu'] = 'press ~INPUT_CONTEXT~ to access the store.', - ['bought'] = 'you bought ~b~1x ', - ['not_enough'] = 'you do not ~r~have enough~s~ money.' - -} + ['press_menu'] = 'press ~INPUT_CONTEXT~ to access the ~y~store~s~.', + ['bought'] = 'you just bought ~y~1x~s~ ~b~%s~s~ for ~r~$%s~s~', + ['not_enough'] = 'you do not have ~r~enough~s~ money, you\'re ~y~missing~s~ ~r~$%s~s~!', + ['player_cannot_hold'] = 'you do ~r~not~s~ have enough ~y~free space~s~ in your inventory!', +} \ No newline at end of file diff --git a/server/main.lua b/server/main.lua index 4ef9dfcb..ffcef5c7 100644 --- a/server/main.lua +++ b/server/main.lua @@ -12,7 +12,7 @@ AddEventHandler('onMySQLReady', function() for i=1, #result, 1 do ItemsLabels[result[i].name] = result[i].label - end-- + end end ) @@ -25,25 +25,19 @@ ESX.RegisterServerCallback('esx_shop:requestDBItems', function(source, cb) 'SELECT * FROM shops', {}, function(result) - local shopItems = {} - for i=1, #result, 1 do - if shopItems[result[i].name] == nil then shopItems[result[i].name] = {} end - + table.insert(shopItems[result[i].name], { name = result[i].item, price = result[i].price, label = ItemsLabels[result[i].item] }) - end - cb(shopItems) - end ) @@ -53,17 +47,23 @@ RegisterServerEvent('esx_shop:buyItem') AddEventHandler('esx_shop:buyItem', function(itemName, price) local _source = source - local xPlayer = ESX.GetPlayerFromId(source) - - if xPlayer.get('money') >= price then - - xPlayer.removeMoney(price) - xPlayer.addInventoryItem(itemName, 1) - - TriggerClientEvent('esx:showNotification', _source, _U('bought') .. ItemsLabels[itemName]) + local xPlayer = ESX.GetPlayerFromId(_source) + local sourceItem = xPlayer.getInventoryItem(itemName) + -- can the player afford this item? + if xPlayer.getMoney() >= price then + + -- can the player carry the said amount of x item? + if sourceItem.limit ~= -1 and (sourceItem.count + 1) > sourceItem.limit then + TriggerClientEvent('esx:showNotification', _source, _U('player_cannot_hold')) + else + xPlayer.removeMoney(price) + xPlayer.addInventoryItem(itemName, 1) + TriggerClientEvent('esx:showNotification', _source, _U('bought', ItemsLabels[itemName], price)) + end else - TriggerClientEvent('esx:showNotification', _source, _U('not_enough')) + local missingMoney = price - xPlayer.getMoney() + TriggerClientEvent('esx:showNotification', _source, _U('not_enough', missingMoney)) end -end) +end) \ No newline at end of file