Skip to content

Commit

Permalink
feat(server): added optimalisation + and removed unused variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mardev committed Nov 6, 2024
1 parent 7c38503 commit f0881ce
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ dependencies {
'es_extended',
'esx_skin',
'esx_datastore'
}
}
26 changes: 11 additions & 15 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
RegisterServerEvent('esx_accessories:pay')
AddEventHandler('esx_accessories:pay', function()
RegisterNetEvent('esx_accessories:pay', function()
local xPlayer = ESX.GetPlayerFromId(source)

xPlayer.removeMoney(Config.Price, "Accessory Purchase")
TriggerClientEvent('esx:showNotification', source, TranslateCap('you_paid', ESX.Math.GroupDigits(Config.Price)))
end)

RegisterServerEvent('esx_accessories:save')
AddEventHandler('esx_accessories:save', function(skin, accessory)
RegisterNetEvent('esx_accessories:save', function(skin, accessory)
local source = source
local xPlayer = ESX.GetPlayerFromId(source)
local lowerAccessory = accessory:lower()

TriggerEvent('esx_datastore:getDataStore', 'user_' .. string.lower(accessory), xPlayer.identifier, function(store)
TriggerEvent('esx_datastore:getDataStore', ('user_%s'):format(lowerAccessory), xPlayer.identifier, function(store)
store.set('has' .. accessory, true)

local itemSkin = {}
local item1 = string.lower(accessory) .. '_1'
local item2 = string.lower(accessory) .. '_2'
itemSkin[item1] = skin[item1]
itemSkin[item2] = skin[item2]

store.set('skin', itemSkin)
store.set('skin', {
[('%s_1'):format(lowerAccessory)] = skin[('%s_1'):format(lowerAccessory)],
[('%s_2'):format(lowerAccessory)] = skin[('%s_2'):format(lowerAccessory)],
})
end)
end)

ESX.RegisterServerCallback('esx_accessories:get', function(source, cb, accessory)
local xPlayer = ESX.GetPlayerFromId(source)

TriggerEvent('esx_datastore:getDataStore', 'user_' .. string.lower(accessory), xPlayer.identifier, function(store)
local hasAccessory = (store.get('has' .. accessory) and store.get('has' .. accessory) or false)
TriggerEvent('esx_datastore:getDataStore', ('user_%s'):format(accessory:lower()), xPlayer.identifier, function(store)
local hasAccessory = (store.get(('has%s'):format(accessory)) and store.get('has' .. ('has%s'):format(accessory)) or false)
local skin = (store.get('skin') and store.get('skin') or {})

cb(hasAccessory, skin)
Expand All @@ -40,4 +36,4 @@ ESX.RegisterServerCallback('esx_accessories:checkMoney', function(source, cb)
local xPlayer = ESX.GetPlayerFromId(source)

cb(xPlayer.getMoney() >= Config.Price)
end)
end)

0 comments on commit f0881ce

Please sign in to comment.